-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathconfig.ts
285 lines (258 loc) · 8.73 KB
/
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
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
import * as core from '@actions/core';
import { promises as fs } from 'fs';
import * as os from 'os';
import * as path from 'path';
export type ToolType = typeof VALID_TOOLS[number];
export interface Config {
name: string;
tool: ToolType;
outputFilePath: string;
ghPagesBranch: string;
ghRepository: string | undefined;
benchmarkDataDirPath: string;
githubToken: string | undefined;
autoPush: boolean;
skipFetchGhPages: boolean;
commentAlways: boolean;
saveDataFile: boolean;
commentOnAlert: boolean;
alertThreshold: number;
failOnAlert: boolean;
failThreshold: number;
alertCommentCcUsers: string[];
externalDataJsonPath: string | undefined;
maxItemsInChart: number | null;
}
export const VALID_TOOLS = [
'cargo',
'go',
'benchmarkjs',
'benchmarkluau',
'pytest',
'googlecpp',
'catch2',
'julia',
'jmh',
'benchmarkdotnet',
'customBiggerIsBetter',
'customSmallerIsBetter',
] as const;
const RE_UINT = /^\d+$/;
function validateToolType(tool: string): asserts tool is ToolType {
if ((VALID_TOOLS as ReadonlyArray<string>).includes(tool)) {
return;
}
throw new Error(`Invalid value '${tool}' for 'tool' input. It must be one of ${VALID_TOOLS}`);
}
function resolvePath(p: string): string {
if (p.startsWith('~')) {
const home = os.homedir();
if (!home) {
throw new Error(`Cannot resolve '~' in ${p}`);
}
p = path.join(home, p.slice(1));
}
return path.resolve(p);
}
async function resolveFilePath(p: string): Promise<string> {
p = resolvePath(p);
let s;
try {
s = await fs.stat(p);
} catch (e) {
throw new Error(`Cannot stat '${p}': ${e}`);
}
if (!s.isFile()) {
throw new Error(`Specified path '${p}' is not a file`);
}
return p;
}
async function validateOutputFilePath(filePath: string): Promise<string> {
try {
return await resolveFilePath(filePath);
} catch (err) {
throw new Error(`Invalid value for 'output-file-path' input: ${err}`);
}
}
function validateGhPagesBranch(branch: string) {
if (branch) {
return;
}
throw new Error(`Branch value must not be empty for 'gh-pages-branch' input`);
}
function validateBenchmarkDataDirPath(dirPath: string): string {
try {
return resolvePath(dirPath);
} catch (e) {
throw new Error(`Invalid value for 'benchmark-data-dir-path': ${e}`);
}
}
function validateName(name: string) {
if (name) {
return;
}
throw new Error('Name must not be empty');
}
function validateGitHubToken(inputName: string, githubToken: string | undefined, todo: string) {
if (!githubToken) {
throw new Error(`'${inputName}' is enabled but 'github-token' is not set. Please give API token ${todo}`);
}
}
function getBoolInput(name: string): boolean {
const input = core.getInput(name);
if (!input) {
return false;
}
if (input !== 'true' && input !== 'false') {
throw new Error(`'${name}' input must be boolean value 'true' or 'false' but got '${input}'`);
}
return input === 'true';
}
function getPercentageInput(name: string): number | null {
const input = core.getInput(name);
if (!input) {
return null;
}
if (!input.endsWith('%')) {
throw new Error(`'${name}' input must ends with '%' for percentage value (e.g. '200%')`);
}
const percentage = parseFloat(input.slice(0, -1)); // Omit '%' at last
if (isNaN(percentage)) {
throw new Error(`Specified value '${input.slice(0, -1)}' in '${name}' input cannot be parsed as float number`);
}
return percentage / 100;
}
function getCommaSeparatedInput(name: string): string[] {
const input = core.getInput(name);
if (!input) {
return [];
}
return input.split(',').map((s) => s.trim());
}
function validateAlertCommentCcUsers(users: string[]) {
for (const u of users) {
if (!u.startsWith('@')) {
throw new Error(`User name in 'alert-comment-cc-users' input must start with '@' but got '${u}'`);
}
}
}
async function isDir(path: string) {
try {
const s = await fs.stat(path);
return s.isDirectory();
} catch (_) {
return false;
}
}
async function validateExternalDataJsonPath(path: string | undefined, autoPush: boolean): Promise<string | undefined> {
if (!path) {
return Promise.resolve(undefined);
}
if (autoPush) {
throw new Error(
'auto-push must be false when external-data-json-path is set since this action reads/writes the given JSON file and never pushes to remote',
);
}
try {
const p = resolvePath(path);
if (await isDir(p)) {
throw new Error(`Specified path '${p}' must be file but it is actually directory`);
}
return p;
} catch (err) {
throw new Error(`Invalid value for 'external-data-json-path' input: ${err}`);
}
}
function getUintInput(name: string): number | null {
const input = core.getInput(name);
if (!input) {
return null;
}
if (!RE_UINT.test(input)) {
throw new Error(`'${name}' input must be unsigned integer but got '${input}'`);
}
const i = parseInt(input, 10);
if (isNaN(i)) {
throw new Error(`Unsigned integer value '${input}' in '${name}' input was parsed as NaN`);
}
return i;
}
function validateMaxItemsInChart(max: number | null) {
if (max !== null && max <= 0) {
throw new Error(`'max-items-in-chart' input value must be one or more but got ${max}`);
}
}
function validateAlertThreshold(alertThreshold: number | null, failThreshold: number | null): asserts alertThreshold {
if (alertThreshold === null) {
throw new Error("'alert-threshold' input must not be empty");
}
if (failThreshold && Math.abs(alertThreshold) > Math.abs(failThreshold)) {
throw new Error(
`'alert-threshold' value must be smaller than 'fail-threshold' value but got ${alertThreshold} > ${failThreshold}`,
);
}
}
export async function configFromJobInput(): Promise<Config> {
const tool: string = core.getInput('tool');
let outputFilePath: string = core.getInput('output-file-path');
const ghPagesBranch: string = core.getInput('gh-pages-branch');
const ghRepository: string = core.getInput('gh-repository');
let benchmarkDataDirPath: string = core.getInput('benchmark-data-dir-path');
const name: string = core.getInput('name');
const githubToken: string | undefined = core.getInput('github-token') || undefined;
const autoPush = getBoolInput('auto-push');
const skipFetchGhPages = getBoolInput('skip-fetch-gh-pages');
const commentAlways = getBoolInput('comment-always');
const saveDataFile = getBoolInput('save-data-file');
const commentOnAlert = getBoolInput('comment-on-alert');
const alertThreshold = getPercentageInput('alert-threshold');
const failOnAlert = getBoolInput('fail-on-alert');
const alertCommentCcUsers = getCommaSeparatedInput('alert-comment-cc-users');
let externalDataJsonPath: undefined | string = core.getInput('external-data-json-path');
const maxItemsInChart = getUintInput('max-items-in-chart');
let failThreshold = getPercentageInput('fail-threshold');
validateToolType(tool);
outputFilePath = await validateOutputFilePath(outputFilePath);
validateGhPagesBranch(ghPagesBranch);
benchmarkDataDirPath = validateBenchmarkDataDirPath(benchmarkDataDirPath);
validateName(name);
if (autoPush) {
validateGitHubToken('auto-push', githubToken, 'to push GitHub pages branch to remote');
}
if (commentAlways) {
validateGitHubToken('comment-always', githubToken, 'to send commit comment');
}
if (commentOnAlert) {
validateGitHubToken('comment-on-alert', githubToken, 'to send commit comment on alert');
}
if (ghRepository) {
validateGitHubToken('gh-repository', githubToken, 'to clone the repository');
}
validateAlertThreshold(alertThreshold, failThreshold);
validateAlertCommentCcUsers(alertCommentCcUsers);
externalDataJsonPath = await validateExternalDataJsonPath(externalDataJsonPath, autoPush);
validateMaxItemsInChart(maxItemsInChart);
if (failThreshold === null) {
failThreshold = alertThreshold;
}
return {
name,
tool,
outputFilePath,
ghPagesBranch,
ghRepository,
benchmarkDataDirPath,
githubToken,
autoPush,
skipFetchGhPages,
commentAlways,
saveDataFile,
commentOnAlert,
alertThreshold,
failOnAlert,
alertCommentCcUsers,
externalDataJsonPath,
maxItemsInChart,
failThreshold,
};
}