Skip to content

Commit

Permalink
Add step to calculate next actual semantic version (#39)
Browse files Browse the repository at this point in the history
* Add step to calculate next actual semantic version

* Don't error on unchanged version

* Fix character range in sed expression

Co-authored-by: Peter Jenkins <[email protected]>

* Fix character range in sed expression

Co-authored-by: Peter Jenkins <[email protected]>

* Consume all non-numeric characters at the start of the version

---------

Co-authored-by: Peter Jenkins <[email protected]>
  • Loading branch information
sjagoe and peterjenkins1 authored Nov 26, 2024
1 parent 54502fd commit b11819e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
30 changes: 26 additions & 4 deletions get-next-semantic-version/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ outputs:
version:
description: "Git base branch"
value: ${{ steps.version_check.outputs.version }}
previous_version:
description: "Previous tagged version"
value: ${{ steps.tag_info.outputs.last_tag }}
next_version:
description: "Next version tag name"
value: ${{ steps.calcaulate-version.outputs.next_version }}
continue_release:
description: "true if it is ok to continue the release process"
value: ${{ steps.version_check.outputs.continue_release }}
runs:
using: "composite"
steps:
- name: Calculate PR filters
id: filters
id: tag_info
shell: bash
run: |
last_tag_date=$(git log -1 --exclude="*alpha*" --tags --no-walk --pretty="format:%cI")
last_tag_commit="$(git log -1 --exclude="*alpha*" --tags --no-walk --pretty="format:%H")"
last_tag="$(git describe --tags --abbrev=0 "$last_tag_commit")"
last_tag_date="$(git log -1 --exclude="*alpha*" --no-walk --pretty="format:%cI" "$last_tag_commit")"
start_date=$(date -Is -d "$last_tag_date")
echo "start_date=$start_date" >> "$GITHUB_OUTPUT"
{
echo "start_date=$start_date"
echo "last_tag=$last_tag"
echo "last_tag_commit=$last_tag_commit"
} >> "$GITHUB_OUTPUT"
- name: Get PR labels
uses: octokit/[email protected]
id: get_latest_prs
Expand All @@ -51,7 +63,7 @@ runs:
}
}
}
filter: repo:${{ github.repository }} is:pr base:${{ inputs.git_default_branch }} merged:>${{ steps.filters.outputs.start_date }}
filter: repo:${{ github.repository }} is:pr base:${{ inputs.git_default_branch }} merged:>${{ steps.tag_info.outputs.start_date }}
env:
GITHUB_TOKEN: "${{ inputs.token }}"
- name: Get version being bumped
Expand All @@ -75,3 +87,13 @@ runs:
if: steps.version_check.outputs.continue_release == 'true'
shell: bash
run: echo "Based on tags, we're updating ${{ steps.version_check.outputs.VERSION }} version!"

- name: Calculate version
id: calcaulate-version
shell: bash
env:
BUMP: ${{ steps.version_check.outputs.VERSION }}
PREVIOUS_VERSION: ${{ steps.tag_info.outputs.last_tag }}
run: |
NEXT_VERSION="$("${GITHUB_ACTION_PATH}/calculate-version.sh" "$BUMP" "$PREVIOUS_VERSION")"
echo "next_version=$NEXT_VERSION" >> "$GITHUB_OUTPUT"
38 changes: 38 additions & 0 deletions get-next-semantic-version/calculate-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -euo pipefail

calculate_version() {
local bump="$1"
local previous_version="$2"
local prefix=
local version=
# shellcheck disable=SC2001
prefix="$(echo "$previous_version" | sed -e 's/^\([a-zA-Z]*\).*/\1/')"
# shellcheck disable=SC2001
version="$(echo "$previous_version" | sed -e 's/^[a-zA-Z]*\(.*\)/\1/')"

local without_patch="${version%.*}"
local major="${version//.*/}"
local minor="${without_patch//*./}"
local patch="${version//*./}"

if [[ "$bump" == "major" ]]; then
major=$(( major + 1 ))
elif [[ "$bump" == "minor" ]]; then
minor=$(( minor + 1 ))
elif [[ "$bump" == "patch" ]]; then
patch=$(( patch + 1 ))
elif [[ "$bump" == "" ]]; then
echo "No version to bump" 1>&2
exit 0
else
echo "Unknown version bump $bump" 1>&2
exit 1
fi

echo "$prefix$major.$minor.$patch"
}


calculate_version "$1" "$2"

0 comments on commit b11819e

Please sign in to comment.