Truncate slug values to 63 characters.

This commit is contained in:
Franz Diebold 2022-09-11 14:19:45 +00:00
parent 960f2f11d8
commit 9d7426b893
3 changed files with 25 additions and 12 deletions

View file

@ -2,7 +2,7 @@
[![GitHub Action: View on Marketplace](https://img.shields.io/badge/GitHub%20Action-View_on_Marketplace-28a745?logo=github)](https://github.com/marketplace/actions/github-environment-variables-action)
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml)
[![Version: v2.5.0](https://img.shields.io/badge/Version-v2.5.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v2.5.0)
[![Version: v2.6.0](https://img.shields.io/badge/Version-v2.6.0-brightgreen)](https://github.com/FranzDiebold/github-env-vars-action/releases/tag/v2.6.0)
[![Lint and Test](https://github.com/FranzDiebold/github-env-vars-action/workflows/Lint%20and%20Test/badge.svg)](https://github.com/FranzDiebold/github-env-vars-action/actions?query=workflow%3A%22Lint+and+Test%22)
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE)
@ -12,21 +12,21 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
| Environment Variable Name | Description | Example value |
|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| `CI_REPOSITORY_SLUG` | The slug of the owner and repository name (i.e. slug of `FranzDiebold/github-env-vars-action`). | `franzdiebold-github-env-vars-action` |
| `CI_REPOSITORY_SLUG` | The slug of the owner and repository name (i.e. slug of `FranzDiebold/github-env-vars-action`). The slug is truncated to 63 characters. | `franzdiebold-github-env-vars-action` |
| `CI_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `CI_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. | `franzdiebold` |
| `CI_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. The slug is truncated to 63 characters. | `franzdiebold` |
| `CI_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `CI_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. | `github-env-vars-action` |
| `CI_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. The slug is truncated to 63 characters. | `github-env-vars-action` |
| `CI_REPOSITORY` | The owner and repository name. Copy of `GITHUB_REPOSITORY` - for reasons of completeness. | `FranzDiebold/github-env-vars-action` |
| `CI_REF_SLUG` | The slug of the branch, tag or PR number *ref* that triggered the workflow (i.e. slug of `refs/heads/feat/feature-branch-1`). <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. | `refs-heads-feat-feature-branch-1` or<br> `refs-tags-v1-3-7` or<br> `refs-pull-42-merge` |
| `CI_REF_SLUG` | The slug of the branch, tag or PR number *ref* that triggered the workflow (i.e. slug of `refs/heads/feat/feature-branch-1`). <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. The slug is truncated to 63 characters. | `refs-heads-feat-feature-branch-1` or<br> `refs-tags-v1-3-7` or<br> `refs-pull-42-merge` |
| `CI_ACTION_REF_NAME` | The branch or tag *name* that triggered the workflow. For pull requests it is the *head* branch name. | `feat/feature-branch-1` or<br> `v1.3.7` |
| `CI_ACTION_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. For pull requests it is the slug of the *head* branch name. | `feat-feature-branch-1` or<br> `v1-3-7` |
| `CI_ACTION_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. For pull requests it is the slug of the *head* branch name. The slug is truncated to 63 characters. | `feat-feature-branch-1` or<br> `v1-3-7` |
| `CI_REF_NAME` | The branch *name*, tag *name* or PR number that triggered the workflow. <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. | `feat/feature-branch-1` or<br> `v1.3.7` or<br> `42/merge` |
| `CI_REF_NAME_SLUG` | The slug of the branch *name*, tag *name* or PR number that triggered the workflow. <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. | `feat-feature-branch-1` or<br> `v1-3-7` or<br> `42-merge` |
| `CI_REF_NAME_SLUG` | The slug of the branch *name*, tag *name* or PR number that triggered the workflow. <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. The slug is truncated to 63 characters. | `feat-feature-branch-1` or<br> `v1-3-7` or<br> `42-merge` |
| `CI_REF` | The branch, tag or PR number *ref* that triggered the workflow. <br>If neither a branch, tag or PR number is available for the event type, the variable will not exist. Copy of `GITHUB_REF` - for reasons of completeness. | `refs/heads/feat/feature-branch-1` or<br> `refs/tags/v1.3.7` or<br> `refs/pull/42/merge` |
| `CI_HEAD_REF_SLUG` | The slug of the head branch *name*. <br>Only set for event type *pull request* or forked repositories. | `feat-feature-branch-1` |
| `CI_HEAD_REF_SLUG` | The slug of the head branch *name*. <br>Only set for event type *pull request* or forked repositories. The slug is truncated to 63 characters. | `feat-feature-branch-1` |
| `CI_HEAD_REF` | Only set for forked repositories / pull request. The branch of the head repository / the head branch name. Copy of `GITHUB_HEAD_REF` - for reasons of completeness. | `feat/feature-branch-1` |
| `CI_BASE_REF_SLUG` | The slug of the base branch *name*. <br>Only set for event type *pull request* or forked repositories. | `main` |
| `CI_BASE_REF_SLUG` | The slug of the base branch *name*. <br>Only set for event type *pull request* or forked repositories. The slug is truncated to 63 characters. | `main` |
| `CI_BASE_REF` | Only set for forked repositories / pull request. The branch of the base repository / the base branch name. Copy of `GITHUB_BASE_REF` - for reasons of completeness. | `main` |
| `CI_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
| `CI_SHA` | The commit SHA that triggered the workflow. Copy of `GITHUB_SHA` - for reasons of completeness. | `ffac537e6cbbf934b08745a378932722df287a53` |

View file

@ -4,17 +4,20 @@ const core = require('@actions/core');
const github = require('@actions/github');
/**
* Slugify a given string.
* Slugify a given string to a maximum length.
* @param {string} inputString
* @param {int} maxLength
* @return {string} The slugified string.
*/
function slugify(inputString) {
function slugify(inputString, maxLength = 63) {
return inputString
.toLowerCase()
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
.replace(/^\s+|\s+$/g, '') // trim
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
.replace(/-+/g, '-') // collapse dashes
.slice(0, maxLength) // truncate to maximum length
.replace(/[-]+$/g, ''); // trim trailing -
}
/**

View file

@ -6,6 +6,16 @@ test('slugifies text', () => {
expect(slugify(' /abc+efg*123/test§xyz ')).toEqual('abc-efg-123-test-xyz');
});
test('slugifies a text to a maximum length', () => {
expect(slugify(' /abc+efg*123/test§xyz 1234567 /(ö) ', 16))
.toEqual('abc-efg-123-test');
});
test('slugifies a text to a maximum length with trailing dash', () => {
expect(slugify(' /abc+efg*123/test§xyz 1234567 /(ö) ', 17))
.toEqual('abc-efg-123-test');
});
test('slugifies ref name with dash', () => {
expect(slugify('feat/feature-branch-1')).toEqual('feat-feature-branch-1');
});