10000 fix: handle repository rename with search API · coderbyheart/github@d51858e · GitHub
[go: up one dir, main page]

Skip to content

Commit d51858e

Browse files
committed
fix: handle repository rename with search API
1 parent 83444bf commit d51858e

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

lib/fail.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ module.exports = async (pluginConfig, context) => {
2121
if (failComment === false || failTitle === false) {
2222
logger.log('Skip issue creation.');
2323
} else {
24-
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
2524
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
25+
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
26+
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
27+
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
2628
const body = failComment ? template(failComment)({branch, errors}) : getFailComment(branch, errors);
2729
const [srIssue] = await findSRIssues(github, failTitle, owner, repo);
2830

lib/success.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ module.exports = async (pluginConfig, context) => {
2929
failTitle,
3030
releasedLabels,
3131
} = resolveConfig(pluginConfig, context);
32-
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
32+
3333
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
34+
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
35+
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
36+
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
37+
3438
const errors = [];
3539

3640
if (successComment === false) {

test/fail.test.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ test.afterEach.always(() => {
2929
test.serial('Open a new issue with the list of errors', async t => {
3030
const owner = 'test_user';
3131
const repo = 'test_repo';
32+
const redirectedOwner = 'test_user_2';
33+
const redirectedRepo = 'test_repo_2';
3234
const env = {GITHUB_TOKEN: 'github_token'};
3335
const failTitle = 'The automated release is failing 🚨';
3436
const pluginConfig = {failTitle};
@@ -39,13 +41,15 @@ test.serial('Open a new issue with the list of errors', async t => {
3941
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
4042
];
4143
const github = authenticate(env)
44+
.get(`/repos/${owner}/${repo}`)
45+
.reply(200, {full_name: `${redirectedOwner}/${redirectedRepo}`})
4246
.get(
43-
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
44-
'state:open'
45-
)}+${escape(failTitle)}`
47+
`/search/issues?q=${escape('in:title')}+${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape(
48+
'type:issue'
49+
)}+${escape('state:open')}+${escape(failTitle)}`
4650
)
4751
.reply(200, {items: []})
48-
.post(`/repos/${owner}/${repo}/issues`, {
52+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues`, {
4953
title: failTitle,
5054
body: /---\n\n### Error message 1\n\nError 1 details\n\n---\n\n### Error message 2\n\nError 2 details\n\n---\n\n### Error message 3\n\nError 3 details\n\n---/,
5155
labels: ['semantic-release'],
@@ -71,6 +75,8 @@ test.serial('Open a new issue with the list of errors, retrying 4 times', async
7175
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
7276
];
7377
const github = authenticate(env)
78+
.get(`/repos/${owner}/${repo}`)
79+
.reply(200, {full_name: `${owner}/${repo}`})
7480
.get(
7581
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
7682
'state:open'
@@ -118,6 +124,8 @@ test.serial('Open a new issue with the list of errors and custom title and comme
118124
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
119125
];
120126
const github = authenticate(env)
127+
.get(`/repos/${owner}/${repo}`)
128+
.reply(200, {full_name: `${owner}/${repo}`})
121129
.get(
122130
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
123131
'state:open'
@@ -150,6 +158,8 @@ test.serial('Open a new issue with assignees and the list of errors', async t =>
150158
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
151159
];
152160
const github = authenticate(env)
161+
.get(`/repos/${owner}/${repo}`)
162+
.reply(200, {full_name: `${owner}/${repo}`})
153163
.get(
154164
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
155165
'state:open'
@@ -183,6 +193,8 @@ test.serial('Open a new issue without labels and the list of errors', async t =>
183193
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
184194
];
185195
const github = authenticate(env)
196+
.get(`/repos/${owner}/${repo}`)
197+
.reply(200, {full_name: `${owner}/${repo}`})
186198
.get(
187199
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${e B41A scape(
188200
'state:open'
@@ -220,6 +232,8 @@ test.serial('Update the first existing issue with the list of errors', async t =
220232
{number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle},
221233
];
222234
const github = authenticate(env)
235+
.get(`/repos/${owner}/${repo}`)
236+
.reply(200, {full_name: `${owner}/${repo}`})
223237
.get(
224238
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
225239
'state:open'

test/integration.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ test.serial('Comment and add labels on PR included in the releases', async t =>
190190
const github = authenticate(env)
191191
.get(`/repos/${owner}/${repo}`)
192192
.reply(200, {permissions: {push: true}})
193+
.get(`/repos/${owner}/${repo}`)
194+
.reply(200, {full_name: `${owner}/${repo}`})
193195
.get(
194196
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
195197
.map(commit => commit.hash)
@@ -231,6 +233,8 @@ test.serial('Open a new issue with the list of errors', async t => {
231233
const github = authenticate(env)
232234
.get(`/repos/${owner}/${repo}`)
233235
.reply(200, {permissions: {push: true}})
236+
.get(`/repos/${owner}/${repo}`)
237+
.reply(200, {full_name: `${owner}/${repo}`})
234238
.get(
235239
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
236240
'state:open'
@@ -281,6 +285,8 @@ test.serial('Verify, release and notify success', async t => {
281285
body: nextRelease.notes,
282286
})
283287
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl})
288+
.get(`/repos/${owner}/${repo}`)
289+
.reply(200, {full_name: `${owner}/${repo}`})
284290
.get(
285291
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
286292
.map(commit => commit.hash)
@@ -342,6 +348,8 @@ test.serial('Verify and notify failure', async t => {
342348
const github = authenticate(env)
343349
.get(`/repos/${owner}/${repo}`)
344350
.reply(200, {permissions: {push: true}})
351+
.get(`/repos/${owner}/${repo}`)
352+
.reply(200, {full_name: `${owner}/${repo}`})
345353
.get(
346354
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
347355
'state:open'

test/success.test.js

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ test.serial(
3131
async t => {
3232
const owner = 'test_user';
3333
const repo = 'test_repo';
34+
const redirectedOwner = 'test_user_2';
35+
const redirectedRepo = 'test_repo_2';
3436
const env = {GITHUB_TOKEN: 'github_token'};
3537
const failTitle = 'The automated release is failing 🚨';
3638
const pluginConfig = {failTitle};
@@ -44,47 +46,49 @@ test.serial(
4446
{hash: '456', message: 'Commit 2 message', tree: {long: 'ccc'}},
4547
{
4648
hash: '789',
47-
message: `Commit 3 message Closes https://github.com/${owner}/${repo}/issues/4`,
49+
message: `Commit 3 message Closes https://github.com/${redirectedOwner}/${redirectedRepo}/issues/4`,
4850
tree: {long: 'ccc'},
4951
},
5052
];
5153
const nextRelease = {version: '1.0.0'};
5254
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
5355
const github = authenticate(env)
56+
.get(`/repos/${owner}/${repo}`)
57+
.reply(200, {full_name: `${redirectedOwner}/${redirectedRepo}`})
5458
.get(
55-
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
56-
.map(commit => commit.hash)
57-
.join('+')}`
59+
`/search/issues?q=${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape('type:pr')}+${escape(
60+
'is:merged'
61+
)}+${commits.map(commit => commit.hash).join('+')}`
5862
)
5963
.reply(200, {items: prs})
60-
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
64+
.get(`/repos/${redirectedOwner}/${redirectedRepo}/pulls/1/commits`)
6165
.reply(200, [{sha: commits[0].hash}])
62-
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
66+
.get(`/repos/${redirectedOwner}/${redirectedRepo}/pulls/2/commits`)
6367
.reply(200, [{sha: commits[1].hash}])
64-
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
68+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/1/comments`, {body: /This PR is included/})
6569
.reply(200, {html_url: 'https://github.com/successcomment-1'})
66-
.post(`/repos/${owner}/${repo}/issues/1/labels`, '["released"]')
70+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/1/labels`, '["released"]')
6771
.reply(200, {})
68-
.post(`/repos/${owner}/${repo}/issues/2/comments`, {body: /This PR is included/})
72+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/2/comments`, {body: /This PR is included/})
6973
.reply(200, {html_url: 'https://github.com/successcomment-2'})
70-
.post(`/repos/${owner}/${repo}/issues/2/labels`, '["released"]')
74+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/2/labels`, '["released"]')
7175
.reply(200, {})
72-
.get(`/repos/${owner}/${repo}/issues/3`)
76+
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3`)
7377
.reply(200, {state: 'closed'})
74-
.post(`/repos/${owner}/${repo}/issues/3/comments`, {body: /This issue has been resolved/})
78+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/comments`, {body: /This issue has been resolved/})
7579
.reply(200, {html_url: 'https://github.com/successcomment-3'})
76-
.post(`/repos/${owner}/${repo}/issues/3/labels`, '["released"]')
80+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/labels`, '["released"]')
7781
.reply(200, {})
78-
.get(`/repos/${owner}/${repo}/issues/4`)
82+
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4`)
7983
.reply(200, {state: 'closed'})
80-
.post(`/repos/${owner}/${repo}/issues/4/comments`, {body: /This issue has been resolved/})
84+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/comments`, {body: /This issue has been resolved/})
8185
.reply(200, {html_url: 'https://github.com/successcomment-4'})
82-
.post(`/repos/${owner}/${repo}/issues/4/labels`, '["released"]')
86+
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/labels`, '["released"]')
8387
.reply(200, {})
8488
.get(
85-
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
86-
'state:open'
87-
)}+${escape(failTitle)}`
89+
`/search/issues?q=${escape('in:title')}+${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape(
90+
'type:issue'
91+
)}+${escape('state:open')}+${escape(failTitle)}`
8892
)
8993
.reply(200, {items: []});
9094

@@ -123,6 +127,8 @@ test.serial(
123127
const nextRelease = {version: '1.0.0'};
124128
const releases = [{name: 'GitHub release', url: 'https://custom-url.com/release'}];
125129
const github = authenticate(env)
130+
.get(`/repos/${owner}/${repo}`)
131+
.reply(200, {full_name: `${owner}/${repo}`})
126132
.get(
127133
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
128134
.map(commit => commit.hash)
@@ -201,6 +207,8 @@ test.serial('Make multiple search queries if necessary', async t => {
201207
const nextRelease = {version: '1.0.0'};
202208
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
203209
const github = authenticate(env)
210+
.get(`/repos/${owner}/${repo}`)
211+
.reply(200, {full_name: `${owner}/${repo}`})
204212
.get(
205213
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${
206214
commits[0].hash
@@ -287,6 +295,8 @@ test.serial(
287295
const nextRelease = {version: '1.0.0'};
288296
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
289297
const github = authenticate(env)
298+
.get(`/repos/${owner}/${repo}`)
299+
.reply(200, {full_name: `${owner}/${repo}`})
290300
.get(
291301
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
292302
.map(commit => commit.hash)
@@ -332,6 +342,8 @@ test.serial('Do not add comment and labels to open issues/PRs', async t => {
332342
const nextRelease = {version: '1.0.0'};
333343
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
334344
const github = authenticate(env)
345+
.get(`/repos/${owner}/${repo}`)
346+
.reply(200, {full_name: `${owner}/${repo}`})
335347
.get(
336348
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
337349
.map(commit => commit.hash)
@@ -372,6 +384,8 @@ test.serial('Do not add comment and labels if no PR is associated with release c
372384
const nextRelease = {version: '1.0.0'};
373385
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
374386
const github = authenticate(env)
387+
.get(`/repos/${owner}/${repo}`)
388+
.reply(200, {full_name: `${owner}/${repo}`})
375389
.get(
376390
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
377391
.map(commit => commit.hash)
@@ -405,6 +419,8 @@ test.serial('Do not add comment and labels to PR/issues from other repo', async
405419
const nextRelease = {version: '1.0.0'};
406420
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
407421
const github = authenticate(env)
422+
.get(`/repos/${owner}/${repo}`)
423+
.reply(200, {full_name: `${owner}/${repo}`})
408424
.get(
409425
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
410426
.map(commit => commit.hash)
@@ -446,6 +462,8 @@ test.serial('Ignore missing issues/PRs', async t => {
446462
const nextRelease = {version: '1.0.0'};
447463
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
448464
const github = authenticate(env)
465+
.get(`/repos/${owner}/${repo}`)
466+
.reply(200, {full_name: `${owner}/${repo}`})
449467
.get(
450468
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
451469
.map(commit => commit.hash)
@@ -502,6 +520,8 @@ test.serial('Add custom comment', async t => {
502520
const nextRelease = {version: '2.0.0'};
503521
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
504522
const github = authenticate(env)
523+
.get(`/repos/${owner}/${repo}`)
524+
.reply(200, {full_name: `${owner}/${repo}`})
505525
.get(
506526
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
507527
.map(commit => commit.hash)
@@ -543,6 +563,8 @@ test.serial('Add custom label', async t => {
543563
const nextRelease = {version: '2.0.0'};
544564
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
545565
const github = authenticate(env)
566+
.get(`/repos/${owner}/${repo}`)
567+
.reply(200, {full_name: `${owner}/${repo}`})
546568
.get(
547569
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
548570
.map(commit => commit.hash)
@@ -582,6 +604,8 @@ test.serial('Comment on issue/PR without ading a label', async t => {
582604
const nextRelease = {version: '2.0.0'};
583605
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
584606
const github = authenticate(env)
607+
.get(`/repos/${owner}/${repo}`)
608+
.reply(200, {full_name: `${owner}/${repo}`})
585609
.get(
586610
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
587611
.map(commit => commit.hash)
@@ -622,6 +646,8 @@ test.serial('Ignore errors when adding comments and closing issues', async t =>
622646
const nextRelease = {version: '1.0.0'};
623647
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
624648
const github = authenticate(env)
649+
.get(`/repos/${owner}/${repo}`)
650+
.reply(200, {full_name: `${owner}/${repo}`})
625651
.get(
626652
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
627653
.map(commit => commit.hash)
@@ -677,6 +703,8 @@ test.serial('Close open issues when a release is successful', async t => {
677703
const nextRelease = {version: '1.0.0'};
678704
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
679705
const github = authenticate(env)
706+
.get(`/repos/${owner}/${repo}`)
707+
.reply(200, {full_name: `${owner}/${repo}`})
680708
.get(
681709
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
682710
.map(commit => commit.hash)
@@ -712,6 +740,8 @@ test.serial('Skip commention on issues/PR if "successComment" is "false"', async
712740
const nextRelease = {version: '1.0.0'};
713741
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
714742
const github = authenticate(env)
743+
.get(`/repos/${owner}/${repo}`)
744+
.reply(200, {full_name: `${owner}/${repo}`})
715745
.get(
716746
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
717747
'state:open'
@@ -735,6 +765,8 @@ test.serial('Skip closing issues if "failComment" is "false"', async t => {
735765
const nextRelease = {version: '1.0.0'};
736766
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
737767
const github = authenticate(env)
768+
.get(`/repos/${owner}/${repo}`)
769+
.reply(200, {full_name: `${owner}/${repo}`})
738770
.get(
739771
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
740772
.map(commit => commit.hash)
@@ -757,6 +789,8 @@ test.serial('Skip closing issues if "failTitle" is "false"', async t => {
757789
const nextRelease = {version: '1.0.0'};
758790
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
759791
const github = authenticate(env)
792+
.get(`/repos/${owner}/${repo}`)
793+
.reply(200, {full_name: `${owner}/${repo}`})
760794
.get(
761795
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
762796
.map(commit => commit.hash)

0 commit comments

Comments
 (0)
0