diff --git a/.github/workflows/slugify-value.yaml b/.github/workflows/slugify-value.yaml index 19abc8d..199e64d 100644 --- a/.github/workflows/slugify-value.yaml +++ b/.github/workflows/slugify-value.yaml @@ -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 diff --git a/README.md b/README.md index e33b45c..8cf0da8 100644 --- a/README.md +++ b/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`. diff --git a/action.yml b/action.yml index 48fc2eb..dec6bc8 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/slugify.sh b/slugify.sh index 610b3ba..be37349 100755 --- a/slugify.sh +++ b/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 } {