Skip to content

Commit

Permalink
feat: add pre-release increment behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
neilime committed Jan 22, 2023
1 parent 7fa235b commit bbdaa52
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 65 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ autolabeler:
- '/JIRA-[0-9]{1,4}/'
```

## Pre-release increment

When creating Pre-release (`prerelease: true`), you can add a pre-release identifier to increment the pre-release version number, with the `prerelease-identifer` option. It accept any string, but it's recommended to use [Semantic Versioning](https://semver.org/) pre-release identifiers (alpha, beta, rc, etc).

```yml
prerelease-identifer: 'alpha' # will create a pre-release with version number x.x.x-alpha.x
```

## Projects that don't use Semantic Versioning

If your project doesn't follow [Semantic Versioning](https://semver.org) you can still use Release Drafter, but you may want to set the `version-template` option to customize how the `$NEXT_{PATCH,MINOR,MAJOR}_VERSION` environment variables are generated.
Expand Down
3 changes: 2 additions & 1 deletion lib/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const DEFAULT_CONFIG = Object.freeze({
'change-template': `* $TITLE (#$NUMBER) @$AUTHOR`,
'change-title-escapes': '',
'no-changes-template': `* No changes`,
'version-template': `$MAJOR.$MINOR.$PATCH`,
'version-template': `$MAJOR.$MINOR.$PATCH$PRERELEASE`,
'version-resolver': {
major: { labels: [] },
minor: { labels: [] },
Expand All @@ -25,6 +25,7 @@ const DEFAULT_CONFIG = Object.freeze({
'sort-by': SORT_BY.mergedAt,
'sort-direction': SORT_DIRECTIONS.descending,
prerelease: false,
'prerelease-identifier': '',
'filter-by-commitish': false,
commitish: '',
'category-template': `## $TITLE`,
Expand Down
23 changes: 20 additions & 3 deletions lib/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,12 +281,22 @@ const generateChangeLog = (mergedPullRequests, config) => {
return changeLog.join('').trim()
}

const resolveVersionKeyIncrement = (mergedPullRequests, config) => {
const resolveVersionKeyIncrement = (
mergedPullRequests,
config,
isPreRelease
) => {
const priorityMap = {
patch: 1,
minor: 2,
major: 3,
}

// Should increment pre-release version
if (isPreRelease && config['prerelease-identifier']) {
return 'prerelease'
}

const labelToKeyMap = Object.fromEntries(
Object.keys(priorityMap)
.flatMap((key) => [
Expand Down Expand Up @@ -340,14 +350,21 @@ const generateReleaseInfo = ({
config.replacers
)

const versionKeyIncrement = resolveVersionKeyIncrement(
mergedPullRequests,
config,
isPreRelease
)

const versionInfo = getVersionInfo(
lastRelease,
config['version-template'],
// Use the first override parameter to identify
// a version, from the most accurate to the least
version || tag || name,
resolveVersionKeyIncrement(mergedPullRequests, config),
config['tag-prefix']
versionKeyIncrement,
config['tag-prefix'],
config['prerelease-identifier']
)

if (versionInfo) {
Expand Down
3 changes: 3 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ const schema = (context) => {
.default(DEFAULT_CONFIG['sort-direction']),

prerelease: Joi.boolean().default(DEFAULT_CONFIG.prerelease),
'prerelease-identifier': Joi.string()
.allow('')
.default(DEFAULT_CONFIG['prerelease-identifier']),

'filter-by-commitish': Joi.boolean().default(
DEFAULT_CONFIG['filter-by-commitish']
Expand Down
30 changes: 28 additions & 2 deletions lib/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ const splitSemVersion = (input, versionKey = 'version') => {
}

const version = input.inc
? semver.inc(input[versionKey], input.inc, true)
? semver.inc(input[versionKey], input.inc, true, input.prereleaseIdentifier)
: input[versionKey].version

const prereleaseVersion = semver.prerelease(version)?.join('.') || ''

return {
...input,
version,
$MAJOR: semver.major(version),
$MINOR: semver.minor(version),
$PATCH: semver.patch(version),
$PRERELEASE: prereleaseVersion ? `-${prereleaseVersion}` : '',
$COMPLETE: version,
}
}
Expand All @@ -29,6 +32,7 @@ const defaultVersionInfo = {
$MAJOR: 1,
$MINOR: 0,
$PATCH: 0,
$PRERELEASE: '',
},
$NEXT_MINOR_VERSION: {
version: '0.1.0',
Expand All @@ -39,6 +43,7 @@ const defaultVersionInfo = {
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '',
},
$NEXT_PATCH_VERSION: {
version: '0.1.0',
Expand All @@ -49,6 +54,19 @@ const defaultVersionInfo = {
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '',
},
$NEXT_PRERELEASE_VERSION: {
version: '0.1.0-rc.0',
template: '$MAJOR.$MINOR.$PATCH$PRERELEASE',
inputVersion: null,
versionKeyIncrement: 'prerelease',
inc: 'prerelease',
prereleaseIdentifier: 'rc',
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '-rc.0',
},
$INPUT_VERSION: null,
$RESOLVED_VERSION: {
Expand All @@ -60,6 +78,7 @@ const defaultVersionInfo = {
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '',
},
}

Expand Down Expand Up @@ -113,6 +132,11 @@ const getTemplatableVersion = (input) => {
inc: 'patch',
template: '$PATCH',
}),
$NEXT_PRERELEASE_VERSION: splitSemVersion({
...input,
inc: 'prerelease',
template: '$PRERELEASE',
}),
$INPUT_VERSION: splitSemVersion(input, 'inputVersion'),
$RESOLVED_VERSION: splitSemVersion({
...input,
Expand Down Expand Up @@ -156,7 +180,8 @@ const getVersionInfo = (
template,
inputVersion,
versionKeyIncrement,
tagPrefix
tagPrefix,
prereleaseIdentifier
) => {
const version = coerceVersion(release, tagPrefix)
inputVersion = coerceVersion(inputVersion, tagPrefix)
Expand All @@ -171,6 +196,7 @@ const getVersionInfo = (
template,
inputVersion,
versionKeyIncrement,
prereleaseIdentifier,
}),
}
}
Expand Down
14 changes: 13 additions & 1 deletion schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"type": "string"
},
"version-template": {
"default": "$MAJOR.$MINOR.$PATCH",
"default": "$MAJOR.$MINOR.$PATCH$PRERELEASE",
"type": "string"
},
"name-template": {
Expand Down Expand Up @@ -117,6 +117,18 @@
"default": false,
"type": "boolean"
},
"prerelease-identifier": {
"anyOf": [
{
"type": "string",
"enum": [""]
},
{
"default": "",
"type": "string"
}
]
},
"filter-by-commitish": {
"default": false,
"type": "boolean"
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/config/config-with-prerelease-identifier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
template: This is a Pre-release with identifier.
name-template: 'v$RESOLVED_VERSION'
tag-template: 'v$RESOLVED_VERSION'
prerelease-identifier: alpha
16 changes: 15 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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')
Expand Down Expand Up @@ -2610,6 +2610,20 @@ describe('release-drafter', () => {
}
)
})

it('resolves tag with incremented pre-release identifier', async () => {
return overridesTest(
{
prerelease: 'true',
configName: 'config-with-prerelease-identifier.yml',
},
{
prerelease: true,
name: 'v2.0.1-alpha.0',
tag_name: 'v2.0.1-alpha.0',
}
)
})
})

describe('with input prerelease: false', () => {
Expand Down

0 comments on commit bbdaa52

Please sign in to comment.