From ef0d40cdf6dcb5fc6a4df2b8c3acd6c3278f27f4 Mon Sep 17 00:00:00 2001 From: rlespinasse Date: Fri, 18 Mar 2022 20:39:45 +0100 Subject: [PATCH] feat: add short-on-error input --- .github/workflows/shortify-git-revision.yaml | 53 ++++++++++++++++++-- README.md | 23 +++++++-- action.yml | 5 ++ shortify.sh | 26 +++++++--- 4 files changed, 90 insertions(+), 17 deletions(-) diff --git a/.github/workflows/shortify-git-revision.yaml b/.github/workflows/shortify-git-revision.yaml index e1fe414..f00910f 100644 --- a/.github/workflows/shortify-git-revision.yaml +++ b/.github/workflows/shortify-git-revision.yaml @@ -1,6 +1,49 @@ name: Shortify git revision testing on: [push] jobs: + error-testing-on-os: + strategy: + fail-fast: false + matrix: + os: [macos-latest, ubuntu-latest, windows-latest] + runs-on: ${{ matrix.os }} + concurrency: + group: error-testing-on-os-${{ github.ref }} + cancel-in-progress: true + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + path: this-action + ref: ${{ github.ref }} + + # Test 1 + - name: Continue on error + uses: ./this-action + with: + name: ROOT_COMMIT + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + continue-on-error: true + - name: Validate // Continue on error + run: | + [[ "${{ env.ROOT_COMMIT }}" == "" ]] + [[ "${{ env.ROOT_COMMIT_SHORT }}" == "" ]] + shell: bash + + # Test 2 + - name: Short on error + uses: ./this-action + with: + name: ROOT_COMMIT + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + short-on-error: true + length: 7 + - name: Validate // Short on error + run: | + [[ "${{ env.ROOT_COMMIT }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] + [[ "${{ env.ROOT_COMMIT_SHORT }}" == "88428f5" ]] + shell: bash + shortify-git-revision-on-os: strategy: fail-fast: false @@ -12,7 +55,7 @@ jobs: cancel-in-progress: true steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Test 1 - name: Shortify an existing git revision @@ -141,11 +184,13 @@ jobs: runs-on: ubuntu-latest concurrency: group: shortify-git-revision-release-${{ github.ref }} - needs: shortify-git-revision-on-os + needs: + - error-testing-on-os + - shortify-git-revision-on-os steps: - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v3 # Release - name: Release this GitHub Action - uses: rlespinasse/release-that@v1.x + uses: rlespinasse/release-that@v1 diff --git a/README.md b/README.md index fe12f60..1ada543 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Produce short revision environment variable based on the input one. -If a revision is a bad revision, this action will produce an error message and fail depending on `continue-on-error` input value. +If a revision is a bad revision, this action will produce an error message and fail depending on `continue-on-error` input (This behavior can be override with `short-on-error` input). ``, and `_SHORT` environment variable will only be available if the revision is not empty and valid. ## Usage @@ -12,7 +12,8 @@ If a revision is a bad revision, this action will produce an error message and f - Shortify an environment variable ```yaml - - uses: rlespinasse/shortify-git-revision@v1.x + - uses: actions/checkout@v3 + - uses: rlespinasse/shortify-git-revision@v1 with: name: GITHUB_SHA ``` @@ -24,7 +25,8 @@ If a revision is a bad revision, this action will produce an error message and f - Shortify an environment variable with prefix ```yaml - - uses: rlespinasse/shortify-git-revision@v1.x + - uses: actions/checkout@v3 + - uses: rlespinasse/shortify-git-revision@v1 with: name: GITHUB_SHA prefix: CI_ @@ -38,7 +40,8 @@ If a revision is a bad revision, this action will produce an error message and f - Shortify any revision ```yaml - - uses: rlespinasse/shortify-git-revision@v1.x + - uses: actions/checkout@v3 + - uses: rlespinasse/shortify-git-revision@v1 with: name: SOME_REVISION revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 @@ -52,7 +55,8 @@ If a revision is a bad revision, this action will produce an error message and f - Shortify a revision with a specific length ```yaml - - uses: rlespinasse/shortify-git-revision@v1.x + - uses: actions/checkout@v3 + - uses: rlespinasse/shortify-git-revision@v1 with: name: SIZED_REVISION revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 @@ -83,6 +87,15 @@ If the input is set to `true`, this action will not fail on a bad revision The default value is `false`. +### `short-on-error` + +If the input is set to `true`, this action will short a bad revision + +The default value is `false`. + +> If this input is set to `true`, the input `continue-on-error` input will be ignored. +> If this input is set to `true`, the input `length` input is mandatory. + ### `prefix` The value will be prepend to each generated variable. diff --git a/action.yml b/action.yml index 29d0178..bff0fce 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,10 @@ inputs: description: "Don't fail the action if the git revision isn't valid" default: "false" required: false + short-on-error: + description: "Still short it if the git revision isn't valid" + default: "false" + required: false prefix: description: "Value to prepend to each generated variable" default: "" @@ -32,5 +36,6 @@ runs: INPUT_NAME: ${{ inputs.name }} INPUT_REVISION: ${{ inputs.revision }} INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }} + INPUT_SHORT_ON_ERROR: ${{ inputs.short-on-error }} INPUT_PREFIX: ${{ inputs.prefix }} INPUT_LENGTH: ${{ inputs.length }} diff --git a/shortify.sh b/shortify.sh index f3a5ad0..0f5a29a 100755 --- a/shortify.sh +++ b/shortify.sh @@ -12,27 +12,37 @@ fi REVISION=${INPUT_REVISION:-${!NAME}} SHORT_LENGTH="" -if [ "$INPUT_LENGTH" != "" ]; then - if [ "$INPUT_LENGTH" -eq "$INPUT_LENGTH" ] 2>/dev/null; then +if [ "${INPUT_LENGTH}" != "" ]; then + if [ "${INPUT_LENGTH}" -eq "${INPUT_LENGTH}" ] 2>/dev/null; then SHORT_LENGTH="=${INPUT_LENGTH}" elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then - echo "::error ::Invalid length: $INPUT_LENGTH, should be a number" + echo "::error ::Invalid length: ${INPUT_LENGTH}, should be a number" exit 1 else - echo "::warning ::Invalid length: $INPUT_LENGTH, the default length will be used." + echo "::warning ::Invalid length: ${INPUT_LENGTH}, the default length will be used." fi fi -if [ -z "$REVISION" ]; then +if [ -z "${REVISION}" ]; then exit 0 fi -if [ "$(git cat-file -e "$REVISION" 2>&1)" == "" ]; then +if [ "$(git cat-file -e "${REVISION}" 2>&1)" == "" ]; then { echo "${PREFIX}${NAME}=${REVISION}" - echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "$REVISION")" + echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "${REVISION}")" } >>"$GITHUB_ENV" +elif [ "${INPUT_SHORT_ON_ERROR}" == "true" ]; then + if [ -n "${INPUT_LENGTH}" ]; then + { + echo "${PREFIX}${NAME}=${REVISION}" + echo "${PREFIX}${NAME}_SHORT=$(cut -c1-"${INPUT_LENGTH}" <<<"${REVISION}")" + } >>"$GITHUB_ENV" + else + echo "::error ::The input 'lenght' is mandatory with 'short-on-error' set to 'true'" + exit 1 + fi elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then - echo "::error ::Invalid revision: $REVISION from $NAME" + echo "::error ::Invalid revision: ${REVISION} from ${NAME}" exit 1 fi