-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathindex.js
139 lines (117 loc) · 5.08 KB
/
index.js
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const { join } = require("path");
const core = require("@actions/core");
const git = require("./git");
const { createCheck } = require("./github/api");
const { getContext } = require("./github/context");
const linters = require("./linters");
const { getSummary } = require("./utils/lint-result");
/**
* Parses the action configuration and runs all enabled linters on matching files
*/
async function runAction() {
const context = getContext();
const autoFix = core.getInput("auto_fix") === "true";
const continueOnError = core.getInput("continue_on_error") === "true";
const gitName = core.getInput("git_name", { required: true });
const gitEmail = core.getInput("git_email", { required: true });
const commitMessage = core.getInput("commit_message", { required: true });
const checkName = core.getInput("check_name", { required: true });
const isPullRequest =
context.eventName === "pull_request" || context.eventName === "pull_request_target";
// If on a PR from fork: Display messages regarding action limitations
if (isPullRequest && context.repository.hasFork) {
core.error(
"This action does not have permission to create annotations on forks. You may want to run it only on `push` events. See https://github.com/wearerequired/lint-action/issues/13 for details",
);
if (autoFix) {
core.error(
"This action does not have permission to push to forks. You may want to run it only on `push` events. See https://github.com/wearerequired/lint-action/issues/13 for details",
);
}
}
if (autoFix) {
// Set Git committer username and password
git.setUserInfo(gitName, gitEmail);
}
if (isPullRequest) {
// Fetch and check out PR branch:
// - "push" event: Already on correct branch
// - "pull_request" event on origin, for code on origin: The Checkout Action
// (https://github.com/actions/checkout) checks out the PR's test merge commit instead of the
// PR branch. Git is therefore in detached head state. To be able to push changes, the branch
// needs to be fetched and checked out first
// - "pull_request" event on origin, for code on fork: Same as above, but the repo/branch where
// changes need to be pushed is not yet available. The fork needs to be added as a Git remote
// first
git.checkOutRemoteBranch(context);
}
let headSha = git.getHeadSha();
let hasFailures = false;
const checks = [];
// Loop over all available linters
for (const [linterId, linter] of Object.entries(linters)) {
// Determine whether the linter should be executed on the commit
if (core.getInput(linterId) === "true") {
core.startGroup(`Run ${linter.name}`);
const fileExtensions = core.getInput(`${linterId}_extensions`, { required: true });
const args = core.getInput(`${linterId}_args`);
const lintDirRel = core.getInput(`${linterId}_dir`) || ".";
const prefix = core.getInput(`${linterId}_command_prefix`);
const lintDirAbs = join(context.workspace, lintDirRel);
// Check that the linter and its dependencies are installed
core.info(`Verifying setup for ${linter.name}…`);
await linter.verifySetup(lintDirAbs, prefix);
core.info(`Verified ${linter.name} setup`);
// Determine which files should be linted
const fileExtList = fileExtensions.split(",");
core.info(`Will use ${linter.name} to check the files with extensions ${fileExtList}`);
// Lint and optionally auto-fix the matching files, parse code style violations
core.info(
`Linting ${autoFix ? "and auto-fixing " : ""}files in ${lintDirAbs} with ${linter.name}…`,
);
const lintOutput = linter.lint(lintDirAbs, fileExtList, args, autoFix, prefix);
// Parse output of linting command
const lintResult = linter.parseOutput(context.workspace, lintOutput);
const summary = getSummary(lintResult);
core.info(
`${linter.name} found ${summary} (${lintResult.isSuccess ? "success" : "failure"})`,
);
if (!lintResult.isSuccess) {
hasFailures = true;
}
if (autoFix) {
// Commit and push auto-fix changes
if (git.hasChanges()) {
git.commitChanges(commitMessage.replace(/\${linter}/g, linter.name));
git.pushChanges();
}
}
const lintCheckName = checkName
.replace(/\${linter}/g, linter.name)
.replace(/\${dir}/g, lintDirRel !== "." ? `${lintDirRel}` : "")
.trim();
checks.push({ lintCheckName, lintResult, summary });
core.endGroup();
}
}
// Add commit annotations after running all linters. To be displayed on pull requests, the
// annotations must be added to the last commit on the branch. This can either be a user commit or
// one of the auto-fix commits
if (isPullRequest && autoFix) {
headSha = git.getHeadSha();
}
core.startGroup("Create check runs with commit annotations");
await Promise.all(
checks.map(({ lintCheckName, lintResult, summary }) =>
createCheck(lintCheckName, headSha, context, lintResult, summary),
),
);
core.endGroup();
if (hasFailures && !continueOnError) {
core.setFailed("Linting failures detected. See check runs with annotations for details.");
}
}
runAction().catch((error) => {
core.debug(error.stack || "No error stack trace");
core.setFailed(error.message);
});