Skip to content

Commit 18728c4

Browse files
committed
fix: compare release and PR commits with tree sha
1 parent 599e0df commit 18728c4

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

lib/success.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ module.exports = async (
1919
const github = getClient({githubToken, githubUrl, githubApiPathPrefix});
2020
const releaseInfos = releases.filter(release => Boolean(release.name));
2121
const shas = commits.map(commit => commit.hash);
22+
const treeShas = commits.map(commit => commit.tree.long);
2223

2324
const searchQueries = getSearchQueries(`repo:${owner}/${repo}+type:pr+is:merged`, shas).map(
2425
async q => (await github.search.issues({q})).data.items
2526
);
2627

2728
const prs = await pFilter(uniqBy(flatten(await Promise.all(searchQueries)), 'number'), async ({number}) =>
28-
(await github.pullRequests.getCommits({owner, repo, number})).data.find(({sha}) => shas.includes(sha))
29+
(await github.pullRequests.getCommits({owner, repo, number})).data.find(
30+
({sha, commit}) => shas.includes(sha) || treeShas.includes(commit.tree.sha)
31+
)
2932
);
3033

3134
debug('found pull requests: %O', prs.map(pr => pr.number));

test/integration.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ test.serial('Comment on PR included in the releases', async t => {
185185
const failTitle = 'The automated release is failing 🚨';
186186
const prs = [{number: 1, pull_request: {}}];
187187
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
188-
const commits = [{hash: '123', message: 'Commit 1 message'}];
188+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
189189
const nextRelease = {version: '1.0.0'};
190190
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
191191
const github = authenticate()
@@ -273,7 +273,7 @@ test.serial('Verify, release and notify success', async t => {
273273
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
274274
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
275275
const prs = [{number: 1, pull_request: {}}];
276-
const commits = [{hash: '123', message: 'Commit 1 message'}];
276+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
277277
const github = authenticate()
278278
.get(`/repos/${owner}/${repo}`)
279279
.reply(200, {permissions: {push: true}})

test/success.test.js

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ test.serial('Add comment to PRs associated with release commits and issues close
5151
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}, body: 'Fixes #3'}];
5252
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
5353
const commits = [
54-
{hash: '123', message: 'Commit 1 message\n\n Fix #1'},
55-
{hash: '456', message: 'Commit 2 message'},
56-
{hash: '789', message: 'Commit 3 message Closes #4'},
54+
{hash: '123', message: 'Commit 1 message\n\n Fix #1', tree: {long: 'aaa'}},
55+
{hash: '456', message: 'Commit 2 message', tree: {long: 'ccc'}},
56+
{hash: '789', message: 'Commit 3 message Closes #4', tree: {long: 'ccc'}},
5757
];
5858
const nextRelease = {version: '1.0.0'};
5959
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
@@ -116,13 +116,13 @@ test.serial('Make multiple search queries if necessary', async t => {
116116
];
117117
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
118118
const commits = [
119-
{hash: repeat('a', 40), message: 'Commit 1 message'},
120-
{hash: repeat('b', 40), message: 'Commit 2 message'},
121-
{hash: repeat('c', 40), message: 'Commit 3 message'},
122-
{hash: repeat('d', 40), message: 'Commit 4 message'},
123-
{hash: repeat('e', 40), message: 'Commit 5 message'},
124-
{hash: repeat('f', 40), message: 'Commit 6 message'},
125-
{hash: repeat('g', 40), message: 'Commit 7 message'},
119+
{hash: repeat('a', 40), message: 'Commit 1 message', tree: {long: 'aaa'}},
120+
{hash: repeat('b', 40), message: 'Commit 2 message', tree: {long: 'bbb'}},
121+
{hash: repeat('c', 40), message: 'Commit 3 message', tree: {long: 'ccc'}},
122+
{hash: repeat('d', 40), message: 'Commit 4 message', tree: {long: 'ddd'}},
123+
{hash: repeat('e', 40), message: 'Commit 5 message', tree: {long: 'eee'}},
124+
{hash: repeat('f', 40), message: 'Commit 6 message', tree: {long: 'fff'}},
125+
{hash: repeat('g', 40), message: 'Commit 7 message', tree: {long: 'ggg'}},
126126
];
127127
const nextRelease = {version: '1.0.0'};
128128
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
@@ -193,15 +193,18 @@ test.serial('Make multiple search queries if necessary', async t => {
193193
t.true(github.isDone());
194194
});
195195

196-
test.serial('Do not add comment for unrelated PR returned by search', async t => {
196+
test.serial('Do not add comment for unrelated PR returned by search (compare sha)', async t => {
197197
const owner = 'test_user';
198198
const repo = 'test_repo';
199199
process.env.GITHUB_TOKEN = 'github_token';
200200
const failTitle = 'The automated release is failing 🚨';
201201
const pluginConfig = {failTitle};
202202
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}}];
203203
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
204-
const commits = [{hash: '123', message: 'Commit 1 message'}, {hash: '456', message: 'Commit 2 message'}];
204+
const commits = [
205+
{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}},
206+
{hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}},
207+
];
205208
const nextRelease = {version: '1.0.0'};
206209
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
207210
const github = authenticate()
@@ -214,7 +217,49 @@ test.serial('Do not add comment for unrelated PR returned by search', async t =>
214217
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
215218
.reply(200, [{sha: commits[0].hash}])
216219
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
217-
.reply(200, [{sha: 'unrelated_commit'}])
220+
.reply(200, [{sha: 'unrelated_commit', commit: {tree: {sha: 'unrelated_commit'}}}])
221+
.get(`/repos/${owner}/${repo}/issues/1`)
222+
.reply(200, {state: 'closed'})
223+
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
224+
.reply(200, {html_url: 'https://github.com/successcomment-1'})
225+
.get(
226+
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
227+
'state:open'
228+
)}+${escape(failTitle)}`
229+
)
230+
.reply(200, {items: []});
231+
232+
await success(pluginConfig, {options, commits, nextRelease, releases, logger: t.context.logger});
233+
234+
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 1, 'https://github.com/successcomment-1'));
235+
t.true(github.isDone());
236+
});
237+
238+
test.serial('Do not add comment for unrelated PR returned by search (compare tree sha)', async t => {
239+
const owner = 'test_user';
240+
const repo = 'test_repo';
241+
process.env.GITHUB_TOKEN = 'github_token';
242+
const failTitle = 'The automated release is failing 🚨';
243+
const pluginConfig = {failTitle};
244+
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}}];
245+
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
246+
const commits = [
247+
{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}},
248+
{hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}},
249+
];
250+
const nextRelease = {version: '1.0.0'};
251+
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
252+
const github = authenticate()
253+
.get(
254+
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
255+
.map(commit => commit.hash)
256+
.join('+')}`
257+
)
258+
.reply(200, {items: prs})
259+
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
260+
.reply(200, [{sha: 'rebased_sha', commit: {tree: {sha: commits[0].tree.long}}}])
261+
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
262+
.reply(200, [{sha: 'unrelated_commit', commit: {tree: {sha: 'unrelated_commit'}}}])
218263
.get(`/repos/${owner}/${repo}/issues/1`)
219264
.reply(200, {state: 'closed'})
220265
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
@@ -240,7 +285,7 @@ test.serial('Do not add comment to open issues/PRs', async t => {
240285
const pluginConfig = {failTitle};
241286
const prs = [{number: 1, pull_request: {}, body: 'Fixes #2'}];
242287
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
243-
const commits = [{hash: '123', message: 'Commit 1 message'}];
288+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
244289
const nextRelease = {version: '1.0.0'};
245290
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
246291
const github = authenticate()
@@ -279,7 +324,7 @@ test.serial('Do not add comment if no PR is associated with release commits', as
279324
const failTitle = 'The automated release is failing 🚨';
280325
const pluginConfig = {failTitle};
281326
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
282-
const commits = [{hash: '123', message: 'Commit 1 message'}];
327+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
283328
const nextRelease = {version: '1.0.0'};
284329
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
285330
const github = authenticate()
@@ -309,9 +354,9 @@ test.serial('Do not add comment to PR/issues from other repo', async t => {
309354
const pluginConfig = {failTitle};
310355
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
311356
const commits = [
312-
{hash: '123', message: 'Commit 1 message\n\n Fix other/other#1'},
313-
{hash: '456', message: `Commit 2 message Fix ${owner}/${repo}#2`},
314-
{hash: '789', message: 'Commit 3 message Closes other/other#3'},
357+
{hash: '123', message: 'Commit 1 message\n\n Fix other/other#1', tree: {long: 'aaa'}},
358+
{hash: '456', message: `Commit 2 message Fix ${owner}/${repo}#2`, tree: {long: 'bbb'}},
359+
{hash: '789', message: 'Commit 3 message Closes other/other#3', tree: {long: 'ccc'}},
315360
];
316361
const nextRelease = {version: '1.0.0'};
317362
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
@@ -346,7 +391,10 @@ test.serial('Ignore missing issues/PRs', async t => {
346391
const pluginConfig = {failTitle};
347392
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}, body: 'Fixes #3'}];
348393
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
349-
const commits = [{hash: '123', message: 'Commit 1 message\n\n Fix #1'}, {hash: '456', message: 'Commit 2 message'}];
394+
const commits = [
395+
{hash: '123', message: 'Commit 1 message\n\n Fix #1', tree: {long: 'aaa'}},
396+
{hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}},
397+
];
350398
const nextRelease = {version: '1.0.0'};
351399
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
352400
const github = authenticate()
@@ -400,7 +448,7 @@ test.serial('Add custom comment', async t => {
400448
const prs = [{number: 1, prop: 'PR prop', pull_request: {}}];
401449
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
402450
const lastRelease = {version: '1.0.0'};
403-
const commits = [{hash: '123', message: 'Commit 1 message'}];
451+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
404452
const nextRelease = {version: '2.0.0'};
405453
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
406454
const github = authenticate()
@@ -443,7 +491,10 @@ test.serial('Ignore errors when adding comments and closing issues', async t =>
443491
];
444492
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}}];
445493
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
446-
const commits = [{hash: '123', message: 'Commit 1 message'}, {hash: '456', message: 'Commit 2 message'}];
494+
const commits = [
495+
{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}},
496+
{hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}},
497+
];
447498
const nextRelease = {version: '1.0.0'};
448499
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
449500
const github = authenticate()
@@ -502,7 +553,7 @@ test.serial('Close open issues when a release is successful', async t => {
502553
{number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle},
503554
];
504555
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
505-
const commits = [{hash: '123', message: 'Commit 1 message'}];
556+
const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}];
506557
const nextRelease = {version: '1.0.0'};
507558
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
508559
const github = authenticate()

0 commit comments

Comments
 (0)