Skip to content

Commit

Permalink
Add use_api feature
Browse files Browse the repository at this point in the history
So that you don't _need_ to checkout (clone) the repo to the worker
  • Loading branch information
ps-jay committed Oct 18, 2023
1 parent 27ee2a7 commit 391a2c7
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 11 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ inputs:
description: 'Specify a non-default branch to use for the release tag (the one without -pre)'
required: false
type: string
use_api:
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

outputs:
current-version:
Expand Down Expand Up @@ -69,7 +73,9 @@ runs:
run: ${{ github.action_path }}/version-lookup.sh
shell: bash
env:
github_token: ${{ github.token }}
scheme: ${{ inputs.scheme }}
use_api: ${{ inputs.use_api }}

- id: version-increment
run: ${{ github.action_path }}/version-increment.sh
Expand Down
14 changes: 14 additions & 0 deletions shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ if [[ "${pep440}" != 'false' && "${pep440}" != 'true' ]] ; then
input_errors='true'
fi

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

if [[ "${use_api}" == 'true' ]] ; then
if [[ -z "${github_token:-}" ]] ; then
echo "🛑 'use_api' is true, but environment variable 'github_token' is not set" 1>&2
input_errors='true'
fi
fi


##==----------------------------------------------------------------------------
## MacOS compatibility - for local testing

Expand Down
30 changes: 22 additions & 8 deletions version-increment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,34 @@ fi
## Git info - branch names, commit short ref

default_branch='main'
# if we're _not_ testing, then _actually_ check the origin
if [[ -z "${BATS_VERSION:-}" ]] ; then
default_branch="$(git remote show origin | ${grep} 'HEAD branch' | cut -d ' ' -f 5)"
fi
# use release_branch if not empty
if [[ -n "${release_branch:-}" ]] ; then
default_branch="${release_branch}"
elif [[ -z "${BATS_VERSION:-}" ]] ; then
# if we're _not_ testing, then _actually_ check the origin
if [[ "${use_api:-}" == 'true' ]] ; then
default_branch="$(
curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${github_token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}" \
| jq -r '.default_branch'
)"
else
default_branch="$(git remote show origin | ${grep} 'HEAD branch' | cut -d ' ' -f 5)"
fi
fi

current_ref="${GITHUB_REF:-}"
git_commit="$(git rev-parse --short HEAD | sed 's/0*//')" # trim leading zeros, because semver doesn't allow that in
# the 'pre-release version' part, but we can't use the + char
# to make it 'build metadata' as that's not supported in K8s
# labels

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

##==----------------------------------------------------------------------------
## Version increment
Expand Down
23 changes: 20 additions & 3 deletions version-lookup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,33 @@ fi
##==----------------------------------------------------------------------------
## Get tags from GitHub repo

# Skip if testing, otherwise pull tags
# Skip if testing, or if use_api is true, otherwise pull tags
if [[ -z "${BATS_VERSION:-}" ]] ; then
git fetch --quiet --force origin 'refs/tags/*:refs/tags/*'
if [[ "${use_api:-}" != 'true' ]] ; then
git fetch --quiet --force origin 'refs/tags/*:refs/tags/*'
fi
fi

##==----------------------------------------------------------------------------
## Version parsing

# detect current version - removing "v" from start of tag if it exists
current_version="$(git tag -l | { ${grep} -P "${pcre_allow_vprefix}" || true; } | sed 's/^v//g' | sort -V | tail -n 1)"
if [[ "${use_api:-}" == 'true' ]] ; then
current_version="$(
curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${github_token}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/git/matching-refs/tags/" \
| jq -r '.[].ref' | sed 's|refs/tags/||g' \
| { ${grep} -P "${pcre_allow_vprefix}" || true; } | sed 's/^v//g' | sort -V | tail -n 1
)"
else
current_version="$(
git tag -l \
| { ${grep} -P "${pcre_allow_vprefix}" || true; } | sed 's/^v//g' | sort -V | tail -n 1
)"
fi

# support transition from an old reecetech calver style (yyyy-mm-Rr, where R is the literal `R`, and r is the nth release for the month)
if [[ -z "${current_version:-}" ]] ; then
Expand Down

0 comments on commit 391a2c7

Please sign in to comment.