-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathConfig.ts
More file actions
140 lines (132 loc) · 4.14 KB
/
Config.ts
File metadata and controls
140 lines (132 loc) · 4.14 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
import yargs from 'yargs';
import { ProjectType } from './@enums';
import { BuildLogFile, ConfigArgument } from './@types';
import { DEFAULT_OUTPUT_FILE } from './constants/defaults';
import { GITHUB_ARGS, GITLAB_ARGS } from './constants/required';
import fs from 'fs';
const projectTypes = Object.keys(ProjectType);
const args = yargs
.config('config', (configPath) => JSON.parse(fs.readFileSync(configPath, 'utf-8')))
.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',
})
.group(['vcs', 'buildLogFile', 'output', 'removeOldComment'], 'Parsing options:')
.group(GITLAB_ARGS, 'GitLab options:')
.group(GITHUB_ARGS, 'GitHub options:')
.check((options) => {
// validate VCS configs
if (options.vcs === 'github' && GITHUB_ARGS.some((arg) => !options[arg]))
throw `GitHub requires [${GITHUB_ARGS.map((a) => `--${a}`).join(', ')}] to be set`;
if (options.vcs === 'gitlab' && GITLAB_ARGS.some((arg) => !options[arg]))
throw `GitLab requires [${GITLAB_ARGS.map((a) => `--${a}`).join(', ')}] to be set`;
return true;
})
.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,
})
.coerce('buildLogFile', (files: string[]) => {
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;
});
})
.check((options) => {
// check arguments parsing
const useConfigArgs = options.config !== undefined;
if (useConfigArgs) return true;
// if (!options.pr || Array.isArray(options.pr))
// throw '--pr config should be a single number';
if (!options.buildLogFile || options.buildLogFile.some((file) => file === null))
throw 'all of `--buildLogFile` options should have correct format';
return true;
})
.option('output', {
alias: 'o',
describe: 'Default output file name',
type: 'string',
default: DEFAULT_OUTPUT_FILE,
})
.option('outputFormat', {
type: 'array',
describe: 'Output formats',
choices: ['default', 'gitlab'],
default: ['default'],
})
.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('suppressRules', {
type: 'array',
string: true,
number: false,
describe:
'List of rules to suppress (in RegExp format), bot will still make comment but mark it as suppressed and not failing the job',
default: [],
})
.option('dryRun', {
describe: 'Running CodeCoach without reporting to VCS',
type: 'boolean',
default: false,
})
.option('silent', {
describe: 'Disable the comment but still report job status to the VCS',
type: 'boolean',
default: false,
})
.check((options) => {
if (options.dryRun) return true;
if (typeof options.vcs === 'undefined') throw 'VCS type is required';
return true;
})
.strict()
.help()
.wrap(120)
.parse(process.argv.slice(1)) as ConfigArgument;
export const configs = args;