From 39fc5724c91f6d6d8446e92065652fb646a9723d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Mon, 29 Apr 2024 13:39:02 +0200 Subject: [PATCH] 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 a721b76..0315f86 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`, },