Skip to content

Commit

Permalink
feat: support GitHub Actions, close #113
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 9, 2019
1 parent 74b5546 commit 8852341
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
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 }

0 comments on commit 8852341

Please sign in to comment.