From 38d00ef9586b9fe096ffc193479cb3992f95b51f Mon Sep 17 00:00:00 2001 From: Franz Diebold Date: Tue, 14 Jul 2020 10:27:45 +0200 Subject: [PATCH] Fix GitHub ref for branches with slash. --- .github/workflows/demo.yml | 6 ++-- README.md | 26 ++++++++--------- dist/index.js | 60 +++++++++++++++++++++++++++----------- index.js | 60 +++++++++++++++++++++++++++----------- package-lock.json | 2 +- package.json | 2 +- 6 files changed, 104 insertions(+), 52 deletions(-) diff --git a/.github/workflows/demo.yml b/.github/workflows/demo.yml index 3fae3f0..460babc 100644 --- a/.github/workflows/demo.yml +++ b/.github/workflows/demo.yml @@ -7,7 +7,7 @@ jobs: name: Linux Demo runs-on: ubuntu-latest steps: - - uses: franzdiebold/github-env-vars-action@v1.1.0 + - uses: franzdiebold/github-env-vars-action@v1.1.1 - name: Print environment variables run: | echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" @@ -19,7 +19,7 @@ jobs: name: Windows Demo runs-on: windows-latest steps: - - uses: franzdiebold/github-env-vars-action@v1.1.0 + - uses: franzdiebold/github-env-vars-action@v1.1.1 - name: Print environment variables run: | echo "GITHUB_REPOSITORY_OWNER=$Env:GITHUB_REPOSITORY_OWNER" @@ -31,7 +31,7 @@ jobs: name: macOS Demo runs-on: macos-latest steps: - - uses: franzdiebold/github-env-vars-action@v1.1.0 + - uses: franzdiebold/github-env-vars-action@v1.1.1 - name: Print environment variables run: | echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" diff --git a/README.md b/README.md index 7dd3fdf..fc8ec21 100644 --- a/README.md +++ b/README.md @@ -9,30 +9,30 @@ 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. | `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_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` | ### 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/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. If neither a branch or tag is available for the event type, the variable will not exist. | `refs/heads/feat/feature-branch-1` | ## Example usage ```yaml steps: - - uses: franzdiebold/github-env-vars-action@v1.1.0 + - uses: franzdiebold/github-env-vars-action@v1.1.1 - name: Print environment variables run: | echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER" diff --git a/dist/index.js b/dist/index.js index a05698b..2501ed4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -163,31 +163,57 @@ module.exports = require("path"); const core = __webpack_require__(827); -function slugify(inputString) { - return inputString.replace(/^\s+|\s+$/g, '') // trim - .toLowerCase() - .replace(/[^a-z0-9 -]/g, '') // remove invalid chars - .replace(/\s+/g, '-') // collapse whitespace and replace by - - .replace(/-+/g, '-'); // collapse dashes +function getShaShort(fullSha) { + return fullSha ? fullSha.substring(0, 8) : null; +} + +function getRepositoryOwner(repository) { + return repository ? repository.split('/')[0] : null; +} + +function getRepositoryName(repository) { + return repository ? repository.split('/')[1] : null; +} + +function getRefName(ref) { + return ref ? ref.split('/').slice(2).join('/') : null; } // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables try { - splitted_github_repository = process.env.GITHUB_REPOSITORY.split('/'); // FranzDiebold/github-env-vars-action + repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action - core.exportVariable('GITHUB_REPOSITORY_OWNER', slugify(splitted_github_repository[0])); - core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); + repositoryOwner = getRepositoryOwner(repository); + if (repositoryOwner) { + core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner); + core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); + } else { + core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".'); + } - core.exportVariable('GITHUB_REPOSITORY_NAME', slugify(splitted_github_repository[1])); - core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); + repositoryName = getRepositoryName(repository); + if (repositoryName) { + core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName); + core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); + } else { + core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".'); + } - splitted_github_ref = process.env.GITHUB_REF.split('/'); // refs/heads/feature-branch-1 - core.exportVariable('GITHUB_REF_NAME', slugify(splitted_github_ref[2])); - core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); + refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1 + if (refName) { + core.exportVariable('GITHUB_REF_NAME', refName); + core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); + } else { + core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".'); + } - github_sha = process.env.GITHUB_SHA; // ffac537e6cbbf934b08745a378932722df287a53 - core.exportVariable('GITHUB_SHA_SHORT', github_sha.substring(0, 8)); - core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); + shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53 + if (shaShort) { + core.exportVariable('GITHUB_SHA_SHORT', shaShort); + core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); + } else { + core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".'); + } } catch (error) { core.setFailed(error.message); } diff --git a/index.js b/index.js index e5ba6b3..9620764 100644 --- a/index.js +++ b/index.js @@ -2,31 +2,57 @@ const core = require('@actions/core'); -function slugify(inputString) { - return inputString.replace(/^\s+|\s+$/g, '') // trim - .toLowerCase() - .replace(/[^a-z0-9 -]/g, '') // remove invalid chars - .replace(/\s+/g, '-') // collapse whitespace and replace by - - .replace(/-+/g, '-'); // collapse dashes +function getShaShort(fullSha) { + return fullSha ? fullSha.substring(0, 8) : null; +} + +function getRepositoryOwner(repository) { + return repository ? repository.split('/')[0] : null; +} + +function getRepositoryName(repository) { + return repository ? repository.split('/')[1] : null; +} + +function getRefName(ref) { + return ref ? ref.split('/').slice(2).join('/') : null; } // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables try { - splitted_github_repository = process.env.GITHUB_REPOSITORY.split('/'); // FranzDiebold/github-env-vars-action + repository = process.env.GITHUB_REPOSITORY; // FranzDiebold/github-env-vars-action - core.exportVariable('GITHUB_REPOSITORY_OWNER', slugify(splitted_github_repository[0])); - core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); + repositoryOwner = getRepositoryOwner(repository); + if (repositoryOwner) { + core.exportVariable('GITHUB_REPOSITORY_OWNER', repositoryOwner); + core.info(`Set GITHUB_REPOSITORY_OWNER=${process.env.GITHUB_REPOSITORY_OWNER}`); + } else { + core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_OWNER".'); + } - core.exportVariable('GITHUB_REPOSITORY_NAME', slugify(splitted_github_repository[1])); - core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); + repositoryName = getRepositoryName(repository); + if (repositoryName) { + core.exportVariable('GITHUB_REPOSITORY_NAME', repositoryName); + core.info(`Set GITHUB_REPOSITORY_NAME=${process.env.GITHUB_REPOSITORY_NAME}`); + } else { + core.warning('Environment variable "GITHUB_REPOSITORY" not set. Cannot set "GITHUB_REPOSITORY_NAME".'); + } - splitted_github_ref = process.env.GITHUB_REF.split('/'); // refs/heads/feature-branch-1 - core.exportVariable('GITHUB_REF_NAME', slugify(splitted_github_ref[2])); - core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); + refName = getRefName(process.env.GITHUB_REF); // refs/heads/feat/feature-branch-1 + if (refName) { + core.exportVariable('GITHUB_REF_NAME', refName); + core.info(`Set GITHUB_REF_NAME=${process.env.GITHUB_REF_NAME}`); + } else { + core.warning('Environment variable "GITHUB_REF" not set. Cannot set "GITHUB_REF_NAME".'); + } - github_sha = process.env.GITHUB_SHA; // ffac537e6cbbf934b08745a378932722df287a53 - core.exportVariable('GITHUB_SHA_SHORT', github_sha.substring(0, 8)); - core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); + shaShort = getShaShort(process.env.GITHUB_SHA); // ffac537e6cbbf934b08745a378932722df287a53 + if (shaShort) { + core.exportVariable('GITHUB_SHA_SHORT', shaShort); + core.info(`Set GITHUB_SHA_SHORT=${process.env.GITHUB_SHA_SHORT}`); + } else { + core.warning('Environment variable "GITHUB_SHA" not set. Cannot set "GITHUB_SHA_SHORT".'); + } } catch (error) { core.setFailed(error.message); } diff --git a/package-lock.json b/package-lock.json index 163c364..3b750b3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "github-env-vars-action", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index a8cc177..429be6f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "github-env-vars-action", - "version": "1.1.0", + "version": "1.1.1", "description": "A GitHub Action to expose useful environment variables.", "main": "index.js", "scripts": {