From 3de9823efcb999c1f17ec1638da496a926a60c51 Mon Sep 17 00:00:00 2001 From: Jared Szechy Date: Wed, 20 Jul 2022 14:00:44 -0400 Subject: [PATCH 1/3] Use default branch for only-new-issues when push event --- .tool-versions | 1 + dist/post_run/index.js | 69 ++++++++++++++++++++++++++++++++++------ dist/run/index.js | 69 ++++++++++++++++++++++++++++++++++------ src/run.ts | 71 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 180 insertions(+), 30 deletions(-) create mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000000..52e118f947 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +nodejs lts diff --git a/dist/post_run/index.js b/dist/post_run/index.js index b3f81b6c55..ee592e74f3 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -67782,17 +67782,42 @@ function fetchPatch() { return ``; } const ctx = github.context; - if (ctx.eventName !== `pull_request`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`); + let patch; + if (ctx.eventName === `pull_request`) { + patch = yield patchFromPR(ctx); + } + else if (ctx.eventName === `push`) { + patch = yield patchFromPush(ctx); + } + else { + core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`); + return ``; + } + if (!patch) { + core.info(`Not using patch for showing only new issues because it's empty`); return ``; } + try { + const tempDir = yield createTempDir(); + const patchPath = path.join(tempDir, "pull.patch"); + core.info(`Writing patch to ${patchPath}`); + yield writeFile(patchPath, patch); + return patchPath; + } + catch (err) { + console.warn(`failed to save pull request patch:`, err); + return ``; // don't fail the action, but analyze without patch + } + }); +} +function patchFromPR(ctx) { + return __awaiter(this, void 0, void 0, function* () { const pull = ctx.payload.pull_request; if (!pull) { core.warning(`No pull request in context`); return ``; } const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); - let patch; try { const patchResp = yield octokit.rest.pulls.get({ owner: ctx.repo.owner, @@ -67807,21 +67832,45 @@ function fetchPatch() { return ``; // don't fail the action, but analyze without patch } // eslint-disable-next-line @typescript-eslint/no-explicit-any - patch = patchResp.data; + return patchResp.data; } catch (err) { console.warn(`failed to fetch pull request patch:`, err); return ``; // don't fail the action, but analyze without patch } + }); +} +function patchFromPush(ctx) { + return __awaiter(this, void 0, void 0, function* () { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); try { - const tempDir = yield createTempDir(); - const patchPath = path.join(tempDir, "pull.patch"); - core.info(`Writing patch to ${patchPath}`); - yield writeFile(patchPath, patch); - return patchPath; + const repoResp = yield octokit.rest.repos.get({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + }); + if (repoResp.status !== 200) { + core.warning(`failed to fetch repo: response status is ${repoResp.status}`); + return ``; + } + const defaultBranch = repoResp.data.default_branch; + const patchResp = yield octokit.rest.repos.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: defaultBranch, + head: ctx.sha, + 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 + return patchResp.data; } catch (err) { - console.warn(`failed to save pull request patch:`, err); + console.warn(`failed to fetch push patch:`, err); return ``; // don't fail the action, but analyze without patch } }); diff --git a/dist/run/index.js b/dist/run/index.js index b7a245f13a..fccb1f87fe 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -67782,17 +67782,42 @@ function fetchPatch() { return ``; } const ctx = github.context; - if (ctx.eventName !== `pull_request`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`); + let patch; + if (ctx.eventName === `pull_request`) { + patch = yield patchFromPR(ctx); + } + else if (ctx.eventName === `push`) { + patch = yield patchFromPush(ctx); + } + else { + core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`); + return ``; + } + if (!patch) { + core.info(`Not using patch for showing only new issues because it's empty`); return ``; } + try { + const tempDir = yield createTempDir(); + const patchPath = path.join(tempDir, "pull.patch"); + core.info(`Writing patch to ${patchPath}`); + yield writeFile(patchPath, patch); + return patchPath; + } + catch (err) { + console.warn(`failed to save pull request patch:`, err); + return ``; // don't fail the action, but analyze without patch + } + }); +} +function patchFromPR(ctx) { + return __awaiter(this, void 0, void 0, function* () { const pull = ctx.payload.pull_request; if (!pull) { core.warning(`No pull request in context`); return ``; } const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); - let patch; try { const patchResp = yield octokit.rest.pulls.get({ owner: ctx.repo.owner, @@ -67807,21 +67832,45 @@ function fetchPatch() { return ``; // don't fail the action, but analyze without patch } // eslint-disable-next-line @typescript-eslint/no-explicit-any - patch = patchResp.data; + return patchResp.data; } catch (err) { console.warn(`failed to fetch pull request patch:`, err); return ``; // don't fail the action, but analyze without patch } + }); +} +function patchFromPush(ctx) { + return __awaiter(this, void 0, void 0, function* () { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })); try { - const tempDir = yield createTempDir(); - const patchPath = path.join(tempDir, "pull.patch"); - core.info(`Writing patch to ${patchPath}`); - yield writeFile(patchPath, patch); - return patchPath; + const repoResp = yield octokit.rest.repos.get({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + }); + if (repoResp.status !== 200) { + core.warning(`failed to fetch repo: response status is ${repoResp.status}`); + return ``; + } + const defaultBranch = repoResp.data.default_branch; + const patchResp = yield octokit.rest.repos.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: defaultBranch, + head: ctx.sha, + 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 + return patchResp.data; } catch (err) { - console.warn(`failed to save pull request patch:`, err); + console.warn(`failed to fetch push patch:`, err); return ``; // don't fail the action, but analyze without patch } }); diff --git a/src/run.ts b/src/run.ts index 2fd55d51ce..e3cb1ec265 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" @@ -29,17 +30,40 @@ async function fetchPatch(): Promise { } const ctx = github.context - if (ctx.eventName !== `pull_request`) { - core.info(`Not fetching patch for showing only new issues because it's not a pull request context: event name is ${ctx.eventName}`) + let patch: string + if (ctx.eventName === `pull_request`) { + patch = await patchFromPR(ctx) + } else if (ctx.eventName === `push`) { + patch = await patchFromPush(ctx) + } else { + core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`) + return `` + } + + if (!patch) { + core.info(`Not using patch for showing only new issues because it's empty`) return `` } + + try { + const tempDir = await createTempDir() + const patchPath = path.join(tempDir, "pull.patch") + core.info(`Writing patch to ${patchPath}`) + await writeFile(patchPath, 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 patchFromPR(ctx: Context): Promise { const pull = ctx.payload.pull_request if (!pull) { 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, @@ -56,20 +80,47 @@ async function fetchPatch(): Promise { } // eslint-disable-next-line @typescript-eslint/no-explicit-any - patch = patchResp.data as any + return 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 } +} +async function patchFromPush(ctx: Context): Promise { + const octokit = github.getOctokit(core.getInput(`github-token`, { required: true })) try { - const tempDir = await createTempDir() - const patchPath = path.join(tempDir, "pull.patch") - core.info(`Writing patch to ${patchPath}`) - await writeFile(patchPath, patch) - return patchPath + const repoResp = await octokit.rest.repos.get({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + }) + + if (repoResp.status !== 200) { + core.warning(`failed to fetch repo: response status is ${repoResp.status}`) + return `` + } + + const defaultBranch = repoResp.data.default_branch + + const patchResp = await octokit.rest.repos.compareCommits({ + owner: ctx.repo.owner, + repo: ctx.repo.repo, + base: defaultBranch, + head: ctx.sha, + 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 + return patchResp.data as any } catch (err) { - console.warn(`failed to save pull request patch:`, err) + console.warn(`failed to fetch push patch:`, err) return `` // don't fail the action, but analyze without patch } } From 2bb22067f10f50f2946cdd2fe05b72547c8763e5 Mon Sep 17 00:00:00 2001 From: Jared Szechy Date: Wed, 20 Jul 2022 14:23:47 -0400 Subject: [PATCH 2/3] Allow empty diff --- dist/post_run/index.js | 4 ---- dist/run/index.js | 4 ---- src/run.ts | 5 ----- 3 files changed, 13 deletions(-) diff --git a/dist/post_run/index.js b/dist/post_run/index.js index ee592e74f3..faeb7c829d 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -67793,10 +67793,6 @@ function fetchPatch() { core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`); return ``; } - if (!patch) { - core.info(`Not using patch for showing only new issues because it's empty`); - return ``; - } try { const tempDir = yield createTempDir(); const patchPath = path.join(tempDir, "pull.patch"); diff --git a/dist/run/index.js b/dist/run/index.js index fccb1f87fe..689d0ee1c0 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -67793,10 +67793,6 @@ function fetchPatch() { core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`); return ``; } - if (!patch) { - core.info(`Not using patch for showing only new issues because it's empty`); - return ``; - } try { const tempDir = yield createTempDir(); const patchPath = path.join(tempDir, "pull.patch"); diff --git a/src/run.ts b/src/run.ts index e3cb1ec265..be16f6b21a 100644 --- a/src/run.ts +++ b/src/run.ts @@ -40,11 +40,6 @@ async function fetchPatch(): Promise { return `` } - if (!patch) { - core.info(`Not using patch for showing only new issues because it's empty`) - return `` - } - try { const tempDir = await createTempDir() const patchPath = path.join(tempDir, "pull.patch") From 4ed8444b7098b3d048ee0107b1fa0e625e44ce94 Mon Sep 17 00:00:00 2001 From: Jared Szechy Date: Wed, 20 Jul 2022 14:29:50 -0400 Subject: [PATCH 3/3] format --- dist/post_run/index.js | 2 +- dist/run/index.js | 2 +- src/run.ts | 8 +++++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/dist/post_run/index.js b/dist/post_run/index.js index faeb7c829d..0f49b3310b 100644 --- a/dist/post_run/index.js +++ b/dist/post_run/index.js @@ -67856,7 +67856,7 @@ function patchFromPush(ctx) { head: ctx.sha, mediaType: { format: `diff`, - } + }, }); if (patchResp.status !== 200) { core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`); diff --git a/dist/run/index.js b/dist/run/index.js index 689d0ee1c0..aa1fb7913f 100644 --- a/dist/run/index.js +++ b/dist/run/index.js @@ -67856,7 +67856,7 @@ function patchFromPush(ctx) { head: ctx.sha, mediaType: { format: `diff`, - } + }, }); if (patchResp.status !== 200) { core.warning(`failed to fetch pull request patch: response status is ${patchResp.status}`); diff --git a/src/run.ts b/src/run.ts index be16f6b21a..17c1e25c24 100644 --- a/src/run.ts +++ b/src/run.ts @@ -1,6 +1,6 @@ import * as core from "@actions/core" import * as github from "@actions/github" -import { Context } from '@actions/github/lib/context' +import { Context } from "@actions/github/lib/context" import { exec, ExecOptions } from "child_process" import * as fs from "fs" import * as path from "path" @@ -36,7 +36,9 @@ async function fetchPatch(): Promise { } else if (ctx.eventName === `push`) { patch = await patchFromPush(ctx) } else { - core.info(`Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}`) + core.info( + `Not fetching patch for showing only new issues because it's not a pull request or push context: event name is ${ctx.eventName}` + ) return `` } @@ -104,7 +106,7 @@ async function patchFromPush(ctx: Context): Promise { head: ctx.sha, mediaType: { format: `diff`, - } + }, }) if (patchResp.status !== 200) {