Skip to content

Commit 9e73536

Browse files
authored
Merge pull request #53 from FranzDiebold/52-_slug-envs-are-not-truncated
slug envs are not truncated
2 parents 61e09f0 + 9d7426b commit 9e73536

File tree

5 files changed

+1678
-1060
lines changed

5 files changed

+1678
-1060
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![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)
44
[![Demo: available](https://img.shields.io/badge/Demo-available-orange)](.github/workflows/demo.yml)
5-
[![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)
5+
[![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)
66
[![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)
77
[![license: MIT](https://img.shields.io/badge/license-MIT-brightgreen.svg)](./LICENSE)
88

@@ -12,21 +12,21 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
1212

1313
| Environment Variable Name | Description | Example value |
1414
|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
15-
| `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` |
15+
| `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` |
1616
| `CI_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
17-
| `CI_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. | `franzdiebold` |
17+
| `CI_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. The slug is truncated to 63 characters. | `franzdiebold` |
1818
| `CI_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
19-
| `CI_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. | `github-env-vars-action` |
19+
| `CI_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. The slug is truncated to 63 characters. | `github-env-vars-action` |
2020
| `CI_REPOSITORY` | The owner and repository name. Copy of `GITHUB_REPOSITORY` - for reasons of completeness. | `FranzDiebold/github-env-vars-action` |
21-
| `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` |
21+
| `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` |
2222
| `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` |
23-
| `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` |
23+
| `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` |
2424
| `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` |
25-
| `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` |
25+
| `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` |
2626
| `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` |
27-
| `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` |
27+
| `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` |
2828
| `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` |
29-
| `CI_BASE_REF_SLUG` | The slug of the base branch *name*. <br>Only set for event type *pull request* or forked repositories. | `main` |
29+
| `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` |
3030
| `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` |
3131
| `CI_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
3232
| `CI_SHA` | The commit SHA that triggered the workflow. Copy of `GITHUB_SHA` - for reasons of completeness. | `ffac537e6cbbf934b08745a378932722df287a53` |

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ const core = require('@actions/core');
44
const github = require('@actions/github');
55

66
/**
7-
* Slugify a given string.
7+
* Slugify a given string to a maximum length.
88
* @param {string} inputString
9+
* @param {int} maxLength
910
* @return {string} The slugified string.
1011
*/
11-
function slugify(inputString) {
12+
function slugify(inputString, maxLength = 63) {
1213
return inputString
1314
.toLowerCase()
1415
.replace(/[^a-z0-9 -]/g, ' ') // remove invalid chars
1516
.replace(/^\s+|\s+$/g, '') // trim
1617
.replace(/\s+/g, '-') // collapse whitespace and replace by -
17-
.replace(/-+/g, '-'); // collapse dashes
18+
.replace(/-+/g, '-') // collapse dashes
19+
.slice(0, maxLength) // truncate to maximum length
20+
.replace(/[-]+$/g, ''); // trim trailing -
1821
}
1922

2023
/**

index.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ test('slugifies text', () => {
66
expect(slugify(' /abc+efg*123/test§xyz ')).toEqual('abc-efg-123-test-xyz');
77
});
88

9+
test('slugifies a text to a maximum length', () => {
10+
expect(slugify(' /abc+efg*123/test§xyz 1234567 /(ö) ', 16))
11+
.toEqual('abc-efg-123-test');
12+
});
13+
14+
test('slugifies a text to a maximum length with trailing dash', () => {
15+
expect(slugify(' /abc+efg*123/test§xyz 1234567 /(ö) ', 17))
16+
.toEqual('abc-efg-123-test');
17+
});
18+
919
test('slugifies ref name with dash', () => {
1020
expect(slugify('feat/feature-branch-1')).toEqual('feat-feature-branch-1');
1121
});

0 commit comments

Comments
 (0)