Skip to content

Commit 072b112

Browse files
committed
feat: add labels to PRs and issues fixed in a release
1 parent c15ca0b commit 072b112

File tree

8 files changed

+343
-115
lines changed

8 files changed

+343
-115
lines changed

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,18 @@ Follow the [Creating a personal access token for the command line](https://help.
6262

6363
### Options
6464

65-
| Option | Description | Default |
66-
|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
67-
| `githubUrl` | The GitHub Enterprise endpoint. | `GH_URL` or `GITHUB_URL` environment variable. |
68-
| `githubApiPathPrefix` | The GitHub Enterprise API prefix. | `GH_PREFIX` or `GITHUB_PREFIX` environment variable. |
69-
| `proxy` | The proxy to use to access the GitHub API. See [proxy](#proxy). | `HTTP_PROXY` environment variable. |
70-
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
71-
| `successComment` | The comment added to each issue and pull request resolved by the release. Set to `false` to disable commenting on issues and pull requests. See [successComment](#successcomment). | `:tada: This issue has been resolved in version ${nextRelease.version} :tada:\n\nThe release is available on [GitHub release](<github_release_url>)` |
72-
| `failComment` | The content of the issue created when a release fails. Set to `false` to disable opening an issue when a release fails. See [failComment](#failcomment). | Friendly message with links to **semantic-release** documentation and support, with the list of errors that caused the release to fail. |
73-
| `failTitle` | The title of the issue created when a release fails. Set to `false` to disable opening an issue when a release fails. | `The automated release is failing 🚨` |
74-
| `labels` | The [labels](https://help.github.com/articles/about-labels) to add to the issue created when a release fails. Set to `false` to not add any label. | `['semantic-release']` |
75-
| `assignees` | The [assignees](https://help.github.com/articles/assigning-issues-and-pull-requests-to-other-github-users) to add to the issue created when a release fails. | - |
65+
| Option | Description | Default |
66+
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
67+
| `githubUrl` | The GitHub Enterprise endpoint. | `GH_URL` or `GITHUB_URL` environment variable. |
68+
| `githubApiPathPrefix` | The GitHub Enterprise API prefix. | `GH_PREFIX` or `GITHUB_PREFIX` environment variable. |
69+
| `proxy` | The proxy to use to access the GitHub API. See [proxy](#proxy). | `HTTP_PROXY` environment variable. |
70+
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
71+
| `successComment` | The comment to add to each issue and pull request resolved by the release. Set to `false` to disable commenting on issues and pull requests. See [successComment](#successcomment). | `:tada: This issue has been resolved in version ${nextRelease.version} :tada:\n\nThe release is available on [GitHub release](<github_release_url>)` |
72+
| `failComment` | The content of the issue created when a release fails. Set to `false` to disable opening an issue when a release fails. See [failComment](#failcomment). | Friendly message with links to **semantic-release** documentation and support, with the list of errors that caused the release to fail. |
73+
| `failTitle` | The title of the issue created when a release fails. Set to `false` to disable opening an issue when a release fails. | `The automated release is failing 🚨` |
74+
| `labels` | The [labels](https://help.github.com/articles/about-labels) to add to the issue created when a release fails. Set to `false` to not add any label. | `['semantic-release']` |
75+
| `assignees` | The [assignees](https://help.github.com/articles/assigning-issues-and-pull-requests-to-other-github-users) to add to the issue created when a release fails. | - |
76+
| `releasedLabels` | The [labels](https://help.github.com/articles/about-labels) to add to each issue and pull request resolved by the release. Set to `false` to not add any label. | `['released']` |
7677

7778
#### proxy
7879

lib/definitions/errors.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Your configuration for the \`labels\` option is \`${stringify(labels)}\`.`,
4949
details: `The [assignees option](${linkify('README.md#options')}) must be an \`Array\` of non empty \`Strings\`.
5050
5151
Your configuration for the \`assignees\` option is \`${stringify(assignees)}\`.`,
52+
}),
53+
EINVALIDRELEASEDLABELS: ({releasedLabels}) => ({
54+
message: 'Invalid `releasedLabels` option.',
55+
details: `The [releasedLabels option](${linkify(
56+
'README.md#options'
57+
)}) if defined, must be an \`Array\` of non empty \`String\`.
58+
59+
Your configuration for the \`releasedLabels\` option is \`${stringify(releasedLabels)}\`.`,
5260
}),
5361
EINVALIDGITHUBURL: () => ({
5462
message: 'The git repository URL is not a valid GitHub URL.',

lib/resolve-config.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
const {isNil, castArray} = require('lodash');
22

33
module.exports = (
4-
{githubUrl, githubApiPathPrefix, proxy, assets, successComment, failTitle, failComment, labels, assignees},
4+
{
5+
githubUrl,
6+
githubApiPathPrefix,
7+
proxy,
8+
assets,
9+
successComment,
10+
failTitle,
11+
failComment,
12+
labels,
13+
assignees,
14+
releasedLabels,
15+
},
516
{env}
617
) => ({
718
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
@@ -14,4 +25,5 @@ module.exports = (
1425
failComment,
1526
labels: isNil(labels) ? ['semantic-release'] : labels === false ? false : castArray(labels),
1627
assignees: assignees ? castArray(assignees) : assignees,
28+
releasedLabels: isNil(releasedLabels) ? ['released'] : releasedLabels === false ? false : castArray(releasedLabels),
1729
});

lib/success.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ module.exports = async (pluginConfig, context) => {
1919
releases,
2020
logger,
2121
} = context;
22-
const {githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failComment, failTitle} = resolveConfig(
23-
pluginConfig,
24-
context
25-
);
22+
const {
23+
githubToken,
24+
githubUrl,
25+
githubApiPathPrefix,
26+
proxy,
27+
successComment,
28+
failComment,
29+
failTitle,
30+
releasedLabels,
31+
} = resolveConfig(pluginConfig, context);
2632
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
2733
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
2834
const errors = [];
@@ -75,8 +81,13 @@ module.exports = async (pluginConfig, context) => {
7581
data: {html_url: url},
7682
} = await github.issues.createComment(comment);
7783
logger.log('Added comment to issue #%d: %s', issue.number, url);
84+
85+
if (releasedLabels) {
86+
await github.issues.addLabels({owner, repo, number: issue.number, labels: releasedLabels});
87+
logger.log('Added labels %O to issue #%d', releasedLabels, issue.number);
88+
}
7889
} else {
79-
logger.log("Skip comment on issue #%d as it's open: %s", issue.number);
90+
logger.log("Skip comment and labels on issue #%d as it's open: %s", issue.number);
8091
}
8192
} catch (error) {
8293
if (error.code === 404) {

lib/verify.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const VALIDATORS = {
2222
failComment: canBeDisabled(isNonEmptyString),
2323
labels: canBeDisabled(isArrayOf(isNonEmptyString)),
2424
assignees: isArrayOf(isNonEmptyString),
25+
releasedLabels: canBeDisabled(isArrayOf(isNonEmptyString)),
2526
};
2627

2728
module.exports = async (pluginConfig, context) => {

test/integration.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ test.serial('Publish a release with an array of assets', async t => {
178178
t.true(githubUpload2.isDone());
179179
});
180180

181-
test.serial('Comment on PR included in the releases', async t => {
181+
test.serial('Comment and add labels on PR included in the releases', async t => {
182182
const owner = 'test_user';
183183
const repo = 'test_repo';
184184
const env = {GITHUB_TOKEN: 'github_token'};
@@ -201,6 +201,8 @@ test.serial('Comment on PR included in the releases', async t => {
201201
.reply(200, [{sha: commits[0].hash}])
202202
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
203203
.reply(200, {html_url: 'https://github.com/successcomment-1'})
204+
.post(`/repos/${owner}/${repo}/issues/1/labels`, '["released"]')
205+
.reply(200, {})
204206
.get(
205207
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
206208
'state:open'
@@ -212,6 +214,7 @@ test.serial('Comment on PR included in the releases', async t => {
212214

213215
t.deepEqual(t.context.log.args[0], ['Verify GitHub authentication']);
214216
t.true(t.context.log.calledWith('Added comment to issue #%d: %s', 1, 'https://github.com/successcomment-1'));
217+
t.true(t.context.log.calledWith('Added labels %O to issue #%d', ['released'], 1));
215218
t.true(github.isDone());
216219
});
217220

@@ -289,6 +292,8 @@ test.serial('Verify, release and notify success', async t => {
289292
.reply(200, [{sha: commits[0].hash}])
290293
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
291294
.reply(200, {html_url: 'https://github.com/successcomment-1'})
295+
.post(`/repos/${owner}/${repo}/issues/1/labels`, '["released"]')
296+
.reply(200, {})
292297
.get(
293298
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
294299
'state:open'

0 commit comments

Comments
 (0)