Skip to content

Commit

Permalink
feat!: extended conventional commits (#19)
Browse files Browse the repository at this point in the history
* fix: comply to conventional commits by default

* fix: action path

* fix: add github token

* fix: separate steps

* fix: use version from output

* fix: missing repo name in release create step

* fix: self release workflow configuration

* chore: add PR title verification

* style: trailing spaces

* chore: change default branch name to main
  • Loading branch information
ArwynFr committed May 30, 2023
1 parent a2f4b3d commit be1fe05
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 98 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ name: release

on:
push:
branches: [master]
branches: [main]

jobs:
release:
runs-on: ubuntu-latest
name: "Publish a new release"
steps:

- name: Create Github release
uses: arwynfr/actions-conventional-versioning@master
with:
allow-additional-modifiers: true
feat-upgrades-minor: false
strict-types: true

- name: Create version tags
uses: arwynfr/actions-conventional-versioning/new-versionTags@master
22 changes: 22 additions & 0 deletions .github/workflows/verify-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Verify PR title

on:
pull_request_target:
types:
- opened
- edited
- synchronize
- labeled
- unlabeled

jobs:
check:
runs-on: ubuntu-latest
steps:

- name: Verify PR title
shell: pwsh
run: |
Install-Module -Force ConventionalCommits
Import-Module -Force ConventionalCommits
"${{ github.event.pull_request.title }}" | ConvertTo-ConventionalCommitHeader -StrictTypes -AdditionalModifiers
36 changes: 16 additions & 20 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
= Conventional Versioning: new release
:toc: preamble

This Github action creates a new Github Release, using https://semver.org/[Semantic Versioning].
This Github action creates a new Github Release, using https://semver.org/[Semantic Versioning] and https://www.conventionalcommits.org/en/v1.0.0/[Conventional Commits].

WARNING: Version 2 of this action dropped support from conventional commits 1.0.0.

== About
=== Current version
The current version is based on latest Github Release *tag name*.
If there is no release or the tag is not a valid version number, the action will default to `0.1.0`. Current version should not include any prefix, values such as `v1.2.3` or `ver-1.2.3` are **not** understood by this action and will have the action use default value of `0.1.0`.

=== Change type
The type of version change is based on HEAD commit message, taken from https://docs.github.com/en/actions/learn-github-actions/contexts[`github.event` context]. The commit message is expected to follow the format `<type>: message`. The type of version change depends on the type provided:

* `patch` will bump the patch version number
* `minor` will bump the minor version number
* `major` will bump the major version number

If type is not found the action will then look for long-format tokens in the commit message:

* `BREAKING CHANGE:` for major updates
* `NEW FEATURE:` for minor updates

If the message is not understood as a valid conventional commit, the change type will default to `patch`.
You should consider using this action in conjunction with another one enforcing commit message format.
=== Versioning rules
See link:get-newVersion/README.adoc[get-newVersion] for details on versioning rules.

=== Release creation
This action uses https://cli.github.com/[Github CLI] and needs **not** to checkout the repository.
Expand All @@ -32,9 +16,21 @@ You can add additional files using the `pattern` input variable.
This pattern matches the syntax for https://cli.github.com/manual/gh_release_create[`gh release create`] command.

== Inputs
_None_

Pattern::
*Optional*. Files to include in the GitHub release.

Allow-additional-modifiers::
*Optional*. Set to `true` to support using additional modifiers `+` and `-` for minor and patch level updates respectively. Also supports using `NEW FEATURE:` footer for minor updates.

Feat-upgrades-minor::
*Optional*. Set to `false` to increment a patch bump instead of a minor one when the commit type is `feat:`.

Strict-types::
*Optional*. Set to `true` to fail this action when the commit type is not in the following list: `build`, `chore`, `ci`, `docs`, `feat`, `fix`, `perf`, `refactor`, `revert`, `style`, `test`.

== Outputs

Current-version::
Current version found in the repository, format `1.2.3`.

Expand Down
33 changes: 26 additions & 7 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ inputs:
description: 'Pattern of files to include in the release'
required: false

whatif:
description: 'Set to "$true" if you want to calculate the next-version without creating the release'
allow-additional-modifiers:
description: 'Whether this action supports additional modifiers and footers for minor and patch updates.'
required: false
default: '$false'
deprecationMessage: 'Please use new action instead: arwynfr/actions-conventional-versioning/get-newVersion'
default: 'false'

feat-upgrades-minor:
description: 'Whether this action should increment a minor bump when the commit type is feat.'
required: false
default: 'true'

strict-types:
description: 'Whether this action should fail if the commit type is a custom type.'
required: false
default: 'false'

outputs:

Expand All @@ -37,10 +46,20 @@ runs:

- shell: pwsh
id: new-github-release
env:
GH_TOKEN: ${{ github.token }}
run: |
"${{ github.token }}" | gh auth login --with-token
& "${env:GITHUB_ACTION_PATH}/functions/New-GithubRelease.ps1" `
& "${env:GITHUB_ACTION_PATH}/functions/Get-NewVersion.ps1" `
-RepositoryName "${{ github.repository }}" `
-CommitMessage "${{ github.event.head_commit.message }}" `
-FeatUpgradesMinor:$${{ inputs.feat-upgrades-minor }} `
-AllowAdditionalModifiers:$${{ inputs.allow-additional-modifiers }}
- shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
run: |
& "${env:GITHUB_ACTION_PATH}/functions/New-GithubRelease.ps1" `
-RepositoryName "${{ github.repository }}" `
-Pattern "${{ inputs.pattern }}" `
-Whatif:${{ inputs.whatif }}
-NextVersion ${{ steps.new-github-release.outputs.next-version }}
27 changes: 24 additions & 3 deletions functions/Get-NewVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,33 @@ param (

[Parameter()]
[string]
$CommitMessage
$CommitMessage,

[Parameter()]
[switch]
$FeatUpgradesMinor,

[Parameter()]
[switch]
$AllowAdditionalModifiers,

[Parameter()]
[switch]
$StrictTypes
)

Install-Module -Force StepSemVer, ConventionalCommits
Import-Module -Force StepSemVer, ConventionalCommits

$Private:parameters = @{
FeatUpgradesMinor = $FeatUpgradesMinor
AllowAdditionalModifiers = $AllowAdditionalModifiers
CommitMessage = $CommitMessage
StrictTypes = $StrictTypes
}
$Private:currentVersion = & $PSScriptRoot/Get-CurrentVersion.ps1 -RepositoryName "$RepositoryName"
$Private:bumpType = & $PSScriptRoot/Get-VersionBumpType.ps1 -CommitMessage "$CommitMessage"
$Private:nextVersion = & $PSScriptRoot/New-SemVer.ps1 -Version $Private:currentVersion -BumpType $Private:bumpType
$Private:bumpType = & $PSScriptRoot/Get-VersionBumpType.ps1 @Private:parameters
$Private:nextVersion = $Private:currentVersion | Step-SemVer -BumpType $Private:bumpType

"current-version=$Private:currentVersion" >> $env:GITHUB_OUTPUT
"bump-type=$Private:bumpType" >> $env:GITHUB_OUTPUT
Expand Down
34 changes: 21 additions & 13 deletions functions/Get-VersionBumpType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@
param (
[Parameter()]
[string]
$CommitMessage
)

$ConventionalCommit = $CommitMessage -match '^(patch|minor|major): '
$CommitMessage,

if ($ConventionalCommit) {
return $Matches[1]
}
[Parameter()]
[switch]
$FeatUpgradesMinor,

if ($CommitMessage -match 'BREAKING CHANGE:') {
return 'major'
}
[Parameter()]
[switch]
$AllowAdditionalModifiers,

[Parameter()]
[switch]
$StrictTypes
)

if ($CommitMessage -match 'NEW FEATURE:') {
return 'minor'
$conventions = $CommitMessage | ConvertTo-ConventionalCommitHeader -StrictTypes:$StrictTypes -AdditionalModifiers:$AllowAdditionalModifiers
$modifier_bump = '-+!'.IndexOf($conventions.Modifier ?? '-')
$type_bump = $conventions.Type -eq 'feat' -and $FeatUpgradesMinor ? 1 : 0
$long_bump = switch ($true) {
($CommitMessage -match '\nBREAKING CHANGE: ') { 2 }
($CommitMessage -match '\nNEW FEATURE: ') { $AllowAdditionalModifiers ? 1 : 0 }
default { 0 }
}

return 'patch'
$bump = (@($modifier_bump, $type_bump, $long_bump) | Measure-Object -Maximum).Maximum
return @('patch', 'minor', 'major')[$bump]
16 changes: 7 additions & 9 deletions functions/New-GithubRelease.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ param (
[Parameter(Mandatory)]
[string]
$RepositoryName,

[Parameter()]
[string]
$CommitMessage,
[Parameter(Mandatory)]
[semver]
$NextVersion,

[Parameter()]
[string]
$Pattern
)

$private:nextVersion = & $PSScriptRoot/Get-NewVersion.ps1 -RepositoryName $RepositoryName -CommitMessage $CommitMessage

if ($PSCmdlet.ShouldProcess($private:nextVersion, 'gh release create')) {
gh release create "$private:nextVersion" --generate-notes --repo "$RepositoryName"
if ($PSCmdlet.ShouldProcess($NextVersion, 'gh release create')) {
gh release create "$NextVersion" --generate-notes --repo "$RepositoryName"
if (-Not [string]::IsNullOrEmpty($Pattern)) {
gh release upload "$private:nextVersion" --repo "$RepositoryName" (Get-Item "$Pattern")
gh release upload "$NextVersion" --repo "$RepositoryName" (Get-Item "$Pattern")
}
}
20 changes: 0 additions & 20 deletions functions/New-SemVer.ps1

This file was deleted.

42 changes: 19 additions & 23 deletions get-newVersion/README.adoc
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
= Conventional Versioning: get new version
= Conventional Versioning: get-newVersion
:toc: preamble

This Github action calculates a new product version, using https://semver.org/[Semantic Versioning].

WARNING: Version 2 of this action dropped support from conventional commits 1.0.0.
This Github action creates a new Github Release, using https://semver.org/[Semantic Versioning] and https://www.conventionalcommits.org/en/v1.0.0/[Conventional Commits].

== About
=== Current version
The current version is based on latest Github Release *tag name*.
If there is no release or the tag is not a valid version number, the action will default to `0.1.0`. Current version should not include any prefix, values such as `v1.2.3` or `ver-1.2.3` are **not** understood by this action and will have the action use default value of `0.1.0`.

=== Change type
The type of version change is based on HEAD commit message, taken from https://docs.github.com/en/actions/learn-github-actions/contexts[`github.event` context]. The commit message is expected to follow the format `<type>: message`. The type of version change depends on the type provided:

* `patch` will bump the patch version number
* `minor` will bump the minor version number
* `major` will bump the major version number

If type is not found the action will then look for long-format tokens in the commit message:
The type of version change is based on HEAD commit message, taken from https://docs.github.com/en/actions/learn-github-actions/contexts[`github.event` context]. The commit message is expected to follow the format specified in the conventional commits specifications:

* `BREAKING CHANGE:` for major updates
* `NEW FEATURE:` for minor updates

If the message is not understood as a valid conventional commit, the change type will default to `patch`.
You should consider using this action in conjunction with another one enforcing commit message format.

=== Release creation
This action uses https://cli.github.com/[Github CLI] and needs **not** to checkout the repository.
The release will autogenerate the notes, tag the source code, and include source archives to the release by default.
You can add additional files using the `pattern` input variable.
This pattern matches the syntax for https://cli.github.com/manual/gh_release_create[`gh release create`] command.
* Using the `!` modifier or the `BREAKING CHANGE:` footer will increment a major version
* Using the `feat:` commit typ will increment a minor version
* Other commits will increment a patch version

== Inputs
_None_

Allow-additional-modifiers::
*Optional*. Set to `true` to support using additional modifiers `+` and `-` for minor and patch level updates respectively. Also supports using `NEW FEATURE:` footer for minor updates.

Feat-upgrades-minor::
*Optional*. Set to `false` to increment a patch bump instead of a minor one when the commit type is `feat:`.

Strict-types::
*Optional*. Set to `true` to fail this action when the commit type is not in the following list: `build`, `chore`, `ci`, `docs`, `feat`, `fix`, `perf`, `refactor`, `revert`, `style`, `test`.

== Outputs
Current-version::
Expand Down Expand Up @@ -62,6 +54,10 @@ jobs:

- uses: arwynfr/actions-conventional-versioning/get-newVersion@v2
id: next-version
with:
allow-additional-modifiers: 'true'
feat-upgrades-minor: 'false'
strict-types: 'true'

- run: echo ${{ steps.next-version.output.next-version }}

Expand Down
22 changes: 20 additions & 2 deletions get-newVersion/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ branding:
icon: tag
color: blue

inputs:

allow-additional-modifiers:
description: 'Whether this action supports additional modifiers and footers for minor and patch updates.'
required: false
default: 'false'

feat-upgrades-minor:
description: 'Whether this action should increment a minor bump when the commit type is feat.'
required: false
default: 'true'

strict-types:
description: 'Whether this action should fail if the commit type is a custom type.'
required: false
default: 'false'

outputs:

current-version:
Expand All @@ -26,7 +43,8 @@ runs:
- shell: pwsh
id: new-github-release
run: |
"${{ github.token }}" | gh auth login --with-token
& "${env:GITHUB_ACTION_PATH}/../functions/Get-NewVersion.ps1" `
-RepositoryName "${{ github.repository }}" `
-CommitMessage "${{ github.event.head_commit.message }}"
-CommitMessage "${{ github.event.head_commit.message }}" `
-FeatUpgradesMinor:$${{ inputs.feat-upgrades-minor }} `
-AllowAdditionalModifiers:$${{ inputs.allow-additional-modifiers }}

0 comments on commit be1fe05

Please sign in to comment.