Skip to content

Commit

Permalink
feat: add discussionCategoryName option (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhahao authored Sep 26, 2023
1 parent 9e2678c commit 34c0afd
Show file tree
Hide file tree
Showing 9 changed files with 325 additions and 29 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,23 @@ When using the _GITHUB_TOKEN_, the **minimum required permissions** are:

### 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. Set to `false` to disable usage of proxy. 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. See [releasedLabels](#releasedlabels). | `['released<%= nextRelease.channel ? \` on @\${nextRelease.channel}\` : "" %>']- |
| `addReleases` | Will add release links to the GitHub Release. Can be `false`, `"bottom"` or `"top"`. See [addReleases](#addReleases). | `false` |
| `draftRelease` | A boolean indicating if a GitHub Draft Release should be created instead of publishing an actual GitHub Release. | `false` |
| `releaseNameTemplate` | A [Lodash template](https://lodash.com/docs#template) to customize the github release's name | `<%= nextverison.name %>` |
| `releaseBodyTemplate` | A [Lodash template](https://lodash.com/docs#template) to customize the github release's body | `<%= nextverison.notes %>` |
| 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. Set to `false` to disable usage of proxy. 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. See [releasedLabels](#releasedlabels). | `['released<%= nextRelease.channel ? \` on @\${nextRelease.channel}\` : "" %>']- |
| `addReleases` | Will add release links to the GitHub Release. Can be `false`, `"bottom"` or `"top"`. See [addReleases](#addReleases). | `false` |
| `draftRelease` | A boolean indicating if a GitHub Draft Release should be created instead of publishing an actual GitHub Release. | `false` |
| `releaseNameTemplate` | A [Lodash template](https://lodash.com/docs#template) to customize the github release's name | `<%= nextverison.name %>` |
| `releaseBodyTemplate` | A [Lodash template](https://lodash.com/docs#template) to customize the github release's body | `<%= nextverison.notes %>` |
| `discussionCategoryName` | The category name in which to create a linked discussion to the release. Set to `false` to disable creating discussion for a release. | `false` |

#### proxy

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export async function verifyConditions(
{ Octokit = SemanticReleaseOctokit } = {},
) {
const { options } = context;
// If the GitHub publish plugin is used and has `assets`, `successComment`, `failComment`, `failTitle`, `labels` or `assignees` configured, validate it now in order to prevent any release if the configuration is wrong
// If the GitHub publish plugin is used and has `assets`, `successComment`, `failComment`, `failTitle`, `labels`, `discussionCategoryName` or `assignees` configured, validate it now in order to prevent any release if the configuration is wrong
if (options.publish) {
const publishPlugin =
castArray(options.publish).find(
Expand All @@ -42,6 +42,10 @@ export async function verifyConditions(
pluginConfig.assignees,
publishPlugin.assignees,
);
pluginConfig.discussionCategoryName = defaultTo(
pluginConfig.discussionCategoryName,
publishPlugin.discussionCategoryName,
);
}

await verifyGitHub(pluginConfig, context, { Octokit });
Expand Down
13 changes: 13 additions & 0 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ Your configuration for the \`releaseNameTemplate\` option is \`${stringify(
)}\`.`,
};
}

export function EINVALIDDISCUSSIONCATEGORYNAME({ discussionCategoryName }) {
return {
message: "Invalid `discussionCategoryName` option.",
details: `The [discussionCategoryName option](${linkify(
"README.md#discussionCategoryName",
)}) if defined, must be a non empty \`String\`.
Your configuration for the \`discussionCategoryName\` option is \`${stringify(
discussionCategoryName,
)}\`.`,
};
}
6 changes: 6 additions & 0 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default async function publish(pluginConfig, context, { Octokit }) {
draftRelease,
releaseNameTemplate,
releaseBodyTemplate,
discussionCategoryName,
} = resolveConfig(pluginConfig, context);
const { owner, repo } = parseGithubUrl(repositoryUrl);
const octokit = new Octokit(
Expand All @@ -53,6 +54,11 @@ export default async function publish(pluginConfig, context, { Octokit }) {

debug("release object: %O", release);

// If discussionCategoryName is not undefined or false
if (discussionCategoryName) {
release.discussion_category_name = discussionCategoryName;
}

const draftReleaseOptions = { ...release, draft: true };

// When there are no assets, we publish a release directly.
Expand Down
4 changes: 4 additions & 0 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function resolveConfig(
draftRelease,
releaseNameTemplate,
releaseBodyTemplate,
discussionCategoryName,
},
{ env },
) {
Expand Down Expand Up @@ -52,5 +53,8 @@ export default function resolveConfig(
releaseNameTemplate: !isNil(releaseNameTemplate)
? releaseNameTemplate
: "<%= nextRelease.name %>",
discussionCategoryName: isNil(discussionCategoryName)
? false
: discussionCategoryName,
};
}
1 change: 1 addition & 0 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const VALIDATORS = {
draftRelease: isBoolean,
releaseBodyTemplate: isNonEmptyString,
releaseNameTemplate: isNonEmptyString,
discussionCategoryName: canBeDisabled(isNonEmptyString),
};

export default async function verify(pluginConfig, context, { Octokit }) {
Expand Down
24 changes: 18 additions & 6 deletions test/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ test("Throw SemanticReleaseError if invalid config", async (t) => {
const failTitle = 42;
const labels = 42;
const assignees = 42;
const discussionCategoryName = 42;
const options = {
publish: [
{ path: "@semantic-release/npm" },
Expand All @@ -132,6 +133,7 @@ test("Throw SemanticReleaseError if invalid config", async (t) => {
failTitle,
labels,
assignees,
discussionCategoryName,
},
],
repositoryUrl: "invalid_url",
Expand Down Expand Up @@ -163,9 +165,11 @@ test("Throw SemanticReleaseError if invalid config", async (t) => {
t.is(errors[5].name, "SemanticReleaseError");
t.is(errors[5].code, "EINVALIDASSIGNEES");
t.is(errors[6].name, "SemanticReleaseError");
t.is(errors[6].code, "EINVALIDGITHUBURL");
t.is(errors[6].code, "EINVALIDDISCUSSIONCATEGORYNAME");
t.is(errors[7].name, "SemanticReleaseError");
t.is(errors[7].code, "ENOGHTOKEN");
t.is(errors[7].code, "EINVALIDGITHUBURL");
t.is(errors[8].name, "SemanticReleaseError");
t.is(errors[8].code, "ENOGHTOKEN");
});

test("Publish a release with an array of assets", async (t) => {
Expand Down Expand Up @@ -219,7 +223,9 @@ test("Publish a release with an array of assets", async (t) => {
`${uploadOrigin}${uploadUri}?name=${encodeURIComponent(
"upload_file_name.txt",
)}&`,
{ browser_download_url: assetUrl },
{
browser_download_url: assetUrl,
},
)
.postOnce(
`${uploadOrigin}${uploadUri}?name=${encodeURIComponent(
Expand Down Expand Up @@ -676,12 +682,16 @@ test("Verify, release and notify success", async (t) => {
`${uploadOrigin}${uploadUri}?name=other_file.txt&label=${encodeURIComponent(
"Other File",
)}`,
{ browser_download_url: otherAssetUrl },
{
browser_download_url: otherAssetUrl,
},
)

.postOnce(
`https://api.github.local/repos/${owner}/${repo}/issues/1/comments`,
{ html_url: "https://github.com/successcomment-1" },
{
html_url: "https://github.com/successcomment-1",
},
);

await t.notThrowsAsync(
Expand Down Expand Up @@ -804,7 +814,9 @@ test("Verify, update release and notify success", async (t) => {
)
.postOnce(
`https://api.github.local/repos/${owner}/${repo}/issues/1/comments`,
{ html_url: "https://github.com/successcomment-1" },
{
html_url: "https://github.com/successcomment-1",
},
)
.postOnce(
`https://api.github.local/repos/${owner}/${repo}/issues/1/labels`,
Expand Down
Loading

0 comments on commit 34c0afd

Please sign in to comment.