This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
knapsack-pro-env.config.ts
189 lines (154 loc) · 6.23 KB
/
knapsack-pro-env.config.ts
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
import childProcess = require('child_process');
import { CIEnvConfig } from '.';
import { KnapsackProLogger } from '../knapsack-pro-logger';
const { spawnSync } = childProcess;
function logLevel(): string {
if (process.env.KNAPSACK_PRO_LOG_LEVEL) {
return process.env.KNAPSACK_PRO_LOG_LEVEL;
}
return 'info';
}
const knapsackProLogger = new KnapsackProLogger(logLevel());
export class KnapsackProEnvConfig {
public static get endpoint(): string {
if (process.env.KNAPSACK_PRO_ENDPOINT) {
return process.env.KNAPSACK_PRO_ENDPOINT;
}
return 'https://api.knapsackpro.com';
}
public static get testSuiteToken(): string | never {
if (process.env.KNAPSACK_PRO_TEST_SUITE_TOKEN) {
return process.env.KNAPSACK_PRO_TEST_SUITE_TOKEN;
}
throw new Error(
`Please set test suite API token in CI environment variables.
Please check README for the Knapsack Pro client library.`
);
}
public static get fixedQueueSplit(): boolean {
if (process.env.KNAPSACK_PRO_FIXED_QUEUE_SPLIT) {
return (
process.env.KNAPSACK_PRO_FIXED_QUEUE_SPLIT.toLowerCase() === 'true'
);
}
return false;
}
public static get ciNodeTotal(): number | never {
if (process.env.KNAPSACK_PRO_CI_NODE_TOTAL) {
return parseInt(process.env.KNAPSACK_PRO_CI_NODE_TOTAL, 10);
}
const ciNodeTotal = CIEnvConfig.ciNodeTotal;
if (ciNodeTotal) {
return parseInt(ciNodeTotal, 10);
}
throw new Error(
// tslint:disable-next-line:max-line-length
'Undefined number of total CI nodes! Please set KNAPSACK_PRO_CI_NODE_TOTAL environment variable.'
);
}
public static get ciNodeIndex(): number | never {
if (process.env.KNAPSACK_PRO_CI_NODE_INDEX) {
return parseInt(process.env.KNAPSACK_PRO_CI_NODE_INDEX, 10);
}
const ciNodeIndex = CIEnvConfig.ciNodeIndex;
if (ciNodeIndex) {
return parseInt(ciNodeIndex, 10);
}
throw new Error(
'Undefined CI node index! Please set KNAPSACK_PRO_CI_NODE_INDEX environment variable.'
);
}
public static get ciNodeBuildId(): string | never {
if (process.env.KNAPSACK_PRO_CI_NODE_BUILD_ID) {
return process.env.KNAPSACK_PRO_CI_NODE_BUILD_ID;
}
const ciNodeBuildId = CIEnvConfig.ciNodeBuildId;
if (ciNodeBuildId) {
return ciNodeBuildId;
}
// this is key known to Knapsack Pro API, do not change it!
const knapsackProMissingBuildIdKey = 'missing-build-id';
// set env variable so next function call won't show information about missing build ID
process.env.KNAPSACK_PRO_CI_NODE_BUILD_ID = knapsackProMissingBuildIdKey;
knapsackProLogger.warn(
// tslint:disable-next-line:prefer-template
'CI node build ID not detected! Your tests will run anyway.\n\n' +
// tslint:disable-next-line:max-line-length
'If you want to be able to run more than one CI build at the same time for exactly the same commit hash, branch name and number of parallel CI nodes then you have to set unique KNAPSACK_PRO_CI_NODE_BUILD_ID environment variable.\n\n' +
// tslint:disable-next-line:max-line-length
'For instance you can generate KNAPSACK_PRO_CI_NODE_BUILD_ID=$(openssl rand - base64 32)\n\n' +
// tslint:disable-next-line:max-line-length
'Please ensure KNAPSACK_PRO_CI_NODE_BUILD_ID has the same value for all parallel CI nodes being part of the single CI build. Thanks to that the parallel nodes will consume tests from the same Queue.'
);
return process.env.KNAPSACK_PRO_CI_NODE_BUILD_ID;
}
public static get commitHash(): string | never {
if (process.env.KNAPSACK_PRO_COMMIT_HASH) {
return process.env.KNAPSACK_PRO_COMMIT_HASH;
}
const commitHash = CIEnvConfig.commitHash;
if (commitHash) {
return commitHash;
}
const gitProcess = spawnSync('git', ['rev-parse', 'HEAD']);
if (gitProcess.status === 0) {
const gitCommitHash = gitProcess.stdout.toString().trim();
// set env variable so next function call won't spawn git process again
process.env.KNAPSACK_PRO_COMMIT_HASH = gitCommitHash;
return gitCommitHash;
}
if (gitProcess.stderr === null) {
// gitProcess may fail with stderr null,
// for instance when git command does not exist on the machine
knapsackProLogger.error(
// tslint:disable-next-line:max-line-length
'We tried to detect commit hash using git but it failed. Please ensure you have have git installed or set KNAPSACK_PRO_COMMIT_HASH environment variable.'
);
} else {
const gitErrorMessage = gitProcess.stderr.toString();
knapsackProLogger.error(
'There was error in detecting commit hash using git installed on the machine:'
);
knapsackProLogger.error(gitErrorMessage);
}
throw new Error(
'Undefined commit hash! Please set KNAPSACK_PRO_COMMIT_HASH environment variable.'
);
}
public static get branch(): string | never {
if (process.env.KNAPSACK_PRO_BRANCH) {
return process.env.KNAPSACK_PRO_BRANCH;
}
const branch = CIEnvConfig.branch;
if (branch) {
return branch;
}
const gitProcess = spawnSync('git', ['rev-parse', '--abbrev-ref', 'HEAD']);
if (gitProcess.status === 0) {
const gitBranch = gitProcess.stdout.toString().trim();
// set env variable so next function call won't spawn git process again
process.env.KNAPSACK_PRO_BRANCH = gitBranch;
return gitBranch;
}
if (gitProcess.stderr === null) {
// gitProcess may fail with stderr null,
// for instance when git command does not exist on the machine
knapsackProLogger.error(
// tslint:disable-next-line:max-line-length
'We tried to detect branch name using git but it failed. Please ensure you have have git installed or set KNAPSACK_PRO_BRANCH environment variable.'
);
} else {
const gitErrorMessage = gitProcess.stderr.toString();
knapsackProLogger.error(
'There was error in detecting branch name using git installed on the machine:'
);
knapsackProLogger.error(gitErrorMessage);
}
throw new Error(
'Undefined branch name! Please set KNAPSACK_PRO_BRANCH environment variable.'
);
}
public static get logLevel(): string {
return logLevel();
}
}