From 5f6520264570efc887b51b8702442320e677d935 Mon Sep 17 00:00:00 2001 From: Plai Date: Fri, 15 Sep 2023 09:24:31 +0000 Subject: [PATCH 01/11] Add new config: output format --- src/Config/@enums/outputFormat.ts | 4 ++++ src/Config/Config.ts | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 src/Config/@enums/outputFormat.ts diff --git a/src/Config/@enums/outputFormat.ts b/src/Config/@enums/outputFormat.ts new file mode 100644 index 0000000..6b41790 --- /dev/null +++ b/src/Config/@enums/outputFormat.ts @@ -0,0 +1,4 @@ +export enum OutputFormat { + default = 'default', + gitlab = 'gitlab' +} \ No newline at end of file diff --git a/src/Config/Config.ts b/src/Config/Config.ts index f81f8b7..fc3e0a3 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -92,6 +92,11 @@ and is build root directory (optional (Will use current context as cwd)). type: 'string', default: DEFAULT_OUTPUT_FILE, }) + .option('outputFormat', { + describe: 'Output format', + choices: ['default', 'gitlab'], + default: 'default', + }) .option('removeOldComment', { alias: 'r', type: 'boolean', From 97d678fafaf477b72c859ca8a25938a9569986fd Mon Sep 17 00:00:00 2001 From: Plai Date: Fri, 15 Sep 2023 12:00:12 +0000 Subject: [PATCH 02/11] Add output formatter --- src/Config/@types/configArgument.ts | 1 + src/OutputFormatter/outputFormatter.ts | 57 ++++++++++++++++++++++++++ src/app.ts | 21 ++++++++-- 3 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/OutputFormatter/outputFormatter.ts diff --git a/src/Config/@types/configArgument.ts b/src/Config/@types/configArgument.ts index feccb8d..86e6bbf 100644 --- a/src/Config/@types/configArgument.ts +++ b/src/Config/@types/configArgument.ts @@ -11,6 +11,7 @@ export type ConfigArgument = { gitlabToken: string; buildLogFile: BuildLogFile[]; output: string; // =logFilePath + outputFormat: 'default' | 'gitlab'; removeOldComment: boolean; failOnWarnings: boolean; suppressRules: string[]; diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/outputFormatter.ts new file mode 100644 index 0000000..cbe6865 --- /dev/null +++ b/src/OutputFormatter/outputFormatter.ts @@ -0,0 +1,57 @@ +import { LogSeverity, LogType } from "../Parser"; + +export type OutputFormatter = (logs: LogType[]) => string; + +type GitLabSeverity = 'info' | 'minor' | 'major' | 'critical' | 'blocker'; + +type GitLabOutputFormat = { + description: string; + check_name: string; + fingerprint: string; + severity: GitLabSeverity; + location: { + path: string; + lines: { + begin?: number; + end?: number; + } + }; +} + +const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { + switch (severity) { + case 'error': + return 'blocker'; + case 'warning': + return 'minor'; + default: + return 'info'; + } +} + +export const gitLab: OutputFormatter = (logs: LogType[]) => { + // create fingerprint by take ruleId + source + line + lineOffset and hash she it + const + + const gitlabReport = logs.map((log) => { + const format: GitLabOutputFormat = { + description: log.msg, + check_name: log.ruleId, + fingerprint: log.ruleId, + severity: mapGitLabSeverity(log.severity), + location: { + path: log.source, + lines: { + begin: log.line ?? 1, + } + } + } + return format; + }) + + return JSON.stringify(gitlabReport, null, 2); +} + +export const defaultFormatter: OutputFormatter = (logs: LogType[]) => { + return JSON.stringify(logs, null, 2); +} \ No newline at end of file diff --git a/src/app.ts b/src/app.ts index 1a261fa..30a7643 100644 --- a/src/app.ts +++ b/src/app.ts @@ -23,9 +23,11 @@ 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, gitLab, OutputFormatter } from './OutputFormatter/outputFormatter'; class App { private vcs: VCS | null = null; + private outputFormatter: OutputFormatter; async start(): Promise { if (!configs.dryRun) { @@ -37,13 +39,15 @@ class App { const analyzer = new AnalyzerBot(configs); this.vcs = new VCSEngine(configs, analyzer, adapter); } + + this.outputFormatter = App.getOutputFormatter(); const logs = await this.parseBuildData(configs.buildLogFile); Log.info('Build data parsing completed'); const reportToVcs = this.reportToVcs(logs); - const logToFile = App.writeLogToFile(logs); - const [passed] = await Promise.all([reportToVcs, logToFile]); + const writeOutputFile = this.writeOutputFile(logs); + const [passed] = await Promise.all([reportToVcs, writeOutputFile]); if (!passed) { Log.error('There are some linting error and exit code reporting is enabled'); process.exit(1); @@ -86,6 +90,15 @@ class App { } } + private static getOutputFormatter(): OutputFormatter { + switch (configs.outputFormat) { + case 'default': + return defaultFormatter; + case 'gitlab': + return gitLab; + } + } + private async parseBuildData(files: BuildLogFile[]): Promise { const logsTasks = files.map(async ({ type, path, cwd }) => { Log.debug('Parsing', { type, path, cwd }); @@ -113,9 +126,9 @@ class App { } } - private static async writeLogToFile(logs: LogType[]): Promise { + private async writeOutputFile(logs: LogType[]): Promise { try { - await File.writeFileHelper(configs.output, JSON.stringify(logs, null, 2)); + await File.writeFileHelper(configs.output, this.outputFormatter(logs)); Log.info('Write output completed'); } catch (error) { Log.error('Write output failed', { error }); From 0da05ec16d4c66ec5e3175d7044f103aaf67dc92 Mon Sep 17 00:00:00 2001 From: Plai Date: Fri, 15 Sep 2023 12:04:15 +0000 Subject: [PATCH 03/11] Add fingerprint --- src/OutputFormatter/outputFormatter.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/outputFormatter.ts index cbe6865..f712b36 100644 --- a/src/OutputFormatter/outputFormatter.ts +++ b/src/OutputFormatter/outputFormatter.ts @@ -1,4 +1,5 @@ import { LogSeverity, LogType } from "../Parser"; +import { createHash } from "crypto"; export type OutputFormatter = (logs: LogType[]) => string; @@ -30,14 +31,14 @@ const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { } export const gitLab: OutputFormatter = (logs: LogType[]) => { - // create fingerprint by take ruleId + source + line + lineOffset and hash she it - const - const gitlabReport = logs.map((log) => { + const fingerprint = createHash('sha256'); + fingerprint.update(`${log.ruleId}${log.source}${log.line}${log.lineOffset}`); + const format: GitLabOutputFormat = { description: log.msg, check_name: log.ruleId, - fingerprint: log.ruleId, + fingerprint: fingerprint.digest('hex'), severity: mapGitLabSeverity(log.severity), location: { path: log.source, From c1b88e4877a8de65d6083009261c310ebc703fd3 Mon Sep 17 00:00:00 2001 From: Plai Date: Fri, 15 Sep 2023 12:06:41 +0000 Subject: [PATCH 04/11] code style update --- src/Config/@enums/outputFormat.ts | 4 ++-- src/OutputFormatter/outputFormatter.ts | 22 +++++++++++----------- src/app.ts | 8 ++++++-- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Config/@enums/outputFormat.ts b/src/Config/@enums/outputFormat.ts index 6b41790..144be9a 100644 --- a/src/Config/@enums/outputFormat.ts +++ b/src/Config/@enums/outputFormat.ts @@ -1,4 +1,4 @@ export enum OutputFormat { default = 'default', - gitlab = 'gitlab' -} \ No newline at end of file + gitlab = 'gitlab', +} diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/outputFormatter.ts index f712b36..c3b30a5 100644 --- a/src/OutputFormatter/outputFormatter.ts +++ b/src/OutputFormatter/outputFormatter.ts @@ -1,5 +1,5 @@ -import { LogSeverity, LogType } from "../Parser"; -import { createHash } from "crypto"; +import { LogSeverity, LogType } from '../Parser'; +import { createHash } from 'crypto'; export type OutputFormatter = (logs: LogType[]) => string; @@ -15,9 +15,9 @@ type GitLabOutputFormat = { lines: { begin?: number; end?: number; - } + }; }; -} +}; const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { switch (severity) { @@ -28,7 +28,7 @@ const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { default: return 'info'; } -} +}; export const gitLab: OutputFormatter = (logs: LogType[]) => { const gitlabReport = logs.map((log) => { @@ -44,15 +44,15 @@ export const gitLab: OutputFormatter = (logs: LogType[]) => { path: log.source, lines: { begin: log.line ?? 1, - } - } - } + }, + }, + }; return format; - }) + }); return JSON.stringify(gitlabReport, null, 2); -} +}; export const defaultFormatter: OutputFormatter = (logs: LogType[]) => { return JSON.stringify(logs, null, 2); -} \ No newline at end of file +}; diff --git a/src/app.ts b/src/app.ts index 30a7643..d294cd4 100644 --- a/src/app.ts +++ b/src/app.ts @@ -23,7 +23,11 @@ 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, gitLab, OutputFormatter } from './OutputFormatter/outputFormatter'; +import { + defaultFormatter, + gitLab, + OutputFormatter, +} from './OutputFormatter/outputFormatter'; class App { private vcs: VCS | null = null; @@ -39,7 +43,7 @@ class App { const analyzer = new AnalyzerBot(configs); this.vcs = new VCSEngine(configs, analyzer, adapter); } - + this.outputFormatter = App.getOutputFormatter(); const logs = await this.parseBuildData(configs.buildLogFile); From 5d5e44e7052d8723e1224ceaca2694c637ac8eae Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 15:20:50 +0700 Subject: [PATCH 05/11] Update doc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c2e5dc4..3267db8 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ Will do the same thing. | `failOnWarnings` | no | `true` or `false` | Fail the job when warnings are found | | `dryRun` | no | `true` or `false` | Running CodeCoach without reporting to VCS | | `suppressRules` | no | `rule-group-1/.*` `rule-id-1` `rule-id-2` | Rule IDs that CodeCoach will still comment but no longer treated as errors or warnings | +| `outputFormat` | no | `default`, `gitlab` | Output file format | ##### `--buildLogFile` or `-f` From c2c9ccffad3f8b745f095cd8836c87f8cdbff5cc Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 15:23:50 +0700 Subject: [PATCH 06/11] Add ref --- src/OutputFormatter/outputFormatter.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/outputFormatter.ts index c3b30a5..88c8e36 100644 --- a/src/OutputFormatter/outputFormatter.ts +++ b/src/OutputFormatter/outputFormatter.ts @@ -5,6 +5,7 @@ export type OutputFormatter = (logs: LogType[]) => string; type GitLabSeverity = 'info' | 'minor' | 'major' | 'critical' | 'blocker'; +// https://docs.gitlab.com/ee/ci/testing/code_quality.html#implement-a-custom-tool type GitLabOutputFormat = { description: string; check_name: string; From e72ee1b55725735ed63d6c699df1def42334bf8e Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 15:37:26 +0700 Subject: [PATCH 07/11] Rename log -> lintItem --- sample/eslint/eslint-output.json | 2 +- sample/git-diff/multi.diff | 2 +- src/AnalyzerBot/@interfaces/IAnalyzerBot.ts | 6 +++--- src/AnalyzerBot/AnalyzerBot.ts | 6 +++--- src/AnalyzerBot/utils/commentUtil.spec.ts | 4 ++-- src/AnalyzerBot/utils/commentUtil.ts | 14 +++++++------- src/AnalyzerBot/utils/filter.util.ts | 6 +++--- src/Config/Config.spec.ts | 6 +++--- src/OutputFormatter/outputFormatter.spec.ts | 5 +++++ src/OutputFormatter/outputFormatter.ts | 8 ++++---- src/Parser/@interfaces/parser.interface.ts | 4 ++-- src/Parser/@types/index.ts | 2 +- src/Parser/@types/log.type.ts | 2 +- src/Parser/AndroidLintStyleParser.ts | 14 +++++++------- src/Parser/DartLintParser.ts | 16 ++++++++-------- src/Parser/DotnetBuildParser.spec.ts | 8 ++++---- src/Parser/DotnetBuildParser.ts | 8 ++++---- src/Parser/ESLintParser.ts | 8 ++++---- src/Parser/JscpdParser.ts | 8 ++++---- src/Parser/MSBuildParser.ts | 8 ++++---- src/Parser/ScalaStyleParser.ts | 10 +++++----- src/Parser/SwiftLintParser.ts | 10 +++++----- src/Parser/TSLintParser.ts | 10 +++++----- src/Parser/index.ts | 2 +- src/Provider/@interfaces/VCS.ts | 4 ++-- src/Provider/CommonVCS/VCSEngine.ts | 6 +++--- src/Provider/mockData.ts | 10 +++++----- src/app.ts | 8 ++++---- 28 files changed, 101 insertions(+), 96 deletions(-) create mode 100644 src/OutputFormatter/outputFormatter.spec.ts diff --git a/sample/eslint/eslint-output.json b/sample/eslint/eslint-output.json index c869fe2..fa67d7b 100644 --- a/sample/eslint/eslint-output.json +++ b/sample/eslint/eslint-output.json @@ -35,7 +35,7 @@ "warningCount": 1, "fixableErrorCount": 0, "fixableWarningCount": 0, - "source": "#!/usr/bin/env node\r\n\r\nimport { Config, ProjectType } from './Config';\r\nimport { File } from './File';\r\nimport { Log } from './Logger';\r\nimport { CSharpParser, LogType, Parser, TSLintParser } from './Parser';\r\nimport { GitHub, GitHubPRService, VCS } from './Provider';\r\n\r\nclass App {\r\n private readonly parser: Parser;\r\n private readonly vcs: VCS\r\n\r\n constructor() {\r\n this.parser = App.setProjectType(Config.app.projectType);\r\n const githubPRService = new GitHubPRService(\r\n Config.provider.token,\r\n Config.provider.repoUrl,\r\n Config.provider.prId,\r\n );\r\n this.vcs = new GitHub(githubPRService);\r\n }\r\n\r\n async start() {\r\n const logs = await this.parseBuildData(Config.app.buildLogFiles);\r\n Log.info('Build data parsing completed');\r\n\r\n await this.vcs.report(logs);\r\n Log.info('Report to VCS completed');\r\n\r\n await App.writeLogToFile(logs);\r\n Log.info('Write output completed');\r\n }\r\n\r\n private static setProjectType(type: ProjectType): Parser {\r\n switch (type) {\r\n case ProjectType.csharp:\r\n return new CSharpParser(Config.app.cwd);\r\n case ProjectType.tslint:\r\n return new TSLintParser(Config.app.cwd);\r\n }\r\n }\r\n\r\n private async parseBuildData(files: string[]): Promise {\r\n const parserTasks = files.map(async (file) => {\r\n const content = await File.readFileHelper(file);\r\n this.parser.withContent(content);\r\n });\r\n\r\n await Promise.all(parserTasks);\r\n\r\n return this.parser.getLogs();\r\n }\r\n\r\n private static async writeLogToFile(logs: LogType[]): Promise {\r\n await File.writeFileHelper(Config.app.logFilePath, JSON.stringify(logs, null, 2));\r\n }\r\n}\r\n\r\nnew App(", + "source": "#!/usr/bin/env node\r\n\r\nimport { Config, ProjectType } from './Config';\r\nimport { File } from './File';\r\nimport { Log } from './Logger';\r\nimport { CSharpParser, LintItem, Parser, TSLintParser } from './Parser';\r\nimport { GitHub, GitHubPRService, VCS } from './Provider';\r\n\r\nclass App {\r\n private readonly parser: Parser;\r\n private readonly vcs: VCS\r\n\r\n constructor() {\r\n this.parser = App.setProjectType(Config.app.projectType);\r\n const githubPRService = new GitHubPRService(\r\n Config.provider.token,\r\n Config.provider.repoUrl,\r\n Config.provider.prId,\r\n );\r\n this.vcs = new GitHub(githubPRService);\r\n }\r\n\r\n async start() {\r\n const logs = await this.parseBuildData(Config.app.buildLogFiles);\r\n Log.info('Build data parsing completed');\r\n\r\n await this.vcs.report(logs);\r\n Log.info('Report to VCS completed');\r\n\r\n await App.writeLogToFile(logs);\r\n Log.info('Write output completed');\r\n }\r\n\r\n private static setProjectType(type: ProjectType): Parser {\r\n switch (type) {\r\n case ProjectType.csharp:\r\n return new CSharpParser(Config.app.cwd);\r\n case ProjectType.tslint:\r\n return new TSLintParser(Config.app.cwd);\r\n }\r\n }\r\n\r\n private async parseBuildData(files: string[]): Promise {\r\n const parserTasks = files.map(async (file) => {\r\n const content = await File.readFileHelper(file);\r\n this.parser.withContent(content);\r\n });\r\n\r\n await Promise.all(parserTasks);\r\n\r\n return this.parser.getLogs();\r\n }\r\n\r\n private static async writeLogToFile(items: LintItem[]): Promise {\r\n await File.writeFileHelper(Config.app.logFilePath, JSON.stringify(logs, null, 2));\r\n }\r\n}\r\n\r\nnew App(", "usedDeprecatedRules": [] } ] \ No newline at end of file diff --git a/sample/git-diff/multi.diff b/sample/git-diff/multi.diff index 80ce7ad..eb8a54c 100644 --- a/sample/git-diff/multi.diff +++ b/sample/git-diff/multi.diff @@ -4,7 +4,7 @@ import { getRelativePath } from '../Provider/utils/path.util'; -import { LogSeverity } from './@enums/log.severity.enum'; import { Parser } from './@interfaces/parser.interface'; - import { LogType } from './@types'; + import { LintItem } from './@types'; +import { mapSeverity } from './utils/dotnetSeverityMap'; import { splitByLine } from './utils/lineBreak.util'; diff --git a/src/AnalyzerBot/@interfaces/IAnalyzerBot.ts b/src/AnalyzerBot/@interfaces/IAnalyzerBot.ts index dc9e37c..1555d72 100644 --- a/src/AnalyzerBot/@interfaces/IAnalyzerBot.ts +++ b/src/AnalyzerBot/@interfaces/IAnalyzerBot.ts @@ -1,14 +1,14 @@ -import { LogType } from '../../Parser'; +import { LintItem } from '../../Parser'; import { Comment } from '../@types/CommentTypes'; import { Diff } from '../../Git/@types/PatchTypes'; export interface IAnalyzerBot { - touchedFileLog: LogType[]; + touchedFileItem: LintItem[]; comments: Comment[]; nError: number; nWarning: number; - analyze(logs: LogType[], touchedDiff: Diff[]): void; + analyze(items: LintItem[], touchedDiff: Diff[]): void; shouldGenerateOverview(): boolean; diff --git a/src/AnalyzerBot/AnalyzerBot.ts b/src/AnalyzerBot/AnalyzerBot.ts index b6bbd43..e2851ed 100644 --- a/src/AnalyzerBot/AnalyzerBot.ts +++ b/src/AnalyzerBot/AnalyzerBot.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LogType } from '../Parser'; +import { LogSeverity, LintItem } from '../Parser'; import { Diff } from '../Git/@types/PatchTypes'; import { onlyIn, onlySeverity } from './utils/filter.util'; import { groupComments } from './utils/commentUtil'; @@ -8,14 +8,14 @@ import { Comment } from './@types/CommentTypes'; import { IAnalyzerBot } from './@interfaces/IAnalyzerBot'; export class AnalyzerBot implements IAnalyzerBot { - touchedFileLog: LogType[]; + touchedFileItem: LintItem[]; comments: Comment[]; nError: number; nWarning: number; constructor(private readonly config: AnalyzerBotConfig) {} - analyze(logs: LogType[], touchedDiff: Diff[]): void { + analyze(items: LintItem[], touchedDiff: Diff[]): void { this.touchedFileLog = logs .filter(onlySeverity(LogSeverity.error, LogSeverity.warning)) .filter(onlyIn(touchedDiff)); diff --git a/src/AnalyzerBot/utils/commentUtil.spec.ts b/src/AnalyzerBot/utils/commentUtil.spec.ts index 049ca0d..1b2c5b7 100644 --- a/src/AnalyzerBot/utils/commentUtil.spec.ts +++ b/src/AnalyzerBot/utils/commentUtil.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LogType } from '../../Parser'; +import { LogSeverity, LintItem } from '../../Parser'; import { file1TouchLine, file2TouchLine, @@ -9,7 +9,7 @@ import { import { groupComments } from './commentUtil'; describe('groupComments', () => { - const logs: LogType[] = [touchFileError, touchFileWarning]; + const items: LintItem[] = [touchFileError, touchFileWarning]; it('returns comments based on lint logs', () => { const comments = groupComments(logs, []); diff --git a/src/AnalyzerBot/utils/commentUtil.ts b/src/AnalyzerBot/utils/commentUtil.ts index 0815298..12fea66 100644 --- a/src/AnalyzerBot/utils/commentUtil.ts +++ b/src/AnalyzerBot/utils/commentUtil.ts @@ -1,8 +1,8 @@ -import { LogSeverity, LogType } from '../../Parser'; +import { LogSeverity, LintItem } from '../../Parser'; import { Comment, CommentStructure } from '../@types/CommentTypes'; import { MessageUtil } from './message.util'; -export function groupComments(logs: LogType[], suppressRules: Array): Comment[] { +export function groupComments(items: LintItem[], suppressRules: Array): Comment[] { const commentMap = logs.reduce((state: CommentStructure, log) => { const { source: file, line, nLines } = log; @@ -35,7 +35,7 @@ function getOrInitComment( ); } -function buildText(currentComment: Comment, log: LogType, isSuppressed: boolean): string { +function buildText(currentComment: Comment, item: LintItem, isSuppressed: boolean): string { const { severity, msg } = log; const { text: currentText } = currentComment; const msgWithSuppression = isSuppressed ? `(SUPPRESSED) ${msg}` : msg; @@ -45,7 +45,7 @@ function buildText(currentComment: Comment, log: LogType, isSuppressed: boolean) function calculateErrors( currentComment: Comment, - log: LogType, + item: LintItem, isSuppressed: boolean, ): number { if (isSuppressed) return currentComment.errors; @@ -55,7 +55,7 @@ function calculateErrors( function calculateWarnings( currentComment: Comment, - log: LogType, + item: LintItem, isSuppressed: boolean, ): number { if (isSuppressed) return currentComment.warnings; @@ -67,14 +67,14 @@ function calculateSuppresses(currentComment: Comment, isSuppressed: boolean): nu return currentComment.suppresses + (isSuppressed ? 1 : 0); } -function shouldBeSuppressed(log: LogType, suppressRules: Array): boolean { +function shouldBeSuppressed(item: LintItem, suppressRules: Array): boolean { const suppressRegexps: Array = suppressRules.map((rule) => new RegExp(rule)); return suppressRegexps.some((regexp) => regexp.test(log.ruleId)); } function updateComment( currentComment: Comment, - log: LogType, + item: LintItem, suppressRules: Array, ): Comment { const isSuppressed = shouldBeSuppressed(log, suppressRules); diff --git a/src/AnalyzerBot/utils/filter.util.ts b/src/AnalyzerBot/utils/filter.util.ts index a8432d0..1ff3076 100644 --- a/src/AnalyzerBot/utils/filter.util.ts +++ b/src/AnalyzerBot/utils/filter.util.ts @@ -1,11 +1,11 @@ -import { LogSeverity, LogType } from '../../Parser'; +import { LogSeverity, LintItem } from '../../Parser'; import { Diff } from '../../Git/@types/PatchTypes'; -export const onlyIn = (diffs: Diff[]) => (log: LogType): boolean => +export const onlyIn = (diffs: Diff[]) => (item: LintItem): boolean => diffs.some( (d) => d.file === log.source && d.patch.some((p) => !log.line || (log.line >= p.from && log.line <= p.to)), ); -export const onlySeverity = (...severities: LogSeverity[]) => (log: LogType): boolean => +export const onlySeverity = (...severities: LogSeverity[]) => (item: LintItem): boolean => severities.includes(log.severity); diff --git a/src/Config/Config.spec.ts b/src/Config/Config.spec.ts index d0c6e3e..5d8e59f 100644 --- a/src/Config/Config.spec.ts +++ b/src/Config/Config.spec.ts @@ -9,10 +9,10 @@ const mockGitLabProjectId = 1234; const mockGitLabMrIid = 69; const mockGitLabToken = 'mockGitLabToken'; -const mockLogType = 'dotnetbuild'; +const mockLintItem = 'dotnetbuild'; const mockLogFile = './sample/dotnetbuild/build.content'; const mockLogCwd = '/repo/src'; -const mockBuildLogFile = `${mockLogType};${mockLogFile};${mockLogCwd}`; +const mockBuildLogFile = `${mockLintItem};${mockLogFile};${mockLogCwd}`; const mockOutput = './tmp/out.json'; const GITHUB_ENV_ARGS = [ @@ -64,7 +64,7 @@ describe('Config parsing Test', () => { const validateBuildLog = (buildLog: BuildLogFile[]) => { expect(buildLog).toHaveLength(1); - expect(buildLog[0].type).toBe(mockLogType); + expect(buildLog[0].type).toBe(mockLintItem); expect(buildLog[0].path).toBe(mockLogFile); expect(buildLog[0].cwd).toBe(mockLogCwd); }; diff --git a/src/OutputFormatter/outputFormatter.spec.ts b/src/OutputFormatter/outputFormatter.spec.ts new file mode 100644 index 0000000..3523258 --- /dev/null +++ b/src/OutputFormatter/outputFormatter.spec.ts @@ -0,0 +1,5 @@ +import { LintItem } from "../Parser" + +describe('OutputFormatter', () => { + it('should format logs to GitLab format', () => { + const items: LintItem[] = [ \ No newline at end of file diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/outputFormatter.ts index 88c8e36..acedb81 100644 --- a/src/OutputFormatter/outputFormatter.ts +++ b/src/OutputFormatter/outputFormatter.ts @@ -1,7 +1,7 @@ -import { LogSeverity, LogType } from '../Parser'; +import { LogSeverity, LintItem } from '../Parser'; import { createHash } from 'crypto'; -export type OutputFormatter = (logs: LogType[]) => string; +export type OutputFormatter = (items: LintItem[]) => string; type GitLabSeverity = 'info' | 'minor' | 'major' | 'critical' | 'blocker'; @@ -31,7 +31,7 @@ const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { } }; -export const gitLab: OutputFormatter = (logs: LogType[]) => { +export const gitLab: OutputFormatter = (items: LintItem[]) => { const gitlabReport = logs.map((log) => { const fingerprint = createHash('sha256'); fingerprint.update(`${log.ruleId}${log.source}${log.line}${log.lineOffset}`); @@ -54,6 +54,6 @@ export const gitLab: OutputFormatter = (logs: LogType[]) => { return JSON.stringify(gitlabReport, null, 2); }; -export const defaultFormatter: OutputFormatter = (logs: LogType[]) => { +export const defaultFormatter: OutputFormatter = (items: LintItem[]) => { return JSON.stringify(logs, null, 2); }; diff --git a/src/Parser/@interfaces/parser.interface.ts b/src/Parser/@interfaces/parser.interface.ts index 8b27cd3..4d669c2 100644 --- a/src/Parser/@interfaces/parser.interface.ts +++ b/src/Parser/@interfaces/parser.interface.ts @@ -1,7 +1,7 @@ -import { LogType } from '..'; +import { LintItem } from '..'; export abstract class Parser { constructor(protected readonly cwd: string) {} - abstract parse(content: string): LogType[]; + abstract parse(content: string): LintItem[]; } diff --git a/src/Parser/@types/index.ts b/src/Parser/@types/index.ts index 393a2c8..e7252bc 100644 --- a/src/Parser/@types/index.ts +++ b/src/Parser/@types/index.ts @@ -2,4 +2,4 @@ export { ESLintIssue } from './ESLintIssue'; export { ESLintLog } from './ESLintLog'; export { TSLintLog } from './TSLintLog'; export { TSLintLogPosition } from './TSLintLogPosition'; -export { LogType } from './log.type'; +export { LintItem } from './log.type'; diff --git a/src/Parser/@types/log.type.ts b/src/Parser/@types/log.type.ts index 9749ee4..b3add6c 100644 --- a/src/Parser/@types/log.type.ts +++ b/src/Parser/@types/log.type.ts @@ -1,7 +1,7 @@ import { LogSeverity } from '..'; import { ProjectType } from '../../Config'; -export type LogType = { +export type LintItem = { ruleId: string; log: string; msg: string; diff --git a/src/Parser/AndroidLintStyleParser.ts b/src/Parser/AndroidLintStyleParser.ts index fbc2f37..d4ead14 100644 --- a/src/Parser/AndroidLintStyleParser.ts +++ b/src/Parser/AndroidLintStyleParser.ts @@ -1,7 +1,7 @@ import { Log } from '../Logger'; import { LogSeverity } from './@enums/log.severity.enum'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { xml2js } from 'xml-js'; import { AndroidLintStyleIssue } from './@types/AndroidLintStyleIssue'; import { AndroidLintStyleLog } from './@types/AndroidLintStyleLog'; @@ -9,14 +9,14 @@ import { AndroidLintStyleLocation } from './@types/AndroidLintStyleLocation'; import { ProjectType } from '../Config/@enums'; export class AndroidLintStyleParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; return ( - AndroidLintStyleParser.xmlToLog(content).issues[0]?.issue?.flatMap( + AndroidLintStyleParser.xmltoLintItem(content).issues[0]?.issue?.flatMap( (issue: AndroidLintStyleIssue) => { - return AndroidLintStyleParser.toLog(issue, issue.location[0], this.cwd); + return AndroidLintStyleParser.toLintItem(issue, issue.location[0], this.cwd); }, ) ?? [] ); @@ -26,11 +26,11 @@ export class AndroidLintStyleParser extends Parser { } } - private static toLog( + private static toLintItem( issue: AndroidLintStyleIssue, location: AndroidLintStyleLocation, cwd: string, - ): LogType { + ): LintItem { return { ruleId: issue._attributes.id, log: issue._attributes.errorLine1?.trim(), @@ -59,7 +59,7 @@ export class AndroidLintStyleParser extends Parser { } } - private static xmlToLog(xmlContent: string): AndroidLintStyleLog { + private static xmltoLintItem(xmlContent: string): AndroidLintStyleLog { return xml2js(xmlContent, convertOption) as AndroidLintStyleLog; } } diff --git a/src/Parser/DartLintParser.ts b/src/Parser/DartLintParser.ts index a99ab78..c116185 100644 --- a/src/Parser/DartLintParser.ts +++ b/src/Parser/DartLintParser.ts @@ -1,22 +1,22 @@ import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { LogSeverity } from './@enums/log.severity.enum'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; export class DartLintParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { return splitByLine(content) - .map((line: string) => DartLintParser.lineToLog(line)) - .filter((f: LogType) => f != DartLintParser.emptyLog); + .map((line: string) => DartLintParser.linetoLintItem(line)) + .filter((f: LintItem) => f != DartLintParser.emptyLog); } - private static lineToLog(line: string): LogType { + private static linetoLintItem(line: string): LintItem { const lineMatch = line.match(/^(.*) • (.*) • (.*):(\d+):(\d+) • (.*)/); - return lineMatch ? DartLintParser.lineMatchToLog(lineMatch) : DartLintParser.emptyLog; + return lineMatch ? DartLintParser.lineMatchtoLintItem(lineMatch) : DartLintParser.emptyLog; } - private static lineMatchToLog(lineMatch: RegExpMatchArray): LogType { + private static lineMatchtoLintItem(lineMatch: RegExpMatchArray): LintItem { const [, severityText, message, source, line, offset, log] = lineMatch; return { ruleId: log, @@ -44,7 +44,7 @@ export class DartLintParser extends Parser { } } - private static emptyLog: LogType = { + private static emptyItem: LintItem = { ruleId: '', log: '', msg: '', diff --git a/src/Parser/DotnetBuildParser.spec.ts b/src/Parser/DotnetBuildParser.spec.ts index df0ad51..8a6beca 100644 --- a/src/Parser/DotnetBuildParser.spec.ts +++ b/src/Parser/DotnetBuildParser.spec.ts @@ -1,5 +1,5 @@ import { LogSeverity } from './@enums/log.severity.enum'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { DotnetBuildParser } from './DotnetBuildParser'; describe('DotnetBuildParser tests', () => { @@ -25,7 +25,7 @@ describe('DotnetBuildParser tests', () => { log: logWithSource, valid: true, type: 'dotnetbuild', - } as LogType); + } as LintItem); }); it('Should parse log without source path correctly and flag as invalid and use csproj as source', () => { @@ -41,7 +41,7 @@ describe('DotnetBuildParser tests', () => { log: logWithNoSource, valid: false, type: 'dotnetbuild', - } as LogType); + } as LintItem); }); it('Should parse log unrelated source path correctly and flag as invalid and use csproj as source', () => { @@ -57,7 +57,7 @@ describe('DotnetBuildParser tests', () => { log: logWithUnrelatedSource, valid: false, type: 'dotnetbuild', - } as LogType); + } as LintItem); }); it('Should do nothing if put empty string', () => { diff --git a/src/Parser/DotnetBuildParser.ts b/src/Parser/DotnetBuildParser.ts index a4cb826..16ccc89 100644 --- a/src/Parser/DotnetBuildParser.ts +++ b/src/Parser/DotnetBuildParser.ts @@ -4,19 +4,19 @@ import slash from 'slash'; import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { mapSeverity } from './utils/dotnetSeverityMap'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; export class DotnetBuildParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { return splitByLine(content) - .map((log) => this.toLog(log)) + .map((log) => this.toLintItem(log)) .filter((log) => log); } - private toLog(log: string): LogType { + private toLintItem(log: string): LintItem { const structureMatch = log.match( /(?:[\d:>]+)?([^ ()]+)(?:\((\d+),(\d+)\))? *: *(\w+) *(\w+) *: *([^\[]+)(?:\[(.+)])?$/, ); diff --git a/src/Parser/ESLintParser.ts b/src/Parser/ESLintParser.ts index ce65607..4f27792 100644 --- a/src/Parser/ESLintParser.ts +++ b/src/Parser/ESLintParser.ts @@ -3,10 +3,10 @@ import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { LogSeverity } from './@enums/log.severity.enum'; import { Parser } from './@interfaces/parser.interface'; -import { ESLintIssue, ESLintLog, LogType } from './@types'; +import { ESLintIssue, ESLintLog, LintItem } from './@types'; export class ESLintParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; @@ -15,7 +15,7 @@ export class ESLintParser extends Parser { .filter((log) => log.messages.length !== 0) .flatMap((log) => { const source = getRelativePath(this.cwd, log.filePath); - return log.messages.map((msg) => ESLintParser.toLog(msg, source)); + return log.messages.map((msg) => ESLintParser.toLintItem(msg, source)); }); } catch (err) { Log.warn('ESLint Parser: parse with content via JSON error', content); @@ -23,7 +23,7 @@ export class ESLintParser extends Parser { } } - private static toLog(log: ESLintIssue, source: string | null): LogType { + private static toLintItem(log: ESLintIssue, source: string | null): LintItem { return { ruleId: log.ruleId ?? '', log: JSON.stringify(log), diff --git a/src/Parser/JscpdParser.ts b/src/Parser/JscpdParser.ts index caf5d41..9f4e1be 100644 --- a/src/Parser/JscpdParser.ts +++ b/src/Parser/JscpdParser.ts @@ -1,6 +1,6 @@ import { Log } from '../Logger'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { ProjectType } from '../Config/@enums'; import { JscpdLog } from './@types/JscpdLog'; import { LogSeverity } from './@enums/log.severity.enum'; @@ -8,19 +8,19 @@ import { LogSeverity } from './@enums/log.severity.enum'; export class JscpdParser extends Parser { private readonly ruleId = 'jscpd'; - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; const logsJson = JSON.parse(content) as JscpdLog; - return logsJson.duplicates.flatMap((el) => this.toLog(el)); + return logsJson.duplicates.flatMap((el) => this.toLintItem(el)); } catch (err) { Log.warn('jscpd Parser: parse with content via JSON error', content); throw err; } } - private toLog(log: JscpdLog['duplicates'][number]): LogType[] { + private toLintItem(log: JscpdLog['duplicates'][number]): LintItem[] { return [ { ruleId: this.ruleId, diff --git a/src/Parser/MSBuildParser.ts b/src/Parser/MSBuildParser.ts index 990cec5..3e40783 100644 --- a/src/Parser/MSBuildParser.ts +++ b/src/Parser/MSBuildParser.ts @@ -4,19 +4,19 @@ import slash from 'slash'; import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { mapSeverity } from './utils/dotnetSeverityMap'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; export class MSBuildParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { return splitByLine(content) - .map((log) => this.toLog(log)) + .map((log) => this.toLintItem(log)) .filter((log) => log); } - private toLog(log: string): LogType { + private toLintItem(log: string): LintItem { const structureMatch = log.match( /^([\\/\w\d.:_ ()-]+)(?:\((\d+),(\d+)\))? ?: (\w+) (\w+): ([^\[]+)(?:\[(.+)])?$/, ); diff --git a/src/Parser/ScalaStyleParser.ts b/src/Parser/ScalaStyleParser.ts index 38b6b57..cbf62f7 100644 --- a/src/Parser/ScalaStyleParser.ts +++ b/src/Parser/ScalaStyleParser.ts @@ -2,14 +2,14 @@ import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { LogSeverity } from './@enums/log.severity.enum'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { xml2js } from 'xml-js'; import { ScalaStyleLog } from './@types/ScalaStyleLog'; import { ScalaStyleError } from './@types/ScalaStyleError'; import { ProjectType } from '../Config/@enums'; export class ScalaStyleParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; @@ -31,7 +31,7 @@ export class ScalaStyleParser extends Parser { const source = getRelativePath(this.cwd, f._attributes.name); return f.error.map((log) => - ScalaStyleParser.toLog(log, source, rawError[rawIndex++]), + ScalaStyleParser.toLintItem(log, source, rawError[rawIndex++]), ); }) ?? [] ); @@ -41,11 +41,11 @@ export class ScalaStyleParser extends Parser { } } - private static toLog( + private static toLintItem( log: ScalaStyleError, source: string | null, raw: string | null, - ): LogType { + ): LintItem { return { ruleId: log._attributes.source ?? '', log: raw ?? '', diff --git a/src/Parser/SwiftLintParser.ts b/src/Parser/SwiftLintParser.ts index f356cc4..a72454a 100644 --- a/src/Parser/SwiftLintParser.ts +++ b/src/Parser/SwiftLintParser.ts @@ -1,26 +1,26 @@ import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { Parser } from './@interfaces/parser.interface'; -import { LogType } from './@types'; +import { LintItem } from './@types'; import { ProjectType } from '../Config/@enums'; import { SwiftLintLog } from './@types/SwiftLintLog'; import { LogSeverity } from './@enums/log.severity.enum'; export class SwiftLintParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; const logsJson = JSON.parse(content) as SwiftLintLog[]; - return logsJson.map((el) => this.toLog(el)); + return logsJson.map((el) => this.toLintItem(el)); } catch (err) { Log.warn('SwiftLint Parser: parse with content via JSON error', content); throw err; } } - private toLog(log: SwiftLintLog): LogType { - const parsed: LogType = { + private toLintItem(log: SwiftLintLog): LintItem { + const parsed: LintItem = { ruleId: log.rule_id, log: JSON.stringify(log), line: log.line ?? 0, diff --git a/src/Parser/TSLintParser.ts b/src/Parser/TSLintParser.ts index 2304908..cefd5b7 100644 --- a/src/Parser/TSLintParser.ts +++ b/src/Parser/TSLintParser.ts @@ -3,23 +3,23 @@ import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; import { LogSeverity } from './@enums/log.severity.enum'; import { Parser } from './@interfaces/parser.interface'; -import { LogType, TSLintLog } from './@types'; +import { LintItem, TSLintLog } from './@types'; export class TSLintParser extends Parser { - parse(content: string): LogType[] { + parse(content: string): LintItem[] { try { if (!content) return []; const logsJson = JSON.parse(content) as TSLintLog[]; - return logsJson.map((el) => this.toLog(el)); + return logsJson.map((el) => this.toLintItem(el)); } catch (err) { Log.warn('TSLint Parser: parse with content via JSON error', content); throw err; } } - private toLog(log: TSLintLog): LogType { - const parsed: LogType = { + private toLintItem(log: TSLintLog): LintItem { + const parsed: LintItem = { ruleId: log.ruleName, log: JSON.stringify(log), line: log.startPosition.line + 1, diff --git a/src/Parser/index.ts b/src/Parser/index.ts index c54362c..be2c767 100644 --- a/src/Parser/index.ts +++ b/src/Parser/index.ts @@ -8,5 +8,5 @@ export { DartLintParser } from './DartLintParser'; export { SwiftLintParser } from './SwiftLintParser'; export { JscpdParser } from './JscpdParser'; export { Parser } from './@interfaces/parser.interface'; -export { LogType } from './@types'; +export { LintItem } from './@types'; export { LogSeverity } from './@enums/log.severity.enum'; diff --git a/src/Provider/@interfaces/VCS.ts b/src/Provider/@interfaces/VCS.ts index 42c8d00..a7233bb 100644 --- a/src/Provider/@interfaces/VCS.ts +++ b/src/Provider/@interfaces/VCS.ts @@ -1,6 +1,6 @@ -import { LogType } from '../../Parser'; +import { LintItem } from '../../Parser'; export interface VCS { // returns boolean indicating process return code. true = zero (pass), false = non-zero (failure) - report(logs: LogType[]): Promise; + report(items: LintItem[]): Promise; } diff --git a/src/Provider/CommonVCS/VCSEngine.ts b/src/Provider/CommonVCS/VCSEngine.ts index fc7d466..7009a02 100644 --- a/src/Provider/CommonVCS/VCSEngine.ts +++ b/src/Provider/CommonVCS/VCSEngine.ts @@ -1,5 +1,5 @@ import { VCS } from '../@interfaces/VCS'; -import { LogType } from '../../Parser'; +import { LintItem } from '../../Parser'; import { Log } from '../../Logger'; import { Comment } from '../../AnalyzerBot/@types/CommentTypes'; import { VCSEngineConfig } from '../@interfaces/VCSEngineConfig'; @@ -13,7 +13,7 @@ export class VCSEngine implements VCS { private readonly adapter: VCSAdapter, ) {} - async report(logs: LogType[]): Promise { + async report(items: LintItem[]): Promise { try { await this.adapter.init(); await this.setup(logs); @@ -35,7 +35,7 @@ export class VCSEngine implements VCS { return await this.adapter.wrapUp(this.analyzerBot); } - private async setup(logs: LogType[]) { + private async setup(items: LintItem[]) { const touchedDiff = await this.adapter.diff(); this.analyzerBot.analyze(logs, touchedDiff); diff --git a/src/Provider/mockData.ts b/src/Provider/mockData.ts index 4539375..5f40e8e 100644 --- a/src/Provider/mockData.ts +++ b/src/Provider/mockData.ts @@ -1,11 +1,11 @@ import { ProjectType } from '../Config/@enums'; -import { LogSeverity, LogType } from '../Parser'; +import { LogSeverity, LintItem } from '../Parser'; export const mockTouchFile = 'file1.cs'; export const file1TouchLine = 11; export const file2TouchLine = 33; -export const touchFileError: LogType = { +export const touchFileError: LintItem = { ruleId: '', log: '', msg: 'msg1', @@ -16,7 +16,7 @@ export const touchFileError: LogType = { valid: true, type: ProjectType.eslint, }; -export const touchFileWarning: LogType = { +export const touchFileWarning: LintItem = { ruleId: '', log: '', msg: 'msg3', @@ -28,7 +28,7 @@ export const touchFileWarning: LogType = { valid: true, type: ProjectType.eslint, }; -export const untouchedError: LogType = { +export const untouchedError: LintItem = { ruleId: '', log: '', msg: 'msg2', @@ -39,7 +39,7 @@ export const untouchedError: LogType = { valid: true, type: ProjectType.eslint, }; -export const untouchedWarning: LogType = { +export const untouchedWarning: LintItem = { ruleId: '', log: '', msg: 'msg4', diff --git a/src/app.ts b/src/app.ts index d294cd4..d7240b8 100644 --- a/src/app.ts +++ b/src/app.ts @@ -7,7 +7,7 @@ import { AndroidLintStyleParser, DotnetBuildParser, ESLintParser, - LogType, + LintItem, MSBuildParser, Parser, ScalaStyleParser, @@ -103,7 +103,7 @@ class App { } } - private async parseBuildData(files: BuildLogFile[]): Promise { + private async parseBuildData(files: BuildLogFile[]): Promise { const logsTasks = files.map(async ({ type, path, cwd }) => { Log.debug('Parsing', { type, path, cwd }); const content = await File.readFileHelper(path); @@ -114,7 +114,7 @@ class App { return (await Promise.all(logsTasks)).flatMap((x) => x); } - private async reportToVcs(logs: LogType[]): Promise { + private async reportToVcs(items: LintItem[]): Promise { if (!this.vcs) { Log.info('Dry run enabled, skip reporting'); return true; @@ -130,7 +130,7 @@ class App { } } - private async writeOutputFile(logs: LogType[]): Promise { + private async writeOutputFile(items: LintItem[]): Promise { try { await File.writeFileHelper(configs.output, this.outputFormatter(logs)); Log.info('Write output completed'); From 8139fcb86e53a1a2eb51e70def7fab7c770808d0 Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 16:03:33 +0700 Subject: [PATCH 08/11] Add test yay --- sample/git-diff/deletesection.diff | 4 +- src/AnalyzerBot/AnalyzerBot.spec.ts | 4 +- src/AnalyzerBot/AnalyzerBot.ts | 4 +- src/AnalyzerBot/utils/commentUtil.spec.ts | 12 +++--- src/AnalyzerBot/utils/commentUtil.ts | 33 +++++++++------ src/AnalyzerBot/utils/filter.util.ts | 6 +-- src/OutputFormatter/OutputFormatter.spec.ts | 40 +++++++++++++++++++ ...{outputFormatter.ts => OutputFormatter.ts} | 18 ++++----- src/OutputFormatter/outputFormatter.spec.ts | 5 --- src/Parser/DartLintParser.ts | 6 ++- src/Provider/CommonVCS/VCSEngine.spec.ts | 2 +- src/Provider/CommonVCS/VCSEngine.ts | 4 +- src/app.ts | 10 ++--- 13 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 src/OutputFormatter/OutputFormatter.spec.ts rename src/OutputFormatter/{outputFormatter.ts => OutputFormatter.ts} (72%) delete mode 100644 src/OutputFormatter/outputFormatter.spec.ts diff --git a/sample/git-diff/deletesection.diff b/sample/git-diff/deletesection.diff index d912fd4..f03c2db 100644 --- a/sample/git-diff/deletesection.diff +++ b/sample/git-diff/deletesection.diff @@ -2,10 +2,10 @@ Log.debug(`Commit SHA ${commitId}`); Log.debug('Touched files', touchedFiles); -- Log.debug('Touched file log', touchedFileLog); +- Log.debug('Touched file log', touchedFileItem); const reviewResults = await Promise.all( - touchedFileLog.map((log) => this.toCreateReviewComment(commitId, log)), + touchedFileItem.map((log) => this.toCreateReviewComment(commitId, log)), @@ -83,10 +82,12 @@ ${issuesTableContent} log.source, log.line, diff --git a/src/AnalyzerBot/AnalyzerBot.spec.ts b/src/AnalyzerBot/AnalyzerBot.spec.ts index a291ba2..44ade9d 100644 --- a/src/AnalyzerBot/AnalyzerBot.spec.ts +++ b/src/AnalyzerBot/AnalyzerBot.spec.ts @@ -21,10 +21,10 @@ describe('AnalyzerBot', () => { const diff = [mockTouchDiff]; const analyzer = new AnalyzerBot(config); - describe('.touchedFileLog', () => { + describe('.touchedFileItem', () => { it('should return only logs that are in touchedDiff', () => { analyzer.analyze(logs, diff); - expect(analyzer.touchedFileLog).toEqual([touchFileError, touchFileWarning]); + expect(analyzer.touchedFileItem).toEqual([touchFileError, touchFileWarning]); }); }); diff --git a/src/AnalyzerBot/AnalyzerBot.ts b/src/AnalyzerBot/AnalyzerBot.ts index e2851ed..3dd54c7 100644 --- a/src/AnalyzerBot/AnalyzerBot.ts +++ b/src/AnalyzerBot/AnalyzerBot.ts @@ -16,10 +16,10 @@ export class AnalyzerBot implements IAnalyzerBot { constructor(private readonly config: AnalyzerBotConfig) {} analyze(items: LintItem[], touchedDiff: Diff[]): void { - this.touchedFileLog = logs + this.touchedFileItem = items .filter(onlySeverity(LogSeverity.error, LogSeverity.warning)) .filter(onlyIn(touchedDiff)); - this.comments = groupComments(this.touchedFileLog, this.config.suppressRules); + this.comments = groupComments(this.touchedFileItem, this.config.suppressRules); this.nError = this.comments.reduce((sum, comment) => sum + comment.errors, 0); this.nWarning = this.comments.reduce((sum, comment) => sum + comment.warnings, 0); } diff --git a/src/AnalyzerBot/utils/commentUtil.spec.ts b/src/AnalyzerBot/utils/commentUtil.spec.ts index 1b2c5b7..943ebdc 100644 --- a/src/AnalyzerBot/utils/commentUtil.spec.ts +++ b/src/AnalyzerBot/utils/commentUtil.spec.ts @@ -11,8 +11,8 @@ import { groupComments } from './commentUtil'; describe('groupComments', () => { const items: LintItem[] = [touchFileError, touchFileWarning]; - it('returns comments based on lint logs', () => { - const comments = groupComments(logs, []); + it('returns comments based on lint items', () => { + const comments = groupComments(items, []); expect(comments).toEqual([ { file: mockTouchFile, @@ -35,10 +35,10 @@ describe('groupComments', () => { ]); }); - it('group multiple logs on the same line to the same comment', () => { + it('group multiple items on the same line to the same comment', () => { const comments = groupComments( [ - ...logs, + ...items, { ...touchFileError, msg: 'additional warning', @@ -74,7 +74,7 @@ describe('groupComments', () => { it('suppress errors and warnings according to provided suppressRules', () => { const comments = groupComments( [ - ...logs, + ...items, { ...touchFileError, msg: 'additional warning', @@ -115,7 +115,7 @@ describe('groupComments', () => { it('support regexp in suppressRules', () => { const comments = groupComments( [ - ...logs, + ...items, { ...touchFileError, msg: 'additional warning', diff --git a/src/AnalyzerBot/utils/commentUtil.ts b/src/AnalyzerBot/utils/commentUtil.ts index 12fea66..709d8fd 100644 --- a/src/AnalyzerBot/utils/commentUtil.ts +++ b/src/AnalyzerBot/utils/commentUtil.ts @@ -2,14 +2,17 @@ import { LogSeverity, LintItem } from '../../Parser'; import { Comment, CommentStructure } from '../@types/CommentTypes'; import { MessageUtil } from './message.util'; -export function groupComments(items: LintItem[], suppressRules: Array): Comment[] { - const commentMap = logs.reduce((state: CommentStructure, log) => { - const { source: file, line, nLines } = log; +export function groupComments( + items: LintItem[], + suppressRules: Array, +): Comment[] { + const commentMap = items.reduce((state: CommentStructure, item) => { + const { source: file, line, nLines } = item; if (!line) return state; const currentComment = getOrInitComment(state, file, line, nLines); - const updatedComment = updateComment(currentComment, log, suppressRules); + const updatedComment = updateComment(currentComment, item, suppressRules); return updateCommentStructure(state, updatedComment); }, {}); @@ -35,8 +38,12 @@ function getOrInitComment( ); } -function buildText(currentComment: Comment, item: LintItem, isSuppressed: boolean): string { - const { severity, msg } = log; +function buildText( + currentComment: Comment, + item: LintItem, + isSuppressed: boolean, +): string { + const { severity, msg } = item; const { text: currentText } = currentComment; const msgWithSuppression = isSuppressed ? `(SUPPRESSED) ${msg}` : msg; const text = MessageUtil.createMessageWithEmoji(msgWithSuppression, severity); @@ -49,7 +56,7 @@ function calculateErrors( isSuppressed: boolean, ): number { if (isSuppressed) return currentComment.errors; - const { severity } = log; + const { severity } = item; return currentComment.errors + (severity === LogSeverity.error ? 1 : 0); } @@ -59,7 +66,7 @@ function calculateWarnings( isSuppressed: boolean, ): number { if (isSuppressed) return currentComment.warnings; - const { severity } = log; + const { severity } = item; return currentComment.warnings + (severity === LogSeverity.warning ? 1 : 0); } @@ -69,7 +76,7 @@ function calculateSuppresses(currentComment: Comment, isSuppressed: boolean): nu function shouldBeSuppressed(item: LintItem, suppressRules: Array): boolean { const suppressRegexps: Array = suppressRules.map((rule) => new RegExp(rule)); - return suppressRegexps.some((regexp) => regexp.test(log.ruleId)); + return suppressRegexps.some((regexp) => regexp.test(item.ruleId)); } function updateComment( @@ -77,11 +84,11 @@ function updateComment( item: LintItem, suppressRules: Array, ): Comment { - const isSuppressed = shouldBeSuppressed(log, suppressRules); + const isSuppressed = shouldBeSuppressed(item, suppressRules); return { - text: buildText(currentComment, log, isSuppressed), - errors: calculateErrors(currentComment, log, isSuppressed), - warnings: calculateWarnings(currentComment, log, isSuppressed), + text: buildText(currentComment, item, isSuppressed), + errors: calculateErrors(currentComment, item, isSuppressed), + warnings: calculateWarnings(currentComment, item, isSuppressed), suppresses: calculateSuppresses(currentComment, isSuppressed), file: currentComment.file, line: currentComment.line, diff --git a/src/AnalyzerBot/utils/filter.util.ts b/src/AnalyzerBot/utils/filter.util.ts index 1ff3076..e2be84f 100644 --- a/src/AnalyzerBot/utils/filter.util.ts +++ b/src/AnalyzerBot/utils/filter.util.ts @@ -4,8 +4,8 @@ import { Diff } from '../../Git/@types/PatchTypes'; export const onlyIn = (diffs: Diff[]) => (item: LintItem): boolean => diffs.some( (d) => - d.file === log.source && - d.patch.some((p) => !log.line || (log.line >= p.from && log.line <= p.to)), + d.file === item.source && + d.patch.some((p) => !item.line || (item.line >= p.from && item.line <= p.to)), ); export const onlySeverity = (...severities: LogSeverity[]) => (item: LintItem): boolean => - severities.includes(log.severity); + severities.includes(item.severity); diff --git a/src/OutputFormatter/OutputFormatter.spec.ts b/src/OutputFormatter/OutputFormatter.spec.ts new file mode 100644 index 0000000..25a0c2c --- /dev/null +++ b/src/OutputFormatter/OutputFormatter.spec.ts @@ -0,0 +1,40 @@ +import { ProjectType } from '../Config/@enums/projectType'; +import { LintItem, LogSeverity } from '../Parser'; +import { gitLabFormatter } from './OutputFormatter'; + +describe('OutputFormatter', () => { + it('should format logs to GitLab format', () => { + const items: LintItem[] = [ + { + ruleId: 'id', + log: 'log', + msg: 'msg', + severity: LogSeverity.error, + source: 'src', + line: 2, + lineOffset: 1, + nLines: 4, + valid: true, + type: ProjectType.dotnetbuild, + }, + ]; + + const result = gitLabFormatter(items); + expect(result).toBe( + `[ + { + "description": "msg", + "check_name": "id", + "fingerprint": "4ffb31820fd8eff505f387bacc348a2fa260714cb15da7b27de69846d3a51c3a", + "severity": "blocker", + "location": { + "path": "src", + "lines": { + "begin": 2 + } + } + } +]`, + ); + }); +}); diff --git a/src/OutputFormatter/outputFormatter.ts b/src/OutputFormatter/OutputFormatter.ts similarity index 72% rename from src/OutputFormatter/outputFormatter.ts rename to src/OutputFormatter/OutputFormatter.ts index acedb81..1f52923 100644 --- a/src/OutputFormatter/outputFormatter.ts +++ b/src/OutputFormatter/OutputFormatter.ts @@ -31,20 +31,20 @@ const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { } }; -export const gitLab: OutputFormatter = (items: LintItem[]) => { - const gitlabReport = logs.map((log) => { +export const gitLabFormatter: OutputFormatter = (items: LintItem[]) => { + const gitlabReport = items.map((item) => { const fingerprint = createHash('sha256'); - fingerprint.update(`${log.ruleId}${log.source}${log.line}${log.lineOffset}`); + fingerprint.update(`${item.ruleId}${item.source}${item.line}${item.lineOffset}`); const format: GitLabOutputFormat = { - description: log.msg, - check_name: log.ruleId, + description: item.msg, + check_name: item.ruleId, fingerprint: fingerprint.digest('hex'), - severity: mapGitLabSeverity(log.severity), + severity: mapGitLabSeverity(item.severity), location: { - path: log.source, + path: item.source, lines: { - begin: log.line ?? 1, + begin: item.line ?? 1, }, }, }; @@ -55,5 +55,5 @@ export const gitLab: OutputFormatter = (items: LintItem[]) => { }; export const defaultFormatter: OutputFormatter = (items: LintItem[]) => { - return JSON.stringify(logs, null, 2); + return JSON.stringify(items, null, 2); }; diff --git a/src/OutputFormatter/outputFormatter.spec.ts b/src/OutputFormatter/outputFormatter.spec.ts deleted file mode 100644 index 3523258..0000000 --- a/src/OutputFormatter/outputFormatter.spec.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { LintItem } from "../Parser" - -describe('OutputFormatter', () => { - it('should format logs to GitLab format', () => { - const items: LintItem[] = [ \ No newline at end of file diff --git a/src/Parser/DartLintParser.ts b/src/Parser/DartLintParser.ts index c116185..013c8dc 100644 --- a/src/Parser/DartLintParser.ts +++ b/src/Parser/DartLintParser.ts @@ -8,12 +8,14 @@ export class DartLintParser extends Parser { parse(content: string): LintItem[] { return splitByLine(content) .map((line: string) => DartLintParser.linetoLintItem(line)) - .filter((f: LintItem) => f != DartLintParser.emptyLog); + .filter((f: LintItem) => f != DartLintParser.emptyItem); } private static linetoLintItem(line: string): LintItem { const lineMatch = line.match(/^(.*) • (.*) • (.*):(\d+):(\d+) • (.*)/); - return lineMatch ? DartLintParser.lineMatchtoLintItem(lineMatch) : DartLintParser.emptyLog; + return lineMatch + ? DartLintParser.lineMatchtoLintItem(lineMatch) + : DartLintParser.emptyItem; } private static lineMatchtoLintItem(lineMatch: RegExpMatchArray): LintItem { diff --git a/src/Provider/CommonVCS/VCSEngine.spec.ts b/src/Provider/CommonVCS/VCSEngine.spec.ts index 0958a93..b8fcf30 100644 --- a/src/Provider/CommonVCS/VCSEngine.spec.ts +++ b/src/Provider/CommonVCS/VCSEngine.spec.ts @@ -25,7 +25,7 @@ function createMockAdapter(): VCSAdapter { function createMockAnalyzerBot(): IAnalyzerBot { return { - touchedFileLog: [], + touchedFileItem: [], getCommitDescription: jest.fn(), isSuccess: jest.fn(), analyze: jest.fn(), diff --git a/src/Provider/CommonVCS/VCSEngine.ts b/src/Provider/CommonVCS/VCSEngine.ts index 7009a02..dac70c2 100644 --- a/src/Provider/CommonVCS/VCSEngine.ts +++ b/src/Provider/CommonVCS/VCSEngine.ts @@ -16,7 +16,7 @@ export class VCSEngine implements VCS { async report(items: LintItem[]): Promise { try { await this.adapter.init(); - await this.setup(logs); + await this.setup(items); if (this.config.removeOldComment) { await this.adapter.removeExistingComments(); @@ -37,7 +37,7 @@ export class VCSEngine implements VCS { private async setup(items: LintItem[]) { const touchedDiff = await this.adapter.diff(); - this.analyzerBot.analyze(logs, touchedDiff); + this.analyzerBot.analyze(items, touchedDiff); Log.debug(`VCS Setup`, { sha: this.adapter.getLatestCommitSha(), diff --git a/src/app.ts b/src/app.ts index d7240b8..9132fc2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -25,9 +25,9 @@ import { VCSAdapter } from './Provider/@interfaces/VCSAdapter'; import { AnalyzerBot } from './AnalyzerBot/AnalyzerBot'; import { defaultFormatter, - gitLab, + gitLabFormatter, OutputFormatter, -} from './OutputFormatter/outputFormatter'; +} from './OutputFormatter/OutputFormatter'; class App { private vcs: VCS | null = null; @@ -99,7 +99,7 @@ class App { case 'default': return defaultFormatter; case 'gitlab': - return gitLab; + return gitLabFormatter; } } @@ -121,7 +121,7 @@ class App { } try { - const passed = await this.vcs.report(logs); + const passed = await this.vcs.report(items); Log.info('Report to VCS completed'); return passed; } catch (error) { @@ -132,7 +132,7 @@ class App { private async writeOutputFile(items: LintItem[]): Promise { try { - await File.writeFileHelper(configs.output, this.outputFormatter(logs)); + await File.writeFileHelper(configs.output, this.outputFormatter(items)); Log.info('Write output completed'); } catch (error) { Log.error('Write output failed', { error }); From 362941dcdc3cd3bfce64728bffd7a646c646f14b Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 16:10:22 +0700 Subject: [PATCH 09/11] Rename LogSeverity -> LintSeverity --- src/AnalyzerBot/AnalyzerBot.ts | 4 ++-- src/AnalyzerBot/utils/commentUtil.spec.ts | 8 ++++---- src/AnalyzerBot/utils/commentUtil.ts | 6 +++--- src/AnalyzerBot/utils/filter.util.ts | 7 ++++--- src/AnalyzerBot/utils/message.util.spec.ts | 6 +++--- src/AnalyzerBot/utils/message.util.ts | 12 ++++++------ src/OutputFormatter/OutputFormatter.spec.ts | 4 ++-- src/OutputFormatter/OutputFormatter.ts | 4 ++-- .../{log.severity.enum.ts => LintSeverity.ts} | 2 +- src/Parser/@types/log.type.ts | 4 ++-- src/Parser/AndroidLintStyleParser.spec.ts | 8 ++++---- src/Parser/AndroidLintStyleParser.ts | 12 ++++++------ src/Parser/DartLintParser.spec.ts | 10 +++++----- src/Parser/DartLintParser.ts | 14 +++++++------- src/Parser/DotnetBuildParser.spec.ts | 8 ++++---- src/Parser/ESLintParser.spec.ts | 6 +++--- src/Parser/ESLintParser.ts | 12 ++++++------ src/Parser/JscpdParser.spec.ts | 6 +++--- src/Parser/JscpdParser.ts | 6 +++--- src/Parser/MSBuildParser.spec.ts | 4 ++-- src/Parser/ScalaStyleParser.spec.ts | 10 +++++----- src/Parser/ScalaStyleParser.ts | 12 ++++++------ src/Parser/SwiftLintParser.spec.ts | 6 +++--- src/Parser/SwiftLintParser.ts | 4 ++-- src/Parser/TSLintParser.spec.ts | 4 ++-- src/Parser/TSLintParser.ts | 4 ++-- src/Parser/index.ts | 2 +- src/Parser/utils/dotnetSeverityMap.spec.ts | 14 +++++++------- src/Parser/utils/dotnetSeverityMap.ts | 14 +++++++------- src/Provider/mockData.ts | 10 +++++----- 30 files changed, 112 insertions(+), 111 deletions(-) rename src/Parser/@enums/{log.severity.enum.ts => LintSeverity.ts} (79%) diff --git a/src/AnalyzerBot/AnalyzerBot.ts b/src/AnalyzerBot/AnalyzerBot.ts index 3dd54c7..c5487c4 100644 --- a/src/AnalyzerBot/AnalyzerBot.ts +++ b/src/AnalyzerBot/AnalyzerBot.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LintItem } from '../Parser'; +import { LintSeverity, LintItem } from '../Parser'; import { Diff } from '../Git/@types/PatchTypes'; import { onlyIn, onlySeverity } from './utils/filter.util'; import { groupComments } from './utils/commentUtil'; @@ -17,7 +17,7 @@ export class AnalyzerBot implements IAnalyzerBot { analyze(items: LintItem[], touchedDiff: Diff[]): void { this.touchedFileItem = items - .filter(onlySeverity(LogSeverity.error, LogSeverity.warning)) + .filter(onlySeverity(LintSeverity.error, LintSeverity.warning)) .filter(onlyIn(touchedDiff)); this.comments = groupComments(this.touchedFileItem, this.config.suppressRules); this.nError = this.comments.reduce((sum, comment) => sum + comment.errors, 0); diff --git a/src/AnalyzerBot/utils/commentUtil.spec.ts b/src/AnalyzerBot/utils/commentUtil.spec.ts index 943ebdc..f0a581e 100644 --- a/src/AnalyzerBot/utils/commentUtil.spec.ts +++ b/src/AnalyzerBot/utils/commentUtil.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LintItem } from '../../Parser'; +import { LintSeverity, LintItem } from '../../Parser'; import { file1TouchLine, file2TouchLine, @@ -42,7 +42,7 @@ describe('groupComments', () => { { ...touchFileError, msg: 'additional warning', - severity: LogSeverity.warning, + severity: LintSeverity.warning, lineOffset: 33, }, ], @@ -78,7 +78,7 @@ describe('groupComments', () => { { ...touchFileError, msg: 'additional warning', - severity: LogSeverity.warning, + severity: LintSeverity.warning, lineOffset: 33, ruleId: 'UNIMPORTANT_RULE2', }, @@ -119,7 +119,7 @@ describe('groupComments', () => { { ...touchFileError, msg: 'additional warning', - severity: LogSeverity.warning, + severity: LintSeverity.warning, lineOffset: 33, ruleId: 'UNIMPORTANT_RULE/RULE2', }, diff --git a/src/AnalyzerBot/utils/commentUtil.ts b/src/AnalyzerBot/utils/commentUtil.ts index 709d8fd..78164d7 100644 --- a/src/AnalyzerBot/utils/commentUtil.ts +++ b/src/AnalyzerBot/utils/commentUtil.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LintItem } from '../../Parser'; +import { LintSeverity, LintItem } from '../../Parser'; import { Comment, CommentStructure } from '../@types/CommentTypes'; import { MessageUtil } from './message.util'; @@ -57,7 +57,7 @@ function calculateErrors( ): number { if (isSuppressed) return currentComment.errors; const { severity } = item; - return currentComment.errors + (severity === LogSeverity.error ? 1 : 0); + return currentComment.errors + (severity === LintSeverity.error ? 1 : 0); } function calculateWarnings( @@ -67,7 +67,7 @@ function calculateWarnings( ): number { if (isSuppressed) return currentComment.warnings; const { severity } = item; - return currentComment.warnings + (severity === LogSeverity.warning ? 1 : 0); + return currentComment.warnings + (severity === LintSeverity.warning ? 1 : 0); } function calculateSuppresses(currentComment: Comment, isSuppressed: boolean): number { diff --git a/src/AnalyzerBot/utils/filter.util.ts b/src/AnalyzerBot/utils/filter.util.ts index e2be84f..7f519d1 100644 --- a/src/AnalyzerBot/utils/filter.util.ts +++ b/src/AnalyzerBot/utils/filter.util.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LintItem } from '../../Parser'; +import { LintSeverity, LintItem } from '../../Parser'; import { Diff } from '../../Git/@types/PatchTypes'; export const onlyIn = (diffs: Diff[]) => (item: LintItem): boolean => @@ -7,5 +7,6 @@ export const onlyIn = (diffs: Diff[]) => (item: LintItem): boolean => d.file === item.source && d.patch.some((p) => !item.line || (item.line >= p.from && item.line <= p.to)), ); -export const onlySeverity = (...severities: LogSeverity[]) => (item: LintItem): boolean => - severities.includes(item.severity); +export const onlySeverity = (...severities: LintSeverity[]) => ( + item: LintItem, +): boolean => severities.includes(item.severity); diff --git a/src/AnalyzerBot/utils/message.util.spec.ts b/src/AnalyzerBot/utils/message.util.spec.ts index 39f82c1..0864ccb 100644 --- a/src/AnalyzerBot/utils/message.util.spec.ts +++ b/src/AnalyzerBot/utils/message.util.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from '../../Parser'; +import { LintSeverity } from '../../Parser'; import { MessageUtil } from './message.util'; describe('createMessageWithEmoji', () => { @@ -6,10 +6,10 @@ describe('createMessageWithEmoji', () => { // ¯\_(ツ)_/¯ const msg = 'test'; - expect(MessageUtil.createMessageWithEmoji(msg, LogSeverity.error)).toBe( + expect(MessageUtil.createMessageWithEmoji(msg, LintSeverity.error)).toBe( `:rotating_light: ${msg}`, ); - expect(MessageUtil.createMessageWithEmoji(msg, LogSeverity.warning)).toBe( + expect(MessageUtil.createMessageWithEmoji(msg, LintSeverity.warning)).toBe( `:warning: ${msg}`, ); }); diff --git a/src/AnalyzerBot/utils/message.util.ts b/src/AnalyzerBot/utils/message.util.ts index 1f0d86a..4fb7186 100644 --- a/src/AnalyzerBot/utils/message.util.ts +++ b/src/AnalyzerBot/utils/message.util.ts @@ -1,16 +1,16 @@ -import { LogSeverity } from '../../Parser'; +import { LintSeverity } from '../../Parser'; const EMOJI_ERROR = ':rotating_light:'; const EMOJI_WARNING = ':warning:'; export class MessageUtil { - static createMessageWithEmoji(msg: string, severity: LogSeverity): string { + static createMessageWithEmoji(msg: string, severity: LintSeverity): string { let emoji = ''; switch (severity) { - case LogSeverity.error: + case LintSeverity.error: emoji = EMOJI_ERROR; break; - case LogSeverity.warning: + case LintSeverity.warning: emoji = EMOJI_WARNING; break; } @@ -24,11 +24,11 @@ export class MessageUtil { const issueCountMsg = this.pluralize('issue', nOfErrors + nOfWarnings); const errorMsg = MessageUtil.createMessageWithEmoji( MessageUtil.pluralize('error', nOfErrors), - LogSeverity.error, + LintSeverity.error, ); const warningMsg = MessageUtil.createMessageWithEmoji( MessageUtil.pluralize('warning', nOfWarnings), - LogSeverity.warning, + LintSeverity.warning, ); return `## CodeCoach reports ${issueCountMsg} diff --git a/src/OutputFormatter/OutputFormatter.spec.ts b/src/OutputFormatter/OutputFormatter.spec.ts index 25a0c2c..533eb06 100644 --- a/src/OutputFormatter/OutputFormatter.spec.ts +++ b/src/OutputFormatter/OutputFormatter.spec.ts @@ -1,5 +1,5 @@ import { ProjectType } from '../Config/@enums/projectType'; -import { LintItem, LogSeverity } from '../Parser'; +import { LintItem, LintSeverity } from '../Parser'; import { gitLabFormatter } from './OutputFormatter'; describe('OutputFormatter', () => { @@ -9,7 +9,7 @@ describe('OutputFormatter', () => { ruleId: 'id', log: 'log', msg: 'msg', - severity: LogSeverity.error, + severity: LintSeverity.error, source: 'src', line: 2, lineOffset: 1, diff --git a/src/OutputFormatter/OutputFormatter.ts b/src/OutputFormatter/OutputFormatter.ts index 1f52923..bdbd5c7 100644 --- a/src/OutputFormatter/OutputFormatter.ts +++ b/src/OutputFormatter/OutputFormatter.ts @@ -1,4 +1,4 @@ -import { LogSeverity, LintItem } from '../Parser'; +import { LintSeverity, LintItem } from '../Parser'; import { createHash } from 'crypto'; export type OutputFormatter = (items: LintItem[]) => string; @@ -20,7 +20,7 @@ type GitLabOutputFormat = { }; }; -const mapGitLabSeverity = (severity: LogSeverity): GitLabSeverity => { +const mapGitLabSeverity = (severity: LintSeverity): GitLabSeverity => { switch (severity) { case 'error': return 'blocker'; diff --git a/src/Parser/@enums/log.severity.enum.ts b/src/Parser/@enums/LintSeverity.ts similarity index 79% rename from src/Parser/@enums/log.severity.enum.ts rename to src/Parser/@enums/LintSeverity.ts index 2fb405b..83b7727 100644 --- a/src/Parser/@enums/log.severity.enum.ts +++ b/src/Parser/@enums/LintSeverity.ts @@ -1,4 +1,4 @@ -export enum LogSeverity { +export enum LintSeverity { warning = 'warning', error = 'error', ignore = 'ignore', diff --git a/src/Parser/@types/log.type.ts b/src/Parser/@types/log.type.ts index b3add6c..596d0ee 100644 --- a/src/Parser/@types/log.type.ts +++ b/src/Parser/@types/log.type.ts @@ -1,11 +1,11 @@ -import { LogSeverity } from '..'; +import { LintSeverity } from '..'; import { ProjectType } from '../../Config'; export type LintItem = { ruleId: string; log: string; msg: string; - severity: LogSeverity; + severity: LintSeverity; source: string; line?: number; lineOffset?: number; diff --git a/src/Parser/AndroidLintStyleParser.spec.ts b/src/Parser/AndroidLintStyleParser.spec.ts index b5722f1..a2d2e09 100644 --- a/src/Parser/AndroidLintStyleParser.spec.ts +++ b/src/Parser/AndroidLintStyleParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { AndroidLintStyleParser } from './AndroidLintStyleParser'; describe('AndroidLintStyleParser', () => { @@ -11,7 +11,7 @@ describe('AndroidLintStyleParser', () => { expect(result[0]).toEqual({ ruleId: 'GradleDependency', source: 'app/build.gradle', - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 42, lineOffset: 5, msg: `A newer version of org.jetbrains.kotlin:kotlin-stdlib than 1.3.72 is available: 1.4.20`, @@ -23,7 +23,7 @@ describe('AndroidLintStyleParser', () => { expect(result[1]).toEqual({ ruleId: 'MissingTranslation', source: `app/src/main/res/values/strings.xml`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 4, lineOffset: 13, msg: `esp is not translated in (Thai)`, @@ -35,7 +35,7 @@ describe('AndroidLintStyleParser', () => { expect(result[2]).toEqual({ ruleId: 'SetJavaScriptEnabled', source: `app/src/main/java/com/example/app/MainActivity.kt`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 16, lineOffset: 9, msg: `Using \`setJavaScriptEnabled\` can introduce XSS vulnerabilities into your application, review carefully`, diff --git a/src/Parser/AndroidLintStyleParser.ts b/src/Parser/AndroidLintStyleParser.ts index d4ead14..65e41e7 100644 --- a/src/Parser/AndroidLintStyleParser.ts +++ b/src/Parser/AndroidLintStyleParser.ts @@ -1,5 +1,5 @@ import { Log } from '../Logger'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { Parser } from './@interfaces/parser.interface'; import { LintItem } from './@types'; import { xml2js } from 'xml-js'; @@ -46,16 +46,16 @@ export class AndroidLintStyleParser extends Parser { }; } - private static getSeverity(levelText: string): LogSeverity { + private static getSeverity(levelText: string): LintSeverity { switch (levelText) { case 'info': - return LogSeverity.info; + return LintSeverity.info; case 'warning': - return LogSeverity.warning; + return LintSeverity.warning; case 'error': - return LogSeverity.error; + return LintSeverity.error; default: - return LogSeverity.unknown; + return LintSeverity.unknown; } } diff --git a/src/Parser/DartLintParser.spec.ts b/src/Parser/DartLintParser.spec.ts index f0a462c..7efa9db 100644 --- a/src/Parser/DartLintParser.spec.ts +++ b/src/Parser/DartLintParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { DartLintParser } from './DartLintParser'; describe('DartLintStyleParser', () => { @@ -29,7 +29,7 @@ describe('DartLintStyleParser', () => { expect(result[0]).toEqual({ ruleId: 'unused_import', source: 'api/modules/lib/auth/auth.dart', - severity: LogSeverity.info, + severity: LintSeverity.info, line: 1, lineOffset: 8, msg: `Unused import: 'dart:async'`, @@ -41,7 +41,7 @@ describe('DartLintStyleParser', () => { expect(result[1]).toEqual({ ruleId: 'await_only_futures', source: `lib/domain/providers/sharable_images_repo.dart`, - severity: LogSeverity.info, + severity: LintSeverity.info, line: 114, lineOffset: 5, msg: `'await' applied to 'void', which is not a 'Future'`, @@ -53,7 +53,7 @@ describe('DartLintStyleParser', () => { expect(result[2]).toEqual({ ruleId: 'sort_child_properties_last', source: `lib/presentation/widgets/platform_flat_button.dart`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 34, lineOffset: 9, msg: `Sort child properties last in widget instance creations`, @@ -65,7 +65,7 @@ describe('DartLintStyleParser', () => { expect(result[3]).toEqual({ ruleId: 'invalid_annotation_target', source: `test_driver/tests/offline/offline_test.dart`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 13, lineOffset: 2, msg: `The annotation 'Timeout' can only be used on libraries`, diff --git a/src/Parser/DartLintParser.ts b/src/Parser/DartLintParser.ts index 013c8dc..73f7b43 100644 --- a/src/Parser/DartLintParser.ts +++ b/src/Parser/DartLintParser.ts @@ -1,6 +1,6 @@ import { Parser } from './@interfaces/parser.interface'; import { LintItem } from './@types'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { splitByLine } from './utils/lineBreak.util'; import { ProjectType } from '../Config/@enums'; @@ -33,16 +33,16 @@ export class DartLintParser extends Parser { }; } - private static stringToSeverity(levelText: string): LogSeverity { + private static stringToSeverity(levelText: string): LintSeverity { switch (levelText) { case 'error': - return LogSeverity.error; + return LintSeverity.error; case 'warning': - return LogSeverity.warning; + return LintSeverity.warning; case 'info': - return LogSeverity.info; + return LintSeverity.info; default: - return LogSeverity.unknown; + return LintSeverity.unknown; } } @@ -50,7 +50,7 @@ export class DartLintParser extends Parser { ruleId: '', log: '', msg: '', - severity: LogSeverity.unknown, + severity: LintSeverity.unknown, source: '', valid: false, type: ProjectType.dartlint, diff --git a/src/Parser/DotnetBuildParser.spec.ts b/src/Parser/DotnetBuildParser.spec.ts index 8a6beca..81d621c 100644 --- a/src/Parser/DotnetBuildParser.spec.ts +++ b/src/Parser/DotnetBuildParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { LintItem } from './@types'; import { DotnetBuildParser } from './DotnetBuildParser'; @@ -18,7 +18,7 @@ describe('DotnetBuildParser tests', () => { expect(result[0]).toEqual({ ruleId: 'AG0030', source: `Broken.cs`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 6, lineOffset: 8, msg: `AG0030: Prevent use of dynamic`, @@ -34,7 +34,7 @@ describe('DotnetBuildParser tests', () => { expect(result[0]).toEqual({ ruleId: 'CS5001', source: `Broken.csproj`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: NaN, lineOffset: NaN, msg: `CS5001: Program does not contain a static 'Main' method suitable for an entry point`, @@ -50,7 +50,7 @@ describe('DotnetBuildParser tests', () => { expect(result[0]).toEqual({ ruleId: 'MSB3277', source: `project.csproj`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 2084, lineOffset: 5, msg: `MSB3277: some message`, diff --git a/src/Parser/ESLintParser.spec.ts b/src/Parser/ESLintParser.spec.ts index d795b80..9fcde3a 100644 --- a/src/Parser/ESLintParser.spec.ts +++ b/src/Parser/ESLintParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { ESLintParser } from './ESLintParser'; describe('ESLintParser', () => { @@ -56,7 +56,7 @@ describe('ESLintParser', () => { expect(result[0]).toEqual({ ruleId: '', source: '', - severity: LogSeverity.error, + severity: LintSeverity.error, line: 59, lineOffset: 8, msg: `Parsing error: ')' expected.`, @@ -68,7 +68,7 @@ describe('ESLintParser', () => { expect(result[1]).toEqual({ ruleId: '@typescript-eslint/no-unused-vars', source: `src/app.ts`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 24, lineOffset: 15, msg: `'content' is defined but never used.`, diff --git a/src/Parser/ESLintParser.ts b/src/Parser/ESLintParser.ts index 4f27792..1277aa6 100644 --- a/src/Parser/ESLintParser.ts +++ b/src/Parser/ESLintParser.ts @@ -1,7 +1,7 @@ import { ProjectType } from '../Config/@enums'; import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { Parser } from './@interfaces/parser.interface'; import { ESLintIssue, ESLintLog, LintItem } from './@types'; @@ -37,16 +37,16 @@ export class ESLintParser extends Parser { }; } - private static getSeverity(esLevel: number): LogSeverity { + private static getSeverity(esLevel: number): LintSeverity { switch (esLevel) { case 0: - return LogSeverity.ignore; + return LintSeverity.ignore; case 1: - return LogSeverity.warning; + return LintSeverity.warning; case 2: - return LogSeverity.error; + return LintSeverity.error; default: - return LogSeverity.unknown; + return LintSeverity.unknown; } } } diff --git a/src/Parser/JscpdParser.spec.ts b/src/Parser/JscpdParser.spec.ts index 9b43bf0..fc9243a 100644 --- a/src/Parser/JscpdParser.spec.ts +++ b/src/Parser/JscpdParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { JscpdParser } from './JscpdParser'; describe('JscpdParser tests', () => { @@ -53,7 +53,7 @@ describe('JscpdParser tests', () => { expect(result[0]).toEqual({ ruleId: 'jscpd', source: `src/WebApi/Controllers/GController.cs`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 1, lineOffset: 1, nLines: 11, @@ -74,7 +74,7 @@ ${mockedContent.duplicates[0].fragment} expect(result[1]).toEqual({ ruleId: 'jscpd', source: `src/WebApi/Controllers/HController.cs`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 1, lineOffset: 2, nLines: 11, diff --git a/src/Parser/JscpdParser.ts b/src/Parser/JscpdParser.ts index 9f4e1be..b6d52a8 100644 --- a/src/Parser/JscpdParser.ts +++ b/src/Parser/JscpdParser.ts @@ -3,7 +3,7 @@ import { Parser } from './@interfaces/parser.interface'; import { LintItem } from './@types'; import { ProjectType } from '../Config/@enums'; import { JscpdLog } from './@types/JscpdLog'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; export class JscpdParser extends Parser { private readonly ruleId = 'jscpd'; @@ -39,7 +39,7 @@ ${log.fragment} `, source: log.secondFile.name, - severity: LogSeverity.warning, + severity: LintSeverity.warning, valid: true, type: ProjectType.jscpd, }, @@ -60,7 +60,7 @@ ${log.fragment} `, source: log.firstFile.name, - severity: LogSeverity.warning, + severity: LintSeverity.warning, valid: true, type: ProjectType.jscpd, }, diff --git a/src/Parser/MSBuildParser.spec.ts b/src/Parser/MSBuildParser.spec.ts index 9882847..e5055a3 100644 --- a/src/Parser/MSBuildParser.spec.ts +++ b/src/Parser/MSBuildParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { MSBuildParser } from './MSBuildParser'; describe('MSBuildParser tests', () => { @@ -13,7 +13,7 @@ describe('MSBuildParser tests', () => { expect(result[0]).toEqual({ ruleId: 'CS0414', source: `Project/Service/Provider.cs`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 67, lineOffset: 29, msg: `CS0414: The field 'Data.field' is assigned but its value is never used`, diff --git a/src/Parser/ScalaStyleParser.spec.ts b/src/Parser/ScalaStyleParser.spec.ts index 6b576cb..166ae70 100644 --- a/src/Parser/ScalaStyleParser.spec.ts +++ b/src/Parser/ScalaStyleParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { ScalaStyleParser } from './ScalaStyleParser'; describe('ScalaStyleParser', () => { @@ -32,7 +32,7 @@ describe('ScalaStyleParser', () => { expect(result[0]).toEqual({ ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it', source: '', - severity: LogSeverity.error, + severity: LintSeverity.error, line: 53, lineOffset: 4, msg: `Avoid mutable fields`, @@ -44,7 +44,7 @@ describe('ScalaStyleParser', () => { expect(result[1]).toEqual({ ruleId: '', source: `src/main/scala/code/dir/subdir/code-a.scala`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: undefined, lineOffset: undefined, msg: `illegal start of definition: Token(VARID,yplTaxWithValue,1704,yplTaxWithValue)`, @@ -56,7 +56,7 @@ describe('ScalaStyleParser', () => { expect(result[2]).toEqual({ ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it', source: `src/main/scala/code/dir/subdir/code-a.scala`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 7, lineOffset: 7, msg: `Number of methods in class exceeds 30`, @@ -68,7 +68,7 @@ describe('ScalaStyleParser', () => { expect(result[3]).toEqual({ ruleId: 'some.gibberish.text.that.i.dont.wanna.keep.it', source: `src/main/scala/code/code-c.scala`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 207, lineOffset: 6, msg: `Avoid mutable local variables`, diff --git a/src/Parser/ScalaStyleParser.ts b/src/Parser/ScalaStyleParser.ts index cbf62f7..2bc9e9d 100644 --- a/src/Parser/ScalaStyleParser.ts +++ b/src/Parser/ScalaStyleParser.ts @@ -1,6 +1,6 @@ import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { Parser } from './@interfaces/parser.interface'; import { LintItem } from './@types'; import { xml2js } from 'xml-js'; @@ -59,16 +59,16 @@ export class ScalaStyleParser extends Parser { }; } - private static getSeverity(ScalaStyleLevel: string): LogSeverity { + private static getSeverity(ScalaStyleLevel: string): LintSeverity { switch (ScalaStyleLevel) { case 'info': - return LogSeverity.info; + return LintSeverity.info; case 'warning': - return LogSeverity.warning; + return LintSeverity.warning; case 'error': - return LogSeverity.error; + return LintSeverity.error; default: - return LogSeverity.unknown; + return LintSeverity.unknown; } } } diff --git a/src/Parser/SwiftLintParser.spec.ts b/src/Parser/SwiftLintParser.spec.ts index 7b49891..e4bfdb7 100644 --- a/src/Parser/SwiftLintParser.spec.ts +++ b/src/Parser/SwiftLintParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { SwiftLintParser } from './SwiftLintParser'; describe('SwiftLintParser tests', () => { @@ -34,7 +34,7 @@ describe('SwiftLintParser tests', () => { expect(result[0]).toEqual({ ruleId: 'line_length', source: `Folder1/SubFolder1/File5.swift`, - severity: LogSeverity.warning, + severity: LintSeverity.warning, line: 130, lineOffset: 0, msg: `Line should be 120 characters or less; currently it has 125 characters`, @@ -45,7 +45,7 @@ describe('SwiftLintParser tests', () => { expect(result[1]).toEqual({ ruleId: 'type_body_length', source: ``, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 9, lineOffset: 7, msg: `Type body should span 400 lines or less excluding comments and whitespace: currently spans 448 lines`, diff --git a/src/Parser/SwiftLintParser.ts b/src/Parser/SwiftLintParser.ts index a72454a..5f52d45 100644 --- a/src/Parser/SwiftLintParser.ts +++ b/src/Parser/SwiftLintParser.ts @@ -4,7 +4,7 @@ import { Parser } from './@interfaces/parser.interface'; import { LintItem } from './@types'; import { ProjectType } from '../Config/@enums'; import { SwiftLintLog } from './@types/SwiftLintLog'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; export class SwiftLintParser extends Parser { parse(content: string): LintItem[] { @@ -27,7 +27,7 @@ export class SwiftLintParser extends Parser { lineOffset: log.character ?? 0, msg: log.reason, source: '', - severity: log.severity.toLowerCase() as LogSeverity, + severity: log.severity.toLowerCase() as LintSeverity, valid: true, type: ProjectType.swiftlint, }; diff --git a/src/Parser/TSLintParser.spec.ts b/src/Parser/TSLintParser.spec.ts index ef7a3d6..7f9518b 100644 --- a/src/Parser/TSLintParser.spec.ts +++ b/src/Parser/TSLintParser.spec.ts @@ -1,4 +1,4 @@ -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { TSLintParser } from './TSLintParser'; describe('TSLintParser tests', () => { @@ -30,7 +30,7 @@ describe('TSLintParser tests', () => { expect(result[0]).toEqual({ ruleId: 'prefer-const', source: `src/app/mobile/component/Layout/Layout.tsx`, - severity: LogSeverity.error, + severity: LintSeverity.error, line: 56, lineOffset: 4, msg: `Identifier 'a' is never reassigned; use 'const' instead of 'let'.`, diff --git a/src/Parser/TSLintParser.ts b/src/Parser/TSLintParser.ts index cefd5b7..58dff4c 100644 --- a/src/Parser/TSLintParser.ts +++ b/src/Parser/TSLintParser.ts @@ -1,7 +1,7 @@ import { ProjectType } from '../Config/@enums'; import { Log } from '../Logger'; import { getRelativePath } from './utils/path.util'; -import { LogSeverity } from './@enums/log.severity.enum'; +import { LintSeverity } from './@enums/LintSeverity'; import { Parser } from './@interfaces/parser.interface'; import { LintItem, TSLintLog } from './@types'; @@ -27,7 +27,7 @@ export class TSLintParser extends Parser { // there are no code portion present in tslint output msg: log.failure, source: '', - severity: log.ruleSeverity.toLowerCase() as LogSeverity, + severity: log.ruleSeverity.toLowerCase() as LintSeverity, valid: true, type: ProjectType.tslint, }; diff --git a/src/Parser/index.ts b/src/Parser/index.ts index be2c767..9d4da0b 100644 --- a/src/Parser/index.ts +++ b/src/Parser/index.ts @@ -9,4 +9,4 @@ export { SwiftLintParser } from './SwiftLintParser'; export { JscpdParser } from './JscpdParser'; export { Parser } from './@interfaces/parser.interface'; export { LintItem } from './@types'; -export { LogSeverity } from './@enums/log.severity.enum'; +export { LintSeverity } from './@enums/LintSeverity'; diff --git a/src/Parser/utils/dotnetSeverityMap.spec.ts b/src/Parser/utils/dotnetSeverityMap.spec.ts index c027dc2..e5cdb40 100644 --- a/src/Parser/utils/dotnetSeverityMap.spec.ts +++ b/src/Parser/utils/dotnetSeverityMap.spec.ts @@ -1,15 +1,15 @@ -import { LogSeverity } from '../@enums/log.severity.enum'; +import { LintSeverity } from '../@enums/LintSeverity'; import { mapSeverity } from './dotnetSeverityMap'; describe('dotnetSeverityMap', () => { describe('mapSeverity', () => { it('should map correctly', () => { - expect(mapSeverity('fatal')).toBe(LogSeverity.error); - expect(mapSeverity('error')).toBe(LogSeverity.error); - expect(mapSeverity('warning')).toBe(LogSeverity.warning); - expect(mapSeverity('info')).toBe(LogSeverity.info); - expect(mapSeverity('hidden')).toBe(LogSeverity.ignore); - expect(mapSeverity('some gibberish text')).toBe(LogSeverity.unknown); + expect(mapSeverity('fatal')).toBe(LintSeverity.error); + expect(mapSeverity('error')).toBe(LintSeverity.error); + expect(mapSeverity('warning')).toBe(LintSeverity.warning); + expect(mapSeverity('info')).toBe(LintSeverity.info); + expect(mapSeverity('hidden')).toBe(LintSeverity.ignore); + expect(mapSeverity('some gibberish text')).toBe(LintSeverity.unknown); }); }); }); diff --git a/src/Parser/utils/dotnetSeverityMap.ts b/src/Parser/utils/dotnetSeverityMap.ts index 41f88a4..8e64d80 100644 --- a/src/Parser/utils/dotnetSeverityMap.ts +++ b/src/Parser/utils/dotnetSeverityMap.ts @@ -1,17 +1,17 @@ -import { LogSeverity } from '../@enums/log.severity.enum'; +import { LintSeverity } from '../@enums/LintSeverity'; -export function mapSeverity(levelText: string): LogSeverity { +export function mapSeverity(levelText: string): LintSeverity { switch (levelText) { case 'fatal': case 'error': - return LogSeverity.error; + return LintSeverity.error; case 'warning': - return LogSeverity.warning; + return LintSeverity.warning; case 'info': - return LogSeverity.info; + return LintSeverity.info; case 'hidden': - return LogSeverity.ignore; + return LintSeverity.ignore; default: - return LogSeverity.unknown; + return LintSeverity.unknown; } } diff --git a/src/Provider/mockData.ts b/src/Provider/mockData.ts index 5f40e8e..652a89f 100644 --- a/src/Provider/mockData.ts +++ b/src/Provider/mockData.ts @@ -1,5 +1,5 @@ import { ProjectType } from '../Config/@enums'; -import { LogSeverity, LintItem } from '../Parser'; +import { LintSeverity, LintItem } from '../Parser'; export const mockTouchFile = 'file1.cs'; export const file1TouchLine = 11; @@ -9,7 +9,7 @@ export const touchFileError: LintItem = { ruleId: '', log: '', msg: 'msg1', - severity: LogSeverity.error, + severity: LintSeverity.error, source: mockTouchFile, line: file1TouchLine, lineOffset: 22, @@ -20,7 +20,7 @@ export const touchFileWarning: LintItem = { ruleId: '', log: '', msg: 'msg3', - severity: LogSeverity.warning, + severity: LintSeverity.warning, source: mockTouchFile, line: file2TouchLine, lineOffset: 44, @@ -32,7 +32,7 @@ export const untouchedError: LintItem = { ruleId: '', log: '', msg: 'msg2', - severity: LogSeverity.error, + severity: LintSeverity.error, source: 'otherfile.cs', line: 55, lineOffset: 66, @@ -43,7 +43,7 @@ export const untouchedWarning: LintItem = { ruleId: '', log: '', msg: 'msg4', - severity: LogSeverity.warning, + severity: LintSeverity.warning, source: 'otherfile.cs', line: 77, lineOffset: 88, From 967b618db6e757e8a7328c209e6b81adf4c17ad5 Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 16:17:25 +0700 Subject: [PATCH 10/11] Fix naming for AndroidLintStyleParser --- src/Parser/AndroidLintStyleParser.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Parser/AndroidLintStyleParser.ts b/src/Parser/AndroidLintStyleParser.ts index 65e41e7..215e9eb 100644 --- a/src/Parser/AndroidLintStyleParser.ts +++ b/src/Parser/AndroidLintStyleParser.ts @@ -14,7 +14,7 @@ export class AndroidLintStyleParser extends Parser { if (!content) return []; return ( - AndroidLintStyleParser.xmltoLintItem(content).issues[0]?.issue?.flatMap( + AndroidLintStyleParser.xmlToAndroidLintStyleLog(content).issues[0]?.issue?.flatMap( (issue: AndroidLintStyleIssue) => { return AndroidLintStyleParser.toLintItem(issue, issue.location[0], this.cwd); }, @@ -59,7 +59,7 @@ export class AndroidLintStyleParser extends Parser { } } - private static xmltoLintItem(xmlContent: string): AndroidLintStyleLog { + private static xmlToAndroidLintStyleLog(xmlContent: string): AndroidLintStyleLog { return xml2js(xmlContent, convertOption) as AndroidLintStyleLog; } } From 67b53618e6e5ecd5341a1333bafdeb96ecbd86af Mon Sep 17 00:00:00 2001 From: pdujtipiya Date: Tue, 19 Sep 2023 16:21:13 +0700 Subject: [PATCH 11/11] fix styling --- src/Parser/AndroidLintStyleParser.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Parser/AndroidLintStyleParser.ts b/src/Parser/AndroidLintStyleParser.ts index 215e9eb..f4745bd 100644 --- a/src/Parser/AndroidLintStyleParser.ts +++ b/src/Parser/AndroidLintStyleParser.ts @@ -14,11 +14,11 @@ export class AndroidLintStyleParser extends Parser { if (!content) return []; return ( - AndroidLintStyleParser.xmlToAndroidLintStyleLog(content).issues[0]?.issue?.flatMap( - (issue: AndroidLintStyleIssue) => { - return AndroidLintStyleParser.toLintItem(issue, issue.location[0], this.cwd); - }, - ) ?? [] + AndroidLintStyleParser.xmlToAndroidLintStyleLog( + content, + ).issues[0]?.issue?.flatMap((issue: AndroidLintStyleIssue) => { + return AndroidLintStyleParser.toLintItem(issue, issue.location[0], this.cwd); + }) ?? [] ); } catch (err) { Log.warn('AndroidStyle Parser: parse with content error', content);