From 0dde2d62b41fc0657530cc7850b6ff7499ac9bce Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 28 Nov 2025 02:58:19 +0100 Subject: [PATCH 1/2] feat: add version-file option --- README.md | 53 ++++++++++++++++++++++++++++++++++++-------------- action.yml | 7 +++++++ src/version.ts | 27 +++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4d6e8b30d9..4ee51988ca 100644 --- a/README.md +++ b/README.md @@ -261,21 +261,22 @@ You will also likely need to add the following `.gitattributes` file to ensure t ### Overview -| Option | Description | -|---------------------------------------------------------------|----------------------------------------------------| -| [`version`](#version) | The version of golangci-lint to use. | -| [`install-mode`](#install-mode) | The mode to install golangci-lint. | -| [`install-only`](#install-only) | Only install golangci-lint. | -| [`verify`](#verify) | Validates golangci-lint configuration file. | -| [`github-token`](#github-token) | Used by the `only-new-issues` option. | -| [`only-new-issues`](#only-new-issues) | Show only new issues. | -| [`working-directory`](#working-directory) | The golangci-lint working directory. | -| [`args`](#args) | Golangci-lint command line arguments. | -| [`skip-cache`](#skip-cache) | Disable cache support. | -| [`skip-save-cache`](#skip-save-cache) | Don't save cache. | -| [`cache-invalidation-interval`](#cache-invalidation-interval) | Number of days before cache invalidation. | -| [`problem-matchers`](#problem-matchers) | Forces the usage of the embedded problem matchers. | -| [Experimental](#experimental) | Experimental options | +| Option | Description | +|---------------------------------------------------------------|-------------------------------------------------------| +| [`version`](#version) | The version of golangci-lint to use. | +| [`version-file`](#version-file) | Gets the version of golangci-lint to use from a file. | +| [`install-mode`](#install-mode) | The mode to install golangci-lint. | +| [`install-only`](#install-only) | Only install golangci-lint. | +| [`verify`](#verify) | Validates golangci-lint configuration file. | +| [`github-token`](#github-token) | Used by the `only-new-issues` option. | +| [`only-new-issues`](#only-new-issues) | Show only new issues. | +| [`working-directory`](#working-directory) | The golangci-lint working directory. | +| [`args`](#args) | Golangci-lint command line arguments. | +| [`skip-cache`](#skip-cache) | Disable cache support. | +| [`skip-save-cache`](#skip-save-cache) | Don't save cache. | +| [`cache-invalidation-interval`](#cache-invalidation-interval) | Number of days before cache invalidation. | +| [`problem-matchers`](#problem-matchers) | Forces the usage of the embedded problem matchers. | +| [Experimental](#experimental) | Experimental options | ### Installation @@ -302,6 +303,28 @@ with: +#### `version-file` + +Gets the version of golangci-lint to use from a file. + +The path must be relative to the root of the project, or the `working-directory` if defined. + +This parameter supports `.golangci-lint-version`, and `.tool-versions` files. + +Only works with `install-mode: binary` (the default). + +
+Example + +```yml +uses: golangci/golangci-lint-action@v9 +with: + version-file: .tool-versions + # ... +``` + +
+ #### `install-mode` (optional) diff --git a/action.yml b/action.yml index 4fedce7ceb..48e8c9bc30 100644 --- a/action.yml +++ b/action.yml @@ -11,6 +11,13 @@ inputs: - `goinstall`: the value can be v2.3.4, `latest`, or the hash of a commit. - `none`: the value is ignored. required: false + version-file: + description: | + Gets the version of golangci-lint to use from a file. + The path must be relative to the root of the project, or the `working-directory` if defined. + This parameter supports `.golangci-lint-version`, and `.tool-versions` files. + Only works with `install-mode: binary` (the default). + required: false install-mode: description: "The mode to install golangci-lint. It can be 'binary', 'goinstall', or 'none'." default: "binary" diff --git a/src/version.ts b/src/version.ts index e8ae68a1fd..59062c23f0 100644 --- a/src/version.ts +++ b/src/version.ts @@ -67,6 +67,12 @@ const isLessVersion = (a: Version, b: Version): boolean => { const getRequestedVersion = (): Version => { let requestedVersion = core.getInput(`version`) + let versionFilePath = core.getInput(`version-file`) + + if (requestedVersion && versionFilePath) { + core.warning(`Both version (${requestedVersion}) and version-file (${versionFilePath}) inputs are specified, only version will be used`) + } + const workingDirectory = core.getInput(`working-directory`) let goMod = "go.mod" @@ -83,6 +89,27 @@ const getRequestedVersion = (): Version => { } } + if (requestedVersion == "" && versionFilePath) { + if (workingDirectory) { + versionFilePath = path.join(workingDirectory, versionFilePath) + } + + if (!fs.existsSync(versionFilePath)) { + throw new Error(`The specified golangci-lint version file at: ${versionFilePath} does not exist`) + } + + const content = fs.readFileSync(versionFilePath, "utf-8") + + if (path.basename(versionFilePath) === ".tool-versions") { + // asdf/mise file. + const match = content.match(/^golangci-lint\s+([^\n#]+)/m) + requestedVersion = match ? "v" + match[1].trim().replace(/^v/gi, "") : "" + } else { + // .golangci-lint-version file. + requestedVersion = "v" + content.trim().replace(/^v/gi, "") + } + } + const parsedRequestedVersion = parseVersion(requestedVersion) if (parsedRequestedVersion == null) { return null From b8bd766d90112a6f869e724423723b2f5b94ab5b Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 28 Nov 2025 16:42:26 +0100 Subject: [PATCH 2/2] chore: generate --- dist/post_run/index.js | 22 ++++++++++++++++++++++ dist/run/index.js | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/dist/post_run/index.js b/dist/post_run/index.js index f86b54ad45..3fe5749f16 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -97912,6 +97912,10 @@ const isLessVersion = (a, b) => { }; const getRequestedVersion = () => { let requestedVersion = core.getInput(`version`); + let versionFilePath = core.getInput(`version-file`); + if (requestedVersion && versionFilePath) { + core.warning(`Both version (${requestedVersion}) and version-file (${versionFilePath}) inputs are specified, only version will be used`); + } const workingDirectory = core.getInput(`working-directory`); let goMod = "go.mod"; if (workingDirectory) { @@ -97925,6 +97929,24 @@ const getRequestedVersion = () => { core.info(`Found golangci-lint version '${requestedVersion}' in '${goMod}' file`); } } + if (requestedVersion == "" && versionFilePath) { + if (workingDirectory) { + versionFilePath = path_1.default.join(workingDirectory, versionFilePath); + } + if (!fs.existsSync(versionFilePath)) { + throw new Error(`The specified golangci-lint version file at: ${versionFilePath} does not exist`); + } + const content = fs.readFileSync(versionFilePath, "utf-8"); + if (path_1.default.basename(versionFilePath) === ".tool-versions") { + // asdf/mise file. + const match = content.match(/^golangci-lint\s+([^\n#]+)/m); + requestedVersion = match ? "v" + match[1].trim().replace(/^v/gi, "") : ""; + } + else { + // .golangci-lint-version file. + requestedVersion = "v" + content.trim().replace(/^v/gi, ""); + } + } const parsedRequestedVersion = parseVersion(requestedVersion); if (parsedRequestedVersion == null) { return null; diff --git a/dist/run/index.js b/dist/run/index.js index 98236e9dcf..5a838212d7 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -97912,6 +97912,10 @@ const isLessVersion = (a, b) => { }; const getRequestedVersion = () => { let requestedVersion = core.getInput(`version`); + let versionFilePath = core.getInput(`version-file`); + if (requestedVersion && versionFilePath) { + core.warning(`Both version (${requestedVersion}) and version-file (${versionFilePath}) inputs are specified, only version will be used`); + } const workingDirectory = core.getInput(`working-directory`); let goMod = "go.mod"; if (workingDirectory) { @@ -97925,6 +97929,24 @@ const getRequestedVersion = () => { core.info(`Found golangci-lint version '${requestedVersion}' in '${goMod}' file`); } } + if (requestedVersion == "" && versionFilePath) { + if (workingDirectory) { + versionFilePath = path_1.default.join(workingDirectory, versionFilePath); + } + if (!fs.existsSync(versionFilePath)) { + throw new Error(`The specified golangci-lint version file at: ${versionFilePath} does not exist`); + } + const content = fs.readFileSync(versionFilePath, "utf-8"); + if (path_1.default.basename(versionFilePath) === ".tool-versions") { + // asdf/mise file. + const match = content.match(/^golangci-lint\s+([^\n#]+)/m); + requestedVersion = match ? "v" + match[1].trim().replace(/^v/gi, "") : ""; + } + else { + // .golangci-lint-version file. + requestedVersion = "v" + content.trim().replace(/^v/gi, ""); + } + } const parsedRequestedVersion = parseVersion(requestedVersion); if (parsedRequestedVersion == null) { return null;