Skip to content

Commit

Permalink
fix: support circle 2.0 env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
krohrsb authored and pvdlg committed Jun 15, 2018
1 parent 3ee13d5 commit 6efe192
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
10 changes: 7 additions & 3 deletions lib/circleci.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// https://circleci.com/docs/1.0/environment-variables
// Circle 1.0 docs: https://circleci.com/docs/1.0/environment-variables
// Circle 2.0 docs: https://circleci.com/docs/2.0/env-vars/

module.exports = {
detect() {
return Boolean(process.env.CIRCLECI);
},
configuration() {
// Support both 1.0 and 2.0
const pullRequestUrl = process.env.CIRCLE_PULL_REQUEST || process.env.CI_PULL_REQUEST;
const pullRequest = pullRequestUrl ? pullRequestUrl.split('/').pop() : process.env.CIRCLE_PR_NUMBER;
return {
name: 'CircleCI',
service: 'circleci',
Expand All @@ -13,8 +17,8 @@ module.exports = {
job: `${process.env.CIRCLE_BUILD_NUM}.${process.env.CIRCLE_NODE_INDEX}`,
commit: process.env.CIRCLE_SHA1,
branch: process.env.CIRCLE_BRANCH,
pr: process.env.CI_PULL_REQUEST ? process.env.CI_PULL_REQUEST.split('/').pop() : undefined,
isPr: Boolean(process.env.CI_PULL_REQUEST),
pr: pullRequest,
isPr: Boolean(pullRequest),
slug: `${process.env.CIRCLE_PROJECT_USERNAME}/${process.env.CIRCLE_PROJECT_REPONAME}`,
};
},
Expand Down
53 changes: 52 additions & 1 deletion test/circleci.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test('Push', t => {
});
});

test('PR', t => {
test('PR 1.0', t => {
process.env.CIRCLECI = 'true';
process.env.CIRCLE_BUILD_NUM = '1234';
process.env.CIRCLE_SHA1 = '5678';
Expand All @@ -49,3 +49,54 @@ test('PR', t => {
slug: 'owner/repo',
});
});

test('PR 2.0', t => {
process.env.CIRCLECI = 'true';
process.env.CIRCLE_BUILD_NUM = '1234';
process.env.CIRCLE_SHA1 = '5678';
process.env.CIRCLE_BRANCH = 'pr_branch';
process.env.CIRCLE_NODE_INDEX = '1';
process.env.CIRCLE_PROJECT_USERNAME = 'owner';
process.env.CIRCLE_PROJECT_REPONAME = 'repo';
process.env.CIRCLE_PULL_REQUEST = 'uri/pr/10';
delete process.env.CI_PULL_REQUEST;

t.deepEqual(circle.configuration(), {
name: 'CircleCI',
service: 'circleci',
commit: '5678',
build: '1234',
buildUrl: 'https://server.com/buildresult',
job: '1234.1',
branch: 'pr_branch',
pr: '10',
isPr: true,
slug: 'owner/repo',
});
});

test('PR 2.0 without pull uri', t => {
process.env.CIRCLECI = 'true';
process.env.CIRCLE_BUILD_NUM = '1234';
process.env.CIRCLE_SHA1 = '5678';
process.env.CIRCLE_BRANCH = 'pr_branch';
process.env.CIRCLE_NODE_INDEX = '1';
process.env.CIRCLE_PROJECT_USERNAME = 'owner';
process.env.CIRCLE_PROJECT_REPONAME = 'repo';
process.env.CIRCLE_PR_NUMBER = '10';
delete process.env.CIRCLE_PULL_REQUEST;
delete process.env.CI_PULL_REQUEST;

t.deepEqual(circle.configuration(), {
name: 'CircleCI',
service: 'circleci',
commit: '5678',
build: '1234',
buildUrl: 'https://server.com/buildresult',
job: '1234.1',
branch: 'pr_branch',
pr: '10',
isPr: true,
slug: 'owner/repo',
});
});

0 comments on commit 6efe192

Please sign in to comment.