From e11806d904d047c40ca4a6ef9da1b50ba0bda48a Mon Sep 17 00:00:00 2001 From: Robbin Janssen Date: Tue, 21 Feb 2023 10:18:51 +0100 Subject: [PATCH] Add `include-pre-releases` configuration option (#1302) --- README.md | 1 + dist/index.js | 18 +++++-- index.js | 2 + lib/default-config.js | 1 + lib/releases.js | 11 ++-- lib/schema.js | 4 ++ schema.json | 4 ++ .../config-with-include-pre-releases-true.yml | 7 +++ test/fixtures/pre-release.json | 39 ++++++++++++++ test/index.test.js | 53 ++++++++++++++++++- 10 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/config/config-with-include-pre-releases-true.yml create mode 100644 test/fixtures/pre-release.json diff --git a/README.md b/README.md index 24ae54038..53bf3ad3e 100644 --- a/README.md +++ b/README.md @@ -133,6 +133,7 @@ You can configure Release Drafter using the following key in your `.github/relea | `exclude-labels` | Optional | Exclude pull requests using labels. Refer to [Exclude Pull Requests](#exclude-pull-requests) to learn more about this option. | | `include-labels` | Optional | Include only the specified pull requests using labels. Refer to [Include Pull Requests](#include-pull-requests) to learn more about this option. | | `exclude-contributors` | Optional | Exclude specific usernames from the generated `$CONTRIBUTORS` variable. Refer to [Exclude Contributors](#exclude-contributors) to learn more about this option. | +| `include-pre-releases` | Optional | Include pre releases as "full" releases when drafting release notes. Default: `false`. | | `no-contributors-template` | Optional | The template to use for `$CONTRIBUTORS` when there's no contributors to list. Default: `"No contributors"`. | | `replacers` | Optional | Search and replace content in the generated changelog body. Refer to [Replacers](#replacers) to learn more about this option. | | `sort-by` | Optional | Sort changelog by merged_at or title. Can be one of: `merged_at`, `title`. Default: `merged_at`. | diff --git a/dist/index.js b/dist/index.js index b851d5f52..f0b23c061 100644 --- a/dist/index.js +++ b/dist/index.js @@ -142364,6 +142364,7 @@ module.exports = (app, { getRouter }) => { const targetCommitish = commitish || config['commitish'] || ref const { 'filter-by-commitish': filterByCommitish, + 'include-pre-releases': includePreReleases, 'tag-prefix': tagPrefix, } = config @@ -142381,6 +142382,7 @@ module.exports = (app, { getRouter }) => { context, targetCommitish, filterByCommitish, + includePreReleases, tagPrefix, }) @@ -142792,6 +142794,7 @@ const DEFAULT_CONFIG = Object.freeze({ 'sort-direction': SORT_DIRECTIONS.descending, prerelease: false, 'filter-by-commitish': false, + 'include-pre-releases': false, commitish: '', 'category-template': `## $TITLE`, header: '', @@ -142909,6 +142912,7 @@ const findReleases = async ({ context, targetCommitish, filterByCommitish, + includePreReleases, tagPrefix, }) => { let releaseCount = 0 @@ -142941,12 +142945,13 @@ const findReleases = async ({ const filteredReleases = tagPrefix ? commitishFilteredReleases.filter((r) => r.tag_name.startsWith(tagPrefix)) : commitishFilteredReleases - const sortedPublishedReleases = sortReleases( - filteredReleases.filter((r) => !r.draft && !r.prerelease) + const sortedSelectedReleases = sortReleases( + filteredReleases.filter( + (r) => !r.draft && (!r.prerelease || includePreReleases) + ) ) const draftRelease = filteredReleases.find((r) => r.draft) - const lastRelease = - sortedPublishedReleases[sortedPublishedReleases.length - 1] + const lastRelease = sortedSelectedReleases[sortedSelectedReleases.length - 1] if (draftRelease) { log({ context, message: `Draft release: ${draftRelease.tag_name}` }) @@ -143208,7 +143213,6 @@ const generateReleaseInfo = ({ const { owner, repo } = context.repo() let body = config['header'] + config.template + config['footer'] - body = template( body, { @@ -143421,6 +143425,10 @@ const schema = (context) => { DEFAULT_CONFIG['filter-by-commitish'] ), + 'include-pre-releases': Joi.boolean().default( + DEFAULT_CONFIG['include-pre-releases'] + ), + commitish: Joi.string().allow('').default(DEFAULT_CONFIG['commitish']), replacers: Joi.array() diff --git a/index.js b/index.js index 52c872c16..d0f8c81c9 100644 --- a/index.js +++ b/index.js @@ -161,6 +161,7 @@ module.exports = (app, { getRouter }) => { const targetCommitish = commitish || config['commitish'] || ref const { 'filter-by-commitish': filterByCommitish, + 'include-pre-releases': includePreReleases, 'tag-prefix': tagPrefix, } = config @@ -178,6 +179,7 @@ module.exports = (app, { getRouter }) => { context, targetCommitish, filterByCommitish, + includePreReleases, tagPrefix, }) diff --git a/lib/default-config.js b/lib/default-config.js index 5affa9465..bc65a4508 100644 --- a/lib/default-config.js +++ b/lib/default-config.js @@ -26,6 +26,7 @@ const DEFAULT_CONFIG = Object.freeze({ 'sort-direction': SORT_DIRECTIONS.descending, prerelease: false, 'filter-by-commitish': false, + 'include-pre-releases': false, commitish: '', 'category-template': `## $TITLE`, header: '', diff --git a/lib/releases.js b/lib/releases.js index 2d30ff1a2..1f99289b4 100644 --- a/lib/releases.js +++ b/lib/releases.js @@ -24,6 +24,7 @@ const findReleases = async ({ context, targetCommitish, filterByCommitish, + includePreReleases, tagPrefix, }) => { let releaseCount = 0 @@ -56,12 +57,13 @@ const findReleases = async ({ const filteredReleases = tagPrefix ? commitishFilteredReleases.filter((r) => r.tag_name.startsWith(tagPrefix)) : commitishFilteredReleases - const sortedPublishedReleases = sortReleases( - filteredReleases.filter((r) => !r.draft && !r.prerelease) + const sortedSelectedReleases = sortReleases( + filteredReleases.filter( + (r) => !r.draft && (!r.prerelease || includePreReleases) + ) ) const draftRelease = filteredReleases.find((r) => r.draft) - const lastRelease = - sortedPublishedReleases[sortedPublishedReleases.length - 1] + const lastRelease = sortedSelectedReleases[sortedSelectedReleases.length - 1] if (draftRelease) { log({ context, message: `Draft release: ${draftRelease.tag_name}` }) @@ -323,7 +325,6 @@ const generateReleaseInfo = ({ const { owner, repo } = context.repo() let body = config['header'] + config.template + config['footer'] - body = template( body, { diff --git a/lib/schema.js b/lib/schema.js index 60dd8209a..b17e40620 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -81,6 +81,10 @@ const schema = (context) => { DEFAULT_CONFIG['filter-by-commitish'] ), + 'include-pre-releases': Joi.boolean().default( + DEFAULT_CONFIG['include-pre-releases'] + ), + commitish: Joi.string().allow('').default(DEFAULT_CONFIG['commitish']), replacers: Joi.array() diff --git a/schema.json b/schema.json index cf5b7c8eb..66ab1148e 100644 --- a/schema.json +++ b/schema.json @@ -121,6 +121,10 @@ "default": false, "type": "boolean" }, + "include-pre-releases": { + "default": false, + "type": "boolean" + }, "commitish": { "anyOf": [ { diff --git a/test/fixtures/config/config-with-include-pre-releases-true.yml b/test/fixtures/config/config-with-include-pre-releases-true.yml new file mode 100644 index 000000000..01e9b1221 --- /dev/null +++ b/test/fixtures/config/config-with-include-pre-releases-true.yml @@ -0,0 +1,7 @@ +name-template: 'v$RESOLVED_VERSION' +tag-template: 'v$RESOLVED_VERSION' +template: | + # What's Changed + + $CHANGES +include-pre-releases: true diff --git a/test/fixtures/pre-release.json b/test/fixtures/pre-release.json new file mode 100644 index 000000000..9f4e329e4 --- /dev/null +++ b/test/fixtures/pre-release.json @@ -0,0 +1,39 @@ +{ + "url": "https://api.github.com/repos/toolmantim/release-drafter-test-project/releases/11691725", + "assets_url": "https://api.github.com/repos/toolmantim/release-drafter-test-project/releases/11691725/assets", + "upload_url": "https://uploads.github.com/repos/toolmantim/release-drafter-test-project/releases/11691725/assets{?name,label}", + "html_url": "https://github.com/toolmantim/release-drafter-test-project/releases/tag/v1.5.0-alpha", + "id": 11691725, + "node_id": "MDc6UmVsZWFzZTExNjkxNzI2", + "tag_name": "v1.5.0-alpha", + "target_commitish": "master", + "name": "v1.5.0-alpha", + "draft": false, + "author": { + "login": "release-drafter[bot]", + "id": 40600115, + "node_id": "MDM6Qm90NDA2MDAxMTU=", + "avatar_url": "https://avatars2.githubusercontent.com/in/14050?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/release-drafter%5Bbot%5D", + "html_url": "https://github.com/apps/release-drafter", + "followers_url": "https://api.github.com/users/release-drafter%5Bbot%5D/followers", + "following_url": "https://api.github.com/users/release-drafter%5Bbot%5D/following{/other_user}", + "gists_url": "https://api.github.com/users/release-drafter%5Bbot%5D/gists{/gist_id}", + "starred_url": "https://api.github.com/users/release-drafter%5Bbot%5D/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/release-drafter%5Bbot%5D/subscriptions", + "organizations_url": "https://api.github.com/users/release-drafter%5Bbot%5D/orgs", + "repos_url": "https://api.github.com/users/release-drafter%5Bbot%5D/repos", + "events_url": "https://api.github.com/users/release-drafter%5Bbot%5D/events{/privacy}", + "received_events_url": "https://api.github.com/users/release-drafter%5Bbot%5D/received_events", + "type": "Bot", + "site_admin": false + }, + "prerelease": true, + "created_at": "2018-06-28T05:45:15Z", + "published_at": "2018-06-28T05:47:08Z", + "assets": [], + "tarball_url": "https://api.github.com/repos/toolmantim/release-drafter-test-project/tarball/v1.5.0-alpha", + "zipball_url": "https://api.github.com/repos/toolmantim/release-drafter-test-project/zipball/v1.5.0-alpha", + "body": "A pre release" +} diff --git a/test/index.test.js b/test/index.test.js index 812f87856..bba6dbed8 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -4,12 +4,13 @@ const { getConfigMock } = require('./helpers/config-mock') const releaseDrafter = require('../index') const mockedEnv = require('mocked-env') const pino = require('pino') -const Stream = require('stream') +const Stream = require('node:stream') const pushPayload = require('./fixtures/push.json') const pushTagPayload = require('./fixtures/push-tag.json') const releasePayload = require('./fixtures/release.json') const release2Payload = require('./fixtures/release-2.json') const release3Payload = require('./fixtures/release-3.json') +const preReleasePayload = require('./fixtures/pre-release.json') const pushNonMasterPayload = require('./fixtures/push-non-master-branch.json') const graphqlCommitsNoPRsPayload = require('./fixtures/graphql-commits-no-prs.json') const graphqlCommitsMergeCommit = require('./fixtures/__generated__/graphql-commits-merge-commit.json') @@ -1255,6 +1256,56 @@ describe('release-drafter', () => { }) }) + describe('with include-pre-releases true config', () => { + it('includes pre releases', async () => { + getConfigMock('config-with-include-pre-releases-true.yml') + + nock('https://api.github.com') + .get('/repos/toolmantim/release-drafter-test-project/releases') + .query(true) + .reply(200, [release2Payload, preReleasePayload]) + + nock('https://api.github.com') + .post('/graphql', (body) => + body.query.includes('query findCommitsWithAssociatedPullRequests') + ) + .reply(200, graphqlCommitsMergeCommit) + + nock('https://api.github.com') + .post( + '/repos/toolmantim/release-drafter-test-project/releases', + (body) => { + expect(body).toMatchInlineSnapshot(` + Object { + "body": "# What's Changed + + * Add documentation (#5) @TimonVS + * Update dependencies (#4) @TimonVS + * Bug fixes (#3) @TimonVS + * Add big feature (#2) @TimonVS + * 👽 Add alien technology (#1) @TimonVS + ", + "draft": true, + "name": "v1.5.0", + "prerelease": false, + "tag_name": "v1.5.0", + "target_commitish": "refs/heads/master", + } + `) + return true + } + ) + .reply(200, preReleasePayload) + + await probot.receive({ + name: 'push', + payload: pushPayload, + }) + + expect.assertions(1) + }) + }) + describe('with exclude-labels config', () => { it('excludes pull requests', async () => { getConfigMock('config-with-exclude-labels.yml')