chore: renaming

This commit is contained in:
Fernandez Ludovic 2025-02-09 01:49:49 +01:00
parent 9aa241a283
commit 658a19ec7f
3 changed files with 69 additions and 67 deletions

View file

@ -5,14 +5,13 @@ import os from "os"
import path from "path" import path from "path"
import { promisify } from "util" import { promisify } from "util"
import { VersionConfig } from "./version" import { VersionInfo } from "./version"
const execShellCommand = promisify(exec) const execShellCommand = promisify(exec)
const downloadURL = "https://github.com/golangci/golangci-lint/releases/download" const getAssetURL = (versionInfo: VersionInfo): string => {
const getAssetURL = (versionConfig: VersionConfig): string => {
let ext = "tar.gz" let ext = "tar.gz"
let platform = os.platform().toString() let platform = os.platform().toString()
switch (platform) { switch (platform) {
case "win32": case "win32":
@ -20,6 +19,7 @@ const getAssetURL = (versionConfig: VersionConfig): string => {
ext = "zip" ext = "zip"
break break
} }
let arch = os.arch() let arch = os.arch()
switch (arch) { switch (arch) {
case "arm64": case "arm64":
@ -33,9 +33,10 @@ const getAssetURL = (versionConfig: VersionConfig): string => {
arch = "386" arch = "386"
break break
} }
const noPrefix = versionConfig.TargetVersion.slice(1)
return `${downloadURL}/${versionConfig.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}` const noPrefix = versionInfo.TargetVersion.slice(1)
return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`
} }
export enum InstallMode { export enum InstallMode {
@ -61,31 +62,31 @@ const printOutput = (res: ExecRes): void => {
/** /**
* Install golangci-lint. * Install golangci-lint.
* *
* @param versionConfig information about version to install. * @param versionInfo information about version to install.
* @param mode installation mode. * @param mode installation mode.
* @returns path to installed binary of golangci-lint. * @returns path to installed binary of golangci-lint.
*/ */
export async function installLint(versionConfig: VersionConfig, mode: InstallMode): Promise<string> { export async function installLint(versionInfo: VersionInfo, mode: InstallMode): Promise<string> {
core.info(`Installation mode: ${mode}`) core.info(`Installation mode: ${mode}`)
switch (mode) { switch (mode) {
case InstallMode.Binary: case InstallMode.Binary:
return installBin(versionConfig) return installBin(versionInfo)
case InstallMode.GoInstall: case InstallMode.GoInstall:
return goInstall(versionConfig) return goInstall(versionInfo)
default: default:
return installBin(versionConfig) return installBin(versionInfo)
} }
} }
/** /**
* Install golangci-lint via `go install`. * Install golangci-lint via `go install`.
* *
* @param versionConfig information about version to install. * @param versionInfo information about version to install.
* @returns path to installed binary of golangci-lint. * @returns path to installed binary of golangci-lint.
*/ */
export async function goInstall(versionConfig: VersionConfig): Promise<string> { export async function goInstall(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint ${versionConfig.TargetVersion}...`) core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`)
const startedAt = Date.now() const startedAt = Date.now()
@ -93,43 +94,43 @@ export async function goInstall(versionConfig: VersionConfig): Promise<string> {
// TODO(ldez): it should be updated for v2. // TODO(ldez): it should be updated for v2.
const exres = await execShellCommand( const exres = await execShellCommand(
`go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, `go install github.com/golangci/golangci-lint/cmd/golangci-lint@${versionInfo.TargetVersion}`,
options options
) )
printOutput(exres) printOutput(exres)
// TODO(ldez): it should be updated for v2. // TODO(ldez): it should be updated for v2.
const res = await execShellCommand( const res = await execShellCommand(
`go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionConfig.TargetVersion}`, `go install -n github.com/golangci/golangci-lint/cmd/golangci-lint@${versionInfo.TargetVersion}`,
options options
) )
printOutput(res) printOutput(res)
// The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`. // The output of `go install -n` when the binary is already installed is `touch <path_to_the_binary>`.
const lintPath = res.stderr const binPath = res.stderr
.split(/\r?\n/) .split(/\r?\n/)
.map((v) => v.trimStart().trimEnd()) .map((v) => v.trimStart().trimEnd())
.filter((v) => v.startsWith("touch ")) .filter((v) => v.startsWith("touch "))
.reduce((a, b) => a + b, "") .reduce((a, b) => a + b, "")
.split(` `, 2)[1] .split(` `, 2)[1]
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`) core.info(`Installed golangci-lint into ${binPath} in ${Date.now() - startedAt}ms`)
return lintPath return binPath
} }
/** /**
* Install golangci-lint via the precompiled binary. * Install golangci-lint via the precompiled binary.
* *
* @param versionConfig information about version to install. * @param versionInfo information about version to install.
* @returns path to installed binary of golangci-lint. * @returns path to installed binary of golangci-lint.
*/ */
export async function installBin(versionConfig: VersionConfig): Promise<string> { export async function installBin(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint binary ${versionConfig.TargetVersion}...`) core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`)
const startedAt = Date.now() const startedAt = Date.now()
const assetURL = getAssetURL(versionConfig) const assetURL = getAssetURL(versionInfo)
core.info(`Downloading binary ${assetURL} ...`) core.info(`Downloading binary ${assetURL} ...`)
@ -151,9 +152,9 @@ export async function installBin(versionConfig: VersionConfig): Promise<string>
const urlParts = assetURL.split(`/`) const urlParts = assetURL.split(`/`)
const dirName = urlParts[urlParts.length - 1].replace(repl, ``) const dirName = urlParts[urlParts.length - 1].replace(repl, ``)
const lintPath = path.join(extractedDir, dirName, `golangci-lint`) const binPath = path.join(extractedDir, dirName, `golangci-lint`)
core.info(`Installed golangci-lint into ${lintPath} in ${Date.now() - startedAt}ms`) core.info(`Installed golangci-lint into ${binPath} in ${Date.now() - startedAt}ms`)
return lintPath return binPath
} }

View file

@ -11,7 +11,7 @@ import which from "which"
import { restoreCache, saveCache } from "./cache" import { restoreCache, saveCache } from "./cache"
import { installLint, InstallMode } from "./install" import { installLint, InstallMode } from "./install"
import { alterDiffPatch } from "./utils/diffUtils" import { alterDiffPatch } from "./utils/diffUtils"
import { findLintVersion } from "./version" import { getVersion } from "./version"
const execShellCommand = promisify(exec) const execShellCommand = promisify(exec)
const writeFile = promisify(fs.writeFile) const writeFile = promisify(fs.writeFile)
@ -25,16 +25,16 @@ async function prepareLint(): Promise<string> {
const mode = core.getInput("install-mode").toLowerCase() const mode = core.getInput("install-mode").toLowerCase()
if (mode === InstallMode.None) { if (mode === InstallMode.None) {
const bin = await which("golangci-lint", { nothrow: true }) const binPath = await which("golangci-lint", { nothrow: true })
if (!bin) { if (!binPath) {
throw new Error("golangci-lint binary not found in the PATH") throw new Error("golangci-lint binary not found in the PATH")
} }
return bin return binPath
} }
const versionConfig = await findLintVersion(<InstallMode>mode) const versionInfo = await getVersion(<InstallMode>mode)
return await installLint(versionConfig, <InstallMode>mode) return await installLint(versionInfo, <InstallMode>mode)
} }
async function fetchPatch(): Promise<string> { async function fetchPatch(): Promise<string> {
@ -141,7 +141,7 @@ async function fetchPushPatch(ctx: Context): Promise<string> {
} }
type Env = { type Env = {
lintPath: string binPath: string
patchPath: string patchPath: string
} }
@ -151,12 +151,12 @@ async function prepareEnv(): Promise<Env> {
// Prepare cache, lint and go in parallel. // Prepare cache, lint and go in parallel.
await restoreCache() await restoreCache()
const lintPath = await prepareLint() const binPath = await prepareLint()
const patchPath = await fetchPatch() const patchPath = await fetchPatch()
core.info(`Prepared env in ${Date.now() - startedAt}ms`) core.info(`Prepared env in ${Date.now() - startedAt}ms`)
return { lintPath, patchPath } return { binPath: binPath, patchPath }
} }
type ExecRes = { type ExecRes = {
@ -292,9 +292,9 @@ async function runLint(lintPath: string, patchPath: string): Promise<void> {
export async function run(): Promise<void> { export async function run(): Promise<void> {
try { try {
const { lintPath, patchPath } = await core.group(`prepare environment`, prepareEnv) const { binPath, patchPath } = await core.group(`prepare environment`, prepareEnv)
core.addPath(path.dirname(lintPath)) core.addPath(path.dirname(binPath))
await core.group(`run golangci-lint`, () => runLint(lintPath, patchPath)) await core.group(`run golangci-lint`, () => runLint(binPath, patchPath))
} catch (error) { } catch (error) {
core.error(`Failed to run: ${error}, ${error.stack}`) core.error(`Failed to run: ${error}, ${error.stack}`)
core.setFailed(error.message) core.setFailed(error.message)

View file

@ -1,7 +1,6 @@
import * as core from "@actions/core" import * as core from "@actions/core"
import * as httpm from "@actions/http-client" import * as httpm from "@actions/http-client"
import * as fs from "fs" import * as fs from "fs"
import os from "os"
import path from "path" import path from "path"
import { InstallMode } from "./install" import { InstallMode } from "./install"
@ -14,6 +13,7 @@ export type Version = {
} | null } | null
const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/ const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/
// TODO(ldez): it should be updated to match v2 module name.
const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/ const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/
const parseVersion = (s: string): Version => { const parseVersion = (s: string): Version => {
@ -64,8 +64,8 @@ const isLessVersion = (a: Version, b: Version): boolean => {
return a.minor < b.minor return a.minor < b.minor
} }
const getRequestedLintVersion = (): Version => { const getRequestedVersion = (): Version => {
let requestedLintVersion = core.getInput(`version`) let requestedVersion = core.getInput(`version`)
const workingDirectory = core.getInput(`working-directory`) const workingDirectory = core.getInput(`working-directory`)
let goMod = "go.mod" let goMod = "go.mod"
@ -73,43 +73,43 @@ const getRequestedLintVersion = (): Version => {
goMod = path.join(workingDirectory, goMod) goMod = path.join(workingDirectory, goMod)
} }
if (requestedLintVersion == "" && fs.existsSync(goMod)) { if (requestedVersion == "" && fs.existsSync(goMod)) {
const content = fs.readFileSync(goMod, "utf-8") const content = fs.readFileSync(goMod, "utf-8")
const match = content.match(modVersionRe) const match = content.match(modVersionRe)
if (match) { if (match) {
requestedLintVersion = match[1] requestedVersion = match[1]
core.info(`Found golangci-lint version '${requestedLintVersion}' in '${goMod}' file`) core.info(`Found golangci-lint version '${requestedVersion}' in '${goMod}' file`)
} }
} }
const parsedRequestedLintVersion = parseVersion(requestedLintVersion) const parsedRequestedVersion = parseVersion(requestedVersion)
if (parsedRequestedLintVersion == null) { if (parsedRequestedVersion == null) {
return null return null
} }
if (isLessVersion(parsedRequestedLintVersion, minVersion)) { if (isLessVersion(parsedRequestedVersion, minVersion)) {
throw new Error( throw new Error(
`requested golangci-lint version '${requestedLintVersion}' isn't supported: we support only ${stringifyVersion( `requested golangci-lint version '${requestedVersion}' isn't supported: we support only ${stringifyVersion(
minVersion minVersion
)} and later versions` )} and later versions`
) )
} }
return parsedRequestedLintVersion return parsedRequestedVersion
} }
export type VersionConfig = { export type VersionInfo = {
Error?: string Error?: string
TargetVersion: string TargetVersion: string
} }
type Config = { type VersionMapping = {
MinorVersionToConfig: { MinorVersionToConfig: {
[minorVersion: string]: VersionConfig [minorVersion: string]: VersionInfo
} }
} }
const fetchConfig = async (): Promise<Config> => { const fetchVersionMapping = async (): Promise<VersionMapping> => {
const http = new httpm.HttpClient(`golangci/golangci-lint-action`, [], { const http = new httpm.HttpClient(`golangci/golangci-lint-action`, [], {
allowRetries: true, allowRetries: true,
maxRetries: 5, maxRetries: 5,
@ -129,7 +129,7 @@ const fetchConfig = async (): Promise<Config> => {
} }
} }
export async function findLintVersion(mode: InstallMode): Promise<VersionConfig> { export async function getVersion(mode: InstallMode): Promise<VersionInfo> {
core.info(`Finding needed golangci-lint version...`) core.info(`Finding needed golangci-lint version...`)
if (mode == InstallMode.GoInstall) { if (mode == InstallMode.GoInstall) {
@ -138,38 +138,39 @@ export async function findLintVersion(mode: InstallMode): Promise<VersionConfig>
return { TargetVersion: v ? v : "latest" } return { TargetVersion: v ? v : "latest" }
} }
const reqLintVersion = getRequestedLintVersion() const reqVersion = getRequestedVersion()
// if the patched version is passed, just use it // if the patched version is passed, just use it
if (reqLintVersion?.major === 1 && reqLintVersion?.minor != null && reqLintVersion?.patch !== null) { // TODO(ldez): should be updated to `reqVersion?.major === 2`.
if (reqVersion?.major === 1 && reqVersion?.minor != null && reqVersion?.patch !== null) {
return new Promise((resolve) => { return new Promise((resolve) => {
const versionWithoutV = `${reqLintVersion.major}.${reqLintVersion.minor}.${reqLintVersion.patch}` const versionWithoutV = `${reqVersion.major}.${reqVersion.minor}.${reqVersion.patch}`
resolve({ TargetVersion: `v${versionWithoutV}` }) resolve({ TargetVersion: `v${versionWithoutV}` })
}) })
} }
const startedAt = Date.now() const startedAt = Date.now()
const config = await fetchConfig() const mapping = await fetchVersionMapping()
if (!config.MinorVersionToConfig) { if (!mapping.MinorVersionToConfig) {
core.warning(JSON.stringify(config)) core.warning(JSON.stringify(mapping))
throw new Error(`invalid config: no MinorVersionToConfig field`) throw new Error(`invalid config: no MinorVersionToConfig field`)
} }
const versionConfig = config.MinorVersionToConfig[stringifyVersion(reqLintVersion)] const versionInfo = mapping.MinorVersionToConfig[stringifyVersion(reqVersion)]
if (!versionConfig) { if (!versionInfo) {
throw new Error(`requested golangci-lint version '${stringifyVersion(reqLintVersion)}' doesn't exist`) throw new Error(`requested golangci-lint version '${stringifyVersion(reqVersion)}' doesn't exist`)
} }
if (versionConfig.Error) { if (versionInfo.Error) {
throw new Error(`failed to use requested golangci-lint version '${stringifyVersion(reqLintVersion)}': ${versionConfig.Error}`) throw new Error(`failed to use requested golangci-lint version '${stringifyVersion(reqVersion)}': ${versionInfo.Error}`)
} }
core.info( core.info(
`Requested golangci-lint '${stringifyVersion(reqLintVersion)}', using '${versionConfig.TargetVersion}', calculation took ${ `Requested golangci-lint '${stringifyVersion(reqVersion)}', using '${versionInfo.TargetVersion}', calculation took ${
Date.now() - startedAt Date.now() - startedAt
}ms` }ms`
) )
return versionConfig return versionInfo
} }