From b0de7a9f9668c9c6b7645652c319f0a73ba9d4ca Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Thu, 11 Jul 2024 00:38:33 +0200 Subject: [PATCH] feat: improvz --- src/install.ts | 12 ++-- src/run.ts | 5 +- src/version.ts | 174 ------------------------------------------------- 3 files changed, 7 insertions(+), 184 deletions(-) delete mode 100644 src/version.ts diff --git a/src/install.ts b/src/install.ts index f4bb3869d4..8a40f90670 100644 --- a/src/install.ts +++ b/src/install.ts @@ -3,8 +3,6 @@ import { exec } from "child_process" import path from "path" import { promisify } from "util" -import { VersionConfig } from "./version" - const execShellCommand = promisify(exec) export enum InstallMode { @@ -33,10 +31,10 @@ const printOutput = (res: ExecRes): void => { * @param mode installation mode. * @returns path to installed binary of golangci-lint. */ -export async function installLint(versionConfig: VersionConfig, mode: InstallMode): Promise { +export async function installLint(version: string, mode: InstallMode): Promise { core.info(`Installation mode: ${mode}`) - return goInstall(versionConfig) + return goInstall(version) } /** @@ -45,15 +43,15 @@ export async function installLint(versionConfig: VersionConfig, mode: InstallMod * @param versionConfig information about version to install. * @returns path to installed binary of gno-lint. */ -export async function goInstall(versionConfig: VersionConfig): Promise { - core.info(`Installing gno-lint ${versionConfig.TargetVersion}...`) +export async function goInstall(version: string): Promise { + core.info(`Installing gno-lint ${version}...`) const startedAt = Date.now() const clres = await execShellCommand(`git clone https://github.com/gnolang/gno.git`) printOutput(clres) - const chres = await execShellCommand(`cd gno && git checkout ${versionConfig.TargetVersion}`) + const chres = await execShellCommand(`cd gno && git checkout ${version}`) printOutput(chres) const bres = await execShellCommand(`cd gnovm && make build && make install`) diff --git a/src/run.ts b/src/run.ts index d5f6092b91..d136013126 100644 --- a/src/run.ts +++ b/src/run.ts @@ -10,7 +10,6 @@ import { promisify } from "util" import { restoreCache, saveCache } from "./cache" import { installLint, InstallMode } from "./install" import { alterDiffPatch } from "./utils/diffUtils" -import { findLintVersion } from "./version" const execShellCommand = promisify(exec) const writeFile = promisify(fs.writeFile) @@ -22,9 +21,9 @@ function isOnlyNewIssues(): boolean { async function prepareLint(): Promise { const mode = core.getInput("install-mode").toLowerCase() - const versionConfig = await findLintVersion(mode) + const v: string = core.getInput(`version`) || "latest" - return await installLint(versionConfig, mode) + return await installLint(v, mode) } async function fetchPatch(): Promise { diff --git a/src/version.ts b/src/version.ts deleted file mode 100644 index aaa294697c..0000000000 --- a/src/version.ts +++ /dev/null @@ -1,174 +0,0 @@ -import * as core from "@actions/core" -import * as httpm from "@actions/http-client" -import * as fs from "fs" -import path from "path" - -import { InstallMode } from "./install" - -// TODO: make a class -export type Version = { - major: number - minor: number - patch: number | null -} | null - -const versionRe = /^v(\d+)\.(\d+)(?:\.(\d+))?$/ -const modVersionRe = /github.com\/golangci\/golangci-lint\s(v.+)/ - -const parseVersion = (s: string): Version => { - if (s == "latest" || s == "") { - return null - } - - const match = s.match(versionRe) - if (!match) { - throw new Error(`invalid version string '${s}', expected format v1.2 or v1.2.3`) - } - - return { - major: parseInt(match[1]), - minor: parseInt(match[2]), - patch: match[3] === undefined ? null : parseInt(match[3]), - } -} - -export const stringifyVersion = (v: Version): string => { - if (v == null) { - return "latest" - } - return `v${v.major}.${v.minor}${v.patch !== null ? `.${v.patch}` : ``}` -} - -const minVersion = { - major: 1, - minor: 28, - patch: 3, -} - -const isLessVersion = (a: Version, b: Version): boolean => { - if (a == null) { - return true - } - if (b == null) { - return false - } - if (a.major != b.major) { - return a.major < b.major - } - - // Do not compare patch parts because if the min version has a non zero value - // then it returns false, since the patch version of requested is always zero - return a.minor < b.minor -} - -const getRequestedLintVersion = (): Version => { - let requestedLintVersion = core.getInput(`version`) - const workingDirectory = core.getInput(`working-directory`) - - let goMod = "go.mod" - if (workingDirectory) { - goMod = path.join(workingDirectory, goMod) - } - - if (requestedLintVersion == "" && fs.existsSync(goMod)) { - const content = fs.readFileSync(goMod, "utf-8") - const match = content.match(modVersionRe) - if (match) { - requestedLintVersion = match[1] - core.info(`Found golangci-lint version '${requestedLintVersion}' in '${goMod}' file`) - } - } - - const parsedRequestedLintVersion = parseVersion(requestedLintVersion) - if (parsedRequestedLintVersion == null) { - return null - } - - if (isLessVersion(parsedRequestedLintVersion, minVersion)) { - throw new Error( - `requested golangci-lint version '${requestedLintVersion}' isn't supported: we support only ${stringifyVersion( - minVersion - )} and later versions` - ) - } - - return parsedRequestedLintVersion -} - -export type VersionConfig = { - Error?: string - TargetVersion: string - AssetURL: string -} - -type Config = { - MinorVersionToConfig: { - [minorVersion: string]: VersionConfig - } -} - -const getConfig = async (): Promise => { - const http = new httpm.HttpClient(`golangci/golangci-lint-action`, [], { - allowRetries: true, - maxRetries: 5, - }) - try { - const url = `https://raw.githubusercontent.com/golangci/golangci-lint/master/assets/github-action-config.json` - const response: httpm.HttpClientResponse = await http.get(url) - if (response.message.statusCode !== 200) { - throw new Error(`failed to download from "${url}". Code(${response.message.statusCode}) Message(${response.message.statusMessage})`) - } - - const body = await response.readBody() - return JSON.parse(body) - } catch (exc) { - throw new Error(`failed to get action config: ${exc.message}`) - } -} - -export async function findLintVersion(mode: InstallMode): Promise { - core.info(`Finding needed golangci-lint version...`) - - if (mode == InstallMode.GoInstall) { - const v: string = core.getInput(`version`) - return { TargetVersion: v ? v : "latest", AssetURL: "github.com/golangci/golangci-lint" } - } - - const reqLintVersion = getRequestedLintVersion() - - // if the patched version is passed, just use it - if (reqLintVersion?.major !== null && reqLintVersion?.minor != null && reqLintVersion?.patch !== null) { - return new Promise((resolve) => { - const versionWithoutV = `${reqLintVersion.major}.${reqLintVersion.minor}.${reqLintVersion.patch}` - resolve({ - TargetVersion: `v${versionWithoutV}`, - AssetURL: `https://github.com/golangci/golangci-lint/releases/download/v${versionWithoutV}/golangci-lint-${versionWithoutV}-linux-amd64.tar.gz`, - }) - }) - } - - const startedAt = Date.now() - - const config = await getConfig() - if (!config.MinorVersionToConfig) { - core.warning(JSON.stringify(config)) - throw new Error(`invalid config: no MinorVersionToConfig field`) - } - - const versionConfig = config.MinorVersionToConfig[stringifyVersion(reqLintVersion)] - if (!versionConfig) { - throw new Error(`requested golangci-lint version '${stringifyVersion(reqLintVersion)}' doesn't exist`) - } - - if (versionConfig.Error) { - throw new Error(`failed to use requested golangci-lint version '${stringifyVersion(reqLintVersion)}': ${versionConfig.Error}`) - } - - core.info( - `Requested golangci-lint '${stringifyVersion(reqLintVersion)}', using '${versionConfig.TargetVersion}', calculation took ${ - Date.now() - startedAt - }ms` - ) - - return versionConfig -}