Skip to content

Commit

Permalink
fix: compare release and PR commits with merge_commit_sha
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Aug 3, 2018
1 parent 8cd24ca commit 152fe32
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 84 deletions.
12 changes: 6 additions & 6 deletions lib/success.js
Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions test/integration.test.js
Expand Up @@ -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)
Expand Down Expand Up @@ -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}})
Expand Down
103 changes: 27 additions & 76 deletions test/success.test.js
Expand Up @@ -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'}];
Expand Down Expand Up @@ -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'}];
Expand Down Expand Up @@ -228,18 +224,15 @@ 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'};
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 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)
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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'}];
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 152fe32

Please sign in to comment.