From 8852341a60b3034eabbb099b3228fb0733ce26fb Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 9 Oct 2019 12:19:30 -0400 Subject: [PATCH 1/2] feat: support GitHub Actions, close #113 --- src/github-actions-spec.js | 81 ++++++++++++++++++++++++++++++++++++++ src/index.js | 25 +++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 src/github-actions-spec.js diff --git a/src/github-actions-spec.js b/src/github-actions-spec.js new file mode 100644 index 0000000..20cccaf --- /dev/null +++ b/src/github-actions-spec.js @@ -0,0 +1,81 @@ +'use strict' + +/* eslint-env mocha */ +const isForkPr = require('.').isForkPr +const getCiName = require('.').getCiName +const mockedEnv = require('mocked-env') +const la = require('lazy-ass') + +context('GitHub Actions', () => { + describe('forked PR', () => { + let restore + + beforeEach(() => { + restore = mockedEnv( + { + GITHUB_ACTION: 'run', + GITHUB_ACTIONS: 'true', + GITHUB_ACTOR: 'bahmutov', + GITHUB_BASE_REF: 'master', + GITHUB_EVENT_NAME: 'pull_request', + GITHUB_EVENT_PATH: + '/home/runner/work/_temp/_github_workflow/event.json', + GITHUB_HEAD_REF: 'master', + GITHUB_REF: 'refs/pull/304/merge', + GITHUB_REPOSITORY: 'cypress-io/cypress-example-kitchensink', + GITHUB_SHA: 'e07026c13fe44fbff7aeccaacf6772c57ddcb5c0', + GITHUB_WORKFLOW: 'CI', + GITHUB_WORKSPACE: + '/home/runner/work/cypress-example-kitchensink/cypress-example-kitchensink' + }, + { clear: true } + ) + }) + + it('finds fork PR', () => { + la(isForkPr(), 'should have found forked PR') + }) + + afterEach(() => { + restore() + }) + }) + + describe('own PR', () => { + let restore + + beforeEach(() => { + restore = mockedEnv( + { + GITHUB_ACTION: 'run', + GITHUB_ACTIONS: 'true', + GITHUB_ACTOR: 'bahmutov', + GITHUB_BASE_REF: '', + GITHUB_EVENT_NAME: 'push', + GITHUB_EVENT_PATH: + '/home/runner/work/_temp/_github_workflow/event.json', + GITHUB_HEAD_REF: '', + GITHUB_REF: 'refs/heads/internal-pr', + GITHUB_REPOSITORY: 'cypress-io/cypress-example-kitchensink', + GITHUB_SHA: 'b04a3e7e8e83d753bb411a69d21f1d6f69c4ec1b', + GITHUB_WORKFLOW: 'CI', + GITHUB_WORKSPACE: + '/home/runner/work/cypress-example-kitchensink/cypress-example-kitchensink' + }, + { clear: true } + ) + }) + + it('does not find fork PR', () => { + la(!isForkPr(), 'should NOT be fork PR') + }) + + it('knows it is GitHub actions', () => { + la(getCiName() === 'GitHub Actions', 'wrong CI name', getCiName()) + }) + + afterEach(() => { + restore() + }) + }) +}) diff --git a/src/index.js b/src/index.js index 65b5708..ad344bd 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const isTravis = () => process.env.TRAVIS === 'true' const isCircle = () => process.env.CIRCLECI === 'true' const isAppVeyor = () => Boolean(process.env.APPVEYOR) +const isGitHubActions = () => process.env.GITHUB_ACTIONS === 'true' /** * Returns true if the Travis CI is building a pull request from @@ -67,8 +68,26 @@ const isForkPrAppVeyor = () => { return APPVEYOR_REPO_NAME !== APPVEYOR_PULL_REQUEST_HEAD_REPO_NAME } +/** + * Returns true if running on GitHub Actions + * and the current build is from forked repository pull request. + * @see https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables + */ +const isForkGitHubActions = () => { + if (!isGitHubActions()) { + return false + } + const { GITHUB_EVENT_NAME, GITHUB_HEAD_REF, GITHUB_BASE_REF } = process.env + return ( + GITHUB_EVENT_NAME === 'pull_request' && GITHUB_HEAD_REF && GITHUB_BASE_REF + ) +} + const isForkPr = () => - isForkPrTravis() || isForkPrCircle() || isForkPrAppVeyor() + isForkPrTravis() || + isForkPrCircle() || + isForkPrAppVeyor() || + isForkGitHubActions() /** * Returns the name of the detected supported CI. @@ -86,6 +105,10 @@ const getCiName = () => { if (isAppVeyor()) { return 'AppVeyor' } + + if (isGitHubActions()) { + return 'GitHub Actions' + } } module.exports = { getCiName, isForkPr } From 7209d156cf742eed2b104f854f4fb59f3ef1dbd0 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 9 Oct 2019 12:22:22 -0400 Subject: [PATCH 2/2] chore: update README --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index 2260e9f..f0e3f5d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ npm install --save is-fork-pr ## Use +### `isForkPr` + ```js const isForkPr = require('is-fork-pr').isForkPr if (isForkPr()) { @@ -27,6 +29,14 @@ if (isForkPr()) { } ``` +### `getCiName` + +```js +const getCiName = require('is-fork-pr').getCiName +// returns undefined if cannot determine the CI +console.log('Running on %s', getCiName()) +``` + ### CLI ``` @@ -40,6 +50,7 @@ Prints if a supported CI detected and if this is a forked PR - Travis CI, [Travis environment variables](https://docs.travis-ci.com/user/environment-variables/) - Circle CI v2, [Circle v2 variables](https://circleci.com/docs/2.0/env-vars/) - AppVeyor CI [env variables](https://www.appveyor.com/docs/environment-variables/) +- GitHub Actions [env variables](https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables) ### Small print