mirror of
https://github.com/rlespinasse/slugify-value.git
synced 2026-05-17 10:15:43 +00:00
feat: add slug-maxlength input
This commit is contained in:
committed by
Romain Lespinasse
parent
e0e1ac60ae
commit
8c6e40fa30
92
.github/workflows/slugify-value.yaml
vendored
92
.github/workflows/slugify-value.yaml
vendored
@@ -1,18 +1,15 @@
|
||||
name: Slugify testing
|
||||
name: Slugify Value
|
||||
on: [push]
|
||||
jobs:
|
||||
slugify-on-os:
|
||||
os-testing:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [macos-latest, ubuntu-latest, windows-latest]
|
||||
runs-on: ${{ matrix.os }}
|
||||
concurrency:
|
||||
group: slugify-on-os-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Test 1
|
||||
- name: Slugify key only
|
||||
@@ -62,15 +59,88 @@ jobs:
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_URL_CS }}" == "feat-Some-Changes_to-be" ]]
|
||||
shell: bash
|
||||
|
||||
slugify-release:
|
||||
# Test 4
|
||||
- name: Slugify with another max length
|
||||
uses: ./
|
||||
with:
|
||||
key: KEY_VALUE_TEST
|
||||
value: refs/pulls/feat/-----Some----Changes_to.be------
|
||||
slug-maxlength: 12
|
||||
- name: Validate // Slugify with another max length
|
||||
run: |
|
||||
[[ "${{ env.KEY_VALUE_TEST }}" == "refs/pulls/feat/-----Some----Changes_to.be------" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG }}" == "feat-some-ch" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_CS }}" == "feat-Some-Ch" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_URL }}" == "feat-some-ch" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_URL_CS }}" == "feat-Some-Ch" ]]
|
||||
shell: bash
|
||||
|
||||
# Test 5
|
||||
- name: Slugify with no limit on max length
|
||||
uses: ./
|
||||
with:
|
||||
key: KEY_VALUE_TEST
|
||||
value: refs/pulls/feat/-----Some----Changes_to.be-----Some----Changes_to.be-----Some----Changes_to.be-----Some----Changes_to.be------
|
||||
slug-maxlength: "nolimit"
|
||||
- name: Validate // Slugify with no limit on max length
|
||||
run: |
|
||||
[[ "${{ env.KEY_VALUE_TEST }}" == "refs/pulls/feat/-----Some----Changes_to.be-----Some----Changes_to.be-----Some----Changes_to.be-----Some----Changes_to.be------" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG }}" == "feat-some-changes_to.be-some-changes_to.be-some-changes_to.be-some-changes_to.be" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_CS }}" == "feat-Some-Changes_to.be-Some-Changes_to.be-Some-Changes_to.be-Some-Changes_to.be" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_URL }}" == "feat-some-changes_to-be-some-changes_to-be-some-changes_to-be-some-changes_to-be" ]]
|
||||
[[ "${{ env.KEY_VALUE_TEST_SLUG_URL_CS }}" == "feat-Some-Changes_to-be-Some-Changes_to-be-Some-Changes_to-be-Some-Changes_to-be" ]]
|
||||
shell: bash
|
||||
|
||||
# Test 6
|
||||
- name: Slugify with empty max length
|
||||
id: slugify-with-empty-max-length
|
||||
uses: ./
|
||||
with:
|
||||
key: EMPTY_MAXLENGTH
|
||||
value: some_value
|
||||
slug-maxlength: ""
|
||||
continue-on-error: true
|
||||
- name: Validate // Slugify with empty max length
|
||||
run: |
|
||||
[[ -z "${{ env.EMPTY_MAXLENGTH }}" ]]
|
||||
[[ -z "${{ env.EMPTY_MAXLENGTH_SLUG }}" ]]
|
||||
[[ -z "${{ env.EMPTY_MAXLENGTH_SLUG_CS }}" ]]
|
||||
[[ -z "${{ env.EMPTY_MAXLENGTH_SLUG_URL }}" ]]
|
||||
[[ -z "${{ env.EMPTY_MAXLENGTH_SLUG_URL_CS }}" ]]
|
||||
[[ "${{ steps.slugify-with-empty-max-length.outcome }}" == "failure" ]]
|
||||
[[ "${{ steps.slugify-with-empty-max-length.conclusion }}" == "success" ]]
|
||||
shell: bash
|
||||
|
||||
# Test 7
|
||||
- name: Slugify with wrong max length
|
||||
id: slugify-with-wrong-max-length
|
||||
uses: ./
|
||||
with:
|
||||
key: WRONG_MAXLENGTH
|
||||
value: some_value
|
||||
slug-maxlength: "wrong"
|
||||
continue-on-error: true
|
||||
- name: Validate // Slugify with wrong max length
|
||||
run: |
|
||||
[[ -z "${{ env.WRONG_MAXLENGTH }}" ]]
|
||||
[[ -z "${{ env.WRONG_MAXLENGTH_SLUG }}" ]]
|
||||
[[ -z "${{ env.WRONG_MAXLENGTH_SLUG_CS }}" ]]
|
||||
[[ -z "${{ env.WRONG_MAXLENGTH_SLUG_URL }}" ]]
|
||||
[[ -z "${{ env.WRONG_MAXLENGTH_SLUG_URL_CS }}" ]]
|
||||
[[ "${{ steps.slugify-with-wrong-max-length.outcome }}" == "failure" ]]
|
||||
[[ "${{ steps.slugify-with-wrong-max-length.conclusion }}" == "success" ]]
|
||||
shell: bash
|
||||
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
concurrency:
|
||||
group: slugify-release-${{ github.ref }}
|
||||
needs: slugify-on-os
|
||||
group: release-${{ github.ref }}
|
||||
needs:
|
||||
- os-testing
|
||||
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
|
||||
|
||||
40
README.md
40
README.md
@@ -70,10 +70,50 @@ Produce some `slug`-ed environment variables based on the input one.
|
||||
- `CI_EXISTING_ENV_VAR_SLUG_URL`
|
||||
- `CI_EXISTING_ENV_VAR_SLUG_URL_CS`
|
||||
|
||||
- Slugify a value with a different slug length
|
||||
|
||||
```yaml
|
||||
- uses: rlespinasse/slugify-value@v1.x
|
||||
with:
|
||||
key: EXISTING_ENV_VAR
|
||||
slug-maxlength: 80
|
||||
```
|
||||
|
||||
Will produce SLUG variables with a 80-char length
|
||||
|
||||
- Slugify a value without
|
||||
|
||||
```yaml
|
||||
- uses: rlespinasse/slugify-value@v1.x
|
||||
with:
|
||||
key: EXISTING_ENV_VAR
|
||||
slug-maxlength: "nolimit"
|
||||
```
|
||||
|
||||
Will produce SLUG variables without limiting the output length
|
||||
|
||||
## Inputs
|
||||
|
||||
### `key`
|
||||
|
||||
Environment variable that will hold the value and serve as prefix to slugified value.
|
||||
|
||||
This input is _Mandatory_.
|
||||
|
||||
### `value`
|
||||
|
||||
The value to slugify. If not set the value will be taken from the `key` input as environment variable.
|
||||
|
||||
This input is _Optional_.
|
||||
|
||||
### `prefix`
|
||||
|
||||
The value will be prepend to each generated variable.
|
||||
|
||||
This input is _Optional_.
|
||||
|
||||
### `slug-maxlength`
|
||||
|
||||
The value is a number or `nolimit` to reflect the length of the slug outputs
|
||||
|
||||
This input is _Optional_. The default value is `63`.
|
||||
|
||||
@@ -12,6 +12,10 @@ inputs:
|
||||
description: "Value to prepend to each generated variable"
|
||||
default: ""
|
||||
required: false
|
||||
slug-maxlength:
|
||||
description: "Max length of the slugified values"
|
||||
default: "63"
|
||||
required: true
|
||||
branding:
|
||||
icon: "crop"
|
||||
color: "gray-dark"
|
||||
@@ -24,3 +28,4 @@ runs:
|
||||
INPUT_KEY: ${{ inputs.key }}
|
||||
INPUT_VALUE: ${{ inputs.value }}
|
||||
INPUT_PREFIX: ${{ inputs.prefix }}
|
||||
INPUT_SLUG_MAXLENGTH: ${{ inputs.slug-maxlength }}
|
||||
|
||||
31
slugify.sh
31
slugify.sh
@@ -14,16 +14,35 @@ else
|
||||
PREFIX=${INPUT_PREFIX^^}
|
||||
fi
|
||||
|
||||
MAX_LENGTH=""
|
||||
if [ -z "${INPUT_SLUG_MAXLENGTH}" ]; then
|
||||
echo "::error ::slug-maxlength cannot be empty"
|
||||
exit 1
|
||||
elif [ "${INPUT_SLUG_MAXLENGTH}" -eq "${INPUT_SLUG_MAXLENGTH}" ] 2>/dev/null; then
|
||||
MAX_LENGTH="${INPUT_SLUG_MAXLENGTH}"
|
||||
elif [ "${INPUT_SLUG_MAXLENGTH}" == "nolimit" ]; then
|
||||
MAX_LENGTH="${INPUT_SLUG_MAXLENGTH}"
|
||||
else
|
||||
echo "::error ::slug-maxlength must be a number or equals to 'nolimit'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
slug() {
|
||||
echo "$1" |
|
||||
sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._]+/-/g;s/-+/-/g;s/^-*//;s/-*$//' |
|
||||
cut -c1-63
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._]+/-/g;s/-+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output"
|
||||
}
|
||||
|
||||
slug_url() {
|
||||
echo "$1" |
|
||||
sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9_]+/-/g;s/-+/-/g;s/^-*//;s/-*$//' |
|
||||
cut -c1-63
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9_]+/-/g;s/-+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output"
|
||||
}
|
||||
|
||||
reduce() {
|
||||
if [ "${MAX_LENGTH}" == "nolimit" ]; then
|
||||
echo "$1"
|
||||
else
|
||||
cut -c1-"${MAX_LENGTH}" <<<"$1"
|
||||
fi
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user