Skip to content

Commit

Permalink
feat(github-release): create a draft release (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldodge79 authored Jan 22, 2021
1 parent bccc4d4 commit bd83c03
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
28 changes: 23 additions & 5 deletions __snapshots__/github-release.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
exports['GitHubRelease createRelease attempts to guess package name for release 1'] = {
"tag_name": "v1.0.3",
"body": "\n* entry",
"name": "@google-cloud/foo v1.0.3"
"name": "@google-cloud/foo v1.0.3",
"draft": false
}

exports['GitHubRelease createRelease attempts to guess package name for release 2'] = {
Expand Down Expand Up @@ -114,7 +115,8 @@ exports['GitHubRelease extractLatestReleaseNotes php-yoshi extracts appropriate
exports['GitHubRelease createRelease creates and labels release on GitHub 1'] = {
"tag_name": "v1.0.3",
"body": "\n* entry",
"name": "foo v1.0.3"
"name": "foo v1.0.3",
"draft": false
}

exports['GitHubRelease createRelease creates and labels release on GitHub 2'] = {
Expand All @@ -126,7 +128,8 @@ exports['GitHubRelease createRelease creates and labels release on GitHub 2'] =
exports['GitHubRelease createRelease supports submodules in nested folders 1'] = {
"tag_name": "foo/v1.0.3",
"body": "\n* entry",
"name": "foo foo/v1.0.3"
"name": "foo foo/v1.0.3",
"draft": false
}

exports['GitHubRelease createRelease supports submodules in nested folders 2'] = {
Expand All @@ -138,7 +141,8 @@ exports['GitHubRelease createRelease supports submodules in nested folders 2'] =
exports['GitHubRelease createRelease creates releases for submodule in monorepo 1'] = {
"tag_name": "bigquery/v1.0.3",
"body": "\n* entry",
"name": "bigquery bigquery/v1.0.3"
"name": "bigquery bigquery/v1.0.3",
"draft": false
}

exports['GitHubRelease createRelease creates releases for submodule in monorepo 2'] = {
Expand All @@ -150,11 +154,25 @@ exports['GitHubRelease createRelease creates releases for submodule in monorepo
exports['GitHubRelease createRelease attempts to guess package name for submodule release 1'] = {
"tag_name": "@google-cloud/foo-v1.0.3",
"body": "\n* entry",
"name": "@google-cloud/foo @google-cloud/foo-v1.0.3"
"name": "@google-cloud/foo @google-cloud/foo-v1.0.3",
"draft": false
}

exports['GitHubRelease createRelease attempts to guess package name for submodule release 2'] = {
"labels": [
"autorelease: tagged"
]
}

exports['GitHubRelease createRelease creates a draft release 1'] = {
"tag_name": "v1.0.3",
"body": "\n* entry",
"name": "foo v1.0.3",
"draft": true
}

exports['GitHubRelease createRelease creates a draft release 2'] = {
"labels": [
"autorelease: tagged"
]
}
8 changes: 8 additions & 0 deletions src/bin/release-please.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ const argv = yargs
describe: 'include library name in tags and release branches',
type: 'boolean',
default: false,
})
.option('draft', {
describe:
'mark release as a draft. no tag is created but tag_name and ' +
'target_commitish are associated with the release for future ' +
'tag creation upon "un-drafting" the release.',
type: 'boolean',
default: false,
});
},
(argv: GitHubReleaseOptions) => {
Expand Down
8 changes: 7 additions & 1 deletion src/github-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ interface ReleaseResponse {
tag_name: string;
upload_url: string;
pr: number;
draft: boolean;
}

export interface GitHubReleaseOptions {
Expand All @@ -47,6 +48,7 @@ export interface GitHubReleaseOptions {
octokitAPIs?: OctokitAPIs;
releaseType?: string;
changelogPath?: string;
draft?: boolean;
}

export class GitHubRelease {
Expand All @@ -61,6 +63,7 @@ export class GitHubRelease {
token?: string;
proxyKey?: string;
releaseType?: string;
draft?: boolean;

constructor(options: GitHubReleaseOptions) {
this.apiUrl = options.apiUrl;
Expand All @@ -72,6 +75,7 @@ export class GitHubRelease {
this.path = options.path;
this.packageName = options.packageName;
this.releaseType = options.releaseType;
this.draft = options.draft;

this.changelogPath = options.changelogPath ?? 'CHANGELOG.md';

Expand Down Expand Up @@ -141,7 +145,8 @@ export class GitHubRelease {
? `${this.packageName}${tagSeparator}${version}`
: version,
gitHubReleasePR.sha,
latestReleaseNotes
latestReleaseNotes,
!!this.draft
);
// Add a label indicating that a release has been created on GitHub,
// but a publication has not yet occurred.
Expand All @@ -162,6 +167,7 @@ export class GitHubRelease {
html_url: release.html_url,
tag_name: release.tag_name,
upload_url: release.upload_url,
draft: release.draft,
};
} else {
console.warn(`failed to parse version informatino from ${version}`);
Expand Down
4 changes: 3 additions & 1 deletion src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,8 @@ export class GitHub {
packageName: string,
tagName: string,
sha: string,
releaseNotes: string
releaseNotes: string,
draft: boolean
): Promise<ReleaseCreateResponse> {
checkpoint(`creating release ${tagName}`, CheckpointType.Success);
return (
Expand All @@ -983,6 +984,7 @@ export class GitHub {
target_commitish: sha,
body: releaseNotes,
name: `${packageName} ${tagName}`,
draft: draft,
}
)
).data;
Expand Down
65 changes: 64 additions & 1 deletion test/github-release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,69 @@ describe('GitHubRelease', () => {
return true;
}
)
.reply(200, {tag_name: 'v1.0.3'})
.reply(200, {tag_name: 'v1.0.3', draft: false})
.post(
'/repos/googleapis/foo/issues/1/labels',
(body: {[key: string]: string}) => {
snapshot(body);
return true;
}
)
.reply(200)
.delete(
'/repos/googleapis/foo/issues/1/labels/autorelease%3A%20pending'
)
.reply(200);

const created = await release.createRelease();
strictEqual(created!.tag_name, 'v1.0.3');
strictEqual(created!.major, 1);
strictEqual(created!.minor, 0);
strictEqual(created!.patch, 3);
strictEqual(created!.draft, false);
requests.done();
});

it('creates a draft release', async () => {
const release = new GitHubRelease({
label: 'autorelease: pending',
repoUrl: 'googleapis/foo',
packageName: 'foo',
apiUrl: 'https://api.github.com',
draft: true,
});
const requests = nock('https://api.github.com')
// check for default branch
.get('/repos/googleapis/foo')
.reply(200, repoInfo)
.get(
'/repos/googleapis/foo/pulls?state=closed&per_page=50&sort=updated&direction=desc'
)
.reply(200, [
{
labels: [{name: 'autorelease: pending'}],
head: {
label: 'head:release-v1.0.3',
},
base: {
label: 'googleapis:main',
},
number: 1,
merged_at: new Date().toISOString(),
},
])
.get('/repos/googleapis/foo/contents/CHANGELOG.md?ref=refs/heads/main')
.reply(200, {
content: Buffer.from('#Changelog\n\n## v1.0.3\n\n* entry', 'utf8'),
})
.post(
'/repos/googleapis/foo/releases',
(body: {[key: string]: string}) => {
snapshot(body);
return true;
}
)
.reply(200, {tag_name: 'v1.0.3', draft: true})
.post(
'/repos/googleapis/foo/issues/1/labels',
(body: {[key: string]: string}) => {
Expand All @@ -87,6 +149,7 @@ describe('GitHubRelease', () => {
strictEqual(created!.major, 1);
strictEqual(created!.minor, 0);
strictEqual(created!.patch, 3);
strictEqual(created!.draft, true);
requests.done();
});

Expand Down

0 comments on commit bd83c03

Please sign in to comment.