From 39fc5724c91f6d6d8446e92065652fb646a9723d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 13:39:02 +0200 Subject: [PATCH 1/5] chore: factorize fetchPullRequestPatch --- src/run.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/run.ts b/src/run.ts index a721b760d9..0315f86b28 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,5 +1,6 @@ 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" @@ -24,6 +25,7 @@ async function prepareLint(): Promise { async function fetchPatch(): Promise { const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim() + if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`) } @@ -32,22 +34,33 @@ async function fetchPatch(): Promise { } const ctx = github.context - if (ctx.eventName !== `pull_request` && ctx.eventName !== `pull_request_target`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`) - return `` + + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + return await fetchPullRequestPatch(ctx) + + 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 `` } - const pull = ctx.payload.pull_request - if (!pull) { +} + +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`]: pull.number, + [`pull_number`]: pr.number, mediaType: { format: `diff`, }, From 2ebc5cd2ab29444c768c64ba9efbdd954663a564 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 14:41:19 +0200 Subject: [PATCH 2/5] feat: support push event --- src/run.ts | 90 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/src/run.ts b/src/run.ts index 0315f86b28..a9703e69a2 100644 --- a/src/run.ts +++ b/src/run.ts @@ -16,6 +16,16 @@ const execShellCommand = promisify(exec) const writeFile = promisify(fs.writeFile) const createTempDir = promisify(dir) +function isOnlyNewIssues(): boolean { + const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim() + + if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { + throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`) + } + + return onlyNewIssues === `true` +} + async function prepareLint(): Promise { const mode = core.getInput("install-mode").toLowerCase() const versionConfig = await findLintVersion(mode) @@ -24,12 +34,7 @@ async function prepareLint(): Promise { } async function fetchPatch(): Promise { - const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim() - - if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { - throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`) - } - if (onlyNewIssues === `false`) { + if (!isOnlyNewIssues()) { return `` } @@ -39,7 +44,8 @@ async function fetchPatch(): Promise { case `pull_request`: case `pull_request_target`: return await fetchPullRequestPatch(ctx) - + case `push`: + return await fetchPushPatch(ctx) 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 `` @@ -90,6 +96,45 @@ async function fetchPullRequestPatch(ctx: Context): Promise { } } +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.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: ctx.payload.before, + head: 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 = { lintPath: string patchPath: string @@ -100,11 +145,9 @@ async function prepareEnv(): Promise { // Prepare cache, lint and go in parallel. await restoreCache() - const prepareLintPromise = prepareLint() - const patchPromise = fetchPatch() - const lintPath = await prepareLintPromise - const patchPath = await patchPromise + const lintPath = await prepareLint() + const patchPath = await fetchPatch() core.info(`Prepared env in ${Date.now() - startedAt}ms`) @@ -157,15 +200,30 @@ async function runLint(lintPath: string, patchPath: string): Promise { addedArgs.push(`--out-format=${formats}`) userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim() - if (patchPath) { + if (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`) } - addedArgs.push(`--new-from-patch=${patchPath}`) - // Override config values. - addedArgs.push(`--new=false`) - addedArgs.push(`--new-from-rev=`) + const ctx = github.context + + core.info(`only new issues on ${ctx.eventName}: ${patchPath}`) + + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + case `push`: + if (patchPath) { + addedArgs.push(`--new-from-patch=${patchPath}`) + + // Override config values. + addedArgs.push(`--new=false`) + addedArgs.push(`--new-from-rev=`) + } + break + default: + break + } } const workingDirectory = core.getInput(`working-directory`) From 2d53ff8a2dba6f0d91db940d66fbb7f411b02bd0 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 16:11:59 +0200 Subject: [PATCH 3/5] feat: support merge queue event --- src/run.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/run.ts b/src/run.ts index a9703e69a2..60ff43201b 100644 --- a/src/run.ts +++ b/src/run.ts @@ -46,6 +46,9 @@ async function fetchPatch(): Promise { return await fetchPullRequestPatch(ctx) case `push`: return await fetchPushPatch(ctx) + case `merge_group`: + core.info(JSON.stringify(ctx.payload)) + 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 `` @@ -221,6 +224,13 @@ async function runLint(lintPath: string, patchPath: string): Promise { addedArgs.push(`--new-from-rev=`) } break + case `merge_group`: + addedArgs.push(`--new-from-rev=${ctx.payload.merge_group.base_sha}`) + + // Override config values. + addedArgs.push(`--new=false`) + addedArgs.push(`--new-from-patch=`) + break default: break } From 3ab19875d7f33278aa6f7cdbdd1f37ed29bbace2 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 16:12:50 +0200 Subject: [PATCH 4/5] chore: generate --- dist/post_run/index.js | 106 +++++++++++++++++++++++++++++++++-------- dist/run/index.js | 106 +++++++++++++++++++++++++++++++++-------- 2 files changed, 172 insertions(+), 40 deletions(-) diff --git a/dist/post_run/index.js b/dist/post_run/index.js index ff6cea0d20..9dd2226db6 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -89146,26 +89146,40 @@ const version_1 = __nccwpck_require__(1946); const execShellCommand = (0, util_1.promisify)(child_process_1.exec); const writeFile = (0, util_1.promisify)(fs.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); +function isOnlyNewIssues() { + const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim(); + if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { + throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`); + } + return onlyNewIssues === `true`; +} async function prepareLint() { const mode = core.getInput("install-mode").toLowerCase(); const versionConfig = await (0, version_1.findLintVersion)(mode); return await (0, install_1.installLint)(versionConfig, mode); } async function fetchPatch() { - const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim(); - if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { - throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`); - } - if (onlyNewIssues === `false`) { + if (!isOnlyNewIssues()) { return ``; } const ctx = github.context; - if (ctx.eventName !== `pull_request` && ctx.eventName !== `pull_request_target`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`); - return ``; + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + return await fetchPullRequestPatch(ctx); + case `push`: + return await fetchPushPatch(ctx); + case `merge_group`: + core.info(JSON.stringify(ctx.payload)); + 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 ``; } - const pull = ctx.payload.pull_request; - if (!pull) { +} +async function fetchPullRequestPatch(ctx) { + const pr = ctx.payload.pull_request; + if (!pr) { core.warning(`No pull request in context`); return ``; } @@ -89175,7 +89189,7 @@ async function fetchPatch() { const patchResp = await octokit.rest.pulls.get({ owner: ctx.repo.owner, repo: ctx.repo.repo, - [`pull_number`]: pull.number, + [`pull_number`]: pr.number, mediaType: { format: `diff`, }, @@ -89203,14 +89217,48 @@ async function fetchPatch() { return ``; // don't fail the action, but analyze without patch } } +async function fetchPushPatch(ctx) { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); + let patch; + try { + const patchResp = await octokit.rest.repos.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: ctx.payload.before, + head: 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; + } + 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, (0, diffUtils_1.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 prepareEnv() { const startedAt = Date.now(); // Prepare cache, lint and go in parallel. await (0, cache_1.restoreCache)(); - const prepareLintPromise = prepareLint(); - const patchPromise = fetchPatch(); - const lintPath = await prepareLintPromise; - const patchPath = await patchPromise; + const lintPath = await prepareLint(); + const patchPath = await fetchPatch(); core.info(`Prepared env in ${Date.now() - startedAt}ms`); return { lintPath, patchPath }; } @@ -89248,14 +89296,32 @@ async function runLint(lintPath, patchPath) { .join(","); addedArgs.push(`--out-format=${formats}`); userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); - if (patchPath) { + if (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`); } - addedArgs.push(`--new-from-patch=${patchPath}`); - // Override config values. - addedArgs.push(`--new=false`); - addedArgs.push(`--new-from-rev=`); + const ctx = github.context; + core.info(`only new issues on ${ctx.eventName}: ${patchPath}`); + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + case `push`: + if (patchPath) { + addedArgs.push(`--new-from-patch=${patchPath}`); + // Override config values. + addedArgs.push(`--new=false`); + addedArgs.push(`--new-from-rev=`); + } + break; + case `merge_group`: + addedArgs.push(`--new-from-rev=${ctx.payload.merge_group.base_sha}`); + // Override config values. + addedArgs.push(`--new=false`); + addedArgs.push(`--new-from-patch=`); + break; + default: + break; + } } const workingDirectory = core.getInput(`working-directory`); const cmdArgs = {}; diff --git a/dist/run/index.js b/dist/run/index.js index 1965346a67..b015ef3b3f 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -89146,26 +89146,40 @@ const version_1 = __nccwpck_require__(1946); const execShellCommand = (0, util_1.promisify)(child_process_1.exec); const writeFile = (0, util_1.promisify)(fs.writeFile); const createTempDir = (0, util_1.promisify)(tmp_1.dir); +function isOnlyNewIssues() { + const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim(); + if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { + throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`); + } + return onlyNewIssues === `true`; +} async function prepareLint() { const mode = core.getInput("install-mode").toLowerCase(); const versionConfig = await (0, version_1.findLintVersion)(mode); return await (0, install_1.installLint)(versionConfig, mode); } async function fetchPatch() { - const onlyNewIssues = core.getInput(`only-new-issues`, { required: true }).trim(); - if (onlyNewIssues !== `false` && onlyNewIssues !== `true`) { - throw new Error(`invalid value of "only-new-issues": "${onlyNewIssues}", expected "true" or "false"`); - } - if (onlyNewIssues === `false`) { + if (!isOnlyNewIssues()) { return ``; } const ctx = github.context; - if (ctx.eventName !== `pull_request` && ctx.eventName !== `pull_request_target`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`); - return ``; + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + return await fetchPullRequestPatch(ctx); + case `push`: + return await fetchPushPatch(ctx); + case `merge_group`: + core.info(JSON.stringify(ctx.payload)); + 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 ``; } - const pull = ctx.payload.pull_request; - if (!pull) { +} +async function fetchPullRequestPatch(ctx) { + const pr = ctx.payload.pull_request; + if (!pr) { core.warning(`No pull request in context`); return ``; } @@ -89175,7 +89189,7 @@ async function fetchPatch() { const patchResp = await octokit.rest.pulls.get({ owner: ctx.repo.owner, repo: ctx.repo.repo, - [`pull_number`]: pull.number, + [`pull_number`]: pr.number, mediaType: { format: `diff`, }, @@ -89203,14 +89217,48 @@ async function fetchPatch() { return ``; // don't fail the action, but analyze without patch } } +async function fetchPushPatch(ctx) { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); + let patch; + try { + const patchResp = await octokit.rest.repos.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: ctx.payload.before, + head: 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; + } + 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, (0, diffUtils_1.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 prepareEnv() { const startedAt = Date.now(); // Prepare cache, lint and go in parallel. await (0, cache_1.restoreCache)(); - const prepareLintPromise = prepareLint(); - const patchPromise = fetchPatch(); - const lintPath = await prepareLintPromise; - const patchPath = await patchPromise; + const lintPath = await prepareLint(); + const patchPath = await fetchPatch(); core.info(`Prepared env in ${Date.now() - startedAt}ms`); return { lintPath, patchPath }; } @@ -89248,14 +89296,32 @@ async function runLint(lintPath, patchPath) { .join(","); addedArgs.push(`--out-format=${formats}`); userArgs = userArgs.replace(/--out-format=\S*/gi, "").trim(); - if (patchPath) { + if (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`); } - addedArgs.push(`--new-from-patch=${patchPath}`); - // Override config values. - addedArgs.push(`--new=false`); - addedArgs.push(`--new-from-rev=`); + const ctx = github.context; + core.info(`only new issues on ${ctx.eventName}: ${patchPath}`); + switch (ctx.eventName) { + case `pull_request`: + case `pull_request_target`: + case `push`: + if (patchPath) { + addedArgs.push(`--new-from-patch=${patchPath}`); + // Override config values. + addedArgs.push(`--new=false`); + addedArgs.push(`--new-from-rev=`); + } + break; + case `merge_group`: + addedArgs.push(`--new-from-rev=${ctx.payload.merge_group.base_sha}`); + // Override config values. + addedArgs.push(`--new=false`); + addedArgs.push(`--new-from-patch=`); + break; + default: + break; + } } const workingDirectory = core.getInput(`working-directory`); const cmdArgs = {}; From a58b56bec5ce2d316e26be611efbfdb8186aa72f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 16:55:37 +0200 Subject: [PATCH 5/5] docs: update readme --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cd727da622..d1754e92ec 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,9 @@ jobs: # The location of the configuration file can be changed by using `--config=` # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 - # Optional: For pull request only, show only new issues. The default value is `false`. + # Optional: Show only new issues. + # If you are using `merge_group` event (merge queue) you should add the option `fetch-depth: 0` to `actions/checkout` step. + # The default value is `false`. # only-new-issues: true # Optional: if set to true, then all caching functionality will be completely disabled, @@ -104,7 +106,7 @@ jobs: strategy: matrix: go: ['1.21'] - os: [macos-latest, windows-latest] + os: [ubuntu-latest, macos-latest, windows-latest] name: lint runs-on: ${{ matrix.os }} steps: @@ -129,7 +131,9 @@ jobs: # The location of the configuration file can be changed by using `--config=` # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 - # Optional: For pull request only, show only new issues. The default value is `false`. + # Optional: Show only new issues. + # If you are using `merge_group` event (merge queue) you should add the option `fetch-depth: 0` to `actions/checkout` step. + # The default value is `false`. # only-new-issues: true # Optional: if set to true, then all caching functionality will be completely disabled,