From 4b016f3c90e5575e399aa298a575f21ad445ea1b Mon Sep 17 00:00:00 2001 From: Stephen Mulrennan Date: Wed, 15 May 2024 12:25:35 +0100 Subject: [PATCH] SCAN-5585 : Adding error for checks. --- src/checks.js | 38 +++++++++++++++++++++++++------------- src/config.js | 3 +++ src/index.js | 4 ++-- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/checks.js b/src/checks.js index ad823be..cf399dc 100644 --- a/src/checks.js +++ b/src/checks.js @@ -1,4 +1,5 @@ const github = require("@actions/github"); +const core = require("@actions/core"); const buildReport = require("./report"); const { title, token } = require("./config"); @@ -12,20 +13,26 @@ async function startCheck() { const pullRequest = github.context.payload.pull_request; const sha = (pullRequest && pullRequest.head.sha) || github.context.sha; - const response = await octokit.rest.checks.create({ - owner, - repo, - name: title, - head_sha: sha, - status: "in_progress", - output: { - title, - summary: "", - text: "", - }, - }); + try { - CHECK_ID = response.data.id; + const response = await octokit.rest.checks.create({ + owner, + repo, + name: title, + head_sha: sha, + status: "in_progress", + output: { + title, + summary: "", + text: "", + }, + }); + + CHECK_ID = response.data.id; + } + catch (error) { + throw new Error("Error creating check. Ensure your workflow has the 'checks:write' permissions - https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs") + } } function getOutputModel(details) { @@ -39,6 +46,11 @@ async function finishCheck(details) { const { owner, repo } = github.context.repo; const { conclusion, report } = getOutputModel(details); + if (!CHECK_ID) { + core.warning("No active check to finish."); + return; + } + await octokit.rest.checks.update({ owner, repo, diff --git a/src/config.js b/src/config.js index 303a235..f79939c 100644 --- a/src/config.js +++ b/src/config.js @@ -4,6 +4,9 @@ const github = require("@actions/github"); const DEFAULT_BRANCH_NAME = github.context.payload?.repository?.default_branch; const thisBranchName = () => { + core.info('Checking branch name'); + core.info(JSON.stringify(github.context.payload, null, 2)); + const refParts = github.context.payload.ref.split('/'); return refParts[refParts.length-1]; diff --git a/src/index.js b/src/index.js index 09fa336..67b2d68 100644 --- a/src/index.js +++ b/src/index.js @@ -25,9 +25,9 @@ async function runScan() { async function runScanWithChecks() { let scanDetails; - try { - await startCheck(); + await startCheck(); + try { scanDetails = await runScan(); core.debug(JSON.stringify(scanDetails, null, 2));