Skip to content

Commit

Permalink
Support function for configuring github/gitlab releaseNotes (#987)
Browse files Browse the repository at this point in the history
* Support function for configuring github's releaseNotes

* releaseNotes function also supported by gitlab

* maybe fix tests?
  • Loading branch information
rchl committed Mar 16, 2023
1 parent c36e06b commit 81e18fa
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
23 changes: 22 additions & 1 deletion docs/github-releases.md
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
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
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
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

0 comments on commit 81e18fa

Please sign in to comment.