Skip to content

Commit ec3d7c8

Browse files
authored
fix: correct production version bump and add tests (#6)
1 parent be8e2a9 commit ec3d7c8

File tree

7 files changed

+61
-28
lines changed

7 files changed

+61
-28
lines changed

__tests__/index.test.ts

-7
This file was deleted.

dist/index.js

+15-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ import { generateChangelog } from './changelog'
44
import { getLastCommitMessage, getLastGitTag, tagCommit } from './git'
55
import { createGithubRelease } from './github'
66
import { notifySlackChannel } from './slack'
7-
import { getNextVersion, getReleaseTypeFromCommitMessage } from './version'
7+
import { getNextVersion, getPureVersion, getReleaseTypeFromCommitMessage } from './version'
88

99
async function run(): Promise<void> {
1010
const isReleaseCandidate = core.getInput('release-candidate') === 'true'
1111
const slackWebhookUrl = core.getInput('slack-webhook-url')
1212

1313
try {
14-
const lastVersion = await getLastGitTag({
14+
const currentVersion = await getLastGitTag({
1515
considerReleaseCandidates: true,
1616
logInGroup: true,
1717
})
18-
if (lastVersion === null)
18+
if (currentVersion === null)
1919
return
2020

2121
const lastCommitMessage = await getLastCommitMessage()
@@ -26,7 +26,9 @@ async function run(): Promise<void> {
2626

2727
// If the commit isn't of type `feat` or `fix`, we don't want to bump the version
2828
if (releaseType !== null) {
29-
const nextVersion = getNextVersion(lastVersion, releaseType)
29+
const nextVersion = isReleaseCandidate
30+
? getNextVersion({ currentVersion, releaseType })
31+
: getPureVersion(currentVersion)
3032
core.info(`Publishing a release candidate for version ${nextVersion}`)
3133

3234
const changelog = await generateChangelog(context)

src/version.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@ export const getReleaseTypeFromCommitMessage = (commitMessage: string): ReleaseT
1111
return null
1212
}
1313

14-
export const getNextVersion = (currentVersion: string, releaseType: ReleaseType): string => {
14+
export const getNextVersion = (options: {
15+
currentVersion: string
16+
releaseType: ReleaseType
17+
}): string => {
1518
// verify that the current version is valid semver
16-
if (currentVersion.match(/^\d+\.\d+\.\d+(-[\w\d]+)?$/) === null) {
17-
core.error(`Invalid current version: ${currentVersion}`)
18-
throw Error
19+
if (options.currentVersion.match(/^\d+\.\d+\.\d+(-[\w\d]+)?$/) === null) {
20+
const errorMessage = `Invalid current version: ${options.currentVersion}`
21+
core.error(errorMessage)
22+
throw new Error(errorMessage)
1923
}
2024

21-
const pureVersion = currentVersion.split('-')[0]
25+
const pureVersion = options.currentVersion.split('-')[0]
2226
const [major, minor, patch] = pureVersion.split('.').map(Number)
2327
return ({
2428
major: () => `${major + 1}.0.0`,
2529
minor: () => `${major}.${minor + 1}.0`,
2630
patch: () => `${major}.${minor}.${patch + 1}`,
27-
})[releaseType]()
31+
})[options.releaseType]()
2832
}
33+
34+
export const getPureVersion = (version: string) => version.split('-')[0]
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Vitest Snapshot v1
2+
3+
exports[`Version should > throw error if version is in the wrong format > "Invalid current version: foo" 1`] = `"Invalid current version: foo"`;

tests/version.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getNextVersion, getPureVersion } from '../src/version'
3+
4+
describe('Version should', () => {
5+
it('infer next version correctly', () => {
6+
expect(getNextVersion({ currentVersion: '1.0.0', releaseType: 'patch' }))
7+
.toMatch('1.0.1')
8+
expect(getNextVersion({ currentVersion: '1.0.0', releaseType: 'minor' }))
9+
.toMatch('1.1.0')
10+
expect(getNextVersion({ currentVersion: '1.0.0', releaseType: 'major' }))
11+
.toMatch('2.0.0')
12+
expect(getNextVersion({ currentVersion: '1.0.0-rc', releaseType: 'major' }))
13+
.toMatch('2.0.0')
14+
})
15+
16+
it('throw error if version is in the wrong format', () => {
17+
expect(() => getNextVersion({ currentVersion: 'foo', releaseType: 'patch' }))
18+
.toThrowErrorMatchingSnapshot('"Invalid current version: foo"')
19+
})
20+
21+
it('get pure version correctly', () => {
22+
expect(getPureVersion('1.0.0-rc-beta')).toMatch('1.0.0')
23+
})
24+
})

0 commit comments

Comments
 (0)