Skip to content
This repository was archived by the owner on Oct 1, 2023. It is now read-only.
Merged
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
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ inputs:
this should be the tip of the pull request, obtainable by running
`git rev-parse ${GITHUB_SHA}^2`"
required: true
mode:
Copy link
Member

Choose a reason for hiding this comment

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

do we even need regex mode?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not anymore, but I'm keeping it around for now so that PyTorch CI doesn't break while we wait for pytorch/pytorch#55569 to land; we can definitely remove it later

description:
required: "Either 'regex' (in which case the linter_output_path file is read
line-by-line and the regex input is used) or 'json' (in which case the
linter_output_path file is expected to be a JSON file containing a single
array of objects, each of which must have the same keys as the capture
groups listed in the description of the regex input)."
default: regex
regex:
description: "Regex that will match against each line of the file.
The following named groups should be captured:
Expand Down
59 changes: 40 additions & 19 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,24 @@ function getAnnotationLevel() {
return val;
}
}
function makeAnnotation(raw) {
// Chop `./` off the front so that Github will recognize the file path
const normalized_path = raw.filename.replace('./', '');
const annotation_level = (getAnnotationLevel() == 'warning') ?
'warning' :
'failure';
return {
path: normalized_path,
start_line: raw.lineNumber,
end_line: raw.lineNumber,
start_column: raw.columnNumber,
end_column: raw.columnNumber,
annotation_level: annotation_level,
message: `[${raw.errorCode}] ${raw.errorDesc}`,
};
}
// Regex match each line in the output and turn them into annotations
function parseOutput(output, regex) {
function parseOutputLines(output, regex) {
let errors = output.split('\n');
let annotations = [];
for (let i = 0; i < errors.length; i++) {
Expand All @@ -40,27 +56,22 @@ function parseOutput(output, regex) {
if (!groups) {
throw "No named capture groups in regex match.";
}
// Chop `./` off the front so that Github will recognize the file path
const normalized_path = groups.filename.replace('./', '');
const line = parseInt(groups.lineNumber);
const column = parseInt(groups.columnNumber);
const annotation_level = (getAnnotationLevel() == 'warning') ?
'warning' :
'failure';
const annotation = {
path: normalized_path,
start_line: line,
end_line: line,
start_column: column,
end_column: column,
annotation_level: annotation_level,
message: `[${groups.errorCode}] ${groups.errorDesc}`,
};
const annotation = makeAnnotation({
filename: groups.filename,
lineNumber: parseInt(groups.lineNumber),
columnNumber: parseInt(groups.columnNumber),
errorCode: groups.errorCode,
errorDesc: groups.errorDesc,
});
annotations.push(annotation);
}
}
return annotations;
}
function parseOutputJSON(output) {
let raw = JSON.parse(output);
return raw.map(makeAnnotation);
}
function createCheck(check_name, title, annotations) {
return __awaiter(this, void 0, void 0, function* () {
const octokit = new github.GitHub(String(GITHUB_TOKEN));
Expand All @@ -84,8 +95,18 @@ function run() {
const linterOutputPath = core.getInput('linter_output_path');
console.log(`Reading linter output from: ${GITHUB_WORKSPACE}/${linterOutputPath}`);
const output = yield fs.promises.readFile(`${GITHUB_WORKSPACE}/${linterOutputPath}`);
const regex = core.getInput('regex');
const annotations = parseOutput(output.toString(), RegExp(regex));
const mode = core.getInput('mode');
let annotations;
if (mode === 'regex') {
const regex = core.getInput('regex');
annotations = parseOutputLines(output.toString(), RegExp(regex));
}
else if (mode === 'json') {
annotations = parseOutputJSON(output.toString());
}
else {
throw `Mode '${mode}' not recognized.`;
}
if (annotations.length > 0) {
console.log("===============================================================");
console.log("| FAILURES DETECTED |");
Expand Down
67 changes: 48 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,33 @@ function getAnnotationLevel(): string {
}
}

interface RawAnnotation {
filename: string;
lineNumber: number;
columnNumber: number;
errorCode: string;
errorDesc: string;
}

function makeAnnotation(raw: RawAnnotation): Annotation {
// Chop `./` off the front so that Github will recognize the file path
const normalized_path = raw.filename.replace('./', '');
const annotation_level = (getAnnotationLevel() == 'warning') ?
<const>'warning' :
<const>'failure';
return {
path: normalized_path,
start_line: raw.lineNumber,
end_line: raw.lineNumber,
start_column: raw.columnNumber,
end_column: raw.columnNumber,
annotation_level: annotation_level,
message: `[${raw.errorCode}] ${raw.errorDesc}`,
}
}

// Regex match each line in the output and turn them into annotations
function parseOutput(output: string, regex: RegExp): Annotation[] {
function parseOutputLines(output: string, regex: RegExp): Annotation[] {
let errors = output.split('\n');
let annotations: Annotation[] = [];
for (let i = 0; i < errors.length; i++) {
Expand All @@ -28,29 +53,25 @@ function parseOutput(output: string, regex: RegExp): Annotation[] {
if (!groups) {
throw "No named capture groups in regex match.";
}
// Chop `./` off the front so that Github will recognize the file path
const normalized_path = groups.filename.replace('./', '');
const line = parseInt(groups.lineNumber);
const column = parseInt(groups.columnNumber);
const annotation_level = (getAnnotationLevel() == 'warning') ?
<const>'warning' :
<const>'failure';
const annotation = {
path: normalized_path,
start_line: line,
end_line: line,
start_column: column,
end_column: column,
annotation_level: annotation_level,
message: `[${groups.errorCode}] ${groups.errorDesc}`,
};
const annotation = makeAnnotation({
filename: groups.filename,
lineNumber: parseInt(groups.lineNumber),
columnNumber: parseInt(groups.columnNumber),
errorCode: groups.errorCode,
errorDesc: groups.errorDesc,
});

annotations.push(annotation);
}
}
return annotations;
}

function parseOutputJSON(output: string): Annotation[] {
let raw: RawAnnotation[] = JSON.parse(output);
return raw.map(makeAnnotation);
}

async function createCheck(check_name: string, title: string, annotations: Annotation[]) {
const octokit = new github.GitHub(String(GITHUB_TOKEN));
const req = {
Expand Down Expand Up @@ -82,8 +103,16 @@ async function run() {
const linterOutputPath = core.getInput('linter_output_path');
console.log(`Reading linter output from: ${GITHUB_WORKSPACE}/${linterOutputPath}`)
const output = await fs.promises.readFile(`${GITHUB_WORKSPACE}/${linterOutputPath}`);
const regex = core.getInput('regex');
const annotations = parseOutput(output.toString(), RegExp(regex));
const mode = core.getInput('mode');
let annotations: Annotation[];
if (mode === 'regex') {
const regex = core.getInput('regex');
annotations = parseOutputLines(output.toString(), RegExp(regex))
} else if (mode === 'json') {
annotations = parseOutputJSON(output.toString());
} else {
throw `Mode '${mode}' not recognized.`;
}
if (annotations.length > 0) {
console.log("===============================================================")
console.log("| FAILURES DETECTED |")
Expand Down