Merge pull request #11 from FranzDiebold/feat/add-slug-values

Add slug values.
This commit is contained in:
Franz Diebold 2020-07-16 11:15:04 +02:00 committed by GitHub
commit f9024825b4
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 112 additions and 24 deletions

View file

@ -7,12 +7,17 @@ jobs:
name: Linux Demo
runs-on: ubuntu-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.1.3
- uses: FranzDiebold/github-env-vars-action@v1.2.0
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
- name: Print environment variables exposed by GitHub
run: |
@ -25,12 +30,17 @@ jobs:
name: Windows Demo
runs-on: windows-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.1.3
- uses: FranzDiebold/github-env-vars-action@v1.2.0
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$Env:GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$Env:GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$Env:GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$Env:GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$Env:GITHUB_REF_SLUG"
echo "GITHUB_REF_NAME=$Env:GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$Env:GITHUB_REF_NAME_SLUG"
echo "GITHUB_SHA_SHORT=$Env:GITHUB_SHA_SHORT"
- name: Print environment variables exposed by GitHub
run: |
@ -43,12 +53,17 @@ jobs:
name: macOS Demo
runs-on: macos-latest
steps:
- uses: FranzDiebold/github-env-vars-action@v1.1.3
- uses: FranzDiebold/github-env-vars-action@v1.2.0
- name: Print environment variables exposed by this action
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
- name: Print environment variables exposed by GitHub
run: |

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: v1.1.3](https://img.shields.io/badge/Version-v1.1.3-brightgreen)
![Version: v1.2.0](https://img.shields.io/badge/Version-v1.2.0-brightgreen)
[![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)
@ -10,35 +10,46 @@ A [GitHub Action](https://github.com/features/actions) to expose useful environm
### Environment Variables exposed by **this Action**
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------|--------------------------|
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `GITHUB_REF_NAME` | The branch name that triggered the workflow. If neither a branch or tag is available for the event type, the variable will not exist. | `feat/feature-branch-1` |
| `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
| Environment Variable Name | Description | Example value |
|--------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------|
| `GITHUB_REPOSITORY_SLUG` | The slug of the owner and repository name (i.e. slug of `FranzDiebold/github-env-vars-action`). | `franzdiebold-github-env-vars-action` |
| `GITHUB_REPOSITORY_OWNER` | The owner of the repository. | `FranzDiebold` |
| `GITHUB_REPOSITORY_OWNER_SLUG` | The slug of the owner of the repository. | `franzdiebold` |
| `GITHUB_REPOSITORY_NAME` | The name of the repository. | `github-env-vars-action` |
| `GITHUB_REPOSITORY_NAME_SLUG` | The slug of the name of the repository. | `github-env-vars-action` |
| `GITHUB_REF_SLUG` | The slug of the branch or tag *ref* that triggered the workflow (i.e. slug of `refs/heads/feat/feature-branch-1`). <br>If neither a branch or tag is available for the event type, the variable will not exist. | `refs-heads-feat-feature-branch-1` |
| `GITHUB_REF_NAME` | The branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat/feature-branch-1` |
| `GITHUB_REF_NAME_SLUG` | The slug of the branch or tag *name* that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `feat-feature-branch-1` |
| `GITHUB_SHA_SHORT` | The shortened commit SHA (8 characters) that triggered the workflow. | `ffac537e` |
> The [slugified](https://en.wikipedia.org/wiki/Clean_URL#Slug) values are designed to be used in a URL.
### Default Environment Variables exposed by GitHub
For a full list of default environment variables exposed by GitHub see [https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables](https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables).
| Environment Variable Name | Description | Example value |
|---------------------------|---------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|
| `GITHUB_ACTOR` | The name of the person or app that initiated the workflow. | `octocat` |
| `GITHUB_REPOSITORY` | The owner and repository name. | `FranzDiebold/github-env-vars-action` |
| `GITHUB_SHA` | The commit SHA that triggered the workflow. | `ffac537e6cbbf934b08745a378932722df287a53` |
| `GITHUB_REF` | The branch or tag ref that triggered the workflow. If neither a branch or tag is available for the event type, the variable will not exist. | `refs/heads/feat/feature-branch-1` |
| Environment Variable Name | Description | Example value |
|---------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------|
| `GITHUB_ACTOR` | The name of the person or app that initiated the workflow. | `octocat` |
| `GITHUB_REPOSITORY` | The owner and repository name. | `FranzDiebold/github-env-vars-action` |
| `GITHUB_SHA` | The commit SHA that triggered the workflow. | `ffac537e6cbbf934b08745a378932722df287a53` |
| `GITHUB_REF` | The branch or tag ref that triggered the workflow. <br>If neither a branch or tag is available for the event type, the variable will not exist. | `refs/heads/feat/feature-branch-1` |
## :rocket: Example usage
```yaml
steps:
- uses: FranzDiebold/github-env-vars-action@v1.1.3
- uses: FranzDiebold/github-env-vars-action@v1.2.0
- name: Print environment variables
run: |
echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"
```

View file

@ -2,6 +2,20 @@
const core = require('@actions/core');
/**
* Slugify a given string.
* @param {string} inputString
* @return {string} The slugified string.
*/
function slugify(inputString) {
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
}
/**
* Get the repository owner from the repository string.
* @param {string} repository
@ -43,14 +57,29 @@ try {
// i.e. FranzDiebold/github-env-vars-action
repository = process.env.GITHUB_REPOSITORY;
if (repository) {
core.exportVariable('GITHUB_REPOSITORY_SLUG', slugify(repository));
core.info(`Set GITHUB_REPOSITORY_SLUG=` +
`${process.env.GITHUB_REPOSITORY_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_SLUG".');
}
repositoryOwner = getRepositoryOwner(repository);
if (repositoryOwner) {
core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner);
core.info(`Set GITHUB_REPOSITORY_OWNER=` +
`${process.env.GITHUB_REPOSITORY_OWNER}`);
core.exportVariable('GITHUB_REPOSITORY_OWNER_SLUG',
slugify(repositoryOwner));
core.info(`Set GITHUB_REPOSITORY_OWNER_SLUG=` +
`${process.env.GITHUB_REPOSITORY_OWNER_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_OWNER".');
'Cannot set "GITHUB_REPOSITORY_OWNER" and ' +
'"GITHUB_REPOSITORY_OWNER_SLUG".');
}
repositoryName = getRepositoryName(repository);
@ -58,19 +87,39 @@ try {
core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName);
core.info(`Set GITHUB_REPOSITORY_NAME=` +
`${process.env.GITHUB_REPOSITORY_NAME}`);
core.exportVariable('GITHUB_REPOSITORY_NAME_SLUG',
slugify(repositoryName));
core.info(`Set GITHUB_REPOSITORY_NAME_SLUG=` +
`${process.env.GITHUB_REPOSITORY_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REPOSITORY" not set. ' +
'Cannot set "GITHUB_REPOSITORY_NAME".');
'Cannot set "GITHUB_REPOSITORY_NAME" and ' +
'"GITHUB_REPOSITORY_NAME_SLUG".');
}
// i.e. refs/heads/feat/feature-branch-1
refName = getRefName(process.env.GITHUB_REF);
ref = process.env.GITHUB_REF;
if (ref) {
core.exportVariable('GITHUB_REF_SLUG', slufigy(ref));
core.info(`Set GITHUB_REF_SLUG=${process.env.GITHUB_REF_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_SLUG".');
}
refName = getRefName(ref);
if (refName) {
core.exportVariable('GITHUB_REF_NAME', refName);
core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`);
core.exportVariable('GITHUB_REF_NAME_SLUG', slufigy(refName));
core.info(`Set GITHUB_REF_NAME_SLUG=${process.env.GITHUB_REF_NAME_SLUG}`);
} else {
core.warning('Environment variable "GITHUB_REF" not set. ' +
'Cannot set "GITHUB_REF_NAME".');
'Cannot set "GITHUB_REF_NAME" and ' +
'"GITHUB_REF_NAME_SLUG".');
}
// i.e. ffac537e6cbbf934b08745a378932722df287a53
@ -87,6 +136,7 @@ try {
}
module.exports = {
slugify,
getRepositoryOwner,
getRepositoryName,
getRefName,

View file

@ -1,7 +1,19 @@
const {
getRepositoryOwner, getRepositoryName, getRefName, getShaShort,
slugify, getRepositoryOwner, getRepositoryName, getRefName, getShaShort,
} = require('./index');
test('slugifies text', () => {
expect(slugify(' /abc+efg*123/test§xyz ')).toEqual('abc-efg-123-test-xyz');
});
test('slugifies ref name with dash', () => {
expect(slugify('feat/feature-branch-1')).toEqual('feat-feature-branch-1');
});
test('slugifies empty text', () => {
expect(slugify('')).toEqual('');
});
test('gets repository owner', () => {
expect(getRepositoryOwner('FranzDiebold/github-env-vars-action'))
.toEqual('FranzDiebold');

2
package-lock.json generated
View file

@ -1,6 +1,6 @@
{
"name": "github-env-vars-action",
"version": "1.1.3",
"version": "1.2.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View file

@ -1,6 +1,6 @@
{
"name": "github-env-vars-action",
"version": "1.1.3",
"version": "1.2.0",
"description": "A GitHub Action to expose useful environment variables.",
"main": "index.js",
"scripts": {