Skip to content

Commit

Permalink
feat: customize version prefix for release and branch names (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
simoneb authored Aug 24, 2022
1 parent 73a9970 commit 3894773
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 31 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: ci
on:
push:
branches:
- main
on:
push:
branches:
- main
pull_request:
jobs:
build:
Expand All @@ -13,6 +13,7 @@ jobs:
with:
node-version-file: '.nvmrc'
- run: npm ci
- run: npm run lint
- run: npm test

automerge:
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ inputs:
artifact-path:
description: 'Set this input to the distribution folder or file you want to add as the main asset for your release. It will be downloadable from the release page and a preview of it will be available in the pull request.'
required: false
version-prefix:
description: 'A prefix to apply to the version number, which reflects in the tag and GitHub release names'
required: true
default: 'v'

runs:
using: 'composite'
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26251,7 +26251,7 @@ module.exports = async function ({ context, inputs, packageVersion }) {
if (!packageVersion) {
throw new Error('packageVersion is missing!')
}
const newVersion = `v${packageVersion}`
const newVersion = `${inputs['version-prefix']}${packageVersion}`

const branchName = `release/${newVersion}`

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"main": "src/index.js",
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint . --fix",
"test": "npm run lint && tap",
"test": "tap",
"build": "ncc build src/index.js",
"prepare": "husky install"
},
Expand Down
2 changes: 1 addition & 1 deletion src/openPr.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = async function ({ context, inputs, packageVersion }) {
if (!packageVersion) {
throw new Error('packageVersion is missing!')
}
const newVersion = `v${packageVersion}`
const newVersion = `${inputs['version-prefix']}${packageVersion}`

const branchName = `release/${newVersion}`

Expand Down
103 changes: 80 additions & 23 deletions test/bump.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const callApiAction = require('../src/utils/callApi')
const artifactAction = require('../src/utils/artifact')
const { PR_TITLE_PREFIX } = require('../src/const')

const TEST_VERSION = 'v3.1.1'
const TEST_VERSION = '3.1.1'
const runSpawnStub = sinon.stub().returns(TEST_VERSION)

function setup() {
Expand Down Expand Up @@ -51,10 +51,11 @@ tap.afterEach(() => {
})

const DEFAULT_ACTION_DATA = {
packageVersion: TEST_VERSION.slice(1),
packageVersion: TEST_VERSION,
inputs: {
semver: 'patch',
'commit-message': 'Release {version}',
'version-prefix': 'v',
},
context: {
actor: 'John',
Expand All @@ -74,7 +75,7 @@ const DEFAULT_ACTION_DATA = {
},
}

tap.test('it trigger an error when the packageVersion is missing', async t => {
tap.test('it triggers an error when the packageVersion is missing', async t => {
const { openPr } = setup()

try {
Expand All @@ -93,7 +94,7 @@ tap.test('should create a new git branch', async () => {
const { openPr, stubs } = setup()
await openPr(DEFAULT_ACTION_DATA)

const branchName = `release/${TEST_VERSION}`
const branchName = `release/v${TEST_VERSION}`

sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'checkout',
Expand All @@ -104,7 +105,7 @@ tap.test('should create a new git branch', async () => {
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'commit',
'-m',
`"Release ${TEST_VERSION}"`,
`"Release v${TEST_VERSION}"`,
])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'push',
Expand All @@ -120,23 +121,78 @@ tap.test('should handle custom commit messages', async () => {
'[{version}] The brand new {version} has been released'
await openPr(data)

const branchName = `release/v${TEST_VERSION}`

sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'checkout',
'-b',
branchName,
])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'commit',
'-m',
`"[v${TEST_VERSION}] The brand new v${TEST_VERSION} has been released"`,
])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'push',
'origin',
branchName,
])
})

tap.test('should work with a custom version-prefix', async () => {
const { openPr, stubs } = setup()

const prData = {
...DEFAULT_ACTION_DATA,
inputs: {
...DEFAULT_ACTION_DATA.inputs,
'version-prefix': '',
},
}

await openPr(prData)

const branchName = `release/${TEST_VERSION}`

// git
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'checkout',
'-b',
branchName,
])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', ['add', '-A'])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'commit',
'-m',
`"[${TEST_VERSION}] The brand new ${TEST_VERSION} has been released"`,
`"Release v${TEST_VERSION}"`,
])
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'push',
'origin',
branchName,
])

// github release
sinon.assert.calledWithExactly(
stubs.callApiStub,
{
method: 'POST',
endpoint: 'release',
body: {
version: TEST_VERSION,
},
},
prData.inputs
)

sinon.assert.calledWithMatch(stubs.callApiStub, {
method: 'POST',
endpoint: 'pr',
body: {
head: `refs/heads/${branchName}`,
},
})
})

tap.test('should call the release endpoint with a new version', async () => {
Expand All @@ -149,7 +205,7 @@ tap.test('should call the release endpoint with a new version', async () => {
method: 'POST',
endpoint: 'release',
body: {
version: TEST_VERSION,
version: `v${TEST_VERSION}`,
},
},
DEFAULT_ACTION_DATA.inputs
Expand All @@ -160,7 +216,8 @@ tap.test('should call the PR endpoint with a new version', async () => {
const { openPr, stubs } = setup()
await openPr(DEFAULT_ACTION_DATA)

const branchName = `release/${TEST_VERSION}`
const branchName = `release/v${TEST_VERSION}`

sinon.assert.calledWithExactly(
stubs.callApiStub,
{
Expand All @@ -175,7 +232,7 @@ tap.test('should call the PR endpoint with a new version', async () => {
'\n' +
'This **draft** PR is opened by Github action [optic-release-automation-action](https://github.com/nearform/optic-release-automation-action).\n' +
'\n' +
`A new **draft** GitHub release [${TEST_VERSION}]() has been created.\n` +
`A new **draft** GitHub release [v${TEST_VERSION}]() has been created.\n` +
'\n' +
`Release author: @John\n` +
'\n' +
Expand All @@ -197,7 +254,7 @@ tap.test('should call the PR endpoint with a new version', async () => {
'\n' +
'\n' +
'<!--\n' +
`<release-meta>{"version":"${TEST_VERSION}"}</release-meta>\n` +
`<release-meta>{"version":"v${TEST_VERSION}"}</release-meta>\n` +
'-->\n',
},
},
Expand All @@ -208,14 +265,14 @@ tap.test('should call the PR endpoint with a new version', async () => {
tap.test(
'should create the correct release for a version with no minor',
async () => {
const localVersion = 'v2.0.0'
const localVersion = '2.0.0'
const { openPr, stubs } = setup()
runSpawnStub.returns(localVersion)
await openPr({
...DEFAULT_ACTION_DATA,
packageVersion: localVersion.slice(1),
packageVersion: localVersion,
})
const branchName = `release/${localVersion}`
const branchName = `release/v${localVersion}`
sinon.assert.calledWithExactly(
stubs.callApiStub,
{
Expand All @@ -230,7 +287,7 @@ tap.test(
'\n' +
'This **draft** PR is opened by Github action [optic-release-automation-action](https://github.com/nearform/optic-release-automation-action).\n' +
'\n' +
`A new **draft** GitHub release [${localVersion}]() has been created.\n` +
`A new **draft** GitHub release [v${localVersion}]() has been created.\n` +
'\n' +
`Release author: @John\n` +
'\n' +
Expand All @@ -252,7 +309,7 @@ tap.test(
'\n' +
'\n' +
'<!--\n' +
`<release-meta>{"version":"${localVersion}"}</release-meta>\n` +
`<release-meta>{"version":"v${localVersion}"}</release-meta>\n` +
'-->\n',
},
},
Expand All @@ -264,14 +321,14 @@ tap.test(
tap.test(
'should create the correct release for a version with no major',
async () => {
const localVersion = 'v0.0.5'
const localVersion = '0.0.5'
const { openPr, stubs } = setup()
runSpawnStub.returns(localVersion)
await openPr({
...DEFAULT_ACTION_DATA,
packageVersion: localVersion.slice(1),
packageVersion: localVersion,
})
const branchName = `release/${localVersion}`
const branchName = `release/v${localVersion}`
sinon.assert.calledWithExactly(
stubs.callApiStub,
{
Expand All @@ -286,7 +343,7 @@ tap.test(
'\n' +
'This **draft** PR is opened by Github action [optic-release-automation-action](https://github.com/nearform/optic-release-automation-action).\n' +
'\n' +
`A new **draft** GitHub release [${localVersion}]() has been created.\n` +
`A new **draft** GitHub release [v${localVersion}]() has been created.\n` +
'\n' +
`Release author: @John\n` +
'\n' +
Expand All @@ -308,7 +365,7 @@ tap.test(
'\n' +
'\n' +
'<!--\n' +
`<release-meta>{"version":"${localVersion}"}</release-meta>\n` +
`<release-meta>{"version":"v${localVersion}"}</release-meta>\n` +
'-->\n',
},
},
Expand All @@ -318,13 +375,13 @@ tap.test(
)

tap.test('should delete branch in case of pr failure', async t => {
const localVersion = 'v0.0.5'
const localVersion = '0.0.5'
const { openPr, stubs } = setup()
const { context, inputs } = DEFAULT_ACTION_DATA
stubs.callApiStub.onCall(1).rejects()
await openPr({ context, inputs, packageVersion: localVersion.slice(1) })
await openPr({ context, inputs, packageVersion: localVersion })

const branchName = `release/${localVersion}`
const branchName = `release/v${localVersion}`
sinon.assert.calledWithExactly(stubs.runSpawnStub, 'git', [
'push',
'origin',
Expand Down

0 comments on commit 3894773

Please sign in to comment.