From 789f114c52eaee9275c87b15fc90e449e56d1b0a Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Tue, 7 May 2024 01:40:17 +0200 Subject: [PATCH] feat: rewrite format handling (#1038) --- README.md | 14 ++++++++------ action.yml | 6 +++--- dist/post_run/index.js | 29 +++++++++++++++++++---------- dist/run/index.js | 29 +++++++++++++++++++---------- problem-matchers.json | 17 +++++++++++++++++ src/run.ts | 31 +++++++++++++++++++++---------- 6 files changed, 87 insertions(+), 39 deletions(-) create mode 100644 problem-matchers.json diff --git a/README.md b/README.md index 5e498a32a7..536ae58c5c 100644 --- a/README.md +++ b/README.md @@ -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 # ... ``` diff --git a/action.yml b/action.yml index bb4a50870e..6a5d1ed648 100644 --- a/action.yml +++ b/action.yml @@ -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" diff --git a/dist/post_run/index.js b/dist/post_run/index.js index 87f67815a6..77ebe7bf45 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -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(","); + 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}`); - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); } + // 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`); diff --git a/dist/run/index.js b/dist/run/index.js index 11ef531598..5a8e29b828 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -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(","); + 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}`); - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); } + // 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`); diff --git a/problem-matchers.json b/problem-matchers.json new file mode 100644 index 0000000000..6032b7f7aa --- /dev/null +++ b/problem-matchers.json @@ -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 + } + ] + } + ] +} \ No newline at end of file diff --git a/src/run.ts b/src/run.ts index 9cffa9c690..a19a621874 100644 --- a/src/run.ts +++ b/src/run.ts @@ -185,21 +185,32 @@ async function runLint(lintPath: string, patchPath: string): Promise { const userArgsMap = new Map(userArgsList) const userArgNames = new Set(userArgsList.map(([key]) => key)) - const annotations = core.getBooleanInput(`annotations`) + 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}`) + } + } - 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(",") + 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}`) - userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim() } + // 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`)