From 270735fbba498b7863002d25e8d13eac9e9f5132 Mon Sep 17 00:00:00 2001 From: Rob van der Leek <5324924+robvanderleek@users.noreply.github.com> Date: Mon, 23 Dec 2024 14:00:39 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Alway=20try=20to=20update?= =?UTF-8?q?=20branch=20and=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/action.ts | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/action.ts b/src/action.ts index fa55917..6525b78 100644 --- a/src/action.ts +++ b/src/action.ts @@ -1,15 +1,17 @@ import fs from "fs"; -import {getInput, getMultilineInput} from "@actions/core"; +import {getInput} from "@actions/core"; import {context} from "@actions/github"; import {Octokit} from "@octokit/action"; import { createBranchIfNotExists, createOrUpdateFile, - createPRComment, getFile, + createPRComment, + getFile, getRepoName, getRepoOwner, getSourceBranch, - isPullRequest, isPullRequestFromFork, updateComment + isPullRequest, + updateComment } from "./github"; import {exec, getExecOutput} from "@actions/exec"; import {downloadCodeLimitBinary, getReportContent, makeNotFoundBadgeSvg, makeStatusBadgeSvg} from "./codelimit"; @@ -28,14 +30,7 @@ async function generateMarkdownReport(clBinary: string) { return result; } -async function updateReportsBranch(octokit: Octokit, markdownReport: string) { - const owner = getRepoOwner(context); - const repo = getRepoName(context); - const branch = getSourceBranch(); - if (!owner || !repo || !branch) { - console.error('Could not determine repository owner, name, or branch'); - process.exit(1); - } +async function updateReportsBranch(octokit: Octokit, owner: string, repo: string, branch: string, markdownReport: string) { await createBranchIfNotExists(octokit, owner, repo, '_codelimit_reports'); const reportContent = getReportContent(); let badgeContent; @@ -50,15 +45,12 @@ async function updateReportsBranch(octokit: Octokit, markdownReport: string) { await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/report.json`, reportContent); } await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, markdownReport); - if (isPullRequest()) { - await updatePullRequestComment(octokit, owner, repo, branch, markdownReport); - } } -async function updatePullRequestComment(octokit: Octokit, owner: string, repo: string, branchName: string, markdownReport: string) { +async function updatePullRequestComment(octokit: Octokit, owner: string, repo: string, branch: string, markdownReport: string) { const prNumber = context.payload.pull_request?.number; if (prNumber) { - const actionStateFile = await getFile(octokit, owner, repo, '_codelimit_reports', `${branchName}/action.json`); + const actionStateFile = await getFile(octokit, owner, repo, '_codelimit_reports', `${branch}/action.json`); if (actionStateFile) { const fileContent = Buffer.from(actionStateFile.content, 'base64').toString('utf-8'); const actionState = JSON.parse(fileContent) as ActionState; @@ -70,7 +62,7 @@ async function updatePullRequestComment(octokit: Octokit, owner: string, repo: s const commentId = await createPRComment(octokit, owner, repo, prNumber, markdownReport); const actionState: ActionState = {commentId: commentId}; const actionStateJson = JSON.stringify(actionState); - await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branchName}/action.json`, actionStateJson); + await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/action.json`, actionStateJson); } } } @@ -99,16 +91,24 @@ async function main() { if (doCheck) { exitCode = await checkChangedFiles(octokit, clBinary); } - if (!isPullRequestFromFork()) { - try { - await updateReportsBranch(octokit, markdownReport); - } catch (e: unknown) { - console.error('Failed to update reports branch'); - if (e instanceof Error) { - console.error(`Reason: ${e.message}`); - } + const owner = getRepoOwner(context); + const repo = getRepoName(context); + const branch = getSourceBranch(); + if (!owner || !repo || !branch) { + console.error('Could not determine repository owner, name, or branch'); + process.exit(1); + } + try { + await updateReportsBranch(octokit, owner, repo, branch, markdownReport); + } catch (e: unknown) { + console.error('Failed to update reports branch'); + if (e instanceof Error) { + console.error(`Reason: ${e.message}`); } } + if (isPullRequest()) { + await updatePullRequestComment(octokit, owner, repo, branch, markdownReport); + } fs.unlinkSync(clBinary); console.log('Done!'); process.exit(exitCode);