diff --git a/.github/workflows/check-version.yml b/.github/workflows/check-version.yml new file mode 100644 index 00000000..afa3c2dd --- /dev/null +++ b/.github/workflows/check-version.yml @@ -0,0 +1,24 @@ +on: + pull_request: + branches: + - master + - main + +name: Check Version + +jobs: + all: + runs-on: ubuntu-latest + + name: Check Version + + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Check version format and availability + run: ./scripts/check_version diff --git a/DESCRIPTION b/DESCRIPTION index f9ce50cb..1b47229b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: orderly2 Title: Orderly Next Generation -Version: 1.99.21 +Version: 1.99.22 Authors@R: c(person("Rich", "FitzJohn", role = c("aut", "cre"), email = "rich.fitzjohn@gmail.com"), person("Robert", "Ashton", role = "aut"), diff --git a/scripts/bump_version b/scripts/bump_version new file mode 100755 index 00000000..8cce07a6 --- /dev/null +++ b/scripts/bump_version @@ -0,0 +1,55 @@ +#!/usr/bin/env bash +set -eu + +ACTION="${1:-patch}" +CURRENT_VERSION=$(grep '^Version' DESCRIPTION | sed 's/.*: *//') +MAJOR=$(echo $CURRENT_VERSION | cut -d. -f1) +MINOR=$(echo $CURRENT_VERSION | cut -d. -f2) +PATCH=$(echo $CURRENT_VERSION | cut -d. -f3) +echo "Current version: ${CURRENT_VERSION}" + +if [ $# -gt 1 ]; then + echo "Invalid args" + exit 1 +fi + +case $ACTION in + reset) + echo "Resetting version" + LAST_VERSION=$(git show origin/main:DESCRIPTION | grep '^Version' | sed 's/.*: *//') + MAJOR=$(echo $LAST_VERSION | cut -d. -f1) + MINOR=$(echo $LAST_VERSION | cut -d. -f2) + PATCH=$(echo $LAST_VERSION | cut -d. -f3) + ;; + major) + echo "Updating major version" + MAJOR=$(($MAJOR + 1)) + MINOR=0 + PATCH=0 + ;; + minor) + echo "Updating minor version" + MINOR=$(($MINOR + 1)) + PATCH=0 + ;; + patch) + echo "Updating patch version" + PATCH=$(($PATCH + 1)) + ;; + *) + if echo "$ACTION" | grep -Eq "[0-9]+[.][0-9]+[.][0-9]+"; then + echo "Updating version directly" + MAJOR=$(echo $VERSION | cut -d. -f1) + MINOR=$(echo $VERSION | cut -d. -f2) + PATCH=$(echo $VERSION | cut -d. -f3) + else + echo "[ERROR] Invalid format version number '$VERSION' must be in format 'x.y.z'" + exit 1 + fi + ;; +esac + +NEXT_VERSION="${MAJOR}.${MINOR}.${PATCH}" +echo "Updating to version: ${NEXT_VERSION}" + +sed -i "s/^Version: .*$/Version: ${NEXT_VERSION}/" DESCRIPTION diff --git a/scripts/check_version b/scripts/check_version new file mode 100755 index 00000000..02798d9d --- /dev/null +++ b/scripts/check_version @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +set -e +# Usage: +# check_version [] +# +# If version is not given as a positional argument, then we'll read it +# from the DESCRIPTION file. +# +# We assume that a version already exists as a tag. +VERSION=${1:-$(grep '^Version' DESCRIPTION | sed 's/.*: *//')} +TAG="v${VERSION}" + +echo "Proposed version number '$VERSION'" + +if echo "$VERSION" | grep -Eq "[0-9]+[.][0-9]+[.][0-9]+"; then + echo "[OK] Version number in correct format" +else + echo "[ERROR] Invalid format version number '$VERSION' must be in format 'x.y.z'" + exit 1 +fi + +EXIT_CODE=0 + +LAST_VERSION=$(git show origin/main:DESCRIPTION | grep '^Version' | sed 's/.*: *//') + +echo "Last version was '$LAST_VERSION'" + +MAJOR=$(echo $VERSION | cut -d. -f1) +MINOR=$(echo $VERSION | cut -d. -f2) +PATCH=$(echo $VERSION | cut -d. -f3) + +LAST_VERSION=$(echo "$LAST_VERSION" | sed 's/^v//') +LAST_MAJOR=$(echo $LAST_VERSION | cut -d. -f1) +LAST_MINOR=$(echo $LAST_VERSION | cut -d. -f2) +LAST_PATCH=$(echo $LAST_VERSION | cut -d. -f3) + +if (( $MAJOR > $LAST_MAJOR )); then + echo "[OK] ${VERSION} increases MAJOR version over old version ${LAST_VERSION}" +elif (( $MINOR > $LAST_MINOR )); then + echo "[OK] ${VERSION} increases MINOR version over old version ${LAST_VERSION}" +elif (( $PATCH > $LAST_PATCH )); then + echo "[OK] ${VERSION} increases PATCH version over old version ${LAST_VERSION}" +else + echo "[ERROR] Version number has not increased relative to $LAST_VERSION" + EXIT_CODE=1 +fi + +exit $EXIT_CODE