Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support function for configuring github/gitlab releaseNotes #987

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion docs/github-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ command-line directly: `--github.releaseName="Arcade Silver"`.

By default, the output of `git.changelog` is used for the GitHub release notes. This is the printed `Changelog: ...`
when release-it boots. This can be overridden with the `github.releaseNotes` option to customize the release notes for
the GitHub release. This script will run just before the actual GitHub release itself. Make sure it outputs to `stdout`.
the GitHub release. This will be invoked just before the actual GitHub release itself.

The value can either be a string or a function but a function is only supported when configuring release-it using
`.release-it.js` or `.release-it.cjs` file.

When the value is a string, it's executed as a shell script. Make sure it outputs to `stdout`.
An example:

```json
Expand All @@ -76,6 +81,22 @@ An example:
}
```

When the value is a function, it's executed with a single `context` parameter that contains the plugin context.
The function can also be `async`. Make sure that it returns a string value.
An example:

```js
{
github: {
release: true,
releaseNotes(context) {
// Remove the first, redundant line with version and date.
return context.changelog.split('\n').slice(1).join('\n');
}
}
}
```

Use `--github.autoGenerate` to have GitHub auto-generate the release notes.

See [Changelog](./changelog.md) for more information about generating changelogs/release notes.
Expand Down
23 changes: 22 additions & 1 deletion docs/gitlab-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ To skip these checks, use `gitlab.skipChecks`.

By default, the output of `git.changelog` is used for the GitLab release notes. This is the printed `Changelog: ...`
when release-it boots. This can be overridden with the `gitlab.releaseNotes` option to customize the release notes for
the GitLab release. This script will run just before the actual GitLab release itself. Make sure it outputs to `stdout`.
the GitLab release. This will be invoked just before the actual GitLab release itself.

The value can either be a string or a function but a function is only supported when configuring release-it using
`.release-it.js` or `.release-it.cjs` file.

When the value is a string, it's executed as a shell script. Make sure it outputs to `stdout`.
An example:

```json
Expand All @@ -38,6 +43,22 @@ An example:
}
```

When the value is a function, it's executed with a single `context` parameter that contains the plugin context.
The function can also be `async`. Make sure that it returns a string value.
An example:

```js
{
gitlab: {
release: true,
releaseNotes(context) {
// Remove the first, redundant line with version and date.
return context.changelog.split('\n').slice(1).join('\n');
}
}
}
```

See [Changelog](./changelog.md) for more information about generating changelogs/release notes.

## Milestones
Expand Down
11 changes: 10 additions & 1 deletion lib/plugin/GitRelease.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ class GitRelease extends GitBase {
async beforeRelease() {
const { releaseNotes: script } = this.options;
const { changelog } = this.config.getContext();
const releaseNotes = script ? await this.exec(script) : changelog;
const releaseNotes = script ? await this.processReleaseNotes(script) : changelog;
this.setContext({ releaseNotes });
if (releaseNotes !== changelog) {
this.log.preview({ title: 'release notes', text: releaseNotes });
}
}

async processReleaseNotes(script) {
if (typeof script === 'function') {
const ctx = Object.assign({}, this.config.getContext(), { [this.namespace]: this.getContext() });
return await script(ctx);
} else {
return await this.exec(script);
}
}

afterRelease() {
const { isReleased, releaseUrl } = this.getContext();
if (isReleased) {
Expand Down
29 changes: 29 additions & 0 deletions test/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,35 @@ test('should update release and upload assets', async t => {
exec.restore();
});

test('should create custom release notes using releaseNotes function', async t => {
const options = {
git,
github: {
pushRepo,
tokenRef,
release: true,
releaseName: 'Release ${tagName}',
releaseNotes(context) {
return `Custom notes for tag ${context.tagName}`;
}
}
};
const github = factory(GitHub, { options });
const exec = sinon.stub(github.shell, 'exec').callThrough();
exec.withArgs('git describe --tags --match=* --abbrev=0').resolves('2.0.1');

interceptAuthentication();
interceptCollaborator();
interceptCreate({ body: { tag_name: '2.0.2', name: 'Release 2.0.2', body: 'Custom notes for tag 2.0.2' } });

await runTasks(github);

const { isReleased, releaseUrl } = github.getContext();
t.true(isReleased);
t.is(releaseUrl, 'https://github.com/user/repo/releases/tag/2.0.2');
exec.restore();
});

test('should create new release for unreleased tag', async t => {
const options = {
increment: false,
Expand Down