From 152fe3264460123fe28c705636444a4d595aebf1 Mon Sep 17 00:00:00 2001 From: Pierre Vanduynslager Date: Thu, 2 Aug 2018 13:16:26 -0400 Subject: [PATCH] fix: compare release and PR commits with merge_commit_sha --- lib/success.js | 12 ++--- test/integration.test.js | 4 +- test/success.test.js | 103 ++++++++++----------------------------- 3 files changed, 35 insertions(+), 84 deletions(-) diff --git a/lib/success.js b/lib/success.js index 12503951..a4bc876d 100644 --- a/lib/success.js +++ b/lib/success.js @@ -27,17 +27,17 @@ module.exports = async (pluginConfig, context) => { const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy}); const parser = issueParser('github', githubUrl ? {hosts: [githubUrl]} : {}); const releaseInfos = releases.filter(release => Boolean(release.name)); - const shas = commits.map(commit => commit.hash); - const treeShas = commits.map(commit => commit.tree.long); + const shas = commits.map(({hash}) => hash); const searchQueries = getSearchQueries(`repo:${owner}/${repo}+type:pr+is:merged`, shas).map( async q => (await github.search.issues({q})).data.items ); - const prs = await pFilter(uniqBy(flatten(await Promise.all(searchQueries)), 'number'), async ({number}) => - (await github.pullRequests.getCommits({owner, repo, number})).data.find( - ({sha, commit}) => shas.includes(sha) || treeShas.includes(commit.tree.sha) - ) + const prs = await pFilter( + uniqBy(flatten(await Promise.all(searchQueries)), 'number'), + async ({number}) => + (await github.pullRequests.getCommits({owner, repo, number})).data.find(({sha}) => shas.includes(sha)) || + shas.includes((await github.pullRequests.get({owner, repo, number})).data.merge_commit_sha) ); debug('found pull requests: %O', prs.map(pr => pr.number)); diff --git a/test/integration.test.js b/test/integration.test.js index 23fedf7d..27b517c2 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -185,7 +185,7 @@ test.serial('Comment on PR included in the releases', async t => { const failTitle = 'The automated release is failing 🚨'; const prs = [{number: 1, pull_request: {}, state: 'closed'}]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -268,7 +268,7 @@ test.serial('Verify, release and notify success', async t => { const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`; const uploadUrl = `https://github.com${uploadUri}{?name,label}`; const prs = [{number: 1, pull_request: {}, state: 'closed'}]; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const github = authenticate(env) .get(`/repos/${owner}/${repo}`) .reply(200, {permissions: {push: true}}) diff --git a/test/success.test.js b/test/success.test.js index 9fd1bbb0..a2b46989 100644 --- a/test/success.test.js +++ b/test/success.test.js @@ -97,13 +97,9 @@ test.serial( ]; const options = {branch: 'master', repositoryUrl: `https://custom-url.com/${owner}/${repo}.git`}; const commits = [ - {hash: '123', message: 'Commit 1 message\n\n Fix #1', tree: {long: 'aaa'}}, - {hash: '456', message: 'Commit 2 message', tree: {long: 'ccc'}}, - { - hash: '789', - message: `Commit 3 message Closes https://custom-url.com/${owner}/${repo}/issues/4`, - tree: {long: 'ccc'}, - }, + {hash: '123', message: 'Commit 1 message\n\n Fix #1'}, + {hash: '456', message: 'Commit 2 message'}, + {hash: '789', message: `Commit 3 message Closes https://custom-url.com/${owner}/${repo}/issues/4`}, ]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://custom-url.com/release'}]; @@ -163,13 +159,13 @@ test.serial('Make multiple search queries if necessary', async t => { ]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; const commits = [ - {hash: repeat('a', 40), message: 'Commit 1 message', tree: {long: 'aaa'}}, - {hash: repeat('b', 40), message: 'Commit 2 message', tree: {long: 'bbb'}}, - {hash: repeat('c', 40), message: 'Commit 3 message', tree: {long: 'ccc'}}, - {hash: repeat('d', 40), message: 'Commit 4 message', tree: {long: 'ddd'}}, - {hash: repeat('e', 40), message: 'Commit 5 message', tree: {long: 'eee'}}, - {hash: repeat('f', 40), message: 'Commit 6 message', tree: {long: 'fff'}}, - {hash: repeat('g', 40), message: 'Commit 7 message', tree: {long: 'ggg'}}, + {hash: repeat('a', 40), message: 'Commit 1 message'}, + {hash: repeat('b', 40), message: 'Commit 2 message'}, + {hash: repeat('c', 40), message: 'Commit 3 message'}, + {hash: repeat('d', 40), message: 'Commit 4 message'}, + {hash: repeat('e', 40), message: 'Commit 5 message'}, + {hash: repeat('f', 40), message: 'Commit 6 message'}, + {hash: repeat('g', 40), message: 'Commit 7 message'}, ]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; @@ -228,7 +224,7 @@ test.serial('Make multiple search queries if necessary', async t => { t.true(github.isDone()); }); -test.serial('Do not add comment for unrelated PR returned by search (compare sha)', async t => { +test.serial('Do not add comment for unrelated PR returned by search (compare sha and merge_commit_sha)', async t => { const owner = 'test_user'; const repo = 'test_repo'; const env = {GITHUB_TOKEN: 'github_token'}; @@ -236,10 +232,7 @@ test.serial('Do not add comment for unrelated PR returned by search (compare sha const pluginConfig = {failTitle}; const prs = [{number: 1, pull_request: {}, state: 'closed'}, {number: 2, pull_request: {}, state: 'closed'}]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [ - {hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}, - {hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}}, - ]; + const commits = [{hash: '123', message: 'Commit 1 message'}, {hash: '456', message: 'Commit 2 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -250,49 +243,13 @@ test.serial('Do not add comment for unrelated PR returned by search (compare sha ) .reply(200, {items: prs}) .get(`/repos/${owner}/${repo}/pulls/1/commits`) - .reply(200, [{sha: commits[0].hash}]) + .reply(200, [{sha: 'rebased_sha'}]) + .get(`/repos/${owner}/${repo}/pulls/1`) + .reply(200, {merge_commit_sha: commits[0].hash}) .get(`/repos/${owner}/${repo}/pulls/2/commits`) - .reply(200, [{sha: 'unrelated_commit', commit: {tree: {sha: 'unrelated_commit'}}}]) - .post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/}) - .reply(200, {html_url: 'https://github.com/successcomment-1'}) - .get( - `/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape( - 'state:open' - )}+${escape(failTitle)}` - ) - .reply(200, {items: []}); - - await success(pluginConfig, {env, options, commits, nextRelease, releases, logger: t.context.logger}); - - t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 1, 'https://github.com/successcomment-1')); - t.true(github.isDone()); -}); - -test.serial('Do not add comment for unrelated PR returned by search (compare tree sha)', async t => { - const owner = 'test_user'; - const repo = 'test_repo'; - const env = {GITHUB_TOKEN: 'github_token'}; - const failTitle = 'The automated release is failing 🚨'; - const pluginConfig = {failTitle}; - const prs = [{number: 1, pull_request: {}, state: 'closed'}, {number: 2, pull_request: {}, state: 'closed'}]; - const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [ - {hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}, - {hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}}, - ]; - const nextRelease = {version: '1.0.0'}; - const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; - const github = authenticate(env) - .get( - `/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits - .map(commit => commit.hash) - .join('+')}` - ) - .reply(200, {items: prs}) - .get(`/repos/${owner}/${repo}/pulls/1/commits`) - .reply(200, [{sha: 'rebased_sha', commit: {tree: {sha: commits[0].tree.long}}}]) - .get(`/repos/${owner}/${repo}/pulls/2/commits`) - .reply(200, [{sha: 'unrelated_commit', commit: {tree: {sha: 'unrelated_commit'}}}]) + .reply(200, [{sha: 'rebased_sha'}]) + .get(`/repos/${owner}/${repo}/pulls/2`) + .reply(200, {merge_commit_sha: 'unrelated_sha'}) .post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/}) .reply(200, {html_url: 'https://github.com/successcomment-1'}) .get( @@ -316,7 +273,7 @@ test.serial('Do not add comment to open issues/PRs', async t => { const pluginConfig = {failTitle}; const prs = [{number: 1, pull_request: {}, body: 'Fixes #2', state: 'closed'}]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -353,7 +310,7 @@ test.serial('Do not add comment if no PR is associated with release commits', as const failTitle = 'The automated release is failing 🚨'; const pluginConfig = {failTitle}; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -383,9 +340,9 @@ test.serial('Do not add comment to PR/issues from other repo', async t => { const pluginConfig = {failTitle}; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; const commits = [ - {hash: '123', message: 'Commit 1 message\n\n Fix other/other#1', tree: {long: 'aaa'}}, - {hash: '456', message: `Commit 2 message Fix ${owner}/${repo}#2`, tree: {long: 'bbb'}}, - {hash: '789', message: 'Commit 3 message Closes other/other#3', tree: {long: 'ccc'}}, + {hash: '123', message: 'Commit 1 message\n\n Fix other/other#1'}, + {hash: '456', message: `Commit 2 message Fix ${owner}/${repo}#2`}, + {hash: '789', message: 'Commit 3 message Closes other/other#3'}, ]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; @@ -423,10 +380,7 @@ test.serial('Ignore missing issues/PRs', async t => { {number: 2, pull_request: {}, body: 'Fixes #3', state: 'closed'}, ]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [ - {hash: '123', message: 'Commit 1 message\n\n Fix #1', tree: {long: 'aaa'}}, - {hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}}, - ]; + const commits = [{hash: '123', message: 'Commit 1 message\n\n Fix #1'}, {hash: '456', message: 'Commit 2 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -476,7 +430,7 @@ test.serial('Add custom comment', async t => { const prs = [{number: 1, prop: 'PR prop', pull_request: {}, state: 'closed'}]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; const lastRelease = {version: '1.0.0'}; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const nextRelease = {version: '2.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -517,10 +471,7 @@ test.serial('Ignore errors when adding comments and closing issues', async t => ]; const prs = [{number: 1, pull_request: {}, state: 'closed'}, {number: 2, pull_request: {}, state: 'closed'}]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [ - {hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}, - {hash: '456', message: 'Commit 2 message', tree: {long: 'bbb'}}, - ]; + const commits = [{hash: '123', message: 'Commit 1 message'}, {hash: '456', message: 'Commit 2 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env) @@ -575,7 +526,7 @@ test.serial('Close open issues when a release is successful', async t => { {number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle}, ]; const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`}; - const commits = [{hash: '123', message: 'Commit 1 message', tree: {long: 'aaa'}}]; + const commits = [{hash: '123', message: 'Commit 1 message'}]; const nextRelease = {version: '1.0.0'}; const releases = [{name: 'GitHub release', url: 'https://github.com/release'}]; const github = authenticate(env)