From 3de9823efcb999c1f17ec1638da496a926a60c51 Mon Sep 17 00:00:00 2001 From: Jared Szechy Date: Wed, 20 Jul 2022 14:00:44 -0400 Subject: [PATCH] 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 0000000..52e118f --- /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 b3f81b6..ee592e7 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 b7a245f..fccb1f8 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 2fd55d5..e3cb1ec 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 } }