Skip to content

Commit f78fc93

Browse files
authored
feat: tagging the release also comments on the release PR (googleapis#751)
1 parent 809f819 commit f78fc93

File tree

6 files changed

+147
-2
lines changed

6 files changed

+147
-2
lines changed

__snapshots__/github.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/github-release.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ export class GitHubRelease {
128128
);
129129
checkpoint(`Created release: ${release.html_url}.`, CheckpointType.Success);
130130

131+
// Comment on the release PR with the
132+
await this.gh.commentOnIssue(
133+
`:robot: Release is at ${release.html_url} :sunflower:`,
134+
candidate.pullNumber
135+
);
136+
131137
// Add a label indicating that a release has been created on GitHub,
132138
// but a publication has not yet occurred.
133139
await this.gh.addLabels([GITHUB_RELEASE_LABEL], candidate.pullNumber);

src/github.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ type GitGetTreeResponse = PromiseValue<
3939
type IssuesListResponseItem = PromiseValue<
4040
ReturnType<InstanceType<typeof Octokit>['issues']['get']>
4141
>['data'];
42-
type FileSearchResponse = PromiseValue<
43-
ReturnType<InstanceType<typeof Octokit>['search']['code']>
42+
type CreateIssueCommentResponse = PromiseValue<
43+
ReturnType<InstanceType<typeof Octokit>['issues']['create']>
4444
>['data'];
4545
// see: PromiseValue<
4646
// ReturnType<InstanceType<typeof Octokit>['repos']['createRelease']>
@@ -1404,6 +1404,33 @@ export class GitHub {
14041404
prefix
14051405
);
14061406
}
1407+
1408+
/**
1409+
* Makes a comment on a issue/pull request.
1410+
*
1411+
* @param {string} comment - The body of the comment to post.
1412+
* @param {number} number - The issue or pull request number.
1413+
*/
1414+
async commentOnIssue(
1415+
comment: string,
1416+
number: number
1417+
): Promise<CreateIssueCommentResponse> {
1418+
checkpoint(
1419+
`adding comment to https://github.com/${this.owner}/${this.repo}/issue/${number}`,
1420+
CheckpointType.Success
1421+
);
1422+
return (
1423+
await this.request(
1424+
'POST /repos/:owner/:repo/issues/:issue_number/comments',
1425+
{
1426+
owner: this.owner,
1427+
repo: this.repo,
1428+
issue_number: number,
1429+
body: comment,
1430+
}
1431+
)
1432+
).data;
1433+
}
14071434
}
14081435

14091436
class AuthError extends Error {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"id": 1,
3+
"node_id": "MDEyOklzc3VlQ29tbWVudDE=",
4+
"url": "https://api.github.com/repos/fake/fake/issues/comments/1",
5+
"html_url": "https://github.com/fake/fake/issues/1347#issuecomment-1",
6+
"body": "This is a comment",
7+
"user": {
8+
"login": "octocat",
9+
"id": 1,
10+
"node_id": "MDQ6VXNlcjE=",
11+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/octocat",
14+
"html_url": "https://github.com/octocat",
15+
"followers_url": "https://api.github.com/users/octocat/followers",
16+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20+
"organizations_url": "https://api.github.com/users/octocat/orgs",
21+
"repos_url": "https://api.github.com/users/octocat/repos",
22+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/octocat/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"created_at": "2011-04-14T16:00:49Z",
28+
"updated_at": "2011-04-14T16:00:49Z",
29+
"issue_url": "https://api.github.com/repos/fake/fake/issues/1347",
30+
"author_association": "COLLABORATOR"
31+
}

test/github-release.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ describe('GitHubRelease', () => {
7676
upload_url: 'https://upload.url/',
7777
});
7878

79+
sandbox
80+
.stub(release.gh, 'commentOnIssue')
81+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
82+
.resolves();
83+
7984
sandbox
8085
.stub(release.gh, 'addLabels')
8186
.withArgs(['autorelease: tagged'], 1)
@@ -132,6 +137,11 @@ describe('GitHubRelease', () => {
132137
upload_url: 'https://upload.url/',
133138
});
134139

140+
sandbox
141+
.stub(release.gh, 'commentOnIssue')
142+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
143+
.resolves();
144+
135145
sandbox
136146
.stub(release.gh, 'addLabels')
137147
.withArgs(['autorelease: tagged'], 1)
@@ -191,6 +201,11 @@ describe('GitHubRelease', () => {
191201
upload_url: 'https://upload.url/',
192202
});
193203

204+
sandbox
205+
.stub(release.gh, 'commentOnIssue')
206+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
207+
.resolves();
208+
194209
sandbox
195210
.stub(release.gh, 'addLabels')
196211
.withArgs(['autorelease: tagged'], 1)
@@ -250,6 +265,11 @@ describe('GitHubRelease', () => {
250265
upload_url: 'https://upload.url/',
251266
});
252267

268+
sandbox
269+
.stub(release.gh, 'commentOnIssue')
270+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
271+
.resolves();
272+
253273
sandbox
254274
.stub(release.gh, 'addLabels')
255275
.withArgs(['autorelease: tagged'], 1)
@@ -310,6 +330,11 @@ describe('GitHubRelease', () => {
310330
upload_url: 'https://upload.url/',
311331
});
312332

333+
sandbox
334+
.stub(release.gh, 'commentOnIssue')
335+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
336+
.resolves();
337+
313338
sandbox
314339
.stub(release.gh, 'addLabels')
315340
.withArgs(['autorelease: tagged'], 1)
@@ -371,6 +396,11 @@ describe('GitHubRelease', () => {
371396
upload_url: 'https://upload.url/',
372397
});
373398

399+
sandbox
400+
.stub(release.gh, 'commentOnIssue')
401+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
402+
.resolves();
403+
374404
sandbox
375405
.stub(release.gh, 'addLabels')
376406
.withArgs(['autorelease: tagged'], 1)
@@ -489,6 +519,11 @@ describe('GitHubRelease', () => {
489519
upload_url: 'https://upload.url/',
490520
});
491521

522+
sandbox
523+
.stub(release.gh, 'commentOnIssue')
524+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
525+
.resolves();
526+
492527
sandbox
493528
.stub(release.gh, 'addLabels')
494529
.withArgs(['autorelease: tagged'], 1)
@@ -546,6 +581,11 @@ describe('GitHubRelease', () => {
546581
upload_url: 'https://upload.url/',
547582
});
548583

584+
sandbox
585+
.stub(release.gh, 'commentOnIssue')
586+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
587+
.resolves();
588+
549589
sandbox
550590
.stub(release.gh, 'addLabels')
551591
.withArgs(['autorelease: tagged'], 1)
@@ -675,6 +715,11 @@ describe('GitHubRelease', () => {
675715
upload_url: 'https://upload.url/',
676716
});
677717

718+
sandbox
719+
.stub(release.gh, 'commentOnIssue')
720+
.withArgs(':robot: Release is at https://release.url :sunflower:', 1)
721+
.resolves();
722+
678723
sandbox
679724
.stub(release.gh, 'addLabels')
680725
.withArgs(['autorelease: tagged'], 1)

test/github.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,4 +838,36 @@ describe('GitHub', () => {
838838
expect(release!.draft).to.be.false;
839839
});
840840
});
841+
842+
describe('commentOnIssue', () => {
843+
it('can create a comment', async () => {
844+
const createCommentResponse = JSON.parse(
845+
readFileSync(
846+
resolve(fixturesPath, 'create-comment-response.json'),
847+
'utf8'
848+
)
849+
);
850+
req
851+
.post('/repos/fake/fake/issues/1347/comments', body => {
852+
snapshot(body);
853+
return true;
854+
})
855+
.reply(201, createCommentResponse);
856+
const comment = await github.commentOnIssue('This is a comment', 1347);
857+
expect(comment.body).to.eql('This is a comment');
858+
});
859+
860+
it('propagates error', async () => {
861+
req.post('/repos/fake/fake/issues/1347/comments').reply(410, 'Gone');
862+
let thrown = false;
863+
try {
864+
await github.commentOnIssue('This is a comment', 1347);
865+
fail('should have thrown');
866+
} catch (err) {
867+
thrown = true;
868+
expect(err.status).to.eql(410);
869+
}
870+
expect(thrown).to.be.true;
871+
});
872+
});
841873
});

0 commit comments

Comments
 (0)