From 391a2c7c80d725cb3023f491625a05dd2310270c Mon Sep 17 00:00:00 2001 From: Phil Jay Date: Wed, 18 Oct 2023 23:44:09 +1100 Subject: [PATCH] Add `use_api` feature So that you don't _need_ to checkout (clone) the repo to the worker --- action.yml | 6 ++++++ shared.sh | 14 ++++++++++++++ version-increment.sh | 30 ++++++++++++++++++++++-------- version-lookup.sh | 23 ++++++++++++++++++++--- 4 files changed, 62 insertions(+), 11 deletions(-) diff --git a/action.yml b/action.yml index 7b51a8f..dec645b 100644 --- a/action.yml +++ b/action.yml @@ -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: @@ -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 diff --git a/shared.sh b/shared.sh index ee14092..e1c97e6 100644 --- a/shared.sh +++ b/shared.sh @@ -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 diff --git a/version-increment.sh b/version-increment.sh index db956e6..7036524 100755 --- a/version-increment.sh +++ b/version-increment.sh @@ -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 diff --git a/version-lookup.sh b/version-lookup.sh index 45ed51c..58c3fae 100755 --- a/version-lookup.sh +++ b/version-lookup.sh @@ -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