Skip to content

Commit

Permalink
✨ Add support for Semaphore 2.0 (#188)
Browse files Browse the repository at this point in the history
* ✨ Add support for Semaphore 2.0

* 🔖 v3.7.0
  • Loading branch information
Wil Wilsman committed Mar 24, 2020
1 parent 06b0c08 commit 73e3919
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "percy-client",
"version": "3.6.1",
"version": "3.7.0",
"description": "JavaScript API client library for Percy (https://percy.io).",
"main": "dist/main.js",
"scripts": {
Expand Down
20 changes: 14 additions & 6 deletions src/environment.js
Expand Up @@ -63,10 +63,12 @@ class Environment {

get ciVersion() {
switch (this.ci) {
case 'gitlab':
return `gitlab/${this._env.CI_SERVER_VERSION}`;
case 'github':
return `github/${this._env.PERCY_GITHUB_ACTION || 'unknown'}`;
case 'gitlab':
return `gitlab/${this._env.CI_SERVER_VERSION}`;
case 'semaphore':
return this._env.SEMAPHORE_GIT_SHA ? 'semaphore/2.0' : 'semaphore';
}
return this.ci;
}
Expand Down Expand Up @@ -194,7 +196,7 @@ class Environment {
case 'drone':
return this._env.DRONE_COMMIT;
case 'semaphore':
return this._env.REVISION;
return this._env.REVISION || this._env.SEMAPHORE_GIT_PR_SHA || this._env.SEMAPHORE_GIT_SHA;
case 'buildkite': {
let commitSha = this._env.BUILDKITE_COMMIT;
// Buildkite mixes SHAs and non-SHAs in BUILDKITE_COMMIT, so we return null if non-SHA.
Expand Down Expand Up @@ -258,7 +260,10 @@ class Environment {
result = this._env.DRONE_BRANCH;
break;
case 'semaphore':
result = this._env.BRANCH_NAME;
result =
this._env.BRANCH_NAME ||
this._env.SEMAPHORE_GIT_PR_BRANCH ||
this._env.SEMAPHORE_GIT_BRANCH;
break;
case 'buildkite':
result = this._env.BUILDKITE_BRANCH;
Expand Down Expand Up @@ -335,7 +340,7 @@ class Environment {
case 'drone':
return this._env.CI_PULL_REQUEST;
case 'semaphore':
return this._env.PULL_REQUEST_NUMBER;
return this._env.PULL_REQUEST_NUMBER || this._env.SEMAPHORE_GIT_PR_NUMBER || null;
case 'buildkite':
return this._env.BUILDKITE_PULL_REQUEST !== 'false'
? this._env.BUILDKITE_PULL_REQUEST
Expand Down Expand Up @@ -385,7 +390,10 @@ class Environment {
case 'drone':
return this._env.DRONE_BUILD_NUMBER;
case 'semaphore':
return `${this._env.SEMAPHORE_BRANCH_ID}/${this._env.SEMAPHORE_BUILD_NUMBER}`;
return (
this._env.SEMAPHORE_WORKFLOW_ID ||
`${this._env.SEMAPHORE_BRANCH_ID}/${this._env.SEMAPHORE_BUILD_NUMBER}`
);
case 'buildkite':
return this._env.BUILDKITE_BUILD_ID;
case 'heroku':
Expand Down
46 changes: 46 additions & 0 deletions test/environment-test.js
Expand Up @@ -499,6 +499,7 @@ COMMIT_MESSAGE:A shiny new feature`);

it('has the correct properties', function() {
assert.strictEqual(environment.ci, 'semaphore');
assert.strictEqual(environment.ciVersion, 'semaphore');
assert.strictEqual(environment.commitSha, 'semaphore-commit-sha');
assert.strictEqual(environment.targetCommitSha, null);
assert.strictEqual(environment.branch, 'semaphore-branch');
Expand All @@ -508,6 +509,51 @@ COMMIT_MESSAGE:A shiny new feature`);
assert.strictEqual(environment.parallelNonce, expected_nonce);
assert.strictEqual(environment.parallelTotalShards, 2);
});

describe('Semaphore 2.0', () => {
beforeEach(() => {
environment = new Environment({
SEMAPHORE: 'true',
SEMAPHORE_GIT_SHA: 'semaphore-2-sha',
SEMAPHORE_GIT_BRANCH: 'semaphore-2-branch',
SEMAPHORE_WORKFLOW_ID: 'semaphore-2-workflow-id',
});
});

it('has the correct properties', () => {
assert.strictEqual(environment.ci, 'semaphore');
assert.strictEqual(environment.ciVersion, 'semaphore/2.0');
assert.strictEqual(environment.commitSha, 'semaphore-2-sha');
assert.strictEqual(environment.branch, 'semaphore-2-branch');
assert.strictEqual(environment.targetCommitSha, null);
assert.strictEqual(environment.targetBranch, null);
assert.strictEqual(environment.pullRequestNumber, null);
assert.strictEqual(environment.parallelNonce, 'semaphore-2-workflow-id');
assert.strictEqual(environment.parallelTotalShards, null);
});

it('has the correct properties for PR builds', () => {
environment = new Environment({
SEMAPHORE: 'true',
SEMAPHORE_GIT_SHA: 'semaphore-2-sha',
SEMAPHORE_GIT_PR_SHA: 'semaphore-2-pr-sha',
SEMAPHORE_GIT_BRANCH: 'semaphore-2-branch',
SEMAPHORE_GIT_PR_BRANCH: 'semaphore-2-pr-branch',
SEMAPHORE_GIT_PR_NUMBER: '50',
SEMAPHORE_WORKFLOW_ID: 'semaphore-2-workflow-id',
});

assert.strictEqual(environment.ci, 'semaphore');
assert.strictEqual(environment.ciVersion, 'semaphore/2.0');
assert.strictEqual(environment.commitSha, 'semaphore-2-pr-sha');
assert.strictEqual(environment.branch, 'semaphore-2-pr-branch');
assert.strictEqual(environment.targetCommitSha, null);
assert.strictEqual(environment.targetBranch, null);
assert.strictEqual(environment.pullRequestNumber, '50');
assert.strictEqual(environment.parallelNonce, 'semaphore-2-workflow-id');
assert.strictEqual(environment.parallelTotalShards, null);
});
});
});

context('in Buildkite', function() {
Expand Down

0 comments on commit 73e3919

Please sign in to comment.