Skip to content

Commit 573a4b1

Browse files
committed
Add json-file option
1 parent c4d8bb0 commit 573a4b1

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ inputs:
1212
description: 'Path or JSON array of paths to check with hlint'
1313
required: false
1414
default: '.'
15+
json-file:
16+
description: 'Existing hlint JSON output file to use instead of running hlint'
17+
required: false
1518
outputs:
1619
ideas:
1720
description: 'A JSON array of HLint output ideas (errors, warnings, suggestions, and so on)'

src/inputs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ function parseCheckMode(arg: string): CheckMode {
3434
const INPUT_KEY_HLINT_BIN = 'hlint-bin';
3535
const INPUT_KEY_HLINT_FILES = 'path';
3636
const INPUT_KEY_HLINT_FAIL_MODE = 'fail-on';
37+
const INPUT_KEY_JSON_FILE = 'json-file';
3738

3839
export default function getInputs(): RunArgs {
3940
const hlintCmd = core.getInput(INPUT_KEY_HLINT_BIN, {required: false}) || 'hlint';
4041
const pathList = parseStringOrJsonArray(core.getInput(INPUT_KEY_HLINT_FILES, {required: false}) || '.');
4142
const failOn = parseCheckMode(core.getInput(INPUT_KEY_HLINT_FAIL_MODE, {required: false}) || 'NEVER');
43+
const jsonFile = core.getInput(INPUT_KEY_JSON_FILE, {required: false});
4244

4345
// NOTE: Because ncc compiles all the files, take care that __dirname represents the dist/ folder.
4446
const baseDir = path.join(__dirname, '..');
4547

46-
return {baseDir, hlintCmd, pathList, failOn};
48+
return {baseDir, hlintCmd, jsonFile, pathList, failOn};
4749
}

src/run.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as core from '@actions/core'
22
import * as path from 'path'
3+
import * as fs from 'fs'
34
import {
45
Idea as HLintIdea,
56
Severity as HLintSeverity,
@@ -15,6 +16,7 @@ export type CheckMode = HLintSeverity | 'STATUS' | 'NEVER';
1516
export interface RunArgs {
1617
baseDir: string,
1718
hlintCmd: string,
19+
jsonFile: string,
1820
pathList: string[],
1921
failOn: CheckMode,
2022
};
@@ -42,6 +44,26 @@ async function runHLint(cmd: string, args: string[]): Promise<HLintResult> {
4244
return {ideas, statusCode};
4345
}
4446

47+
async function readHLintFile(path: string): Promise<HLintResult> {
48+
const fileContents = await fs.promises.readFile(path, 'utf8');
49+
const hints: Array<HLintIdea> = JSON.parse(fileContents) || [];
50+
hints.forEach(hint => {
51+
const fromTo = hint.to
52+
? [`(Found: ${hint.from})`, `(Perhaps: ${hint.to})`]
53+
: [`(Remove: ${hint.from})`];
54+
const message = [...fromTo, ...hint.note].join('\n');
55+
const properties = {...hint, title: `${hint.severity}: ${hint.hint}`};
56+
if (hint.severity == "Error") {
57+
core.error(message, properties);
58+
} else {
59+
core.warning(message, properties);
60+
}
61+
});
62+
const ideas = hints;
63+
const statusCode = ideas.length;
64+
return {ideas, statusCode};
65+
}
66+
4567
function getOverallCheckResult(failOn: CheckMode, {ideas, statusCode}: HLintResult): CheckResult {
4668
const hintsBySev = HLINT_SEV_LEVELS.map(sev => ([sev, ideas.filter(hint => hint.severity === sev).length]));
4769
const hintSummary = hintsBySev
@@ -66,10 +88,16 @@ function getOverallCheckResult(failOn: CheckMode, {ideas, statusCode}: HLintResu
6688
return {ok, hintSummary}
6789
}
6890

69-
export default async function run({baseDir, hlintCmd, pathList, failOn}: RunArgs): Promise<RunResult> {
70-
const hlintArgs = ['-j', '--json', '--', ...pathList]
71-
const matcherDefPath = path.join(baseDir, MATCHER_DEF_PATH);
72-
const {ideas, statusCode} = await withMatcherAtPath(matcherDefPath, () => runHLint(hlintCmd, hlintArgs));
73-
const {ok, hintSummary} = getOverallCheckResult(failOn, {ideas, statusCode});
74-
return {ok, statusCode, ideas, hintSummary};
91+
export default async function run({baseDir, hlintCmd, jsonFile, pathList, failOn}: RunArgs): Promise<RunResult> {
92+
if (jsonFile) {
93+
const {ideas, statusCode} = await readHLintFile(jsonFile);
94+
const {ok, hintSummary} = getOverallCheckResult(failOn, {ideas, statusCode});
95+
return {ok, statusCode, ideas, hintSummary};
96+
} else {
97+
const hlintArgs = ['-j', '--json', '--', ...pathList]
98+
const matcherDefPath = path.join(baseDir, MATCHER_DEF_PATH);
99+
const {ideas, statusCode} = await withMatcherAtPath(matcherDefPath, () => runHLint(hlintCmd, hlintArgs));
100+
const {ok, hintSummary} = getOverallCheckResult(failOn, {ideas, statusCode});
101+
return {ok, statusCode, ideas, hintSummary};
102+
}
75103
}

0 commit comments

Comments
 (0)