Skip to content

Commit

Permalink
using commit messages instead of PR labels
Browse files Browse the repository at this point in the history
  • Loading branch information
MCKanpolat committed Nov 25, 2020
1 parent 8460a0f commit 551739e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 25 deletions.
13 changes: 9 additions & 4 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
import {increment} from '../src/versionBuilder'

test('increment minor version', () => {
const nextVersion = increment('1.0.0', '', ['minor'])
const nextVersion = increment('1.0.0', '', ['#minor'], 'patch')
console.log(nextVersion.version)
expect(nextVersion.version).toEqual('1.1.0')
})

test('increment major version', () => {
const nextVersion = increment('1.0.0', '', ['major'])
const nextVersion = increment('1.0.0', '', ['#major'], 'patch')
console.log(nextVersion.version)
expect(nextVersion.version).toEqual('2.0.0')
})

test('increment patch version', () => {
const nextVersion = increment('1.0.0', '', ['patch'])
const nextVersion = increment('1.0.0', '', ['#patch'], 'patch')
console.log(nextVersion.version)
expect(nextVersion.version).toEqual('1.0.1')
})

test('increment pre patch version', () => {
const nextVersion = increment('1.0.0', 'beta', ['prepatch'])
const nextVersion = increment(
'1.0.0',
'beta',
['this is test #prepatch version'],
'patch'
)
console.log(nextVersion.version)
expect(nextVersion.version).toEqual('1.0.1-beta.0')
})
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
name: 'Auto Increment Semver Action'
description: 'Generates a new version using Pull Request Labels with Semver'
description: 'Generates a new version using Commit Message Syntax with Semver'
author: 'Mehmet Can Kanpolat'
inputs:
identifier:
required: false
description: 'identifier for version (beta,alpha..)'
default: ''
releaseType:
required: true
description: 'Uses this if any labels cannot be found in the commit messages.'
default: 'patch'
github_token:
description: 'Token to get tags from the repo. Pass in using `secrets.GITHUB_TOKEN`.'
required: true
Expand Down
21 changes: 12 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "auto-semver-action",
"version": "1.0.0",
"private": true,
"description": "Generates a new version using Pull Request Labels with Semver",
"description": "Generates a new version using Commit Message Syntax with Semver",
"main": "lib/main.js",
"scripts": {
"build": "tsc",
Expand Down
9 changes: 6 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import {getMostRecentVersionFromTags, increment} from './versionBuilder'
async function run(): Promise<void> {
try {
const versionIdentifier: string = core.getInput('identifier') || ''
const payloadLabels = context.payload.pull_request?.labels || []
const defaultReleaseType: string = core.getInput('releaseType') || ''
const commitMessages = context.payload.commits?.message || []

core.debug(`Context payload => ${JSON.stringify(context.payload)}`)
const latestVer = await getMostRecentVersionFromTags(context)
const nextVersion = increment(
latestVer.version,
versionIdentifier,
payloadLabels
commitMessages,
defaultReleaseType
)

core.exportVariable('VERSION', nextVersion?.version)
core.exportVariable('version', nextVersion?.version)
core.setOutput('version', nextVersion?.version)
} catch (error) {
core.setFailed(error.message)
Expand Down
17 changes: 11 additions & 6 deletions src/versionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,28 @@ const defaultConfig = {
export function increment(
versionNumber: string,
versionIdentifier: string,
labels: string[]
commitMessages: string[],
defaultReleaseType: string
): semver.SemVer {
const version = semver.parse(versionNumber) || new semver.SemVer('0.0.0')
core.debug(`Config used => ${JSON.stringify(defaultConfig)}`)

const matchedLabels = new Set<string>()

core.debug(`Found PR labels on Payload => ${JSON.stringify(labels)}`)
for (const label of labels) {
for (const message of commitMessages) {
for (const [key, value] of Object.entries(defaultConfig)) {
if (matcher.isMatch(label, value)) {
if (matcher.isMatch(message, `*#${value}*`)) {
matchedLabels.add(key)
}
}
}

core.debug(`Parsed PR labels => ${JSON.stringify(matchedLabels)}`)
core.debug(
`Parsed labels from commit messages => ${JSON.stringify(matchedLabels)}`
)

if (matchedLabels.size === 0) {
matchedLabels.add(defaultReleaseType)
}

for (const label of matchedLabels) {
version?.inc(label as semver.ReleaseType, versionIdentifier)
Expand Down

0 comments on commit 551739e

Please sign in to comment.