Skip to content

Commit

Permalink
Add a couple GitHub Action vars (#163)
Browse files Browse the repository at this point in the history
* ✨ Add some environment variables for GitHub Actions

The actions are still needed to set a PR number, however we should be able to
handle the commit sha and branch name here to avoid a little boilerplate

* 🔖 v3.4.0
  • Loading branch information
Wil Wilsman committed Dec 4, 2019
1 parent e8f042e commit 5cbaad7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "percy-client",
"version": "3.3.0",
"version": "3.4.0",
"description": "JavaScript API client library for Percy (https://percy.io).",
"main": "dist/main.js",
"scripts": {
Expand Down
15 changes: 13 additions & 2 deletions src/environment.js
Expand Up @@ -49,8 +49,8 @@ class Environment {
return 'probo';
} else if (this._env.BITBUCKET_BUILD_NUMBER) {
return 'bitbucket';
} else if (this._env.PERCY_GITHUB_ACTION) {
return `github-action/${this._env.PERCY_GITHUB_ACTION}`;
} else if (this._env.GITHUB_ACTIONS == 'true') {
return 'github';
} else if (this._env.CI) {
// this should always be the last branch
return 'CI/unknown';
Expand All @@ -63,6 +63,8 @@ class Environment {
switch (this.ci) {
case 'gitlab':
return `gitlab/${this._env.CI_SERVER_VERSION}`;
case 'github':
return `github/${this._env.PERCY_GITHUB_ACTION || 'unkown'}`;
}
return this.ci;
}
Expand Down Expand Up @@ -208,6 +210,8 @@ class Environment {
return this._env.COMMIT_REF;
case 'bitbucket':
return this._env.BITBUCKET_COMMIT;
case 'github':
return this._env.GITHUB_SHA;
}

return null;
Expand Down Expand Up @@ -274,6 +278,13 @@ class Environment {
case 'bitbucket':
result = this._env.BITBUCKET_BRANCH;
break;
case 'github':
if (this._env.GITHUB_REF && this._env.GITHUB_REF.match(/^refs\//)) {
result = this._env.GITHUB_REF.replace(/^refs\/\w+?\//, '');
} else {
result = this._env.GITHUB_REF;
}
break;
}

if (result == '') {
Expand Down
18 changes: 13 additions & 5 deletions test/environment-test.js
Expand Up @@ -215,18 +215,26 @@ COMMIT_MESSAGE:A shiny new feature`);
});
});

// Most of the env vars are set within the GitHub action
// Which are tested in that repo. This will test to verify
// the user agent is properly passed through
context('in GitHub Actions', function() {
beforeEach(function() {
environment = new Environment({
PERCY_GITHUB_ACTION: 'exec-action/0.1.0',
PERCY_GITHUB_ACTION: 'test-action/0.1.0',
GITHUB_ACTIONS: 'true',
GITHUB_SHA: 'github-commit-sha',
GITHUB_REF: 'refs/head/github/branch',
});
});

it('has the correct properties', function() {
assert.strictEqual(environment.ci, 'github-action/exec-action/0.1.0');
assert.strictEqual(environment.ci, 'github');
assert.strictEqual(environment.ciVersion, 'github/test-action/0.1.0');
assert.strictEqual(environment.commitSha, 'github-commit-sha');
assert.strictEqual(environment.targetCommitSha, null);
assert.strictEqual(environment.branch, 'github/branch');
assert.strictEqual(environment.targetBranch, null);
assert.strictEqual(environment.pullRequestNumber, null);
assert.strictEqual(environment.parallelNonce, null);
assert.strictEqual(environment.parallelTotalShards, null);
});
});

Expand Down

0 comments on commit 5cbaad7

Please sign in to comment.