Skip to content

Commit

Permalink
feat: Add pre-release support
Browse files Browse the repository at this point in the history
  • Loading branch information
DevScyu committed Oct 28, 2022
1 parent 3a5eb5d commit 14cc315
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ This action will bump version, tag commit and generate a changelog with conventi
- **Optional** `pre-changelog-generation`: Path to the pre-changelog-generation script file. No hook by default.
- **Optional** `skip-ci`: Adds instruction to Github to not consider the push something to rebuild. Default `true`.
- **Optional** `create-summary`: Adds the generated changelog as Action Summary. Default `false`.
- **Optional** `pre-release`: Marks the release as pre-release. Default `false`.
- **Optional** `pre-release-identifier`: The identifier to use for the pre-release. Default `rc`.

### Pre-Commit hook

Expand Down
14 changes: 12 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ inputs:
git-branch:
description: 'The git branch to be pushed'
default: ${{ github.ref }}
required: false
required: false

preset:
description: 'The preset from Conventional Changelog to use'
Expand Down Expand Up @@ -80,7 +80,7 @@ inputs:
description: 'Do not pull the repo before tagging. Ensure you full cloned the repo in the first place to get tags'
default: 'false'
required: false

skip-on-empty:
description: 'Do nothing when the changelog from the latest release is empty'
default: 'true'
Expand Down Expand Up @@ -133,6 +133,16 @@ inputs:
default: 'false'
required: false

pre-release:
description: 'Marks the release as pre-release'
default: 'false'
required: false

pre-release-identifier:
description: 'The identifier to use for pre-releases'
default: 'rc'
required: false

outputs:
changelog:
description: 'The generated changelog for the new version'
Expand Down
37 changes: 10 additions & 27 deletions src/helpers/bumpVersion.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const core = require('@actions/core')
const semverValid = require('semver').valid
const semver = require('semver')

const requireScript = require('./requireScript')

Expand All @@ -11,45 +11,28 @@ const requireScript = require('./requireScript')
* @returns {string}
*/
module.exports = async (releaseType, version) => {
let major, minor, patch
let newVersion

if (version) {
[major, minor, patch] = version.split('.')

switch (releaseType) {
case 'major':
major = parseInt(major, 10) + 1
minor = 0
patch = 0
break

case 'minor':
minor = parseInt(minor, 10) + 1
patch = 0
break
const prerelease = core.getBooleanInput('pre-release')
const identifier = core.getInput('pre-release-identifier')

default:
patch = parseInt(patch, 10) + 1
}
if (version) {
newVersion = semver.inc(version, (prerelease ? 'prerelease' : releaseType), identifier)
} else {
let version = semverValid(core.getInput('fallback-version'))
let version = semver.valid(core.getInput('fallback-version'))

if (version) {
[major, minor, patch] = version.split('.')
newVersion = version
} else {
// default
major = 0
minor = 1
patch = 0
newVersion = '0.1.0'
}

core.info(`The version could not be detected, using fallback version '${major}.${minor}.${patch}'.`)
core.info(`The version could not be detected, using fallback version '${newVersion}'.`)
}

const preChangelogGenerationFile = core.getInput('pre-changelog-generation')

let newVersion = `${major}.${minor}.${patch}`

if (preChangelogGenerationFile) {
const preChangelogGenerationScript = requireScript(preChangelogGenerationFile)

Expand Down
11 changes: 7 additions & 4 deletions src/helpers/generateChangelog.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ const conventionalChangelog = require('conventional-changelog')
* @param releaseCount
* @param config
* @param gitPath
* @param skipUnstable
* @returns {*}
*/
const getChangelogStream = (tagPrefix, preset, version, releaseCount, config, gitPath) => conventionalChangelog({
const getChangelogStream = (tagPrefix, preset, version, releaseCount, config, gitPath, skipUnstable) => conventionalChangelog({
preset,
releaseCount: parseInt(releaseCount, 10),
tagPrefix,
config
config,
skipUnstable
},
{
version,
Expand All @@ -40,10 +42,11 @@ module.exports = getChangelogStream
* @param releaseCount
* @param config
* @param gitPath
* @param skipUnstable
* @returns {Promise<string>}
*/
module.exports.generateStringChangelog = (tagPrefix, preset, version, releaseCount, config, gitPath) => new Promise((resolve, reject) => {
const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount, config, gitPath)
module.exports.generateStringChangelog = (tagPrefix, preset, version, releaseCount, config, gitPath, skipUnstable) => new Promise((resolve, reject) => {
const changelogStream = getChangelogStream(tagPrefix, preset, version, releaseCount, config, gitPath, skipUnstable)

let changelog = ''

Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ async function run() {
const gitPath = core.getInput('git-path')
const skipCi = core.getBooleanInput('skip-ci')
const createSummary = core.getBooleanInput('create-summary')
const prerelease = core.getBooleanInput('pre-release')

if (skipCi) {
gitCommitMessage += ' [skip ci]'
Expand Down Expand Up @@ -84,7 +85,7 @@ async function run() {

const config = conventionalConfigFile && requireScript(conventionalConfigFile)

conventionalRecommendedBump({ preset, tagPrefix, config }, async (error, recommendation) => {
conventionalRecommendedBump({ preset, tagPrefix, config, skipUnstable: !prerelease }, async (error, recommendation) => {
if (error) {
core.setFailed(error.message)
return
Expand Down Expand Up @@ -146,7 +147,7 @@ async function run() {
}

// Generate the string changelog
const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1, config, gitPath)
const stringChangelog = await changelog.generateStringChangelog(tagPrefix, preset, newVersion, 1, config, gitPath, !prerelease)
core.info('Changelog generated')
core.info(stringChangelog)

Expand Down
3 changes: 2 additions & 1 deletion src/version/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ module.exports = class Git extends BaseVersioning {
bump = (releaseType) => {
return new Promise((resolve) => {
const tagPrefix = core.getInput('tag-prefix')
const prerelease = core.getBooleanInput('pre-release')

gitSemverTags({ tagPrefix, }, async (err, tags) => {
gitSemverTags({ tagPrefix, skipUnstable: !prerelease }, async (err, tags) => {
const currentVersion = tags.length > 0 ? tags.shift().replace(tagPrefix, '') : null

// Get the new version
Expand Down

0 comments on commit 14cc315

Please sign in to comment.