Skip to content

Commit

Permalink
fix: reimplement draft releases (#1111)
Browse files Browse the repository at this point in the history
  • Loading branch information
chingor13 committed Nov 24, 2021
1 parent 173ce70 commit 6f38b4a
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 75 deletions.
46 changes: 23 additions & 23 deletions __snapshots__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,34 @@ release-please manifest-pr
create a release-PR using a manifest file
Options:
--help Show help [boolean]
--version Show version number [boolean]
--debug print verbose errors (use only for local debugging).
[boolean] [default: false]
--trace print extra verbose errors (use only for local debugging).
--help Show help [boolean]
--version Show version number [boolean]
--debug print verbose errors (use only for local debugging).
[boolean] [default: false]
--token GitHub token with repo write permissions
--api-url URL to use when making API requests
--trace print extra verbose errors (use only for local
debugging). [boolean] [default: false]
--token GitHub token with repo write permissions
--api-url URL to use when making API requests
[string] [default: "https://api.github.com"]
--graphql-url URL to use when making GraphQL requests
--graphql-url URL to use when making GraphQL requests
[string] [default: "https://api.github.com"]
--default-branch The branch to open release PRs against and tag releases on
[deprecated: use --target-branch instead] [string]
--target-branch The branch to open release PRs against and tag releases on
[string]
--repo-url GitHub URL to generate release for [required]
--dry-run Prepare but do not take action [boolean] [default: false]
--label comma-separated list of labels to add to from release PR
--default-branch The branch to open release PRs against and tag releases
on [deprecated: use --target-branch instead] [string]
--target-branch The branch to open release PRs against and tag releases
on [string]
--repo-url GitHub URL to generate release for [required]
--dry-run Prepare but do not take action[boolean] [default: false]
--label comma-separated list of labels to add to from release PR
[default: "autorelease: pending"]
--fork should the PR be created from a fork
--fork should the PR be created from a fork
[boolean] [default: false]
--draft mark pull request as a draft [boolean] [default: false]
--signoff Add Signed-off-by line at the end of the commit log message
using the user and email provided. (format "Name
<email@example.com>"). [string]
--config-file where can the config file be found in the project?
--draft-pull-request mark pull request as a draft [boolean] [default: false]
--signoff Add Signed-off-by line at the end of the commit log
message using the user and email provided. (format "Name
<email@example.com>"). [string]
--config-file where can the config file be found in the project?
[default: "release-please-config.json"]
--manifest-file where can the manifest file be found in the project?
--manifest-file where can the manifest file be found in the project?
[default: ".release-please-manifest.json"]
`

Expand Down Expand Up @@ -176,7 +176,7 @@ Options:
[default: "autorelease: pending"]
--fork should the PR be created from a fork
[boolean] [default: false]
--draft mark pull request as a draft
--draft-pull-request mark pull request as a draft
[boolean] [default: false]
--signoff Add Signed-off-by line at the end of the
commit log message using the user and email
Expand Down
16 changes: 13 additions & 3 deletions __snapshots__/github.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 13 additions & 6 deletions src/bin/release-please.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ interface ReleaseArgs {
}

interface PullRequestArgs {
draft?: boolean;
draftPullRequest?: boolean;
label?: string;
signoff?: string;
}
Expand Down Expand Up @@ -129,7 +129,8 @@ interface BootstrapArgs
ManifestConfigArgs,
VersioningArgs,
PullRequestArgs,
PullRequestStrategyArgs {
PullRequestStrategyArgs,
ReleaseArgs {
initialVersion?: string;
}

Expand Down Expand Up @@ -207,7 +208,7 @@ function pullRequestOptions(yargs: yargs.Argv): yargs.Argv {
type: 'boolean',
default: false,
})
.option('draft', {
.option('draft-pull-request', {
describe: 'mark pull request as a draft',
type: 'boolean',
default: false,
Expand Down Expand Up @@ -374,7 +375,7 @@ const createReleasePullRequestCommand: yargs.CommandModule<
releaseType: argv.releaseType,
component: argv.component,
packageName: argv.packageName,
draft: argv.draft,
draftPullRequest: argv.draftPullRequest,
bumpMinorPreMajor: argv.bumpMinorPreMajor,
bumpPatchForMinorPreMajor: argv.bumpPatchForMinorPreMajor,
changelogPath: argv.changelogPath,
Expand Down Expand Up @@ -565,7 +566,9 @@ const bootstrapCommand: yargs.CommandModule<{}, BootstrapArgs> = {
command: 'bootstrap',
describe: 'configure release manifest',
builder(yargs) {
return manifestOptions(pullRequestStrategyOptions(gitHubOptions(yargs)))
return manifestOptions(
releaseOptions(pullRequestStrategyOptions(gitHubOptions(yargs)))
)
.option('initial-version', {
description: 'current version',
})
Expand All @@ -591,6 +594,7 @@ const bootstrapCommand: yargs.CommandModule<{}, BootstrapArgs> = {
component: argv.component,
packageName: argv.packageName,
draft: argv.draft,
draftPullRequest: argv.draftPullRequest,
bumpMinorPreMajor: argv.bumpMinorPreMajor,
bumpPatchForMinorPreMajor: argv.bumpPatchForMinorPreMajor,
changelogPath: argv.changelogPath,
Expand Down Expand Up @@ -648,7 +652,7 @@ interface HandleError {
}
function extractManifestOptions(
argv: (GitHubArgs & PullRequestArgs) | ReleaseArgs
argv: GitHubArgs & (PullRequestArgs | ReleaseArgs)
): ManifestOptions {
const manifestOptions: ManifestOptions = {};
if ('fork' in argv && argv.fork !== undefined) {
Expand All @@ -666,6 +670,9 @@ function extractManifestOptions(
if ('draft' in argv && argv.draft !== undefined) {
manifestOptions.draft = argv.draft;
}
if ('draftPullRequest' in argv && argv.draftPullRequest !== undefined) {
manifestOptions.draftPullRequest = argv.draftPullRequest;
}
return manifestOptions;
}
Expand Down
41 changes: 10 additions & 31 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ interface GraphQLRelease {
};
url: string;
description: string;
isDraft: boolean;
}

interface CommitHistory {
Expand Down Expand Up @@ -140,6 +141,7 @@ export interface GitHubRelease {
sha: string;
notes?: string;
url: string;
draft?: boolean;
}

export class GitHub {
Expand Down Expand Up @@ -608,6 +610,7 @@ export class GitHub {
}
url
description
isDraft
}
pageInfo {
endCursor
Expand Down Expand Up @@ -641,41 +644,12 @@ export class GitHub {
sha: release.tagCommit.oid,
notes: release.description,
url: release.url,
draft: release.isDraft,
};
}),
};
}

/**
* Return a list of tags. The list is not guaranteed to be sorted.
*
* @param {number} page - Page of results. Defaults to 1.
* @param {number} perPage - Number of results per page. Defaults to 100.
* @returns {GitHubRelease[]} - List of tags
* @throws {GitHubAPIError} on an API error
*/
private listReleases = wrapAsync(
async (page = 1, perPage = 100): Promise<GitHubRelease[]> => {
logger.debug(`Fetching releases page ${page}`);
const releases = await this.octokit.repos.listReleases({
owner: this.repository.owner,
repo: this.repository.repo,
page,
per_page: perPage,
});

return releases.data.map(release => {
return {
name: release.name || undefined,
tagName: release.tag_name,
sha: release.target_commitish,
notes: release.body_text,
url: release.html_url,
};
});
}
);

/**
* Fetch the contents of a file from the configured branch
*
Expand Down Expand Up @@ -1165,20 +1139,25 @@ export class GitHub {
* @throws {GitHubAPIError} on other API errors
*/
createRelease = wrapAsync(
async (release: Release): Promise<GitHubRelease> => {
async (
release: Release,
options: {draft?: boolean} = {}
): Promise<GitHubRelease> => {
const resp = await this.octokit.repos.createRelease({
owner: this.repository.owner,
repo: this.repository.repo,
tag_name: release.tag.toString(),
body: release.notes,
sha: release.sha,
draft: !!options.draft,
});
return {
name: resp.data.name || undefined,
tagName: resp.data.tag_name,
sha: resp.data.target_commitish,
notes: resp.data.body_text,
url: resp.data.html_url,
draft: resp.data.draft,
};
},
e => {
Expand Down
17 changes: 14 additions & 3 deletions src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface ReleaserConfig {
releaseAs?: string;
skipGithubRelease?: boolean;
draft?: boolean;
draftPullRequest?: boolean;
component?: string;
packageName?: string;

Expand All @@ -70,6 +71,7 @@ export interface CandidateReleasePullRequest {

export interface CandidateRelease extends Release {
pullRequest: PullRequest;
draft?: boolean;
}

interface ReleaserConfigJson {
Expand All @@ -80,6 +82,7 @@ interface ReleaserConfigJson {
'release-as'?: string;
'skip-github-release'?: boolean;
draft?: boolean;
'draft-pull-request'?: boolean;
label?: string;
'release-label'?: string;

Expand All @@ -101,7 +104,7 @@ export interface ManifestOptions {
labels?: string[];
releaseLabels?: string[];
draft?: boolean;
releaseDraft?: boolean;
draftPullRequest?: boolean;
}

interface ReleaserPackageConfig extends ReleaserConfigJson {
Expand Down Expand Up @@ -154,6 +157,8 @@ export class Manifest {
private manifestPath: string;
private bootstrapSha?: string;
private lastReleaseSha?: string;
private draft?: boolean;
private draftPullRequest?: boolean;

/**
* Create a Manifest from explicit config in code. This assumes that the
Expand Down Expand Up @@ -203,6 +208,8 @@ export class Manifest {
this.labels = manifestOptions?.labels || DEFAULT_LABELS;
this.bootstrapSha = manifestOptions?.bootstrapSha;
this.lastReleaseSha = manifestOptions?.lastReleaseSha;
this.draft = manifestOptions?.draft;
this.draftPullRequest = manifestOptions?.draftPullRequest;
}

/**
Expand Down Expand Up @@ -457,7 +464,7 @@ export class Manifest {
const releasePullRequest = await strategy.buildReleasePullRequest(
pathCommits,
latestRelease,
config.draft,
config.draftPullRequest ?? this.draftPullRequest,
this.labels
);
if (releasePullRequest) {
Expand Down Expand Up @@ -663,6 +670,7 @@ export class Manifest {
releases.push({
...release,
pullRequest,
draft: config.draft ?? this.draft,
});
}
}
Expand Down Expand Up @@ -726,7 +734,9 @@ export class Manifest {
private async createRelease(
release: CandidateRelease
): Promise<GitHubRelease> {
const githubRelease = await this.github.createRelease(release);
const githubRelease = await this.github.createRelease(release, {
draft: release.draft,
});

// comment on pull request
const comment = `:robot: Release is at ${githubRelease.url} :sunflower:`;
Expand Down Expand Up @@ -791,6 +801,7 @@ function extractReleaserConfig(config: ReleaserPackageConfig): ReleaserConfig {
releaseAs: config['release-as'],
skipGithubRelease: config['skip-github-release'],
draft: config.draft,
draftPullRequest: config['draft-pull-request'],
component: config['component'],
packageName: config['package-name'],
versionFile: config['version-file'],
Expand Down
10 changes: 5 additions & 5 deletions test/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,9 +782,9 @@ describe('CLI', () => {
sinon.assert.calledOnce(createPullRequestsStub);
});

it('handles --draft', async () => {
it('handles --draft-pull-request', async () => {
await parser.parseAsync(
'release-pr --repo-url=googleapis/release-please-cli --release-type=java-yoshi --draft'
'release-pr --repo-url=googleapis/release-please-cli --release-type=java-yoshi --draft-pull-request'
);

sinon.assert.calledOnceWithExactly(gitHubCreateStub, {
Expand All @@ -796,8 +796,8 @@ describe('CLI', () => {
fromConfigStub,
fakeGitHub,
'main',
sinon.match({releaseType: 'java-yoshi'}),
sinon.match({draft: true}),
sinon.match({releaseType: 'java-yoshi', draftPullRequest: true}),
sinon.match.any,
undefined
);
sinon.assert.calledOnce(createPullRequestsStub);
Expand Down Expand Up @@ -1115,7 +1115,7 @@ describe('CLI', () => {
fakeGitHub,
'main',
sinon.match({releaseType: 'java-yoshi', draft: true}),
sinon.match({draft: true}),
sinon.match.any,
undefined
);
sinon.assert.calledOnce(createReleasesStub);
Expand Down

0 comments on commit 6f38b4a

Please sign in to comment.