feat: rewrite format handling (#1038)

This commit is contained in:
Ludovic Fernandez 2024-05-07 01:40:17 +02:00 committed by GitHub
parent d36b91c294
commit 789f114c52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 91 additions and 43 deletions

View File

@ -10,6 +10,7 @@ The action runs [golangci-lint](https://github.com/golangci/golangci-lint) and r
## Compatibility
* `v6.0.0+` removes `annotations` option.
* `v5.0.0+` removes `skip-pkg-cache` and `skip-build-cache` because the cache related to Go itself is already handled by `actions/setup-go`.
* `v4.0.0+` requires an explicit `actions/setup-go` installation step before using this action: `uses: actions/setup-go@v5`.
The `skip-go-installation` option has been removed.
@ -212,23 +213,24 @@ with:
If set the number is `<= 0`, the cache will be always invalidate (Not recommended).
### `annotations`
### `problem-matchers`
(optional)
To enable/disable GitHub Action annotations.
Force the usage of the embedded problem matchers.
If disabled (`false`), the output format(s) will follow the golangci-lint configuration file (or CLI flags from `args`)
and use the same default as golangci-lint (i.e. `colored-line-number`).
By default, the [problem matcher of Go (`actions/setup-go`)](https://github.com/actions/setup-go/blob/main/matchers.json) already handles the golangci-lint output (`colored-line-number`).
Works only with `colored-line-number` (the golangci-lint default).
https://golangci-lint.run/usage/configuration/#output-configuration
The default value is `true`.
The default value is `false`.
```yml
uses: golangci/golangci-lint-action@v5
with:
annotations: false
problem-matchers: true
# ...
```

View File

@ -36,9 +36,9 @@ inputs:
restore existing caches, subject to other options.
default: 'false'
required: false
annotations:
description: "To Enable/disable GitHub Action annotations"
default: 'true'
problem-matchers:
description: "Force the usage of the embedded problem matchers"
default: 'false'
required: false
args:
description: "golangci-lint command line arguments"

31
dist/post_run/index.js generated vendored
View File

@ -89290,18 +89290,27 @@ async function runLint(lintPath, patchPath) {
.map(([key, value]) => [key.toLowerCase(), value ?? ""]);
const userArgsMap = new Map(userArgsList);
const userArgNames = new Set(userArgsList.map(([key]) => key));
const annotations = core.getBooleanInput(`annotations`);
if (annotations) {
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`))
.concat("github-actions")
.join(",");
addedArgs.push(`--out-format=${formats}`);
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim();
const problemMatchers = core.getBooleanInput(`problem-matchers`);
if (problemMatchers) {
const matchersPath = path.join(__dirname, "../..", "problem-matchers.json");
if (fs.existsSync(matchersPath)) {
// Adds problem matchers.
// https://github.com/actions/setup-go/blob/cdcb36043654635271a94b9a6d1392de5bb323a7/src/main.ts#L81-L83
core.info(`##[add-matcher]${matchersPath}`);
}
}
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`)) // Removes `github-actions` format.
.join(",");
if (formats) {
// Adds formats but without `github-actions` format.
addedArgs.push(`--out-format=${formats}`);
}
// Removes `--out-format` from the user flags because it's already inside `addedArgs`.
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim();
if (isOnlyNewIssues()) {
if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) {
throw new Error(`please, don't specify manually --new* args when requesting only new issues`);

31
dist/run/index.js generated vendored
View File

@ -89290,18 +89290,27 @@ async function runLint(lintPath, patchPath) {
.map(([key, value]) => [key.toLowerCase(), value ?? ""]);
const userArgsMap = new Map(userArgsList);
const userArgNames = new Set(userArgsList.map(([key]) => key));
const annotations = core.getBooleanInput(`annotations`);
if (annotations) {
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`))
.concat("github-actions")
.join(",");
addedArgs.push(`--out-format=${formats}`);
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim();
const problemMatchers = core.getBooleanInput(`problem-matchers`);
if (problemMatchers) {
const matchersPath = path.join(__dirname, "../..", "problem-matchers.json");
if (fs.existsSync(matchersPath)) {
// Adds problem matchers.
// https://github.com/actions/setup-go/blob/cdcb36043654635271a94b9a6d1392de5bb323a7/src/main.ts#L81-L83
core.info(`##[add-matcher]${matchersPath}`);
}
}
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`)) // Removes `github-actions` format.
.join(",");
if (formats) {
// Adds formats but without `github-actions` format.
addedArgs.push(`--out-format=${formats}`);
}
// Removes `--out-format` from the user flags because it's already inside `addedArgs`.
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim();
if (isOnlyNewIssues()) {
if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) {
throw new Error(`please, don't specify manually --new* args when requesting only new issues`);

17
problem-matchers.json Normal file
View File

@ -0,0 +1,17 @@
{
"problemMatcher": [
{
"owner": "golangci-lint-colored-line-number",
"severity": "error",
"pattern": [
{
"regexp": "^([^:]+):(\\d+):(?:(\\d+):)?\\s+(.+ \\(.+\\))$",
"file": 1,
"line": 2,
"column": 3,
"message": 4
}
]
}
]
}

View File

@ -185,21 +185,32 @@ async function runLint(lintPath: string, patchPath: string): Promise<void> {
const userArgsMap = new Map<string, string>(userArgsList)
const userArgNames = new Set<string>(userArgsList.map(([key]) => key))
const annotations = core.getBooleanInput(`annotations`)
const problemMatchers = core.getBooleanInput(`problem-matchers`)
if (annotations) {
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`))
.concat("github-actions")
.join(",")
addedArgs.push(`--out-format=${formats}`)
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim()
if (problemMatchers) {
const matchersPath = path.join(__dirname, "../..", "problem-matchers.json")
if (fs.existsSync(matchersPath)) {
// Adds problem matchers.
// https://github.com/actions/setup-go/blob/cdcb36043654635271a94b9a6d1392de5bb323a7/src/main.ts#L81-L83
core.info(`##[add-matcher]${matchersPath}`)
}
}
const formats = (userArgsMap.get("out-format") || "")
.trim()
.split(",")
.filter((f) => f.length > 0)
.filter((f) => !f.startsWith(`github-actions`)) // Removes `github-actions` format.
.join(",")
if (formats) {
// Adds formats but without `github-actions` format.
addedArgs.push(`--out-format=${formats}`)
}
// Removes `--out-format` from the user flags because it's already inside `addedArgs`.
userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim()
if (isOnlyNewIssues()) {
if (userArgNames.has(`new`) || userArgNames.has(`new-from-rev`) || userArgNames.has(`new-from-patch`)) {
throw new Error(`please, don't specify manually --new* args when requesting only new issues`)