From 32f961426b50770ba03673e8d73efc3c59576ef3 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 13 Feb 2025 22:16:32 +0100 Subject: [PATCH 1/3] chore: extract patch related code (#1166) --- dist/post_run/index.js | 171 +++++++++++++++++++++++++++-------------- dist/run/index.js | 171 +++++++++++++++++++++++++++-------------- src/install.ts | 90 ++++++++++++++-------- src/patch.ts | 119 ++++++++++++++++++++++++++++ src/run.ts | 135 +------------------------------- 5 files changed, 404 insertions(+), 282 deletions(-) create mode 100644 src/patch.ts diff --git a/dist/post_run/index.js b/dist/post_run/index.js index bb3388ab0d..8978b4d15a 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -93679,41 +93679,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.InstallMode = void 0; -exports.installLint = installLint; -exports.goInstall = goInstall; -exports.installBin = installBin; +exports.install = install; +exports.installBinary = installBinary; const core = __importStar(__nccwpck_require__(7484)); const tc = __importStar(__nccwpck_require__(3472)); const child_process_1 = __nccwpck_require__(5317); const os_1 = __importDefault(__nccwpck_require__(857)); const path_1 = __importDefault(__nccwpck_require__(6928)); const util_1 = __nccwpck_require__(9023); +const which_1 = __importDefault(__nccwpck_require__(1189)); +const version_1 = __nccwpck_require__(311); const execShellCommand = (0, util_1.promisify)(child_process_1.exec); -const getAssetURL = (versionInfo) => { - let ext = "tar.gz"; - let platform = os_1.default.platform().toString(); - switch (platform) { - case "win32": - platform = "windows"; - ext = "zip"; - break; - } - let arch = os_1.default.arch(); - switch (arch) { - case "arm64": - arch = "arm64"; - break; - case "x64": - arch = "amd64"; - break; - case "x32": - case "ia32": - arch = "386"; - break; - } - const noPrefix = versionInfo.TargetVersion.slice(1); - return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`; -}; var InstallMode; (function (InstallMode) { InstallMode["Binary"] = "binary"; @@ -93728,6 +93704,23 @@ const printOutput = (res) => { core.info(res.stderr); } }; +/** + * Install golangci-lint. + * + * @returns path to installed binary of golangci-lint. + */ +async function install() { + const mode = core.getInput("install-mode").toLowerCase(); + if (mode === InstallMode.None) { + const binPath = await (0, which_1.default)("golangci-lint", { nothrow: true }); + if (!binPath) { + throw new Error("golangci-lint binary not found in the PATH"); + } + return binPath; + } + const versionInfo = await (0, version_1.getVersion)(mode); + return await installBinary(versionInfo, mode); +} /** * Install golangci-lint. * @@ -93735,7 +93728,7 @@ const printOutput = (res) => { * @param mode installation mode. * @returns path to installed binary of golangci-lint. */ -async function installLint(versionInfo, mode) { +async function installBinary(versionInfo, mode) { core.info(`Installation mode: ${mode}`); switch (mode) { case InstallMode.Binary: @@ -93804,11 +93797,36 @@ async function installBin(versionInfo) { core.info(`Installed golangci-lint into ${binPath} in ${Date.now() - startedAt}ms`); return binPath; } +function getAssetURL(versionInfo) { + let ext = "tar.gz"; + let platform = os_1.default.platform().toString(); + switch (platform) { + case "win32": + platform = "windows"; + ext = "zip"; + break; + } + let arch = os_1.default.arch(); + switch (arch) { + case "arm64": + arch = "arm64"; + break; + case "x64": + arch = "amd64"; + break; + case "x32": + case "ia32": + arch = "386"; + break; + } + const noPrefix = versionInfo.TargetVersion.slice(1); + return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`; +} /***/ }), -/***/ 9786: +/***/ 7161: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -93850,38 +93868,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.run = run; -exports.postRun = postRun; +exports.isOnlyNewIssues = isOnlyNewIssues; +exports.fetchPatch = fetchPatch; const core = __importStar(__nccwpck_require__(7484)); const github = __importStar(__nccwpck_require__(3228)); -const child_process_1 = __nccwpck_require__(5317); -const fs = __importStar(__nccwpck_require__(9896)); -const path = __importStar(__nccwpck_require__(6928)); +const fs_1 = __importDefault(__nccwpck_require__(9896)); +const path_1 = __importDefault(__nccwpck_require__(6928)); const tmp_1 = __nccwpck_require__(1288); const util_1 = __nccwpck_require__(9023); -const which_1 = __importDefault(__nccwpck_require__(1189)); -const cache_1 = __nccwpck_require__(7377); -const install_1 = __nccwpck_require__(232); const diffUtils_1 = __nccwpck_require__(3441); -const version_1 = __nccwpck_require__(311); -const execShellCommand = (0, util_1.promisify)(child_process_1.exec); -const writeFile = (0, util_1.promisify)(fs.writeFile); +const writeFile = (0, util_1.promisify)(fs_1.default.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); function isOnlyNewIssues() { return core.getBooleanInput(`only-new-issues`, { required: true }); } -async function prepareLint() { - const mode = core.getInput("install-mode").toLowerCase(); - if (mode === install_1.InstallMode.None) { - const binPath = await (0, which_1.default)("golangci-lint", { nothrow: true }); - if (!binPath) { - throw new Error("golangci-lint binary not found in the PATH"); - } - return binPath; - } - const versionInfo = await (0, version_1.getVersion)(mode); - return await (0, install_1.installLint)(versionInfo, mode); -} async function fetchPatch() { if (!isOnlyNewIssues()) { return ``; @@ -93930,7 +93930,7 @@ async function fetchPullRequestPatch(ctx) { } try { const tempDir = await createTempDir(); - const patchPath = path.join(tempDir, "pull.patch"); + const patchPath = path_1.default.join(tempDir, "pull.patch"); core.info(`Writing patch to ${patchPath}`); await writeFile(patchPath, (0, diffUtils_1.alterDiffPatch)(patch)); return patchPath; @@ -93965,7 +93965,7 @@ async function fetchPushPatch(ctx) { } try { const tempDir = await createTempDir(); - const patchPath = path.join(tempDir, "push.patch"); + const patchPath = path_1.default.join(tempDir, "push.patch"); core.info(`Writing patch to ${patchPath}`); await writeFile(patchPath, (0, diffUtils_1.alterDiffPatch)(patch)); return patchPath; @@ -93975,12 +93975,67 @@ async function fetchPushPatch(ctx) { return ``; // don't fail the action, but analyze without patch } } + + +/***/ }), + +/***/ 9786: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || (function () { + var ownKeys = function(o) { + ownKeys = Object.getOwnPropertyNames || function (o) { + var ar = []; + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; + return ar; + }; + return ownKeys(o); + }; + return function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + __setModuleDefault(result, mod); + return result; + }; +})(); +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.run = run; +exports.postRun = postRun; +const core = __importStar(__nccwpck_require__(7484)); +const github = __importStar(__nccwpck_require__(3228)); +const child_process_1 = __nccwpck_require__(5317); +const fs = __importStar(__nccwpck_require__(9896)); +const path = __importStar(__nccwpck_require__(6928)); +const util_1 = __nccwpck_require__(9023); +const cache_1 = __nccwpck_require__(7377); +const install_1 = __nccwpck_require__(232); +const patch_1 = __nccwpck_require__(7161); +const execShellCommand = (0, util_1.promisify)(child_process_1.exec); async function prepareEnv() { const startedAt = Date.now(); // Prepare cache, lint and go in parallel. await (0, cache_1.restoreCache)(); - const binPath = await prepareLint(); - const patchPath = await fetchPatch(); + const binPath = await (0, install_1.install)(); + const patchPath = await (0, patch_1.fetchPatch)(); core.info(`Prepared env in ${Date.now() - startedAt}ms`); return { binPath, patchPath }; } @@ -94030,7 +94085,7 @@ async function runLint(binPath, patchPath) { } // Removes `--out-format` from the user flags because it's already inside `addedArgs`. userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); - if (isOnlyNewIssues()) { + if ((0, patch_1.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 20e94c79f5..1ab7483a27 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -93679,41 +93679,17 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", ({ value: true })); exports.InstallMode = void 0; -exports.installLint = installLint; -exports.goInstall = goInstall; -exports.installBin = installBin; +exports.install = install; +exports.installBinary = installBinary; const core = __importStar(__nccwpck_require__(7484)); const tc = __importStar(__nccwpck_require__(3472)); const child_process_1 = __nccwpck_require__(5317); const os_1 = __importDefault(__nccwpck_require__(857)); const path_1 = __importDefault(__nccwpck_require__(6928)); const util_1 = __nccwpck_require__(9023); +const which_1 = __importDefault(__nccwpck_require__(1189)); +const version_1 = __nccwpck_require__(311); const execShellCommand = (0, util_1.promisify)(child_process_1.exec); -const getAssetURL = (versionInfo) => { - let ext = "tar.gz"; - let platform = os_1.default.platform().toString(); - switch (platform) { - case "win32": - platform = "windows"; - ext = "zip"; - break; - } - let arch = os_1.default.arch(); - switch (arch) { - case "arm64": - arch = "arm64"; - break; - case "x64": - arch = "amd64"; - break; - case "x32": - case "ia32": - arch = "386"; - break; - } - const noPrefix = versionInfo.TargetVersion.slice(1); - return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`; -}; var InstallMode; (function (InstallMode) { InstallMode["Binary"] = "binary"; @@ -93728,6 +93704,23 @@ const printOutput = (res) => { core.info(res.stderr); } }; +/** + * Install golangci-lint. + * + * @returns path to installed binary of golangci-lint. + */ +async function install() { + const mode = core.getInput("install-mode").toLowerCase(); + if (mode === InstallMode.None) { + const binPath = await (0, which_1.default)("golangci-lint", { nothrow: true }); + if (!binPath) { + throw new Error("golangci-lint binary not found in the PATH"); + } + return binPath; + } + const versionInfo = await (0, version_1.getVersion)(mode); + return await installBinary(versionInfo, mode); +} /** * Install golangci-lint. * @@ -93735,7 +93728,7 @@ const printOutput = (res) => { * @param mode installation mode. * @returns path to installed binary of golangci-lint. */ -async function installLint(versionInfo, mode) { +async function installBinary(versionInfo, mode) { core.info(`Installation mode: ${mode}`); switch (mode) { case InstallMode.Binary: @@ -93804,11 +93797,36 @@ async function installBin(versionInfo) { core.info(`Installed golangci-lint into ${binPath} in ${Date.now() - startedAt}ms`); return binPath; } +function getAssetURL(versionInfo) { + let ext = "tar.gz"; + let platform = os_1.default.platform().toString(); + switch (platform) { + case "win32": + platform = "windows"; + ext = "zip"; + break; + } + let arch = os_1.default.arch(); + switch (arch) { + case "arm64": + arch = "arm64"; + break; + case "x64": + arch = "amd64"; + break; + case "x32": + case "ia32": + arch = "386"; + break; + } + const noPrefix = versionInfo.TargetVersion.slice(1); + return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}`; +} /***/ }), -/***/ 9786: +/***/ 7161: /***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { "use strict"; @@ -93850,38 +93868,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.run = run; -exports.postRun = postRun; +exports.isOnlyNewIssues = isOnlyNewIssues; +exports.fetchPatch = fetchPatch; const core = __importStar(__nccwpck_require__(7484)); const github = __importStar(__nccwpck_require__(3228)); -const child_process_1 = __nccwpck_require__(5317); -const fs = __importStar(__nccwpck_require__(9896)); -const path = __importStar(__nccwpck_require__(6928)); +const fs_1 = __importDefault(__nccwpck_require__(9896)); +const path_1 = __importDefault(__nccwpck_require__(6928)); const tmp_1 = __nccwpck_require__(1288); const util_1 = __nccwpck_require__(9023); -const which_1 = __importDefault(__nccwpck_require__(1189)); -const cache_1 = __nccwpck_require__(7377); -const install_1 = __nccwpck_require__(232); const diffUtils_1 = __nccwpck_require__(3441); -const version_1 = __nccwpck_require__(311); -const execShellCommand = (0, util_1.promisify)(child_process_1.exec); -const writeFile = (0, util_1.promisify)(fs.writeFile); +const writeFile = (0, util_1.promisify)(fs_1.default.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); function isOnlyNewIssues() { return core.getBooleanInput(`only-new-issues`, { required: true }); } -async function prepareLint() { - const mode = core.getInput("install-mode").toLowerCase(); - if (mode === install_1.InstallMode.None) { - const binPath = await (0, which_1.default)("golangci-lint", { nothrow: true }); - if (!binPath) { - throw new Error("golangci-lint binary not found in the PATH"); - } - return binPath; - } - const versionInfo = await (0, version_1.getVersion)(mode); - return await (0, install_1.installLint)(versionInfo, mode); -} async function fetchPatch() { if (!isOnlyNewIssues()) { return ``; @@ -93930,7 +93930,7 @@ async function fetchPullRequestPatch(ctx) { } try { const tempDir = await createTempDir(); - const patchPath = path.join(tempDir, "pull.patch"); + const patchPath = path_1.default.join(tempDir, "pull.patch"); core.info(`Writing patch to ${patchPath}`); await writeFile(patchPath, (0, diffUtils_1.alterDiffPatch)(patch)); return patchPath; @@ -93965,7 +93965,7 @@ async function fetchPushPatch(ctx) { } try { const tempDir = await createTempDir(); - const patchPath = path.join(tempDir, "push.patch"); + const patchPath = path_1.default.join(tempDir, "push.patch"); core.info(`Writing patch to ${patchPath}`); await writeFile(patchPath, (0, diffUtils_1.alterDiffPatch)(patch)); return patchPath; @@ -93975,12 +93975,67 @@ async function fetchPushPatch(ctx) { return ``; // don't fail the action, but analyze without patch } } + + +/***/ }), + +/***/ 9786: +/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) { + +"use strict"; + +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || (function () { + var ownKeys = function(o) { + ownKeys = Object.getOwnPropertyNames || function (o) { + var ar = []; + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; + return ar; + }; + return ownKeys(o); + }; + return function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + __setModuleDefault(result, mod); + return result; + }; +})(); +Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.run = run; +exports.postRun = postRun; +const core = __importStar(__nccwpck_require__(7484)); +const github = __importStar(__nccwpck_require__(3228)); +const child_process_1 = __nccwpck_require__(5317); +const fs = __importStar(__nccwpck_require__(9896)); +const path = __importStar(__nccwpck_require__(6928)); +const util_1 = __nccwpck_require__(9023); +const cache_1 = __nccwpck_require__(7377); +const install_1 = __nccwpck_require__(232); +const patch_1 = __nccwpck_require__(7161); +const execShellCommand = (0, util_1.promisify)(child_process_1.exec); async function prepareEnv() { const startedAt = Date.now(); // Prepare cache, lint and go in parallel. await (0, cache_1.restoreCache)(); - const binPath = await prepareLint(); - const patchPath = await fetchPatch(); + const binPath = await (0, install_1.install)(); + const patchPath = await (0, patch_1.fetchPatch)(); core.info(`Prepared env in ${Date.now() - startedAt}ms`); return { binPath, patchPath }; } @@ -94030,7 +94085,7 @@ async function runLint(binPath, patchPath) { } // Removes `--out-format` from the user flags because it's already inside `addedArgs`. userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); - if (isOnlyNewIssues()) { + if ((0, patch_1.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/src/install.ts b/src/install.ts index 75cb75af77..960fa8dcc3 100644 --- a/src/install.ts +++ b/src/install.ts @@ -4,41 +4,12 @@ import { exec, ExecOptions } from "child_process" import os from "os" import path from "path" import { promisify } from "util" +import which from "which" -import { VersionInfo } from "./version" +import { getVersion, VersionInfo } from "./version" const execShellCommand = promisify(exec) -const getAssetURL = (versionInfo: VersionInfo): string => { - let ext = "tar.gz" - - let platform = os.platform().toString() - switch (platform) { - case "win32": - platform = "windows" - ext = "zip" - break - } - - let arch = os.arch() - switch (arch) { - case "arm64": - arch = "arm64" - break - case "x64": - arch = "amd64" - break - case "x32": - case "ia32": - arch = "386" - break - } - - 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 { Binary = "binary", GoInstall = "goinstall", @@ -59,6 +30,27 @@ const printOutput = (res: ExecRes): void => { } } +/** + * Install golangci-lint. + * + * @returns path to installed binary of golangci-lint. + */ +export async function install(): Promise { + const mode = core.getInput("install-mode").toLowerCase() + + if (mode === InstallMode.None) { + const binPath = await which("golangci-lint", { nothrow: true }) + if (!binPath) { + throw new Error("golangci-lint binary not found in the PATH") + } + return binPath + } + + const versionInfo = await getVersion(mode) + + return await installBinary(versionInfo, mode) +} + /** * Install golangci-lint. * @@ -66,7 +58,7 @@ const printOutput = (res: ExecRes): void => { * @param mode installation mode. * @returns path to installed binary of golangci-lint. */ -export async function installLint(versionInfo: VersionInfo, mode: InstallMode): Promise { +export async function installBinary(versionInfo: VersionInfo, mode: InstallMode): Promise { core.info(`Installation mode: ${mode}`) switch (mode) { @@ -85,7 +77,7 @@ export async function installLint(versionInfo: VersionInfo, mode: InstallMode): * @param versionInfo information about version to install. * @returns path to installed binary of golangci-lint. */ -export async function goInstall(versionInfo: VersionInfo): Promise { +async function goInstall(versionInfo: VersionInfo): Promise { core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`) const startedAt = Date.now() @@ -125,7 +117,7 @@ export async function goInstall(versionInfo: VersionInfo): Promise { * @param versionInfo information about version to install. * @returns path to installed binary of golangci-lint. */ -export async function installBin(versionInfo: VersionInfo): Promise { +async function installBin(versionInfo: VersionInfo): Promise { core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`) const startedAt = Date.now() @@ -158,3 +150,33 @@ export async function installBin(versionInfo: VersionInfo): Promise { return binPath } + +function getAssetURL(versionInfo: VersionInfo): string { + let ext = "tar.gz" + + let platform = os.platform().toString() + switch (platform) { + case "win32": + platform = "windows" + ext = "zip" + break + } + + let arch = os.arch() + switch (arch) { + case "arm64": + arch = "arm64" + break + case "x64": + arch = "amd64" + break + case "x32": + case "ia32": + arch = "386" + break + } + + const noPrefix = versionInfo.TargetVersion.slice(1) + + return `https://github.com/golangci/golangci-lint/releases/download/${versionInfo.TargetVersion}/golangci-lint-${noPrefix}-${platform}-${arch}.${ext}` +} diff --git a/src/patch.ts b/src/patch.ts new file mode 100644 index 0000000000..aed412b878 --- /dev/null +++ b/src/patch.ts @@ -0,0 +1,119 @@ +import * as core from "@actions/core" +import * as github from "@actions/github" +import { Context } from "@actions/github/lib/context" +import fs from "fs" +import path from "path" +import { dir } from "tmp" +import { promisify } from "util" + +import { alterDiffPatch } from "./utils/diffUtils" + +const writeFile = promisify(fs.writeFile) +const createTempDir = promisify(dir) + +export function isOnlyNewIssues(): boolean { + return core.getBooleanInput(`only-new-issues`, { required: true }) +} + +export async function fetchPatch(): Promise { + if (!isOnlyNewIssues()) { + return `` + } + + const ctx = github.context + + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + return await fetchPullRequestPatch(ctx) + case `push`: + return await fetchPushPatch(ctx) + case `merge_group`: + return `` + default: + core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`) + return `` + } +} + +async function fetchPullRequestPatch(ctx: Context): Promise { + const pr = ctx.payload.pull_request + if (!pr) { + core.warning(`No pull request in context`) + return `` + } + + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) + + let patch: string + try { + const patchResp = await octokit.rest.pulls.get({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + [`pull_number`]: pr.number, + mediaType: { + format: `diff`, + }, + }) + + if (patchResp.status !== 200) { + core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`) + return `` // don't fail the action, but analyze without patch + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + patch = patchResp.data as any + } catch (err) { + console.warn(`failed to fetch pull request patch:`, err) + return `` // don't fail the action, but analyze without patch + } + + try { + const tempDir = await createTempDir() + const patchPath = path.join(tempDir, "pull.patch") + core.info(`Writing patch to ${patchPath}`) + await writeFile(patchPath, alterDiffPatch(patch)) + return patchPath + } catch (err) { + console.warn(`failed to save pull request patch:`, err) + return `` // don't fail the action, but analyze without patch + } +} + +async function fetchPushPatch(ctx: Context): Promise { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) + + let patch: string + try { + const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + basehead: `${ctx.payload.before}...${ctx.payload.after}`, + mediaType: { + format: `diff`, + }, + }) + + if (patchResp.status !== 200) { + core.warning(`failed to fetch push patch: response status is ${patchResp.status}`) + return `` // don't fail the action, but analyze without patch + } + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + patch = patchResp.data as any + } catch (err) { + console.warn(`failed to fetch push patch:`, err) + return `` // don't fail the action, but analyze without patch + } + + try { + const tempDir = await createTempDir() + const patchPath = path.join(tempDir, "push.patch") + core.info(`Writing patch to ${patchPath}`) + await writeFile(patchPath, alterDiffPatch(patch)) + return patchPath + } catch (err) { + console.warn(`failed to save pull request patch:`, err) + return `` // don't fail the action, but analyze without patch + } +} diff --git a/src/run.ts b/src/run.ts index da83f1e840..09b0e1d69e 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,144 +1,15 @@ import * as core from "@actions/core" import * as github from "@actions/github" -import { Context } from "@actions/github/lib/context" import { exec, ExecOptions } from "child_process" import * as fs from "fs" import * as path from "path" -import { dir } from "tmp" import { promisify } from "util" -import which from "which" import { restoreCache, saveCache } from "./cache" -import { installLint, InstallMode } from "./install" -import { alterDiffPatch } from "./utils/diffUtils" -import { getVersion } from "./version" +import { install } from "./install" +import { fetchPatch, isOnlyNewIssues } from "./patch" const execShellCommand = promisify(exec) -const writeFile = promisify(fs.writeFile) -const createTempDir = promisify(dir) - -function isOnlyNewIssues(): boolean { - return core.getBooleanInput(`only-new-issues`, { required: true }) -} - -async function prepareLint(): Promise { - const mode = core.getInput("install-mode").toLowerCase() - - if (mode === InstallMode.None) { - const binPath = await which("golangci-lint", { nothrow: true }) - if (!binPath) { - throw new Error("golangci-lint binary not found in the PATH") - } - return binPath - } - - const versionInfo = await getVersion(mode) - - return await installLint(versionInfo, mode) -} - -async function fetchPatch(): Promise { - if (!isOnlyNewIssues()) { - return `` - } - - const ctx = github.context - - switch (ctx.eventName) { - case `pull_request`: - case `pull_request_target`: - return await fetchPullRequestPatch(ctx) - case `push`: - return await fetchPushPatch(ctx) - case `merge_group`: - return `` - default: - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`) - return `` - } -} - -async function fetchPullRequestPatch(ctx: Context): Promise { - const pr = ctx.payload.pull_request - if (!pr) { - core.warning(`No pull request in context`) - return `` - } - - const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) - - let patch: string - try { - const patchResp = await octokit.rest.pulls.get({ - owner: ctx.repo.owner, - repo: ctx.repo.repo, - [`pull_number`]: pr.number, - mediaType: { - format: `diff`, - }, - }) - - if (patchResp.status !== 200) { - core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`) - return `` // don't fail the action, but analyze without patch - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - patch = patchResp.data as any - } catch (err) { - console.warn(`failed to fetch pull request patch:`, err) - return `` // don't fail the action, but analyze without patch - } - - try { - const tempDir = await createTempDir() - const patchPath = path.join(tempDir, "pull.patch") - core.info(`Writing patch to ${patchPath}`) - await writeFile(patchPath, alterDiffPatch(patch)) - return patchPath - } catch (err) { - console.warn(`failed to save pull request patch:`, err) - return `` // don't fail the action, but analyze without patch - } -} - -async function fetchPushPatch(ctx: Context): Promise { - const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) - - let patch: string - try { - const patchResp = await octokit.rest.repos.compareCommitsWithBasehead({ - owner: ctx.repo.owner, - repo: ctx.repo.repo, - basehead: `${ctx.payload.before}...${ctx.payload.after}`, - mediaType: { - format: `diff`, - }, - }) - - if (patchResp.status !== 200) { - core.warning(`failed to fetch push patch: response status is ${patchResp.status}`) - return `` // don't fail the action, but analyze without patch - } - - // eslint-disable-next-line @typescript-eslint/no-explicit-any - patch = patchResp.data as any - } catch (err) { - console.warn(`failed to fetch push patch:`, err) - return `` // don't fail the action, but analyze without patch - } - - try { - const tempDir = await createTempDir() - const patchPath = path.join(tempDir, "push.patch") - core.info(`Writing patch to ${patchPath}`) - await writeFile(patchPath, alterDiffPatch(patch)) - return patchPath - } catch (err) { - console.warn(`failed to save pull request patch:`, err) - return `` // don't fail the action, but analyze without patch - } -} type Env = { binPath: string @@ -151,7 +22,7 @@ async function prepareEnv(): Promise { // Prepare cache, lint and go in parallel. await restoreCache() - const binPath = await prepareLint() + const binPath = await install() const patchPath = await fetchPatch() core.info(`Prepared env in ${Date.now() - startedAt}ms`) From f7463c56f6527097c8fff14480d3a4fa81ccdb81 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 13 Feb 2025 23:14:24 +0100 Subject: [PATCH 2/3] feat: add an option to verify with the JSONSchema (#1167) --- .github/workflows/test.yml | 32 ++++++++------------------------ .golangci.yml | 6 ++++++ README.md | 18 ++++++++++++++++++ action.yml | 4 ++++ dist/post_run/index.js | 6 ++++++ dist/run/index.js | 6 ++++++ sample-go-mod/.golangci.yml | 6 ++++++ sample-go-tool/.golangci.yml | 6 ++++++ src/run.ts | 9 +++++++++ 9 files changed, 69 insertions(+), 24 deletions(-) create mode 100644 .golangci.yml create mode 100644 sample-go-mod/.golangci.yml create mode 100644 sample-go-tool/.golangci.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa8ab55de2..af8bb9aeac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -73,6 +73,7 @@ jobs: version: ${{ matrix.version }} args: --timeout=5m --issues-exit-code=0 ./sample/... only-new-issues: true + verify: true test-go-install: # make sure the action works on a clean machine without building (go-install mode) needs: [ build ] @@ -103,8 +104,9 @@ jobs: args: --timeout=5m --issues-exit-code=0 ./sample/... only-new-issues: true install-mode: goinstall + verify: true - test-go-mod-version: + test-go-mod: needs: [ build ] strategy: matrix: @@ -113,6 +115,9 @@ jobs: - ubuntu-24.04-arm - macos-latest - windows-latest + wd: + - sample-go-mod + - sample-go-tool runs-on: ${{ matrix.os }} permissions: contents: read @@ -123,27 +128,6 @@ jobs: go-version: oldstable - uses: ./ with: - working-directory: sample-go-mod - args: --timeout=5m --issues-exit-code=0 ./... - - test-go-tool-version: - needs: [ build ] - strategy: - matrix: - os: - - ubuntu-latest - - ubuntu-24.04-arm - - macos-latest - - windows-latest - runs-on: ${{ matrix.os }} - permissions: - contents: read - steps: - - uses: actions/checkout@v4 - - uses: actions/setup-go@v5 - with: - go-version: stable - - uses: ./ - with: - working-directory: sample-go-tool + working-directory: ${{ matrix.wd }} args: --timeout=5m --issues-exit-code=0 ./... + verify: true diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000000..ad24fce4c0 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,6 @@ +output: + show-stats: true + sort-results: true + sort-order: + - linter + - file \ No newline at end of file diff --git a/README.md b/README.md index be93de974d..282afa91ec 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,24 @@ with: +### `verify` + +(optional) + +If set to true and the action verify the configuration file against the JSONSchema. + +
+Example + +```yml +uses: golangci/golangci-lint-action@v6 +with: + verify: true + # ... +``` + +
+ ### `only-new-issues` (optional) diff --git a/action.yml b/action.yml index f4bdaaa181..5e8e8e3be1 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,10 @@ inputs: description: "the token is used for fetching patch of a pull request to show only new issues" default: ${{ github.token }} required: false + verify: + description: "if set to true and the action verify the configuration file against the JSONSchema" + default: 'false' + required: false only-new-issues: description: "if set to true and the action runs on a pull request - the action outputs only newly found issues" default: 'false' diff --git a/dist/post_run/index.js b/dist/post_run/index.js index 8978b4d15a..8586a27319 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -94123,6 +94123,12 @@ async function runLint(binPath, patchPath) { } cmdArgs.cwd = path.resolve(workingDirectory); } + if (core.getBooleanInput(`verify`, { required: true })) { + const cmdVerify = `${binPath} config verify`; + core.info(`Running [${cmdVerify}] in [${cmdArgs.cwd || process.cwd()}] ...`); + const res = await execShellCommand(cmdVerify, cmdArgs); + printOutput(res); + } const cmd = `${binPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd(); core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`); const startedAt = Date.now(); diff --git a/dist/run/index.js b/dist/run/index.js index 1ab7483a27..b9e694e10a 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -94123,6 +94123,12 @@ async function runLint(binPath, patchPath) { } cmdArgs.cwd = path.resolve(workingDirectory); } + if (core.getBooleanInput(`verify`, { required: true })) { + const cmdVerify = `${binPath} config verify`; + core.info(`Running [${cmdVerify}] in [${cmdArgs.cwd || process.cwd()}] ...`); + const res = await execShellCommand(cmdVerify, cmdArgs); + printOutput(res); + } const cmd = `${binPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd(); core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`); const startedAt = Date.now(); diff --git a/sample-go-mod/.golangci.yml b/sample-go-mod/.golangci.yml new file mode 100644 index 0000000000..ad24fce4c0 --- /dev/null +++ b/sample-go-mod/.golangci.yml @@ -0,0 +1,6 @@ +output: + show-stats: true + sort-results: true + sort-order: + - linter + - file \ No newline at end of file diff --git a/sample-go-tool/.golangci.yml b/sample-go-tool/.golangci.yml new file mode 100644 index 0000000000..ad24fce4c0 --- /dev/null +++ b/sample-go-tool/.golangci.yml @@ -0,0 +1,6 @@ +output: + show-stats: true + sort-results: true + sort-order: + - linter + - file \ No newline at end of file diff --git a/src/run.ts b/src/run.ts index 09b0e1d69e..3addc14efa 100644 --- a/src/run.ts +++ b/src/run.ts @@ -137,6 +137,15 @@ async function runLint(binPath: string, patchPath: string): Promise { cmdArgs.cwd = path.resolve(workingDirectory) } + if (core.getBooleanInput(`verify`, { required: true })) { + const cmdVerify = `${binPath} config verify` + + core.info(`Running [${cmdVerify}] in [${cmdArgs.cwd || process.cwd()}] ...`) + + const res = await execShellCommand(cmdVerify, cmdArgs) + printOutput(res) + } + const cmd = `${binPath} run ${addedArgs.join(` `)} ${userArgs}`.trimEnd() core.info(`Running [${cmd}] in [${cmdArgs.cwd || process.cwd()}] ...`) From 0adbc47a5910e47adb692df88187ec8c73c76778 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Thu, 13 Feb 2025 23:15:38 +0100 Subject: [PATCH 3/3] 6.4.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9f7594b787..8db02beb43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "golanci-lint-action", - "version": "6.3.3", + "version": "6.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "golanci-lint-action", - "version": "6.3.3", + "version": "6.4.0", "license": "MIT", "dependencies": { "@actions/cache": "^4.0.0", diff --git a/package.json b/package.json index deae74ef30..6462a8eb68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "golanci-lint-action", - "version": "6.3.3", + "version": "6.4.0", "private": true, "description": "golangci-lint github action", "main": "dist/main.js",