Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support GitHub Actions, close #113 #117

Merged
merged 2 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ npm install --save is-fork-pr

## Use

### `isForkPr`

```js
const isForkPr = require('is-fork-pr').isForkPr
if (isForkPr()) {
console.log('building forked PR, no environment vars')
}
```

### `getCiName`

```js
const getCiName = require('is-fork-pr').getCiName
// returns undefined if cannot determine the CI
console.log('Running on %s', getCiName())
```

### CLI

```
Expand All @@ -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

Expand Down
81 changes: 81 additions & 0 deletions src/github-actions-spec.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})
25 changes: 24 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -86,6 +105,10 @@ const getCiName = () => {
if (isAppVeyor()) {
return 'AppVeyor'
}

if (isGitHubActions()) {
return 'GitHub Actions'
}
}

module.exports = { getCiName, isForkPr }