diff --git a/docs/github-releases.md b/docs/github-releases.md index d409274a..fe8dde71 100644 --- a/docs/github-releases.md +++ b/docs/github-releases.md @@ -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 @@ -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. diff --git a/docs/gitlab-releases.md b/docs/gitlab-releases.md index 9f5ce3e0..8c57ef01 100644 --- a/docs/gitlab-releases.md +++ b/docs/gitlab-releases.md @@ -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 @@ -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 diff --git a/lib/plugin/GitRelease.js b/lib/plugin/GitRelease.js index 205a6ffa..27e94c43 100644 --- a/lib/plugin/GitRelease.js +++ b/lib/plugin/GitRelease.js @@ -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) { diff --git a/test/github.js b/test/github.js index 124b9bcb..9f7c5f1a 100644 --- a/test/github.js +++ b/test/github.js @@ -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,