forked from reviewdog/action-golangci-lint
/
main.ts
122 lines (109 loc) · 3.82 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { promises as fs } from "fs";
import * as os from "os";
import * as path from "path";
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as io from "@actions/io";
import * as installer from "./installer";
import * as flags from "./flags";
import * as setupGo from "./setup-go/main";
import * as cache from "./cache";
async function run() {
const runnerTmpdir = process.env["RUNNER_TEMP"] || os.tmpdir();
const tmpdir = await fs.mkdtemp(path.join(runnerTmpdir, "reviewdog-"));
try {
const reviewdogVersion = core.getInput("reviewdog_version") || "latest";
const golangciLintVersion = core.getInput("golangci_lint_version") || "latest";
const goVersion = core.getInput("go_version");
const goVersionFile = core.getInput("go_version_file");
const golangciLintFlags = core.getInput("golangci_lint_flags");
const toolName = core.getInput("tool_name") || "golangci";
const level = core.getInput("level") || "error";
const reporter = core.getInput("reporter") || "github-pr-check";
const filterMode = core.getInput("filter_mode") || "added";
const failOnError = core.getInput("fail_on_error") || "false";
const reviewdogFlags = core.getInput("reviewdog_flags");
const workdir = core.getInput("workdir") || ".";
const cwd = path.relative(process.env["GITHUB_WORKSPACE"] || process.cwd(), workdir);
const enableCache = core.getBooleanInput("cache");
await core.group("Installing Go ...", async () => {
await setupGo.run(goVersion, goVersionFile);
});
const reviewdog = await core.group(
"🐶 Installing reviewdog ... https://github.com/reviewdog/reviewdog",
async () => {
return await installer.installReviewdog(reviewdogVersion, tmpdir);
}
);
const golangci = await core.group(
"Installing golangci-lint ... https://github.com/golangci/golangci-lint",
async () => {
return await installer.installGolangciLint(golangciLintVersion, tmpdir);
}
);
let cacheState: cache.State | undefined = undefined;
if (enableCache) {
cacheState = await core.group("Restoring cache ...", async () => {
return await cache.restore(cwd);
});
}
const code = await core.group("Running golangci-lint with reviewdog 🐶 ...", async (): Promise<number> => {
const output = await exec.getExecOutput(
golangci,
["run", "--out-format", "line-number", ...flags.parse(golangciLintFlags)],
{
cwd,
ignoreReturnCode: true,
}
);
process.env["REVIEWDOG_GITHUB_API_TOKEN"] = core.getInput("github_token");
return await exec.exec(
reviewdog,
[
"-f=golangci-lint",
`-name=${toolName}`,
`-reporter=${reporter}`,
`-filter-mode=${filterMode}`,
`-fail-on-error=${failOnError}`,
`-level=${level}`,
...flags.parse(reviewdogFlags),
],
{
cwd,
input: Buffer.from(output.stdout, "utf-8"),
ignoreReturnCode: true,
}
);
});
if (cacheState) {
await core.group("Saving cache ...", async () => {
if (cacheState) {
await cache.save(cacheState);
}
});
}
if (code !== 0) {
core.setFailed(`reviewdog exited with status code: ${code}`);
}
} catch (error) {
if (error instanceof Error) {
core.setFailed(error);
} else {
core.setFailed(`${error}`);
}
} finally {
// clean up the temporary directory
try {
await io.rmRF(tmpdir);
} catch (error) {
// suppress errors
// Garbage will remain, but it may be harmless.
if (error instanceof Error) {
core.info(`clean up failed: ${error.message}`);
} else {
core.info(`clean up failed: ${error}`);
}
}
}
}
run();