Skip to content

Commit b69986f

Browse files
committedDec 17, 2018
fix: also add comment/label to open issues
1 parent 5f19284 commit b69986f

File tree

2 files changed

+19
-80
lines changed

2 files changed

+19
-80
lines changed
 

‎lib/success.js

+18-24
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,24 @@ module.exports = async (pluginConfig, context) => {
7575
? template(successComment)({...context, issue})
7676
: getSuccessComment(issue, releaseInfos, nextRelease);
7777
try {
78-
const state = issue.state || (await github.issues.get({owner, repo, number: issue.number})).data.state;
79-
80-
if (state === 'closed') {
81-
const comment = {owner, repo, number: issue.number, body};
82-
debug('create comment: %O', comment);
83-
const {
84-
data: {html_url: url},
85-
} = await github.issues.createComment(comment);
86-
logger.log('Added comment to issue #%d: %s', issue.number, url);
87-
88-
if (releasedLabels) {
89-
const labels = releasedLabels.map(label => template(label)(context));
90-
// Don’t use .issues.addLabels for GHE < 2.16 support
91-
// https://github.com/semantic-release/github/issues/138
92-
await github.request('POST /repos/:owner/:repo/issues/:number/labels', {
93-
owner,
94-
repo,
95-
number: issue.number,
96-
data: labels,
97-
});
98-
logger.log('Added labels %O to issue #%d', labels, issue.number);
99-
}
100-
} else {
101-
logger.log("Skip comment and labels on issue #%d as it's open: %s", issue.number);
78+
const comment = {owner, repo, number: issue.number, body};
79+
debug('create comment: %O', comment);
80+
const {
81+
data: {html_url: url},
82+
} = await github.issues.createComment(comment);
83+
logger.log('Added comment to issue #%d: %s', issue.number, url);
84+
85+
if (releasedLabels) {
86+
const labels = releasedLabels.map(label => template(label)(context));
87+
// Don’t use .issues.addLabels for GHE < 2.16 support
88+
// https://github.com/semantic-release/github/issues/138
89+
await github.request('POST /repos/:owner/:repo/issues/:number/labels', {
90+
owner,
91+
repo,
92+
number: issue.number,
93+
data: labels,
94+
});
95+
logger.log('Added labels %O to issue #%d', labels, issue.number);
10296
}
10397
} catch (error) {
10498
if (error.status === 404) {

‎test/success.test.js

+1-56
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test.afterEach.always(() => {
2727
});
2828

2929
test.serial(
30-
'Add comment and labels to PRs associated with release commits and issues closed by PR/commits comments',
30+
'Add comment and labels to PRs associated with release commits and issues solved by PR/commits comments',
3131
async t => {
3232
const owner = 'test_user';
3333
const repo = 'test_repo';
@@ -73,14 +73,10 @@ test.serial(
7373
.reply(200, {html_url: 'https://github.com/successcomment-2'})
7474
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/2/labels`, '["released"]')
7575
.reply(200, {})
76-
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3`)
77-
.reply(200, {state: 'closed'})
7876
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/comments`, {body: /This issue has been resolved/})
7977
.reply(200, {html_url: 'https://github.com/successcomment-3'})
8078
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/labels`, '["released"]')
8179
.reply(200, {})
82-
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4`)
83-
.reply(200, {state: 'closed'})
8480
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/comments`, {body: /This issue has been resolved/})
8581
.reply(200, {html_url: 'https://github.com/successcomment-4'})
8682
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/labels`, '["released"]')
@@ -147,14 +143,10 @@ test.serial(
147143
.reply(200, {html_url: 'https://custom-url.com/successcomment-2'})
148144
.post(`/repos/${owner}/${repo}/issues/2/labels`, '["released on @next"]')
149145
.reply(200, {})
150-
.get(`/repos/${owner}/${repo}/issues/3`)
151-
.reply(200, {state: 'closed'})
152146
.post(`/repos/${owner}/${repo}/issues/3/comments`, {body: /This issue has been resolved/})
153147
.reply(200, {html_url: 'https://custom-url.com/successcomment-3'})
154148
.post(`/repos/${owner}/${repo}/issues/3/labels`, '["released on @next"]')
155149
.reply(200, {})
156-
.get(`/repos/${owner}/${repo}/issues/4`)
157-
.reply(200, {state: 'closed'})
158150
.post(`/repos/${owner}/${repo}/issues/4/comments`, {body: /This issue has been resolved/})
159151
.reply(200, {html_url: 'https://custom-url.com/successcomment-4'})
160152
.post(`/repos/${owner}/${repo}/issues/4/labels`, '["released on @next"]')
@@ -330,49 +322,6 @@ test.serial(
330322
}
331323
);
332324

333-
test.serial('Do not add comment and labels to open issues/PRs', async t => {
334-
const owner = 'test_user';
335-
const repo = 'test_repo';
336-
const env = {GITHUB_TOKEN: 'github_token'};
337-
const failTitle = 'The automated release is failing 🚨';
338-
const pluginConfig = {failTitle};
339-
const prs = [{number: 1, pull_request: {}, body: 'Fixes #2', state: 'closed'}];
340-
const options = {branch: 'master', repositoryUrl: `https://github.com/${owner}/${repo}.git`};
341-
const commits = [{hash: '123', message: 'Commit 1 message'}];
342-
const nextRelease = {version: '1.0.0'};
343-
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
344-
const github = authenticate(env)
345-
.get(`/repos/${owner}/${repo}`)
346-
.reply(200, {full_name: `${owner}/${repo}`})
347-
.get(
348-
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
349-
.map(commit => commit.hash)
350-
.join('+')}`
351-
)
352-
.reply(200, {items: prs})
353-
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
354-
.reply(200, [{sha: commits[0].hash}])
355-
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
356-
.reply(200, {html_url: 'https://github.com/successcomment-1'})
357-
.post(`/repos/${owner}/${repo}/issues/1/labels`, '["released"]')
358-
.reply(200, {})
359-
.get(`/repos/${owner}/${repo}/issues/2`)
360-
.reply(200, {state: 'open'})
361-
.get(
362-
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
363-
'state:open'
364-
)}+${escape(failTitle)}`
365-
)
366-
.reply(200, {items: []});
367-
368-
await success(pluginConfig, {env, options, commits, nextRelease, releases, logger: t.context.logger});
369-
370-
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 1, 'https://github.com/successcomment-1'));
371-
t.true(t.context.log.calledWith('Added labels %O to issue #%d', ['released'], 1));
372-
t.true(t.context.log.calledWith("Skip comment and labels on issue #%d as it's open: %s", 2));
373-
t.true(github.isDone());
374-
});
375-
376325
test.serial('Do not add comment and labels if no PR is associated with release commits', async t => {
377326
const owner = 'test_user';
378327
const repo = 'test_repo';
@@ -427,8 +376,6 @@ test.serial('Do not add comment and labels to PR/issues from other repo', async
427376
.join('+')}`
428377
)
429378
.reply(200, {items: []})
430-
.get(`/repos/${owner}/${repo}/issues/2`)
431-
.reply(200, {state: 'closed'})
432379
.post(`/repos/${owner}/${repo}/issues/2/comments`, {body: /This issue has been resolved/})
433380
.reply(200, {html_url: 'https://github.com/successcomment-2'})
434381
.post(`/repos/${owner}/${repo}/issues/2/labels`, '["released"]')
@@ -481,8 +428,6 @@ test.serial('Ignore missing issues/PRs', async t => {
481428
.post(`/repos/${owner}/${repo}/issues/2/comments`, {body: /This PR is included/})
482429
.times(3)
483430
.reply(404)
484-
.get(`/repos/${owner}/${repo}/issues/3`)
485-
.reply(200, {state: 'closed'})
486431
.post(`/repos/${owner}/${repo}/issues/3/comments`, {body: /This issue has been resolved/})
487432
.reply(200, {html_url: 'https://github.com/successcomment-3'})
488433
.post(`/repos/${owner}/${repo}/issues/3/labels`, '["released"]')

0 commit comments

Comments
 (0)