-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathenvironment.js
476 lines (439 loc) · 14.4 KB
/
environment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const utils = require('./utils');
const GIT_COMMIT_FORMAT = [
'COMMIT_SHA:%H',
'AUTHOR_NAME:%an',
'AUTHOR_EMAIL:%ae',
'COMMITTER_NAME:%cn',
'COMMITTER_EMAIL:%ce',
'COMMITTED_DATE:%ai',
// Note: order is important, this must come last because the regex is a multiline match.
'COMMIT_MESSAGE:%B',
].join('%n'); // git show format uses %n for newlines.
class Environment {
constructor(env) {
if (!env) {
throw new Error('"env" arg is required to create an Environment.');
}
this._env = env;
}
get ci() {
if (this._env.TRAVIS_BUILD_ID) {
return 'travis';
} else if (this._env.JENKINS_URL && this._env.ghprbPullId) {
// Pull Request Builder plugin.
return 'jenkins-prb';
} else if (this._env.JENKINS_URL) {
return 'jenkins';
} else if (this._env.CIRCLECI) {
return 'circle';
} else if (this._env.CI_NAME && this._env.CI_NAME == 'codeship') {
return 'codeship';
} else if (this._env.DRONE == 'true') {
return 'drone';
} else if (this._env.SEMAPHORE == 'true') {
return 'semaphore';
} else if (this._env.BUILDKITE == 'true') {
return 'buildkite';
} else if (this._env.HEROKU_TEST_RUN_ID) {
return 'heroku';
} else if (this._env.GITLAB_CI == 'true') {
return 'gitlab';
} else if (this._env.TF_BUILD == 'True') {
return 'azure';
} else if (this._env.APPVEYOR == 'True' || this._env.APPVEYOR == 'true') {
return 'appveyor';
} else if (this._env.PROBO_ENVIRONMENT == 'TRUE') {
return 'probo';
} else if (this._env.BITBUCKET_BUILD_NUMBER) {
return 'bitbucket';
} else if (this._env.GITHUB_ACTIONS == 'true') {
return 'github';
} else if (this._env.NETLIFY == 'true') {
return 'netlify';
} else if (this._env.CI) {
// this should always be the last branch
return 'CI/unknown';
}
return null;
}
get ciVersion() {
switch (this.ci) {
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;
}
gitExec(args) {
const child_process = require('child_process');
try {
let result = child_process.spawnSync('git', args);
if (result.status == 0) {
return result.stdout.toString().trim();
} else {
return '';
}
} catch (error) {
return '';
}
}
rawCommitData(commitSha) {
// Make sure commitSha is only alphanumeric characters and ^ to prevent command injection.
if (commitSha.length > 100 || !commitSha.match(/^[0-9a-zA-Z^]+$/)) {
return '';
}
const args = ['show', commitSha, '--quiet', `--format="${GIT_COMMIT_FORMAT}"`];
return this.gitExec(args);
}
// If not running in a git repo, allow undefined for certain commit attributes.
parse(formattedCommitData, regex) {
return ((formattedCommitData && formattedCommitData.match(regex)) || [])[1];
}
get commitData() {
// Read the result from environment data
let result = {
branch: this.branch,
sha: this.commitSha,
// These GIT_ environment vars are from the Jenkins Git Plugin, but could be
// used generically. This behavior may change in the future.
authorName: this._env['GIT_AUTHOR_NAME'],
authorEmail: this._env['GIT_AUTHOR_EMAIL'],
committerName: this._env['GIT_COMMITTER_NAME'],
committerEmail: this._env['GIT_COMMITTER_EMAIL'],
};
// Try and get more meta-data from git
let formattedCommitData = '';
if (this.commitSha) {
formattedCommitData = this.rawCommitData(this.commitSha);
}
if (!formattedCommitData) {
formattedCommitData = this.rawCommitData('HEAD');
}
if (!formattedCommitData) {
return result;
}
// If this.commitSha didn't provide a sha, use the one from the commit
if (!result.sha) {
result.sha = this.parse(formattedCommitData, /COMMIT_SHA:(.*)/);
}
result.message = this.parse(formattedCommitData, /COMMIT_MESSAGE:(.*)/m);
result.committedAt = this.parse(formattedCommitData, /COMMITTED_DATE:(.*)/);
result.authorName = this.parse(formattedCommitData, /AUTHOR_NAME:(.*)/);
result.authorEmail = this.parse(formattedCommitData, /AUTHOR_EMAIL:(.*)/);
result.committerName = this.parse(formattedCommitData, /COMMITTER_NAME:(.*)/);
result.committerEmail = this.parse(formattedCommitData, /COMMITTER_EMAIL:(.*)/);
return result;
}
get jenkinsMergeCommitBuild() {
let formattedCommitData = this.rawCommitData('HEAD');
if (!formattedCommitData) {
return false;
}
let authorName = this.parse(formattedCommitData, /AUTHOR_NAME:(.*)/);
let authorEmail = this.parse(formattedCommitData, /AUTHOR_EMAIL:(.*)/);
let message = this.parse(formattedCommitData, /COMMIT_MESSAGE:(.*)/m);
if (authorName === 'Jenkins' && authorEmail === 'nobody@nowhere') {
// Example merge message: Merge commit 'ec4d24c3d22f3c95e34af95c1fda2d462396d885' into HEAD
if (message.substring(0, 13) === 'Merge commit ' && message.substring(55) === ' into HEAD') {
return true;
}
}
return false;
}
get secondToLastCommitSHA() {
let formattedCommitData = this.rawCommitData('HEAD^');
if (!formattedCommitData) {
return null;
}
return this.parse(formattedCommitData, /COMMIT_SHA:(.*)/);
}
get commitSha() {
if (this._env.PERCY_COMMIT) {
return this._env.PERCY_COMMIT;
}
switch (this.ci) {
case 'travis':
return this._env.TRAVIS_COMMIT;
case 'jenkins-prb':
// Pull Request Builder Plugin OR Git Plugin.
return this._env.ghprbActualCommit || this._env.GIT_COMMIT;
case 'jenkins':
if (this.jenkinsMergeCommitBuild) {
return this.secondToLastCommitSHA;
}
return this._env.GIT_COMMIT;
case 'circle':
return this._env.CIRCLE_SHA1;
case 'codeship':
return this._env.CI_COMMIT_ID;
case 'drone':
return this._env.DRONE_COMMIT;
case 'semaphore':
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.
return commitSha !== 'HEAD' ? this._env.BUILDKITE_COMMIT : null;
}
case 'heroku':
return this._env.HEROKU_TEST_RUN_COMMIT_VERSION;
case 'gitlab':
return this._env.CI_COMMIT_SHA;
case 'azure':
return this._env.SYSTEM_PULLREQUEST_SOURCECOMMITID || this._env.BUILD_SOURCEVERSION;
case 'appveyor':
return this._env.APPVEYOR_PULL_REQUEST_HEAD_COMMIT || this._env.APPVEYOR_REPO_COMMIT;
case 'probo':
case 'netlify':
return this._env.COMMIT_REF;
case 'bitbucket':
return this._env.BITBUCKET_COMMIT;
case 'github':
return this._env.GITHUB_SHA;
}
return null;
}
get targetCommitSha() {
return this._env.PERCY_TARGET_COMMIT || null;
}
get partialBuild() {
let partial = this._env.PERCY_PARTIAL_BUILD;
return !!partial && partial !== '0';
}
get branch() {
if (this._env.PERCY_BRANCH) {
return this._env.PERCY_BRANCH;
}
let result = '';
switch (this.ci) {
case 'travis':
if (this.pullRequestNumber && this._env.TRAVIS_PULL_REQUEST_BRANCH) {
result = this._env.TRAVIS_PULL_REQUEST_BRANCH;
} else {
result = this._env.TRAVIS_BRANCH;
}
break;
case 'jenkins-prb':
result = this._env.ghprbSourceBranch;
break;
case 'jenkins':
result = this._env.CHANGE_BRANCH || this._env.GIT_BRANCH;
break;
case 'circle':
result = this._env.CIRCLE_BRANCH;
break;
case 'codeship':
result = this._env.CI_BRANCH;
break;
case 'drone':
result = this._env.DRONE_BRANCH;
break;
case 'semaphore':
result =
this._env.BRANCH_NAME ||
this._env.SEMAPHORE_GIT_PR_BRANCH ||
this._env.SEMAPHORE_GIT_BRANCH;
break;
case 'buildkite':
result = this._env.BUILDKITE_BRANCH;
break;
case 'heroku':
result = this._env.HEROKU_TEST_RUN_BRANCH;
break;
case 'gitlab':
result = this._env.CI_COMMIT_REF_NAME;
break;
case 'azure':
result = this._env.SYSTEM_PULLREQUEST_SOURCEBRANCH || this._env.BUILD_SOURCEBRANCHNAME;
break;
case 'appveyor':
result = this._env.APPVEYOR_PULL_REQUEST_HEAD_REPO_BRANCH || this._env.APPVEYOR_REPO_BRANCH;
break;
case 'probo':
result = this._env.BRANCH_NAME;
break;
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;
case 'netlify':
result = this._env.HEAD;
break;
}
if (result == '') {
result = this.rawBranch();
}
if (result == '') {
// Branch not specified
result = null;
}
return result;
}
rawBranch() {
return this.gitExec(['rev-parse', '--abbrev-ref', 'HEAD']);
}
get targetBranch() {
return this._env.PERCY_TARGET_BRANCH || null;
}
get pullRequestNumber() {
if (this._env.PERCY_PULL_REQUEST) {
return this._env.PERCY_PULL_REQUEST;
}
switch (this.ci) {
case 'travis':
return this._env.TRAVIS_PULL_REQUEST !== 'false' ? this._env.TRAVIS_PULL_REQUEST : null;
case 'jenkins-prb':
return this._env.ghprbPullId;
case 'jenkins':
return this._env.CHANGE_ID || null;
case 'circle':
if (this._env.CI_PULL_REQUESTS && this._env.CI_PULL_REQUESTS !== '') {
return this._env.CI_PULL_REQUESTS.split('/').slice(-1)[0];
}
break;
case 'codeship':
// Unfortunately, codeship always returns 'false' for CI_PULL_REQUEST. For now, return null.
break;
case 'drone':
return this._env.CI_PULL_REQUEST;
case 'semaphore':
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
: null;
case 'gitlab':
return this._env.CI_MERGE_REQUEST_IID || null;
case 'azure':
return (
this._env.SYSTEM_PULLREQUEST_PULLREQUESTID ||
this._env.SYSTEM_PULLREQUEST_PULLREQUESTNUMBER ||
null
);
case 'appveyor':
return this._env.APPVEYOR_PULL_REQUEST_NUMBER || null;
case 'probo':
if (this._env.PULL_REQUEST_LINK && this._env.PULL_REQUEST_LINK !== '') {
return this._env.PULL_REQUEST_LINK.split('/').slice(-1)[0];
}
break;
case 'bitbucket':
return this._env.BITBUCKET_PR_ID || null;
case 'netlify':
if (this._env.PULL_REQUEST == 'true') {
return this._env.REVIEW_ID;
}
break;
}
return null;
}
// A nonce which will be the same for all nodes of a parallel build, used to identify shards
// of the same CI build. This is usually just the CI environment build ID.
get parallelNonce() {
if (this._env.PERCY_PARALLEL_NONCE) {
return this._env.PERCY_PARALLEL_NONCE;
}
switch (this.ci) {
case 'travis':
return this._env.TRAVIS_BUILD_NUMBER;
case 'jenkins-prb':
return this._env.BUILD_NUMBER;
case 'jenkins':
if (this._env.BUILD_TAG) {
return utils.reverseString(this._env.BUILD_TAG).substring(0, 60);
}
break;
case 'circle':
return this._env.CIRCLE_WORKFLOW_ID || this._env.CIRCLE_BUILD_NUM;
case 'codeship':
return this._env.CI_BUILD_NUMBER || this._env.CI_BUILD_ID;
case 'drone':
return this._env.DRONE_BUILD_NUMBER;
case 'semaphore':
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':
return this._env.HEROKU_TEST_RUN_ID;
case 'gitlab':
return this._env.CI_PIPELINE_ID;
case 'azure':
return this._env.BUILD_BUILDID;
case 'appveyor':
return this._env.APPVEYOR_BUILD_ID;
case 'probo':
return this._env.BUILD_ID;
case 'bitbucket':
return this._env.BITBUCKET_BUILD_NUMBER;
}
return null;
}
get parallelTotalShards() {
if (this._env.PERCY_PARALLEL_TOTAL) {
return parseInt(this._env.PERCY_PARALLEL_TOTAL);
}
switch (this.ci) {
case 'travis':
// Support for https://github.com/ArturT/knapsack
if (this._env.CI_NODE_TOTAL) {
return parseInt(this._env.CI_NODE_TOTAL);
}
break;
case 'jenkins-prb':
break;
case 'jenkins':
break;
case 'circle':
if (this._env.CIRCLE_NODE_TOTAL) {
return parseInt(this._env.CIRCLE_NODE_TOTAL);
}
break;
case 'codeship':
if (this._env.CI_NODE_TOTAL) {
return parseInt(this._env.CI_NODE_TOTAL);
}
break;
case 'drone':
break;
case 'semaphore':
if (this._env.SEMAPHORE_THREAD_COUNT) {
return parseInt(this._env.SEMAPHORE_THREAD_COUNT);
}
break;
case 'buildkite':
if (this._env.BUILDKITE_PARALLEL_JOB_COUNT) {
return parseInt(this._env.BUILDKITE_PARALLEL_JOB_COUNT);
}
break;
case 'heroku':
if (this._env.CI_NODE_TOTAL) {
return parseInt(this._env.CI_NODE_TOTAL);
}
break;
case 'azure':
// SYSTEM_TOTALJOBSINPHASE is set for parallel builds and non-parallel matrix builds, so
// check build strategy is parallel by ensuring SYSTEM_PARALLELEXECUTIONTYPE == MultiMachine
if (
this._env.SYSTEM_PARALLELEXECUTIONTYPE == 'MultiMachine' &&
this._env.SYSTEM_TOTALJOBSINPHASE
) {
return parseInt(this._env.SYSTEM_TOTALJOBSINPHASE);
}
break;
}
return null;
}
}
module.exports = Environment;