chore: extract patch related code (#1166)

This commit is contained in:
Ludovic Fernandez
2025-02-13 22:16:32 +01:00
committed by GitHub
parent e0ebdd245e
commit 32f961426b
5 changed files with 404 additions and 282 deletions

171
dist/post_run/index.js generated vendored
View File

@ -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`);
}

171
dist/run/index.js generated vendored
View File

@ -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`);
}

View File

@ -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<string> {
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(<InstallMode>mode)
return await installBinary(versionInfo, <InstallMode>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<string> {
export async function installBinary(versionInfo: VersionInfo, mode: InstallMode): Promise<string> {
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<string> {
async function goInstall(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint ${versionInfo.TargetVersion}...`)
const startedAt = Date.now()
@ -125,7 +117,7 @@ export async function goInstall(versionInfo: VersionInfo): Promise<string> {
* @param versionInfo information about version to install.
* @returns path to installed binary of golangci-lint.
*/
export async function installBin(versionInfo: VersionInfo): Promise<string> {
async function installBin(versionInfo: VersionInfo): Promise<string> {
core.info(`Installing golangci-lint binary ${versionInfo.TargetVersion}...`)
const startedAt = Date.now()
@ -158,3 +150,33 @@ export async function installBin(versionInfo: VersionInfo): Promise<string> {
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}`
}

119
src/patch.ts Normal file
View File

@ -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<string> {
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<string> {
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<string> {
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
}
}

View File

@ -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<string> {
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(<InstallMode>mode)
return await installLint(versionInfo, <InstallMode>mode)
}
async function fetchPatch(): Promise<string> {
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<string> {
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<string> {
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<Env> {
// 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`)