Skip to content

Commit

Permalink
feat: conventional commits support
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielAtanasovski committed Mar 17, 2024
1 parent 71036b2 commit 8a8d9d0
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 2 deletions.
1 change: 1 addition & 0 deletions .shellcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
external-sources=true
13 changes: 12 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ inputs:
description: 'Use the GitHub API to discover current tags, which avoids the need for a git checkout, but requires `curl` and `jq`'
required: false
default: false
conventional_commits:
description: |
Will parse the last commit message to determine the increment type
Supports the following types:
- feat
- fix
- 'BREAKING CHANGE:' in commit body
required: false
default: false

outputs:
current-version:
description: 'Current normal version detected'
value: ${{ steps.version-lookup.outputs.CURRENT_VERSION }}
current-v-version:
description: 'Current normal version detected, prefixed with a `v` charatcter'
description: 'Current normal version detected, prefixed with a `v` character'
value: ${{ steps.version-lookup.outputs.CURRENT_V_VERSION }}
version:
description: 'Incremented version calculated'
Expand Down Expand Up @@ -88,3 +98,4 @@ runs:
scheme: ${{ inputs.scheme }}
release_branch: ${{ inputs.release_branch }}
use_api: ${{ inputs.use_api }}
conventional_commits: ${{ inputs.conventional_commits }}
24 changes: 24 additions & 0 deletions shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ pcre_master_ver='^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9
pcre_allow_vprefix="^v{0,1}${pcre_master_ver:1}"
pcre_old_calver='^(?P<major>0|[1-9]\d*)-0{0,1}(?P<minor>0|[0-9]\d*)-R(?P<patch>0|[1-9]\d*)$'

##==----------------------------------------------------------------------------
## Conventional commit regexes
## see: https://www.conventionalcommits.org/en/v1.0.0/

pcre_conventional_commit_feat='^feat(\([a-zA-Z0-9-]+\))?:\s.*'
pcre_conventional_commit_feat_breaking='^feat(\([a-zA-Z0-9-]+\))?!:.*'
pcre_conventional_commit_fix='^fix(\([a-zA-Z0-9-]+\))?:\s.*'
pcre_conventional_commit_fix_breaking='^fix(\([a-zA-Z0-9-]+\))?!:.*'
pcre_conventional_commit_breaking_change_in_body='BREAKING CHANGE:'

declare -A pcre_conventional_commit_to_increment=(
["$pcre_conventional_commit_feat"]="minor"
["$pcre_conventional_commit_feat_breaking"]="major"
["$pcre_conventional_commit_fix"]="patch"
["$pcre_conventional_commit_fix_breaking"]="major"
["$pcre_conventional_commit_breaking_change_in_body"]="major"
)

##==----------------------------------------------------------------------------
## Input validation

Expand Down Expand Up @@ -40,6 +58,12 @@ if [[ "${use_api}" == 'true' ]] ; then
fi
fi

conventional_commits="${conventional_commits:-false}"
if [[ "${conventional_commits}" != 'false' && "${conventional_commits}" != 'true' ]] ; then
echo "🛑 Value of 'conventional_commits' is not valid, choose from 'false' or 'true'" 1>&2
input_errors='true'
fi


##==----------------------------------------------------------------------------
## MacOS compatibility - for local testing
Expand Down
135 changes: 135 additions & 0 deletions tests/test_version-increment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,138 @@ function init_repo {
[[ "$output" = *"PRE_RELEASE_LABEL=pre.${short_ref}"* ]] &&
[[ "$output" = *"VERSION=$(date +%Y.%-m.1)+pre.${short_ref}"* ]]
}

@test "increments the patch version after a fix commit (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "fix: something" > fix.txt
git add fix.txt
git commit -m "fix: something"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=1.2.4"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}


@test "increments the patch version after a scoped fix commit (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "fix: something" > fix.txt
git add fix.txt
git commit -m "fix(api): something"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=1.2.4"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}

@test "increments the major version after a breaking fix commit (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "fix: something" > fix.txt
git add fix.txt
git commit -m "fix!: something"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=2.0.0"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}

@test "increments the minor version after a feat commit (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "new feature" > feat.txt
git add feat.txt
git commit -m "feat: something"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=1.3.0"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}

@test "increments the major version after a breaking feat commit (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "new feature" > feat.txt
git add feat.txt
git commit -m "feat!: something"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=2.0.0"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}

@test "increments the major version after a breaking change in the commit body (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "breaking change" > feat.txt
git add feat.txt
git commit -m "Fix: something
BREAKING CHANGE: really important"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=2.0.0"* ]]
[[ "$output" != *"No conventional commit found"* ]]
}

@test "increments the patch version by default if no conventional commits found and enabled (conventional commits)" {
init_repo

export current_version=1.2.3
export conventional_commits="true"

# Create a commit with a fix
echo "breaking change" > feat.txt
git add feat.txt
git commit -m "something important"

run ../../version-increment.sh

print_run_info
[ "$status" -eq 0 ] &&
[[ "$output" = *"VERSION=1.2.4"* ]]
[[ "$output" = *"No conventional commit found"* ]]
}
26 changes: 25 additions & 1 deletion version-increment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,38 @@ elif [[ -z "${BATS_VERSION:-}" ]] ; then
fi

current_ref="${GITHUB_REF:-}"
git_commit_sha=${GITHUB_SHA:-}

if [[ "${use_api:-}" == 'true' ]] ; then
# because we cannot use `rev-parse` with the API, we'll take a punt that 9 characters is enough for uniqueness
# shellcheck disable=SC2001
git_commit="$(echo "${GITHUB_SHA:0:9}" | sed 's/0*//')" # Also, trim leading zeros, because semver doesn't allow that in
else # the 'pre-release version' part, but we can't use the + char
git_commit="$(git rev-parse --short HEAD | sed 's/0*//')" # to make it 'build metadata' as that's not supported in K8s
fi # labels
git_commit_sha="$(git rev-parse --short HEAD)" # labels
fi

##==----------------------------------------------------------------------------
## Conventional commits

if [[ "${conventional_commits:-}" == 'true' ]] ; then
# Get message from given commit
commit_message=$(git log -1 --pretty=format:%B "${git_commit_sha}")

# Check commit message header
found_match=false
for pcre in "${!pcre_conventional_commit_to_increment[@]}" ; do
if [[ -n "$(echo "${commit_message}" | ${grep} -P "${pcre}")" ]] ; then
increment="${pcre_conventional_commit_to_increment[${pcre}]}"
found_match=true
break
fi
done

if [[ "${found_match}" == 'false' ]] ; then
echo "⚠️ No conventional commit found, defaulting to ${increment} increment"
fi
fi

##==----------------------------------------------------------------------------
## Version increment
Expand Down

0 comments on commit 8a8d9d0

Please sign in to comment.