Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple output file formats #160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/Config/@enums/outputFormat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
export enum OutputFormat {
default = 'default',
gitlab = 'gitlab',
}
2 changes: 1 addition & 1 deletion src/Config/@types/configArgument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ConfigArgument = {
gitlabToken: string;
buildLogFile: BuildLogFile[];
output: string; // =logFilePath
outputFormat: 'default' | 'gitlab';
outputFormat: string[];
removeOldComment: boolean;
failOnWarnings: boolean;
suppressRules: string[];
Expand Down
7 changes: 4 additions & 3 deletions src/Config/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,15 @@ and <cwd> is build root directory (optional (Will use current context as cwd)).
})
.option('output', {
alias: 'o',
describe: 'Output parsed log file',
describe: 'Default output file name',
type: 'string',
default: DEFAULT_OUTPUT_FILE,
})
.option('outputFormat', {
describe: 'Output format',
type: 'array',
describe: 'Output formats',
choices: ['default', 'gitlab'],
default: 'default',
default: ['default'],
})
.option('removeOldComment', {
alias: 'r',
Expand Down
2 changes: 1 addition & 1 deletion src/Config/constants/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const DEFAULT_OUTPUT_FILE = 'build.json';
export const DEFAULT_OUTPUT_FILE = 'output.json';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this is going to be a breaking change


export const USER_AGENT = 'CodeCoach';
export const TIME_ZONE = 'Asia/Bangkok';
53 changes: 33 additions & 20 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { File } from './File';
import { Log } from './Logger';
import {
AndroidLintStyleParser,
DartLintParser,
DotnetBuildParser,
ESLintParser,
JscpdParser,
LintItem,
MSBuildParser,
Parser,
ScalaStyleParser,
TSLintParser,
DartLintParser,
SwiftLintParser,
JscpdParser,
TSLintParser,
} from './Parser';
import { GitHubPRService, VCS } from './Provider';
import { GitLabMRService } from './Provider/GitLab/GitLabMRService';
Expand All @@ -23,15 +23,16 @@ import { VCSEngine } from './Provider/CommonVCS/VCSEngine';
import { GitLabAdapter } from './Provider/GitLab/GitLabAdapter';
import { VCSAdapter } from './Provider/@interfaces/VCSAdapter';
import { AnalyzerBot } from './AnalyzerBot/AnalyzerBot';
import {
defaultFormatter,
gitLabFormatter,
OutputFormatter,
} from './OutputFormatter/OutputFormatter';
import { defaultFormatter, gitLabFormatter } from './OutputFormatter/OutputFormatter';

type OutputFileMeta = {
formatter: (items: LintItem[]) => string;
filename: string;
};

class App {
private vcs: VCS | null = null;
private outputFormatter: OutputFormatter;
private outputFileMeta: OutputFileMeta[];

async start(): Promise<void> {
if (!configs.dryRun) {
Expand All @@ -44,14 +45,14 @@ class App {
this.vcs = new VCSEngine(configs, analyzer, adapter);
}

this.outputFormatter = App.getOutputFormatter();
this.outputFileMeta = App.getOutputFileMetas();

const logs = await this.parseBuildData(configs.buildLogFile);
Log.info('Build data parsing completed');

const reportToVcs = this.reportToVcs(logs);
const writeOutputFile = this.writeOutputFile(logs);
const [passed] = await Promise.all([reportToVcs, writeOutputFile]);
const writeOutputFiles = this.writeOutputFiles(logs);
const [passed] = await Promise.all([reportToVcs, writeOutputFiles]);
if (!passed) {
Log.error('There are some linting error and exit code reporting is enabled');
process.exit(1);
Expand Down Expand Up @@ -94,13 +95,19 @@ class App {
}
}

private static getOutputFormatter(): OutputFormatter {
switch (configs.outputFormat) {
case 'default':
return defaultFormatter;
case 'gitlab':
return gitLabFormatter;
private static getOutputFileMetas(): OutputFileMeta[] {
const outputFileMetas: OutputFileMeta[] = [];
if (configs.outputFormat.includes('default')) {
outputFileMetas.push({ formatter: defaultFormatter, filename: configs.output });
}
if (configs.outputFormat.includes('gitlab')) {
outputFileMetas.push({
formatter: gitLabFormatter,
filename: 'gl-code-quality-report.json',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should let users set the filename for gitlab or any other format if we have in the future as well.

});
}

return outputFileMetas;
}

private async parseBuildData(files: BuildLogFile[]): Promise<LintItem[]> {
Expand Down Expand Up @@ -130,9 +137,15 @@ class App {
}
}

private async writeOutputFile(items: LintItem[]): Promise<void> {
private async writeOutputFiles(items: LintItem[]): Promise<void> {
try {
await File.writeFileHelper(configs.output, this.outputFormatter(items));
await Promise.all(
this.outputFileMeta.map((meta) => {
Log.info('Write output to ' + meta.filename, { meta });
return File.writeFileHelper(meta.filename, meta.formatter(items));
}),
);

Log.info('Write output completed');
} catch (error) {
Log.error('Write output failed', { error });
Expand Down