-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapp.ts
More file actions
164 lines (146 loc) · 4.87 KB
/
app.ts
File metadata and controls
164 lines (146 loc) · 4.87 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
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
import { BuildLogFile, configs, ProjectType } from './Config';
import { File } from './File';
import { Log } from './Logger';
import {
AndroidLintStyleParser,
DartLintParser,
DotnetBuildParser,
ESLintParser,
JscpdParser,
LintItem,
MSBuildParser,
Parser,
ScalaStyleParser,
SwiftLintParser,
TSLintParser,
} from './Parser';
import { GitHubPRService, VCS } from './Provider';
import { GitLabMRService } from './Provider/GitLab/GitLabMRService';
import { GitHubAdapter } from './Provider/GitHub/GitHubAdapter';
import { VCSEngine } from './Provider/CommonVCS/VCSEngine';
import { GitLabAdapter } from './Provider/GitLab/GitLabAdapter';
import { VCSAdapter } from './Provider/@interfaces/VCSAdapter';
import { AnalyzerBot } from './AnalyzerBot/AnalyzerBot';
import { defaultFormatter, gitLabFormatter } from './OutputFormatter/OutputFormatter';
type OutputFileMeta = {
formatter: (items: LintItem[]) => string;
filename: string;
};
class App {
private vcs: VCS | null = null;
private outputFileMeta: OutputFileMeta[];
async start(): Promise<void> {
if (!configs.dryRun) {
const adapter = App.getAdapter();
if (!adapter) {
Log.error('VCS adapter is not found');
process.exit(1);
}
const analyzer = new AnalyzerBot(configs);
this.vcs = new VCSEngine(configs, analyzer, adapter);
}
this.outputFileMeta = App.getOutputFileMetas();
const logs = await this.parseBuildData(configs.buildLogFile);
Log.info('Build data parsing completed');
const reportToVcs = this.reportToVcs(logs);
const writeOutputFiles = this.writeOutputFiles(logs);
const [passed] = await Promise.all([reportToVcs, writeOutputFiles]);
if (!passed) {
Log.error('There are some linting error and exit code reporting is enabled');
process.exit(1);
}
}
private static getAdapter(): VCSAdapter | undefined {
if (configs.vcs === 'github') {
const githubPRService = new GitHubPRService(
configs.githubToken,
configs.githubRepoUrl,
configs.githubPr,
);
return new GitHubAdapter(githubPRService);
} else if (configs.vcs === 'gitlab') {
return new GitLabAdapter(new GitLabMRService());
}
}
private static getParser(type: ProjectType, cwd: string): Parser {
switch (type) {
case ProjectType.dotnetbuild:
return new DotnetBuildParser(cwd);
case ProjectType.msbuild:
return new MSBuildParser(cwd);
case ProjectType.tslint:
return new TSLintParser(cwd);
case ProjectType.eslint:
return new ESLintParser(cwd);
case ProjectType.scalastyle:
return new ScalaStyleParser(cwd);
case ProjectType.androidlint:
return new AndroidLintStyleParser(cwd);
case ProjectType.dartlint:
return new DartLintParser(cwd);
case ProjectType.swiftlint:
return new SwiftLintParser(cwd);
case ProjectType.jscpd:
return new JscpdParser(cwd);
}
}
private static getOutputFileMetas(): OutputFileMeta[] {
const outputFileMetas: OutputFileMeta[] = [];
if (configs.outputFormat.includes('default')) {
outputFileMetas.push({ formatter: defaultFormatter, filename: configs.output });
}
if (configs.outputFormat.includes('gitlab')) {
outputFileMetas.push({
formatter: gitLabFormatter,
filename: 'gl-code-quality-report.json',
});
}
return outputFileMetas;
}
private async parseBuildData(files: BuildLogFile[]): Promise<LintItem[]> {
const logsTasks = files.map(async ({ type, path, cwd }) => {
Log.debug('Parsing', { type, path, cwd });
const content = await File.readFileHelper(path);
const parser = App.getParser(type, cwd);
return parser.parse(content);
});
return (await Promise.all(logsTasks)).flatMap((x) => x);
}
private async reportToVcs(items: LintItem[]): Promise<boolean> {
if (!this.vcs) {
Log.info('Dry run enabled, skip reporting');
return true;
}
try {
const passed = await this.vcs.report(items);
Log.info('Report to VCS completed');
return passed;
} catch (error) {
Log.error('Report to VCS failed', { error });
throw error;
}
}
private async writeOutputFiles(items: LintItem[]): Promise<void> {
try {
await Promise.all(
this.outputFileMeta.map((meta) => {
Log.info('Write output to ' + meta.filename, { meta });
return File.writeFileHelper(meta.filename, meta.formatter(items));
}),
);
Log.info('Write output completed');
} catch (error) {
Log.error('Write output failed', { error });
throw error;
}
}
}
new App().start().catch((error) => {
if (error instanceof Error) {
const { stack, message } = error;
Log.error('Unexpected error', { stack, message });
}
Log.error('Unexpected error', { error });
process.exit(2);
});