Skip to content

Commit

Permalink
feat: parse issue referenced with full URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Jul 10, 2018
1 parent 5714da6 commit 3dd2907
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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 issueParser = require('issue-parser');
const debug = require('debug')('semantic-release:github');
const resolveConfig = require('./resolve-config');
const getClient = require('./get-client');
Expand All @@ -17,6 +17,7 @@ module.exports = async (
const {githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failTitle} = resolveConfig(pluginConfig);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
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);
Expand All @@ -37,7 +38,7 @@ module.exports = async (
const issues = [...prs.map(pr => pr.body), ...commits.map(commit => commit.message)].reduce((issues, message) => {
return message
? issues.concat(
issueParser(message)
parser(message)
.actions.filter(action => isUndefined(action.slug) || action.slug === `${owner}/${repo}`)
.map(action => ({number: parseInt(action.issue, 10)}))
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"globby": "^8.0.0",
"http-proxy-agent": "^2.1.0",
"https-proxy-agent": "^2.2.1",
"issue-parser": "^2.0.0",
"issue-parser": "^2.2.0",
"lodash": "^4.17.4",
"mime": "^2.0.3",
"p-filter": "^1.0.0",
Expand Down
68 changes: 67 additions & 1 deletion test/success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test.serial('Add comment to PRs associated with release commits and issues close
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 #4', tree: {long: 'ccc'}},
{hash: '789', message: `Commit 3 message Closes https://github.com/${owner}/${repo}/issues/4`, tree: {long: 'ccc'}},
];
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
Expand Down Expand Up @@ -99,6 +99,72 @@ test.serial('Add comment to PRs associated with release commits and issues close
t.true(github.isDone());
});

test.serial(
'Add comment to PRs associated with release commits and issues closed by PR/commits comments with custom URL',
async t => {
const owner = 'test_user';
const repo = 'test_repo';
process.env.GH_URL = 'https://custom-url.com';
process.env.GH_TOKEN = 'github_token';
process.env.GH_PREFIX = 'prefix';
const failTitle = 'The automated release is failing 🚨';
const pluginConfig = {failTitle};
const prs = [
{number: 1, pull_request: {}, state: 'closed'},
{number: 2, pull_request: {}, body: 'Fixes #3', state: 'closed'},
];
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'},
},
];
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://custom-url.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: commits[1].hash}])
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
.reply(200, {html_url: 'https://custom-url.com/successcomment-1'})
.post(`/repos/${owner}/${repo}/issues/2/comments`, {body: /This PR is included/})
.reply(200, {html_url: 'https://custom-url.com/successcomment-2'})
.get(`/repos/${owner}/${repo}/issues/3`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/3/comments`, {body: /This issue has been resolved/})
.reply(200, {html_url: 'https://custom-url.com/successcomment-3'})
.get(`/repos/${owner}/${repo}/issues/4`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/4/comments`, {body: /This issue has been resolved/})
.reply(200, {html_url: 'https://custom-url.com/successcomment-4'})
.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://custom-url.com/successcomment-1'));
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 2, 'https://custom-url.com/successcomment-2'));
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 3, 'https://custom-url.com/successcomment-3'));
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 4, 'https://custom-url.com/successcomment-4'));
t.true(github.isDone());
}
);

test.serial('Make multiple search queries if necessary', async t => {
const owner = 'test_user';
const repo = 'test_repo';
Expand Down

0 comments on commit 3dd2907

Please sign in to comment.