feat: add version-file option (#1320)

This commit is contained in:
Ludovic Fernandez 2025-11-28 16:54:48 +01:00 committed by GitHub
parent a6071aaacb
commit aa6fad0ea0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 116 additions and 15 deletions

View file

@ -262,8 +262,9 @@ You will also likely need to add the following `.gitattributes` file to ensure t
### Overview ### Overview
| Option | Description | | Option | Description |
|---------------------------------------------------------------|----------------------------------------------------| |---------------------------------------------------------------|-------------------------------------------------------|
| [`version`](#version) | The version of golangci-lint to use. | | [`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-mode`](#install-mode) | The mode to install golangci-lint. |
| [`install-only`](#install-only) | Only install golangci-lint. | | [`install-only`](#install-only) | Only install golangci-lint. |
| [`verify`](#verify) | Validates golangci-lint configuration file. | | [`verify`](#verify) | Validates golangci-lint configuration file. |
@ -302,6 +303,28 @@ with:
</details> </details>
#### `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).
<details>
<summary>Example</summary>
```yml
uses: golangci/golangci-lint-action@v9
with:
version-file: .tool-versions
# ...
```
</details>
#### `install-mode` #### `install-mode`
(optional) (optional)

View file

@ -11,6 +11,13 @@ inputs:
- `goinstall`: the value can be v2.3.4, `latest`, or the hash of a commit. - `goinstall`: the value can be v2.3.4, `latest`, or the hash of a commit.
- `none`: the value is ignored. - `none`: the value is ignored.
required: false 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: install-mode:
description: "The mode to install golangci-lint. It can be 'binary', 'goinstall', or 'none'." description: "The mode to install golangci-lint. It can be 'binary', 'goinstall', or 'none'."
default: "binary" default: "binary"

22
dist/post_run/index.js generated vendored
View file

@ -97912,6 +97912,10 @@ const isLessVersion = (a, b) => {
}; };
const getRequestedVersion = () => { const getRequestedVersion = () => {
let requestedVersion = core.getInput(`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`); const workingDirectory = core.getInput(`working-directory`);
let goMod = "go.mod"; let goMod = "go.mod";
if (workingDirectory) { if (workingDirectory) {
@ -97925,6 +97929,24 @@ const getRequestedVersion = () => {
core.info(`Found golangci-lint version '${requestedVersion}' in '${goMod}' file`); 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); const parsedRequestedVersion = parseVersion(requestedVersion);
if (parsedRequestedVersion == null) { if (parsedRequestedVersion == null) {
return null; return null;

22
dist/run/index.js generated vendored
View file

@ -97912,6 +97912,10 @@ const isLessVersion = (a, b) => {
}; };
const getRequestedVersion = () => { const getRequestedVersion = () => {
let requestedVersion = core.getInput(`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`); const workingDirectory = core.getInput(`working-directory`);
let goMod = "go.mod"; let goMod = "go.mod";
if (workingDirectory) { if (workingDirectory) {
@ -97925,6 +97929,24 @@ const getRequestedVersion = () => {
core.info(`Found golangci-lint version '${requestedVersion}' in '${goMod}' file`); 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); const parsedRequestedVersion = parseVersion(requestedVersion);
if (parsedRequestedVersion == null) { if (parsedRequestedVersion == null) {
return null; return null;

View file

@ -67,6 +67,12 @@ const isLessVersion = (a: Version, b: Version): boolean => {
const getRequestedVersion = (): Version => { const getRequestedVersion = (): Version => {
let requestedVersion = core.getInput(`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`) const workingDirectory = core.getInput(`working-directory`)
let goMod = "go.mod" 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) const parsedRequestedVersion = parseVersion(requestedVersion)
if (parsedRequestedVersion == null) { if (parsedRequestedVersion == null) {
return null return null