mirror of
https://github.com/rlespinasse/slugify-value.git
synced 2026-05-18 02:35:20 +00:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e258f56ef | ||
|
|
930d4521cc | ||
|
|
cb3e46ca61 | ||
|
|
cb5f2aad22 | ||
|
|
1ce1e1b126 | ||
|
|
38f26c3445 |
35
.github/workflows/slugify-value.yaml
vendored
35
.github/workflows/slugify-value.yaml
vendored
@@ -17,6 +17,7 @@ jobs:
|
||||
|
||||
# Test 1
|
||||
- name: Slugify key only
|
||||
id: slugify-key-only
|
||||
uses: ./
|
||||
with:
|
||||
key: KEY_ONLY
|
||||
@@ -29,6 +30,11 @@ jobs:
|
||||
[[ "${{ env.KEY_ONLY_SLUG_CS }}" == "Key_Only.test--value" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG_URL }}" == "key-only-test--value" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG_URL_CS }}" == "Key-Only-test--value" ]]
|
||||
[[ "${{ env.KEY_ONLY }}" == "${{ steps.slugify-key-only.outputs.value }}" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG }}" == "${{ steps.slugify-key-only.outputs.slug }}" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG_CS }}" == "${{ steps.slugify-key-only.outputs.slug-cs }}" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG_URL }}" == "${{ steps.slugify-key-only.outputs.slug-url }}" ]]
|
||||
[[ "${{ env.KEY_ONLY_SLUG_URL_CS }}" == "${{ steps.slugify-key-only.outputs.slug-url-cs }}" ]]
|
||||
shell: bash
|
||||
|
||||
# Test 2
|
||||
@@ -69,12 +75,12 @@ jobs:
|
||||
with:
|
||||
key: ANOTHER_MAX_LENGTH
|
||||
value: "Never gonna give you up Never gonna let you down"
|
||||
slug-maxlength: 23
|
||||
slug-maxlength: 24
|
||||
- name: Validate // Slugify with another max length
|
||||
run: |
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH }}" == "Never gonna give you up Never gonna let you down" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG }}" == "never-gonna-give-you-up-" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_CS }}" == "Never-gonna-give-you-up-" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ env.ANOTHER_MAX_LENGTH_SLUG_URL_CS }}" == "Never-gonna-give-you-up" ]]
|
||||
shell: bash
|
||||
@@ -135,6 +141,29 @@ jobs:
|
||||
[[ "${{ steps.slugify-with-wrong-max-length.conclusion }}" == "success" ]]
|
||||
shell: bash
|
||||
|
||||
# Test 8
|
||||
- name: Slugify key without env publication
|
||||
id: slugify-key-without-env-publication
|
||||
uses: ./
|
||||
with:
|
||||
key: KEY_WITHOUT_ENV_PUBLICATION
|
||||
publish-env: false
|
||||
env:
|
||||
KEY_WITHOUT_ENV_PUBLICATION: "Never gonna give you up"
|
||||
- name: Validate // Slugify key without env publication
|
||||
run: |
|
||||
[[ -z "${{ env.KEY_WITHOUT_ENV_PUBLICATION }}" ]]
|
||||
[[ -z "${{ env.KEY_WITHOUT_ENV_PUBLICATION_SLUG }}" ]]
|
||||
[[ -z "${{ env.KEY_WITHOUT_ENV_PUBLICATION_SLUG_CS }}" ]]
|
||||
[[ -z "${{ env.KEY_WITHOUT_ENV_PUBLICATION_SLUG_URL }}" ]]
|
||||
[[ -z "${{ env.KEY_WITHOUT_ENV_PUBLICATION_SLUG_URL_CS }}" ]]
|
||||
[[ "${{ steps.slugify-key-without-env-publication.outputs.value }}" == "Never gonna give you up" ]]
|
||||
[[ "${{ steps.slugify-key-without-env-publication.outputs.slug }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ steps.slugify-key-without-env-publication.outputs.slug-cs }}" == "Never-gonna-give-you-up" ]]
|
||||
[[ "${{ steps.slugify-key-without-env-publication.outputs.slug-url }}" == "never-gonna-give-you-up" ]]
|
||||
[[ "${{ steps.slugify-key-without-env-publication.outputs.slug-url-cs }}" == "Never-gonna-give-you-up" ]]
|
||||
shell: bash
|
||||
|
||||
release:
|
||||
runs-on: ubuntu-latest
|
||||
concurrency:
|
||||
|
||||
54
README.md
54
README.md
@@ -18,6 +18,7 @@ Produce some `slug`-ed environment variables based on the input one.
|
||||
- `<env name>_SLUG_URL` (or `<env name>_SLUG_URL_CS`)
|
||||
|
||||
- like `<env name>_SLUG` (or `<env name>_SLUG_CS`) with the `.`, and `_` characters also replaced by `-`
|
||||
- will not end with `-`
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -92,28 +93,39 @@ Produce some `slug`-ed environment variables based on the input one.
|
||||
|
||||
Will produce SLUG variables without limiting the output length
|
||||
|
||||
- Slugify a value without publishing the environment variables
|
||||
|
||||
```yaml
|
||||
- uses: rlespinasse/slugify-value@v1.x
|
||||
with:
|
||||
key: KEY_NAME
|
||||
value: value_to_slugify
|
||||
```
|
||||
|
||||
Will **not** make available
|
||||
|
||||
- `KEY_NAME`
|
||||
- `KEY_NAME_SLUG`
|
||||
- `KEY_NAME_SLUG_CS`
|
||||
- `KEY_NAME_SLUG_URL`
|
||||
- `KEY_NAME_SLUG_URL_CS`
|
||||
|
||||
## Inputs
|
||||
|
||||
### `key`
|
||||
| Input | Description | Mandatory | Default |
|
||||
| -------------- | ----------------------------------------------------------------------------------------------------- | --------- | ------- |
|
||||
| key | Environment variable that will hold the value and serve as prefix to slugified value | Yes | |
|
||||
| value | The value to slugify. If not set the value will be taken from the `key` input as environment variable | No | |
|
||||
| prefix | The value will be prepend to each generated variable | No | |
|
||||
| slug-maxlength | The value is a number or `nolimit` to reflect the length of the slug outputs | No | 63 |
|
||||
| publish-env | In addition of the action output, the slug values are publish as environment variables | No | true |
|
||||
|
||||
Environment variable that will hold the value and serve as prefix to slugified value.
|
||||
## Outputs
|
||||
|
||||
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`.
|
||||
| Output | Description |
|
||||
| ----------- | ------------------------------- |
|
||||
| value | The value to be slugify |
|
||||
| slug | Value Slug |
|
||||
| slug-cs | Value Slug (Case-sensitive) |
|
||||
| slug-url | Value Slug URL |
|
||||
| slug-url-cs | Value Slug URL (Case-sensitive) |
|
||||
|
||||
30
action.yml
30
action.yml
@@ -1,6 +1,9 @@
|
||||
name: "Slugify Value"
|
||||
description: "Github Action to slugify a value"
|
||||
author: "Romain Lespinasse"
|
||||
branding:
|
||||
icon: "crop"
|
||||
color: "gray-dark"
|
||||
inputs:
|
||||
key:
|
||||
description: "Environment variable that will hold the value and serve as prefix to slugified value"
|
||||
@@ -16,16 +19,35 @@ inputs:
|
||||
description: "Max length of the slugified values"
|
||||
default: "63"
|
||||
required: true
|
||||
branding:
|
||||
icon: "crop"
|
||||
color: "gray-dark"
|
||||
publish-env:
|
||||
description: "Publish slugs as environment variables"
|
||||
default: "true"
|
||||
required: true
|
||||
outputs:
|
||||
value:
|
||||
description: "The value to be slugify"
|
||||
value: ${{ steps.slugify.outputs.value }}
|
||||
slug:
|
||||
description: "Value Slug"
|
||||
value: ${{ steps.slugify.outputs.slug }}
|
||||
slug-cs:
|
||||
description: "Value Slug (Case-sensitive)"
|
||||
value: ${{ steps.slugify.outputs.slug-cs }}
|
||||
slug-url:
|
||||
description: "Value Slug URL"
|
||||
value: ${{ steps.slugify.outputs.slug-url }}
|
||||
slug-url-cs:
|
||||
description: "Value Slug URL (Case-sensitive)"
|
||||
value: ${{ steps.slugify.outputs.slug-url-cs }}
|
||||
runs:
|
||||
using: "composite"
|
||||
steps:
|
||||
- run: $GITHUB_ACTION_PATH/slugify.sh
|
||||
- id: slugify
|
||||
run: $GITHUB_ACTION_PATH/slugify.sh
|
||||
shell: bash
|
||||
env:
|
||||
INPUT_KEY: ${{ inputs.key }}
|
||||
INPUT_VALUE: ${{ inputs.value }}
|
||||
INPUT_PREFIX: ${{ inputs.prefix }}
|
||||
INPUT_SLUG_MAXLENGTH: ${{ inputs.slug-maxlength }}
|
||||
INPUT_PUBLISH_ENV: ${{ inputs.publish-env }}
|
||||
|
||||
43
slugify.sh
43
slugify.sh
@@ -33,7 +33,7 @@ slug() {
|
||||
# 3d : Remove leading dashes
|
||||
# 4d : Remove trailing dashes
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9._-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output"
|
||||
reduce "$output" false
|
||||
}
|
||||
|
||||
slug_url() {
|
||||
@@ -42,21 +42,38 @@ slug_url() {
|
||||
# 3d : Remove leading dashes
|
||||
# 4d : Remove trailing dashes
|
||||
output=$(sed -E 's#refs/[^\/]*/##;s/[^a-zA-Z0-9-]+/-/g;s/^-*//;s/-*$//' <<<"$1")
|
||||
reduce "$output"
|
||||
reduce "$output" true
|
||||
}
|
||||
|
||||
reduce() {
|
||||
if [ "${MAX_LENGTH}" == "nolimit" ]; then
|
||||
echo "$1"
|
||||
else
|
||||
cut -c1-"${MAX_LENGTH}" <<<"$1"
|
||||
reduced_value="$1"
|
||||
remove_ending_hypen="$2"
|
||||
if [ "${MAX_LENGTH}" != "nolimit" ]; then
|
||||
reduced_value=$(cut -c1-"${MAX_LENGTH}" <<<"$1")
|
||||
fi
|
||||
if [ "$remove_ending_hypen" == "true" ]; then
|
||||
reduced_value=$(sed -E 's/-*$//' <<<"$reduced_value")
|
||||
fi
|
||||
echo "$reduced_value"
|
||||
}
|
||||
|
||||
{
|
||||
echo "${PREFIX}${KEY}=${CS_VALUE}"
|
||||
echo "${PREFIX}${KEY}_SLUG=$(slug "$VALUE")"
|
||||
echo "${PREFIX}${KEY}_SLUG_CS=$(slug "$CS_VALUE")"
|
||||
echo "${PREFIX}${KEY}_SLUG_URL=$(slug_url "$VALUE")"
|
||||
echo "${PREFIX}${KEY}_SLUG_URL_CS=$(slug_url "$CS_VALUE")"
|
||||
} >>"$GITHUB_ENV"
|
||||
SLUG_VALUE=$(slug "$VALUE")
|
||||
SLUG_CS_VALUE=$(slug "$CS_VALUE")
|
||||
SLUG_URL_VALUE=$(slug_url "$VALUE")
|
||||
SLUG_URL_CS_VALUE=$(slug_url "$CS_VALUE")
|
||||
|
||||
echo "::set-output name=value::${CS_VALUE}"
|
||||
echo "::set-output name=slug::${SLUG_VALUE}"
|
||||
echo "::set-output name=slug-cs::${SLUG_CS_VALUE}"
|
||||
echo "::set-output name=slug-url::${SLUG_URL_VALUE}"
|
||||
echo "::set-output name=slug-url-cs::${SLUG_URL_CS_VALUE}"
|
||||
|
||||
if [ "${INPUT_PUBLISH_ENV}" == "true" ]; then
|
||||
{
|
||||
echo "${PREFIX}${KEY}=${CS_VALUE}"
|
||||
echo "${PREFIX}${KEY}_SLUG=${SLUG_VALUE}"
|
||||
echo "${PREFIX}${KEY}_SLUG_CS=${SLUG_CS_VALUE}"
|
||||
echo "${PREFIX}${KEY}_SLUG_URL=${SLUG_URL_VALUE}"
|
||||
echo "${PREFIX}${KEY}_SLUG_URL_CS=${SLUG_URL_CS_VALUE}"
|
||||
} >>"$GITHUB_ENV"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user