From 512ffd902347652b4022ac905ca857b54a58e506 Mon Sep 17 00:00:00 2001 From: rlespinasse Date: Thu, 9 Dec 2021 22:47:58 +0100 Subject: [PATCH] feat: enable short sha with specific length --- .github/workflows/shortify-git-revision.yaml | 44 ++++++++++++++++++++ README.md | 23 +++++++++- action.yml | 5 +++ shortify.sh | 14 ++++++- 4 files changed, 84 insertions(+), 2 deletions(-) diff --git a/.github/workflows/shortify-git-revision.yaml b/.github/workflows/shortify-git-revision.yaml index 0ea2a7c..e1fe414 100644 --- a/.github/workflows/shortify-git-revision.yaml +++ b/.github/workflows/shortify-git-revision.yaml @@ -93,6 +93,50 @@ jobs: [[ -z "${{ env.WRONG_AND_MISSING_REVISION_SHORT }}" ]] shell: bash + # Test 7 + - name: Shortify a git revision with specific length + uses: ./ + with: + name: SIZED_REVISION + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + length: 10 + - name: Validate // Shortify a git revision with specific length + run: | + [[ "${{ env.SIZED_REVISION }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] + [[ "${{ env.SIZED_REVISION_SHORT }}" == "88428f56bd" ]] + shell: bash + + # Test 8 + - id: test-shortify-git-revision-with-wrong-length + name: Shortify a git revision with wrong length + uses: ./ + with: + name: WRONGFULLY_SIZED_REVISION + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + length: "not_a_number" + continue-on-error: true + - name: Validate // Shortify a git revision with wrong length + run: | + [[ -z "${{ env.WRONGFULLY_SIZED_REVISION }}" ]] + [[ -z "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" ]] + [[ "${{ steps.test-shortify-git-revision-with-wrong-length.outcome }}" == "failure" ]] + [[ "${{ steps.test-shortify-git-revision-with-wrong-length.conclusion }}" == "success" ]] + shell: bash + + # Test 9 + - name: Shortify a git revision with wrong length without failing + uses: ./ + with: + name: WRONGFULLY_SIZED_REVISION + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + length: "not_a_number" + continue-on-error: true + - name: Validate // Shortify a git revision with wrong length without failing + run: | + [[ "${{ env.WRONGFULLY_SIZED_REVISION }}" == "88428f56bd9d2751c47106bedfd148162dfa50b8" ]] + [[ "${{ env.WRONGFULLY_SIZED_REVISION_SHORT }}" == "88428f5" ]] + shell: bash + shortify-git-revision-release: runs-on: ubuntu-latest concurrency: diff --git a/README.md b/README.md index 192deab..fe12f60 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ If a revision is a bad revision, this action will produce an error message and f - Shortify any revision ```yaml - - uses: rlespinasse/slugify-value@v1.x + - uses: rlespinasse/shortify-git-revision@v1.x with: name: SOME_REVISION revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 @@ -49,6 +49,21 @@ If a revision is a bad revision, this action will produce an error message and f - `SOME_REVISION` - `SOME_REVISION_SHORT` +- Shortify a revision with a specific length + + ```yaml + - uses: rlespinasse/shortify-git-revision@v1.x + with: + name: SIZED_REVISION + revision: 88428f56bd9d2751c47106bedfd148162dfa50b8 + length: 10 + ``` + + Will make available + + - `SIZED_REVISION` + - `SIZED_REVISION_SHORT` (with value `88428f56bd`) + ## Inputs ### `name` @@ -73,3 +88,9 @@ The default value is `false`. The value will be prepend to each generated variable. This input is _Optional_. + +### `length` + +the `short` sha produce will have the length defined by the input. + +This input is _Optional_. diff --git a/action.yml b/action.yml index babc789..29d0178 100644 --- a/action.yml +++ b/action.yml @@ -16,6 +16,10 @@ inputs: description: "Value to prepend to each generated variable" default: "" required: false + length: + description: "Value to configure the length of the shorted sha" + default: "" + required: false branding: icon: "crop" color: "gray-dark" @@ -29,3 +33,4 @@ runs: INPUT_REVISION: ${{ inputs.revision }} INPUT_CONTINUE_ON_ERROR: ${{ inputs.continue-on-error }} INPUT_PREFIX: ${{ inputs.prefix }} + INPUT_LENGTH: ${{ inputs.length }} diff --git a/shortify.sh b/shortify.sh index 0633fc7..f3a5ad0 100755 --- a/shortify.sh +++ b/shortify.sh @@ -11,6 +11,18 @@ else fi REVISION=${INPUT_REVISION:-${!NAME}} +SHORT_LENGTH="" +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" + exit 1 + else + echo "::warning ::Invalid length: $INPUT_LENGTH, the default length will be used." + fi +fi + if [ -z "$REVISION" ]; then exit 0 fi @@ -18,7 +30,7 @@ fi if [ "$(git cat-file -e "$REVISION" 2>&1)" == "" ]; then { echo "${PREFIX}${NAME}=${REVISION}" - echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short "$REVISION")" + echo "${PREFIX}${NAME}_SHORT=$(git rev-parse --short"${SHORT_LENGTH}" "$REVISION")" } >>"$GITHUB_ENV" elif [ "${INPUT_CONTINUE_ON_ERROR}" == "false" ]; then echo "::error ::Invalid revision: $REVISION from $NAME"