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 afe1c5d
Show file tree
Hide file tree
Showing 10 changed files with 257 additions and 71 deletions.
8 changes: 8 additions & 0 deletions README.md
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
59 changes: 53 additions & 6 deletions dist/index.js
Expand Up @@ -142773,7 +142773,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 @@ -142791,6 +142791,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 Expand Up @@ -143166,12 +143167,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 @@ -143225,14 +143236,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 Expand Up @@ -143416,6 +143434,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 Expand Up @@ -143756,15 +143777,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 @@ -143779,6 +143803,7 @@ const defaultVersionInfo = {
$MAJOR: 1,
$MINOR: 0,
$PATCH: 0,
$PRERELEASE: '',
},
$NEXT_MINOR_VERSION: {
version: '0.1.0',
Expand All @@ -143789,6 +143814,7 @@ const defaultVersionInfo = {
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '',
},
$NEXT_PATCH_VERSION: {
version: '0.1.0',
Expand All @@ -143799,6 +143825,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 @@ -143810,6 +143849,7 @@ const defaultVersionInfo = {
$MAJOR: 0,
$MINOR: 1,
$PATCH: 0,
$PRERELEASE: '',
},
}

Expand Down Expand Up @@ -143863,6 +143903,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 @@ -143906,7 +143951,8 @@ const getVersionInfo = (
template,
inputVersion,
versionKeyIncrement,
tagPrefix
tagPrefix,
prereleaseIdentifier
) => {
const version = coerceVersion(release, tagPrefix)
inputVersion = coerceVersion(inputVersion, tagPrefix)
Expand All @@ -143921,6 +143967,7 @@ const getVersionInfo = (
template,
inputVersion,
versionKeyIncrement,
prereleaseIdentifier,
}),
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/default-config.js
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
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
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
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
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
@@ -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

0 comments on commit afe1c5d

Please sign in to comment.