Skip to content

Commit

Permalink
Extract some code from checkstyle action.ts for (hopefully) easier de…
Browse files Browse the repository at this point in the history
…bugging

Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Apr 25, 2024
1 parent 1da6328 commit 8f6856d
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 113 deletions.
47 changes: 16 additions & 31 deletions actions/checkstyle/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28909,23 +28909,6 @@ module.exports = parseParams
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __nccwpck_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
Expand All @@ -28949,11 +28932,6 @@ var __webpack_exports__ = {};
// ESM COMPAT FLAG
__nccwpck_require__.r(__webpack_exports__);

// EXPORTS
__nccwpck_require__.d(__webpack_exports__, {
"parseData": () => (/* binding */ action_parseData)
});

// EXTERNAL MODULE: ../../node_modules/@actions/core/lib/core.js
var core = __nccwpck_require__(7117);
// EXTERNAL MODULE: external "fs"
Expand Down Expand Up @@ -29029,13 +29007,13 @@ run().catch((err) => (0,core.setFailed)(err));

// EXTERNAL MODULE: external "path"
var external_path_ = __nccwpck_require__(1017);
;// CONCATENATED MODULE: ./src/action.ts
;// CONCATENATED MODULE: ./src/checkstyle.ts





function action_parseFile(trim, fileData) {
function checkstyle_parseFile(trim, fileData) {
let subFile = fileData["@_name"].split(external_path_.sep);
let currentlyTrimmed = 0;
while (!(0,external_fs_.existsSync)((0,external_path_.join)(...subFile))) {
Expand Down Expand Up @@ -29066,7 +29044,7 @@ function action_parseFile(trim, fileData) {
}
return problems;
}
function action_parseData(trim, data) {
function checkstyle_parseData(trim, data) {
const alwaysArray = ["checkstyle.file", "checkstyle.file.error"];
const parser = new fxp.XMLParser({
ignoreAttributes: false,
Expand All @@ -29078,13 +29056,11 @@ function action_parseData(trim, data) {
const files = parsed["checkstyle"]["file"];
let problems = [];
for (const file of files) {
problems = problems.concat(action_parseFile(trim, file));
problems = problems.concat(checkstyle_parseFile(trim, file));
}
return problems;
}
async function action_run() {
const checkstyleFile = (0,core.getInput)("file");
const pathTrim = (0,core.getInput)("pathTrim");
function checkstyle_run(pathTrim, checkstyleFile) {
let trim;
if (pathTrim === "") {
trim = process.cwd().split(external_path_.sep).length;
Expand All @@ -29094,10 +29070,19 @@ async function action_run() {
}
const data = (0,external_fs_.readFileSync)(checkstyleFile);
(0,core.debug)(data.toString());
logProblems(action_parseData(trim, data));
logProblems(checkstyle_parseData(trim, data));
}

;// CONCATENATED MODULE: ./src/action.ts


async function action_run() {
const checkstyleFile = (0,core.getInput)("file");
const pathTrim = (0,core.getInput)("pathTrim");
checkstyle_run(checkstyleFile, pathTrim);
}
action_run().catch((err) => {
console.log(err);
console.log(err.stack);
(0,core.setFailed)(err);
});

Expand Down
84 changes: 4 additions & 80 deletions actions/checkstyle/src/action.ts
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);
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseData } from "./action";
import { parseData, run as actionRun } from "./checkstyle";
import { describe, expect, test } from "@jest/globals";
import { expectProblem } from "pmd/src/problem.test";

Expand Down Expand Up @@ -30,4 +30,9 @@ describe("Test action/parseData", () => {
problems[0],
);
});
test("JOSM Checkstyle XML, single file", () => {
expect(() =>
actionRun("", "/Users/tsmock/workspace/josm/core/checkstyle-josm.xml"),
).not.toThrow();
});
});
82 changes: 82 additions & 0 deletions actions/checkstyle/src/checkstyle.ts
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));
}
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f6856d

Please sign in to comment.