feat: enable short sha with specific length

This commit is contained in:
rlespinasse
2021-12-09 22:47:58 +01:00
committed by Romain Lespinasse
parent 928e740f90
commit 512ffd9023
4 changed files with 84 additions and 2 deletions

View File

@@ -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:

View File

@@ -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_.

View File

@@ -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 }}

View File

@@ -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"