-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract some code from checkstyle action.ts for (hopefully) easier de…
…bugging Signed-off-by: Taylor Smock <[email protected]>
- Loading branch information
Showing
5 changed files
with
110 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,13 @@ | ||
import { debug, getInput, setFailed } from "@actions/core"; | ||
import { readFileSync, existsSync } from "fs"; | ||
import { XMLParser } from "fast-xml-parser"; | ||
import { Problem } from "pmd"; | ||
import { logProblems } from "pmd"; | ||
import { sep, join } from "path"; | ||
|
||
interface Violations { | ||
"@_source": string; | ||
"@_message": string; | ||
"@_severity": string; | ||
"@_column": string; | ||
"@_line": string; | ||
} | ||
|
||
interface FileData { | ||
"@_name": string; | ||
error?: Array<Violations>; | ||
} | ||
|
||
function parseFile(trim: number, fileData: FileData): Problem[] { | ||
let subFile = fileData["@_name"].split(sep); | ||
let currentlyTrimmed = 0; | ||
while (!existsSync(join(...subFile))) { | ||
subFile = subFile.slice(1); | ||
currentlyTrimmed += 1; | ||
if (currentlyTrimmed >= trim) { | ||
break; | ||
} | ||
} | ||
const file = join(...subFile); | ||
const violations = fileData["error"]; | ||
const problems: Problem[] = []; | ||
if ( | ||
violations !== undefined && | ||
violations !== null && | ||
violations.length !== 0 | ||
) { | ||
for (const violation of violations) { | ||
const title = violation["@_source"]; | ||
problems.push({ | ||
file: file, | ||
title: title, | ||
startColumn: parseInt(violation["@_column"]), | ||
endColumn: parseInt(violation["@_column"]), | ||
startLine: parseInt(violation["@_line"]), | ||
endLine: parseInt(violation["@_line"]), | ||
info: violation["@_message"], | ||
}); | ||
} | ||
} | ||
return problems; | ||
} | ||
|
||
export function parseData(trim: number, data: string | Buffer): Problem[] { | ||
const alwaysArray = ["checkstyle.file", "checkstyle.file.error"]; | ||
const parser = new XMLParser({ | ||
ignoreAttributes: false, | ||
isArray: (name, jpath) => { | ||
return alwaysArray.indexOf(jpath) >= 0; | ||
}, | ||
}); | ||
const parsed = parser.parse(data); | ||
const files = parsed["checkstyle"]["file"]; | ||
let problems: Problem[] = []; | ||
for (const file of files) { | ||
problems = problems.concat(parseFile(trim, file)); | ||
} | ||
return problems; | ||
} | ||
import { getInput, setFailed } from "@actions/core"; | ||
import { run as realRun } from "./checkstyle"; | ||
|
||
async function run(): Promise<void> { | ||
const checkstyleFile = getInput("file"); | ||
const pathTrim = getInput("pathTrim"); | ||
let trim: number; | ||
if (pathTrim === "") { | ||
trim = process.cwd().split(sep).length; | ||
} else { | ||
trim = parseInt(pathTrim); | ||
} | ||
const data = readFileSync(checkstyleFile); | ||
debug(data.toString()); | ||
logProblems(parseData(trim, data)); | ||
realRun(checkstyleFile, pathTrim); | ||
} | ||
|
||
run().catch((err) => { | ||
console.log(err); | ||
console.log(err.stack); | ||
setFailed(err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { debug } from "@actions/core"; | ||
import { readFileSync, existsSync } from "fs"; | ||
import { XMLParser } from "fast-xml-parser"; | ||
import { Problem } from "pmd"; | ||
import { logProblems } from "pmd"; | ||
import { sep, join } from "path"; | ||
|
||
interface Violations { | ||
"@_source": string; | ||
"@_message": string; | ||
"@_severity": string; | ||
"@_column": string; | ||
"@_line": string; | ||
} | ||
|
||
interface FileData { | ||
"@_name": string; | ||
error?: Array<Violations>; | ||
} | ||
|
||
function parseFile(trim: number, fileData: FileData): Problem[] { | ||
let subFile = fileData["@_name"].split(sep); | ||
let currentlyTrimmed = 0; | ||
while (!existsSync(join(...subFile))) { | ||
subFile = subFile.slice(1); | ||
currentlyTrimmed += 1; | ||
if (currentlyTrimmed >= trim) { | ||
break; | ||
} | ||
} | ||
const file = join(...subFile); | ||
const violations = fileData["error"]; | ||
const problems: Problem[] = []; | ||
if ( | ||
violations !== undefined && | ||
violations !== null && | ||
violations.length !== 0 | ||
) { | ||
for (const violation of violations) { | ||
const title = violation["@_source"]; | ||
problems.push({ | ||
file: file, | ||
title: title, | ||
startColumn: parseInt(violation["@_column"]), | ||
endColumn: parseInt(violation["@_column"]), | ||
startLine: parseInt(violation["@_line"]), | ||
endLine: parseInt(violation["@_line"]), | ||
info: violation["@_message"], | ||
}); | ||
} | ||
} | ||
return problems; | ||
} | ||
|
||
export function parseData(trim: number, data: string | Buffer): Problem[] { | ||
const alwaysArray = ["checkstyle.file", "checkstyle.file.error"]; | ||
const parser = new XMLParser({ | ||
ignoreAttributes: false, | ||
isArray: (name, jpath) => { | ||
return alwaysArray.indexOf(jpath) >= 0; | ||
}, | ||
}); | ||
const parsed = parser.parse(data); | ||
const files = parsed["checkstyle"]["file"]; | ||
let problems: Problem[] = []; | ||
for (const file of files) { | ||
problems = problems.concat(parseFile(trim, file)); | ||
} | ||
return problems; | ||
} | ||
|
||
export function run(pathTrim: string, checkstyleFile: string) { | ||
let trim: number; | ||
if (pathTrim === "") { | ||
trim = process.cwd().split(sep).length; | ||
} else { | ||
trim = parseInt(pathTrim); | ||
} | ||
const data = readFileSync(checkstyleFile); | ||
debug(data.toString()); | ||
logProblems(parseData(trim, data)); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.