Skip to content

Commit

Permalink
feat: remove title from the release notes
Browse files Browse the repository at this point in the history
This information is redundant in the GitHub Release.

BREAKING CHANGE: The title is now removed from the release notes by
default. You can disable the removal of title/restore the old behavior
with [`removeTitleFromReleaseNotes: false`](https://github.com/semantic-release/github#removetitlefromreleasenotes).

Fixes: semantic-release#316
  • Loading branch information
felipecrs committed Dec 28, 2020
1 parent 8d3df0e commit 949d5c8
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"samverschueren.linter-xo"
]
}
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"xo.enable": true,
"xo.format.enable": true,
"[javascript]": {
"editor.defaultFormatter": "samverschueren.linter-xo",
"editor.formatOnSave": true
}
}
75 changes: 50 additions & 25 deletions README.md

Large diffs are not rendered by default.

22 changes: 19 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ module.exports = async (pluginConfig, context) => {
cwd,
options: {repositoryUrl},
branch,
nextRelease: {name, gitTag, notes},
nextRelease: {name, gitTag, notes, version},
logger,
} = context;
const {githubToken, githubUrl, githubApiPathPrefix, proxy, assets} = resolveConfig(pluginConfig, context);
const {
githubToken,
githubUrl,
githubApiPathPrefix,
proxy,
assets,
removeTitleFromReleaseNotes: shouldRemoveTitleFromReleaseNotes,
} = resolveConfig(pluginConfig, context);
const {owner, repo} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
const release = {
Expand All @@ -27,7 +34,7 @@ module.exports = async (pluginConfig, context) => {
tag_name: gitTag,
target_commitish: branch.name,
name,
body: notes,
body: shouldRemoveTitleFromReleaseNotes ? removeTitleFromReleaseNotes(notes, version) : notes,
prerelease: isPrerelease(branch),
};

Expand Down Expand Up @@ -104,3 +111,12 @@ module.exports = async (pluginConfig, context) => {
logger.log('Published GitHub release: %s', url);
return {url, name: RELEASE_NAME, id: releaseId};
};

const removeTitleFromReleaseNotes = (notes, version) => {
const titlePrefix = `### [${version}]`;
if (notes.startsWith(titlePrefix)) {
return notes.split('\n').slice(2).join('\n');
}

return notes;
};
2 changes: 2 additions & 0 deletions lib/resolve-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = (
assignees,
releasedLabels,
addReleases,
removeTitleFromReleaseNotes,
},
{env}
) => ({
Expand All @@ -32,4 +33,5 @@ module.exports = (
? false
: castArray(releasedLabels),
addReleases: isNil(addReleases) ? false : addReleases,
removeTitleFromReleaseNotes: isNil(removeTitleFromReleaseNotes) ? true : removeTitleFromReleaseNotes,
});
1 change: 1 addition & 0 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const VALIDATORS = {
assignees: isArrayOf(isNonEmptyString),
releasedLabels: canBeDisabled(isArrayOf(isNonEmptyString)),
addReleases: canBeDisabled(oneOf(['bottom', 'top'])),
removeTitleFromReleaseNotes: canBeDisabled(oneOf([false, true])),
};

module.exports = async (pluginConfig, context) => {
Expand Down
128 changes: 128 additions & 0 deletions test/publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,134 @@ test.serial('Publish a release', async (t) => {
t.true(github.isDone());
});

test.serial('Publish a release removing the title from the release notes by default', async (t) => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {};
const expectedBody = 'Test release note body';
const nextRelease = {
version: '1.0.1',
gitTag: 'v1.0.1',
name: 'v1.0.1',
notes: `### [1.0.1](https://github.com/${owner}/${repo}/compare/v1.0.0...v1.0.1) (2020-12-12)\n\n${expectedBody}`,
};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
const branch = 'test_branch';

const github = authenticate(env)
.post(`/repos/${owner}/${repo}/releases`, {
tag_name: nextRelease.gitTag,
target_commitish: branch,
name: nextRelease.name,
body: expectedBody,
prerelease: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const result = await publish(pluginConfig, {
cwd,
env,
options,
branch: {name: branch, type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(github.isDone());
});

test.serial('Publish a release removing the title from the release notes', async (t) => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {removeTitleFromReleaseNotes: true};
const expectedBody = 'Test release note body';
const nextRelease = {
version: '1.0.1',
gitTag: 'v1.0.1',
name: 'v1.0.1',
notes: `### [1.0.1](https://github.com/${owner}/${repo}/compare/v1.0.0...v1.0.1) (2020-12-12)\n\n${expectedBody}`,
};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
const branch = 'test_branch';

const github = authenticate(env)
.post(`/repos/${owner}/${repo}/releases`, {
tag_name: nextRelease.gitTag,
target_commitish: branch,
name: nextRelease.name,
body: expectedBody,
prerelease: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const result = await publish(pluginConfig, {
cwd,
env,
options,
branch: {name: branch, type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(github.isDone());
});

test.serial('Publish a release without removing the title from the release notes', async (t) => {
const owner = 'test_user';
const repo = 'test_repo';
const env = {GITHUB_TOKEN: 'github_token'};
const pluginConfig = {removeTitleFromReleaseNotes: false};
const nextRelease = {
version: '1.0.1',
gitTag: 'v1.0.1',
name: 'v1.0.1',
notes: `### [1.0.1](https://github.com/${owner}/${repo}/compare/v1.0.0...v1.0.1) (2020-12-12)\n\nTest release note body`,
};
const options = {repositoryUrl: `https://github.com/${owner}/${repo}.git`};
const releaseUrl = `https://github.com/${owner}/${repo}/releases/${nextRelease.version}`;
const releaseId = 1;
const uploadUri = `/api/uploads/repos/${owner}/${repo}/releases/${releaseId}/assets`;
const uploadUrl = `https://github.com${uploadUri}{?name,label}`;
const branch = 'test_branch';

const github = authenticate(env)
.post(`/repos/${owner}/${repo}/releases`, {
tag_name: nextRelease.gitTag,
target_commitish: branch,
name: nextRelease.name,
body: nextRelease.notes,
prerelease: false,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl});

const result = await publish(pluginConfig, {
cwd,
env,
options,
branch: {name: branch, type: 'release', main: true},
nextRelease,
logger: t.context.logger,
});

t.is(result.url, releaseUrl);
t.deepEqual(t.context.log.args[0], ['Published GitHub release: %s', releaseUrl]);
t.true(github.isDone());
});

test.serial('Publish a release on a channel', async (t) => {
const owner = 'test_user';
const repo = 'test_repo';
Expand Down

0 comments on commit 949d5c8

Please sign in to comment.