From b38237834a5d6914cbf89c1c6d44feb469c73160 Mon Sep 17 00:00:00 2001 From: Rob van der Leek <5324924+robvanderleek@users.noreply.github.com> Date: Tue, 3 Dec 2024 16:21:43 +0100 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=9A=A7=20Create=20PR=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/action.ts | 21 +++++++++++++++++---- src/github.ts | 9 +++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/action.ts b/src/action.ts index 429e391..05d0586 100644 --- a/src/action.ts +++ b/src/action.ts @@ -5,7 +5,15 @@ import {getInput} from "@actions/core"; import {promisify} from "util"; import {context} from "@actions/github"; import {Octokit} from "@octokit/action"; -import {branchExists, createBranch, createInitialCommit, createOrUpdateFile, getRepoName, getRepoOwner} from "./github"; +import { + branchExists, + createBranch, + createInitialCommit, + createOrUpdateFile, + createPRComment, + getRepoName, + getRepoOwner +} from "./github"; import {exec, getExecOutput} from "@actions/exec"; import {makeBadge} from "badge-maker"; @@ -140,8 +148,7 @@ async function main() { await exec(filename, ['scan', '.']); const totalsMarkdown = await getExecOutput(filename, ['report', '--totals', '--format', 'markdown']); const unitsMarkdown = await getExecOutput(filename, ['report', '--full', '--format', 'markdown']); - console.log(totalsMarkdown.stdout); - console.log(unitsMarkdown.stdout); + const markdownReport = `${totalsMarkdown.stdout}\n${unitsMarkdown.stdout}`; const doUpload = getInput('upload') || false; const token = getInput('token'); const octokit = new Octokit({auth: token}); @@ -158,7 +165,13 @@ async function main() { if (reportContent) { await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/report.json`, reportContent); } - await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, `${totalsMarkdown}\n${unitsMarkdown}`); + await createOrUpdateFile(octokit, owner, repo, '_codelimit_reports', `${branch}/codelimit.md`, markdownReport); + if (isPullRequest()) { + const prNumber = context.payload.pull_request?.number; + if (prNumber) { + await createPRComment(octokit, owner, repo, prNumber, markdownReport); + } + } let exitCode = 0; if (doUpload) { console.log('Uploading results...'); diff --git a/src/github.ts b/src/github.ts index aa04ecf..9663dc9 100644 --- a/src/github.ts +++ b/src/github.ts @@ -90,3 +90,12 @@ export async function createOrUpdateFile(octokit: Octokit, owner: string, repo: } }); } + +export async function createPRComment(octokit: Octokit, owner: string, repo: string, prNumber: number, comment: string) { + await octokit.issues.createComment({ + owner: owner, + repo: repo, + issue_number: prNumber, + body: comment + }); +} \ No newline at end of file