Skip to content

Commit

Permalink
feat: add prefix, additionalCommits and current noVersionBumpBehavior…
Browse files Browse the repository at this point in the history
… options
  • Loading branch information
NGPixel committed Feb 25, 2023
1 parent c675608 commit 35f2f92
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 105 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

steps:
- name: Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3
if: ${{ !env.ACT }}

- name: Get Next Version
Expand Down Expand Up @@ -47,6 +47,7 @@ jobs:
with:
allowUpdates: true
draft: false
makeLatest: true
tag: ${{ steps.semver.outputs.next }}
name: ${{ steps.semver.outputs.next }}
body: ${{ steps.changelog.outputs.changes }}
Expand All @@ -67,7 +68,7 @@ jobs:
# LOCAL TEST

- name: (local) Checkout Code
uses: actions/checkout@v2
uses: actions/checkout@v3
if: ${{ env.ACT }}
with:
path: changelog-action
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ jobs:
| `patchList` | Comma separated commit prefixes, used to bump Patch version. | :x: | `fix, bugfix, perf, refactor, test, tests` |
| `patchAll` | If set to `true`, will ignore `patchList` and always count commits as a Patch. | :x: | `false` |
| `skipInvalidTags` | If set to `true`, will skip tags that are not valid semver until it finds a proper one (up to 10 from latest). | :x: | `false` |
| `noVersionBumpBehavior` | Whether to exit with an error *(default)*, a warning or silently when none of the commits result in a version bump. (Possible values: `error`, `warn` or `silent`) | :x: | `error` |
| `noVersionBumpBehavior` | Whether to exit with an error *(default)*, a warning, the current version or silently when none of the commits result in a version bump. (Possible values: `error`, `warn`, `current` or `silent`) | :x: | `error` |
| `prefix` | A prefix that will be ignored when parsing tags (e.g. `foobar/`). Useful for monorepos. The prefix will be added back to the output values. | :x: | |
| `additionalCommits` | A list of additional commit messages to parse in order to calculate semver. | :x: | |

## Outputs

Expand Down
9 changes: 8 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,16 @@ inputs:
required: false
default: 'false'
noVersionBumpBehavior:
description: Whether to exit with an error, warning or silently when none of the commits result in a version bump. (error, warn, silent)
description: Whether to exit with an error, warning or silently when none of the commits result in a version bump. (error, warn, current, silent)
required: false
default: error
prefix:
description: A prefix that will be ignored when parsing tags. Useful for monorepos. The prefix will be added back to the output values.
required: false
default: ''
additionalCommits:
description: A list of additional commit messages to parse in order to calculate semver.
required: false
outputs:
current:
description: Current version number / latest tag.
Expand Down
51 changes: 36 additions & 15 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31051,6 +31051,8 @@ async function main () {
const repo = github.context.repo.repo
const skipInvalidTags = core.getBooleanInput('skipInvalidTags')
const noVersionBumpBehavior = core.getInput('noVersionBumpBehavior')
const prefix = core.getInput('prefix') || ''
const additionalCommits = core.getInput('additionalCommits').split('\n').map(l => l.trim()).filter(l => l !== '')

const bumpTypes = {
major: core.getInput('majorList').split(',').map(p => p.trim()).filter(p => p),
Expand All @@ -31059,6 +31061,16 @@ async function main () {
patchAll: (core.getInput('patchAll') === true || core.getInput('patchAll') === 'true'),
}

function outputVersion (version) {
core.exportVariable('next', `${prefix}v${version}`)
core.exportVariable('nextStrict', `${prefix}${version}`)

core.setOutput('next', `${version}v${version}`)
core.setOutput('nextStrict', `${prefix}${version}`)
core.setOutput('nextMajor', `${prefix}v${semver.major(version)}`)
core.setOutput('nextMajorStrict', `${prefix}${semver.major(version)}`)
}

// GET LATEST + PREVIOUS TAGS

const tagsRaw = await gh.graphql(`
Expand Down Expand Up @@ -31087,6 +31099,9 @@ async function main () {
let latestTag = null
let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
Expand All @@ -31100,7 +31115,12 @@ async function main () {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
}

core.info(`Comparing against latest tag: ${latestTag.name}`)
core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)

// OUTPUT CURRENT VARS

core.exportVariable('current', `${prefix}${latestTag.name}`)
core.setOutput('current', `${prefix}${latestTag.name}`)

// GET COMMITS

Expand All @@ -31126,6 +31146,10 @@ async function main () {
}
} while (hasMoreCommits)

if (additionalCommits && additionalCommits.length > 0) {
commits.push(...additionalCommits)
}

if (!commits || commits.length < 1) {
return core.setFailed('Couldn\'t find any commits between HEAD and latest tag.')
}
Expand Down Expand Up @@ -31170,35 +31194,32 @@ async function main () {
bump = 'patch'
} else {
switch (noVersionBumpBehavior) {
case 'warn': {
return core.warning('No commit resulted in a version bump since last release!')
case 'current': {
core.info('No commit resulted in a version bump since last release! Exiting with current as next version...')
outputVersion(semver.clean(latestTag.name))
return
}
case 'silent': {
return core.info('No commit resulted in a version bump since last release! Exiting silently...')
}
case 'warn': {
return core.warning('No commit resulted in a version bump since last release!')
}
default: {
return core.setFailed('No commit resulted in a version bump since last release!')
}
}
}
core.info(`\n>>> Will bump version ${latestTag.name} using ${bump.toUpperCase()}\n`)
core.info(`\n>>> Will bump version ${prefix}${latestTag.name} using ${bump.toUpperCase()}\n`)

// BUMP VERSION

const next = semver.inc(latestTag.name, bump)

core.info(`Current version is ${latestTag.name}`)
core.info(`Next version is v${next}`)

core.exportVariable('current', latestTag.name)
core.exportVariable('next', `v${next}`)
core.exportVariable('nextStrict', next)
core.info(`Current version is ${prefix}${latestTag.name}`)
core.info(`Next version is ${prefix}v${next}`)

core.setOutput('current', latestTag.name)
core.setOutput('next', `v${next}`)
core.setOutput('nextStrict', next)
core.setOutput('nextMajor', `v${semver.major(next)}`)
core.setOutput('nextMajorStrict', semver.major(next))
outputVersion(next)
}

main()
Expand Down
51 changes: 36 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ async function main () {
const repo = github.context.repo.repo
const skipInvalidTags = core.getBooleanInput('skipInvalidTags')
const noVersionBumpBehavior = core.getInput('noVersionBumpBehavior')
const prefix = core.getInput('prefix') || ''
const additionalCommits = core.getInput('additionalCommits').split('\n').map(l => l.trim()).filter(l => l !== '')

const bumpTypes = {
major: core.getInput('majorList').split(',').map(p => p.trim()).filter(p => p),
Expand All @@ -20,6 +22,16 @@ async function main () {
patchAll: (core.getInput('patchAll') === true || core.getInput('patchAll') === 'true'),
}

function outputVersion (version) {
core.exportVariable('next', `${prefix}v${version}`)
core.exportVariable('nextStrict', `${prefix}${version}`)

core.setOutput('next', `${version}v${version}`)
core.setOutput('nextStrict', `${prefix}${version}`)
core.setOutput('nextMajor', `${prefix}v${semver.major(version)}`)
core.setOutput('nextMajorStrict', `${prefix}${semver.major(version)}`)
}

// GET LATEST + PREVIOUS TAGS

const tagsRaw = await gh.graphql(`
Expand Down Expand Up @@ -48,6 +60,9 @@ async function main () {
let latestTag = null
let idx = 0
for (const tag of tagsList) {
if (prefix && tag.name.indexOf(prefix) === 0) {
tag.name = tag.name.replace(prefix, '')
}
if (semver.valid(tag.name)) {
latestTag = tag
break
Expand All @@ -61,7 +76,12 @@ async function main () {
return core.setFailed(skipInvalidTags ? 'None of the 10 latest tags are valid semver!' : 'Latest tag is invalid (does not conform to semver)!')
}

core.info(`Comparing against latest tag: ${latestTag.name}`)
core.info(`Comparing against latest tag: ${prefix}${latestTag.name}`)

// OUTPUT CURRENT VARS

core.exportVariable('current', `${prefix}${latestTag.name}`)
core.setOutput('current', `${prefix}${latestTag.name}`)

// GET COMMITS

Expand All @@ -87,6 +107,10 @@ async function main () {
}
} while (hasMoreCommits)

if (additionalCommits && additionalCommits.length > 0) {
commits.push(...additionalCommits)
}

if (!commits || commits.length < 1) {
return core.setFailed('Couldn\'t find any commits between HEAD and latest tag.')
}
Expand Down Expand Up @@ -131,35 +155,32 @@ async function main () {
bump = 'patch'
} else {
switch (noVersionBumpBehavior) {
case 'warn': {
return core.warning('No commit resulted in a version bump since last release!')
case 'current': {
core.info('No commit resulted in a version bump since last release! Exiting with current as next version...')
outputVersion(semver.clean(latestTag.name))
return
}
case 'silent': {
return core.info('No commit resulted in a version bump since last release! Exiting silently...')
}
case 'warn': {
return core.warning('No commit resulted in a version bump since last release!')
}
default: {
return core.setFailed('No commit resulted in a version bump since last release!')
}
}
}
core.info(`\n>>> Will bump version ${latestTag.name} using ${bump.toUpperCase()}\n`)
core.info(`\n>>> Will bump version ${prefix}${latestTag.name} using ${bump.toUpperCase()}\n`)

// BUMP VERSION

const next = semver.inc(latestTag.name, bump)

core.info(`Current version is ${latestTag.name}`)
core.info(`Next version is v${next}`)

core.exportVariable('current', latestTag.name)
core.exportVariable('next', `v${next}`)
core.exportVariable('nextStrict', next)
core.info(`Current version is ${prefix}${latestTag.name}`)
core.info(`Next version is ${prefix}v${next}`)

core.setOutput('current', latestTag.name)
core.setOutput('next', `v${next}`)
core.setOutput('nextStrict', next)
core.setOutput('nextMajor', `v${semver.major(next)}`)
core.setOutput('nextMajorStrict', semver.major(next))
outputVersion(next)
}

main()
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"semver": "7.3.8"
},
"devDependencies": {
"@vercel/ncc": "0.36.0",
"eslint": "8.30.0",
"@vercel/ncc": "0.36.1",
"eslint": "8.34.0",
"eslint-config-standard": "17.0.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-import": "2.27.5",
"eslint-plugin-node": "11.1.0",
"eslint-plugin-promise": "6.1.1"
}
Expand Down
Loading

0 comments on commit 35f2f92

Please sign in to comment.