Skip to content

Commit

Permalink
feat: add labels to PRs and issues fixed in a release
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Oct 8, 2018
1 parent c15ca0b commit 072b112
Show file tree
Hide file tree
Showing 8 changed files with 343 additions and 115 deletions.
23 changes: 12 additions & 11 deletions README.md
Expand Up @@ -62,17 +62,18 @@ Follow the [Creating a personal access token for the command line](https://help.

### Options

| Option | Description | Default |
|-----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `githubUrl` | The GitHub Enterprise endpoint. | `GH_URL` or `GITHUB_URL` environment variable. |
| `githubApiPathPrefix` | The GitHub Enterprise API prefix. | `GH_PREFIX` or `GITHUB_PREFIX` environment variable. |
| `proxy` | The proxy to use to access the GitHub API. See [proxy](#proxy). | `HTTP_PROXY` environment variable. |
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
| `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>)` |
| `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. |
| `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 🚨` |
| `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']` |
| `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. | - |
| Option | Description | Default |
|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `githubUrl` | The GitHub Enterprise endpoint. | `GH_URL` or `GITHUB_URL` environment variable. |
| `githubApiPathPrefix` | The GitHub Enterprise API prefix. | `GH_PREFIX` or `GITHUB_PREFIX` environment variable. |
| `proxy` | The proxy to use to access the GitHub API. See [proxy](#proxy). | `HTTP_PROXY` environment variable. |
| `assets` | An array of files to upload to the release. See [assets](#assets). | - |
| `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>)` |
| `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. |
| `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 🚨` |
| `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']` |
| `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. | - |
| `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']` |

#### proxy

Expand Down
8 changes: 8 additions & 0 deletions lib/definitions/errors.js
Expand Up @@ -49,6 +49,14 @@ Your configuration for the \`labels\` option is \`${stringify(labels)}\`.`,
details: `The [assignees option](${linkify('README.md#options')}) must be an \`Array\` of non empty \`Strings\`.
Your configuration for the \`assignees\` option is \`${stringify(assignees)}\`.`,
}),
EINVALIDRELEASEDLABELS: ({releasedLabels}) => ({
message: 'Invalid `releasedLabels` option.',
details: `The [releasedLabels option](${linkify(
'README.md#options'
)}) if defined, must be an \`Array\` of non empty \`String\`.
Your configuration for the \`releasedLabels\` option is \`${stringify(releasedLabels)}\`.`,
}),
EINVALIDGITHUBURL: () => ({
message: 'The git repository URL is not a valid GitHub URL.',
Expand Down
14 changes: 13 additions & 1 deletion lib/resolve-config.js
@@ -1,7 +1,18 @@
const {isNil, castArray} = require('lodash');

module.exports = (
{githubUrl, githubApiPathPrefix, proxy, assets, successComment, failTitle, failComment, labels, assignees},
{
githubUrl,
githubApiPathPrefix,
proxy,
assets,
successComment,
failTitle,
failComment,
labels,
assignees,
releasedLabels,
},
{env}
) => ({
githubToken: env.GH_TOKEN || env.GITHUB_TOKEN,
Expand All @@ -14,4 +25,5 @@ module.exports = (
failComment,
labels: isNil(labels) ? ['semantic-release'] : labels === false ? false : castArray(labels),
assignees: assignees ? castArray(assignees) : assignees,
releasedLabels: isNil(releasedLabels) ? ['released'] : releasedLabels === false ? false : castArray(releasedLabels),
});
21 changes: 16 additions & 5 deletions lib/success.js
Expand Up @@ -19,10 +19,16 @@ module.exports = async (pluginConfig, context) => {
releases,
logger,
} = context;
const {githubToken, githubUrl, githubApiPathPrefix, proxy, successComment, failComment, failTitle} = resolveConfig(
pluginConfig,
context
);
const {
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
successComment,
failComment,
failTitle,
releasedLabels,
} = resolveConfig(pluginConfig, context);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
const errors = [];
Expand Down Expand Up @@ -75,8 +81,13 @@ module.exports = async (pluginConfig, context) => {
data: {html_url: url},
} = await github.issues.createComment(comment);
logger.log('Added comment to issue #%d: %s', issue.number, url);

if (releasedLabels) {
await github.issues.addLabels({owner, repo, number: issue.number, labels: releasedLabels});
logger.log('Added labels %O to issue #%d', releasedLabels, issue.number);
}
} else {
logger.log("Skip comment on issue #%d as it's open: %s", issue.number);
logger.log("Skip comment and labels on issue #%d as it's open: %s", issue.number);
}
} catch (error) {
if (error.code === 404) {
Expand Down
1 change: 1 addition & 0 deletions lib/verify.js
Expand Up @@ -22,6 +22,7 @@ const VALIDATORS = {
failComment: canBeDisabled(isNonEmptyString),
labels: canBeDisabled(isArrayOf(isNonEmptyString)),
assignees: isArrayOf(isNonEmptyString),
releasedLabels: canBeDisabled(isArrayOf(isNonEmptyString)),
};

module.exports = async (pluginConfig, context) => {
Expand Down
7 changes: 6 additions & 1 deletion test/integration.test.js
Expand Up @@ -178,7 +178,7 @@ test.serial('Publish a release with an array of assets', async t => {
t.true(githubUpload2.isDone());
});

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

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

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

0 comments on commit 072b112

Please sign in to comment.