Skip to content

Commit

Permalink
fix: do not add comment for unrelated PR returned by search
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Apr 6, 2018
1 parent c4347eb commit 599e0df
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
17 changes: 9 additions & 8 deletions lib/success.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {isUndefined, uniqBy, template} = require('lodash');
const {isUndefined, uniqBy, template, flatten} = require('lodash');
const parseGithubUrl = require('parse-github-url');
const pFilter = require('p-filter');
const AggregateError = require('aggregate-error');
const issueParser = require('issue-parser')('github');
const debug = require('debug')('semantic-release:github');
Expand All @@ -17,14 +18,14 @@ module.exports = async (
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix});
const releaseInfos = releases.filter(release => Boolean(release.name));
const shas = commits.map(commit => commit.hash);

const prs = [].concat(
...(await Promise.all(
getSearchQueries(`repo:${owner}/${repo}+type:pr+is:merged`, commits.map(commit => commit.hash)).map(async q => {
const {data: {items}} = await github.search.issues({q});
return items;
})
))
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}) => shas.includes(sha))
);

debug('found pull requests: %O', prs.map(pr => pr.number));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"issue-parser": "^1.0.2",
"lodash": "^4.17.4",
"mime": "^2.0.3",
"p-filter": "^1.0.0",
"p-retry": "^1.0.0",
"parse-github-url": "^1.0.1",
"url-join": "^4.0.0"
Expand Down
4 changes: 4 additions & 0 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ test.serial('Comment on PR included in the releases', async t => {
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down Expand Up @@ -288,6 +290,8 @@ test.serial('Verify, release and notify success', async t => {
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down
69 changes: 68 additions & 1 deletion test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ test.serial('Add comment to PRs associated with release commits and issues close
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.reply(200, [{sha: commits[1].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down Expand Up @@ -135,6 +139,18 @@ test.serial('Make multiple search queries if necessary', async t => {
}+${commits[6].hash}`
)
.reply(200, {items: [prs[5], prs[1]]})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.reply(200, [{sha: commits[1].hash}])
.get(`/repos/${owner}/${repo}/pulls/3/commits`)
.reply(200, [{sha: commits[2].hash}])
.get(`/repos/${owner}/${repo}/pulls/4/commits`)
.reply(200, [{sha: commits[3].hash}])
.get(`/repos/${owner}/${repo}/pulls/5/commits`)
.reply(200, [{sha: commits[4].hash}])
.get(`/repos/${owner}/${repo}/pulls/6/commits`)
.reply(200, [{sha: commits[5].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down Expand Up @@ -177,6 +193,45 @@ 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', async t => {
const owner = 'test_user';
const repo = 'test_repo';
process.env.GITHUB_TOKEN = 'github_token';
const failTitle = 'The automated release is failing 🚨';
const pluginConfig = {failTitle};
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}}];
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
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()
.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: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.reply(200, [{sha: 'unrelated_commit'}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.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, {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 to open issues/PRs', async t => {
const owner = 'test_user';
const repo = 'test_repo';
Expand All @@ -195,6 +250,8 @@ test.serial('Do not add comment to open issues/PRs', async t => {
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down Expand Up @@ -299,6 +356,10 @@ test.serial('Ignore missing issues/PRs', async t => {
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.reply(200, [{sha: commits[1].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down Expand Up @@ -349,6 +410,8 @@ test.serial('Add custom comment', async t => {
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {
Expand Down Expand Up @@ -380,7 +443,7 @@ test.serial('Ignore errors when adding comments and closing issues', async t =>
];
const prs = [{number: 1, pull_request: {}}, {number: 2, pull_request: {}}];
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const commits = [{hash: '123', message: 'Commit 1 message'}];
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()
Expand All @@ -390,6 +453,10 @@ test.serial('Ignore errors when adding comments and closing issues', async t =>
.join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.reply(200, [{sha: commits[1].hash}])
.get(`/repos/${owner}/${repo}/issues/1`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
Expand Down

0 comments on commit 599e0df

Please sign in to comment.