-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathConfig.ts
More file actions
155 lines (144 loc) · 4.27 KB
/
Config.ts
File metadata and controls
155 lines (144 loc) · 4.27 KB
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
import yargs from 'yargs';
import { ProjectType } from './@enums';
import { BuildLogFile } from './@types';
import { DEFAULT_OUTPUT_FILE } from './constants/defaults';
import { GITHUB_ARGS, GITLAB_ARGS } from './constants/required';
import { z } from 'zod';
const projectTypes = Object.keys(ProjectType);
export const configSchema = z
.object({
vcs: z.enum(['github', 'gitlab']).optional().describe('VCS Type'),
githubRepoUrl: z.string().optional(),
githubPr: z.number().optional(),
githubToken: z.string().optional(),
gitlabHost: z.string().optional(),
gitlabProjectId: z.number().optional(),
gitlabMrIid: z.number().optional(),
gitlabToken: z.string().optional(),
buildLogFile: z.array(z.string()).transform((files) => {
return files
.map((opt) => {
const [type, path, cwd] = opt.split(';');
if (!projectTypes.includes(type) || !path) return null;
return { type, path, cwd: cwd ?? process.cwd() } as BuildLogFile;
})
.filter((file) => file !== null) as BuildLogFile[];
}),
output: z.string().default(DEFAULT_OUTPUT_FILE),
removeOldComment: z.boolean().default(false),
failOnWarnings: z.boolean().default(false),
dryRun: z.boolean().default(false),
})
.superRefine((options, ctx) => {
if (!options.vcs && !options.dryRun) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'VCS type is required',
});
}
})
.superRefine((options, ctx) => {
if (options.vcs === 'github' && GITHUB_ARGS.some((arg) => !options[arg])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `GitHub requires [${GITHUB_ARGS.map((a) => `--${a}`).join(
', ',
)}] to be set`,
});
}
if (options.vcs === 'gitlab' && GITLAB_ARGS.some((arg) => !options[arg])) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `GitLab requires [${GITLAB_ARGS.map((a) => `--${a}`).join(
', ',
)}] to be set`,
});
}
});
export const args = yargs
.config('config', (file) => {
console.log(`Loading config from ${file}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const config = require(file);
return config;
})
.option('vcs', {
alias: 'g',
describe: 'VCS Type',
choices: ['github', 'gitlab'],
})
.option('githubRepoUrl', {
describe: 'GitHub repo url (https or ssh)',
type: 'string',
})
.option('githubPr', {
describe: 'GitHub PR number',
type: 'number',
})
.option('githubToken', {
describe: 'GitHub token',
type: 'string',
})
.option('gitlabHost', {
describe: 'GitLab server URL (https://gitlab.yourcompany.com)',
type: 'string',
})
.option('gitlabProjectId', {
describe: 'GitLab project ID',
type: 'number',
})
.option('gitlabMrIid', {
describe: 'GitLab merge request IID (not to be confused with ID)',
type: 'number',
})
.option('gitlabToken', {
describe: 'GitLab token',
type: 'string',
})
.option('buildLogFile', {
alias: 'f',
describe: `Build log content files formatted in '<type>;<path>[;<cwd>]'
where <type> is one of [${projectTypes.join(', ')}]
<path> is build log file path to be processed
and <cwd> is build root directory (optional (Will use current context as cwd)).
`,
type: 'array',
string: true,
number: false,
})
.option('output', {
alias: 'o',
describe: 'Output parsed log file',
type: 'string',
default: DEFAULT_OUTPUT_FILE,
})
.option('removeOldComment', {
alias: 'r',
type: 'boolean',
describe: 'Remove existing CodeCoach comments before putting new one',
default: false,
})
.option('failOnWarnings', {
type: 'boolean',
describe: 'Fail the job if warnings are found',
default: false,
})
.option('dryRun', {
describe: 'Running CodeCoach without reporting to VCS',
type: 'boolean',
default: false,
})
.strict()
.help()
.wrap(120)
.parse(process.argv.slice(1));
const getConfigs = () => {
const result = configSchema.safeParse(args);
if (!result.success) {
const firstIssue = result.error.issues[0];
console.log(`${firstIssue.message} ${firstIssue.path.join('.')}`);
process.exit(1);
}
return result.data;
};
export const configs = getConfigs();