Skip to content

Commit 8403ff1

Browse files
authored
Refactor common vcs engine to use proper composition (#141)
* Create common VCS engine * Add suppressPattern config, pass config object to gitlab and github directly * Create VCSEngine config interface to avoid accessing unrelated configs from VCSEngine * Move bot logic into AnalyzerBot * Move files around and clean up complex methods * Remove unused imports * Change inheritance into composition * Remove dedicate GitHub and GitLab engine and use VCSEngine directly with different adapter * Remove unrelated changes from refactoring to make it less confusing
1 parent e9ff8e2 commit 8403ff1

30 files changed

+222
-175
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface AnalyzerBotConfig {
2+
failOnWarnings: boolean;
3+
}

src/AnalyzerBot/AnalyzerBot.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { LogSeverity, LogType } from '../Parser';
2+
import { Diff } from '../Git/@types/PatchTypes';
3+
import { onlyIn, onlySeverity } from './utils/filter.util';
4+
import { groupComments } from './utils/commentUtil';
5+
import { MessageUtil } from './utils/message.util';
6+
import { AnalyzerBotConfig } from './@interfaces/AnalyzerBotConfig';
7+
import { Comment } from './@types/CommentTypes';
8+
9+
export class AnalyzerBot {
10+
readonly touchedFileLog: LogType[];
11+
readonly comments: Comment[];
12+
readonly nError: number;
13+
readonly nWarning: number;
14+
15+
constructor(
16+
private readonly config: AnalyzerBotConfig,
17+
private readonly logs: LogType[],
18+
private readonly touchedDiff: Diff[],
19+
) {
20+
this.touchedFileLog = logs
21+
.filter(onlySeverity(LogSeverity.error, LogSeverity.warning))
22+
.filter(onlyIn(touchedDiff));
23+
this.comments = groupComments(this.touchedFileLog);
24+
this.nError = this.comments.reduce((sum, comment) => sum + comment.errors, 0);
25+
this.nWarning = this.comments.reduce((sum, comment) => sum + comment.warnings, 0);
26+
}
27+
28+
shouldGenerateOverview(): boolean {
29+
return this.nError + this.nWarning > 0;
30+
}
31+
32+
getOverviewMessage(): string {
33+
return MessageUtil.generateOverviewMessage(this.nError, this.nWarning);
34+
}
35+
36+
getCommitDescription(): string {
37+
return MessageUtil.generateCommitDescription(this.nError);
38+
}
39+
40+
isSuccess(): boolean {
41+
return this.config.failOnWarnings
42+
? this.nError + this.nWarning === 0
43+
: this.nError === 0;
44+
}
45+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LogSeverity, LogType } from '../../Parser';
2-
import { Diff } from '../@types/PatchTypes';
2+
import { Diff } from '../../Git/@types/PatchTypes';
33

44
export const onlyIn = (diffs: Diff[]) => (log: LogType): boolean =>
55
diffs.some(
File renamed without changes.

0 commit comments

Comments
 (0)