From a4107da76d4fda26ff3beb629e7a293dcecc55ab Mon Sep 17 00:00:00 2001 From: daz Date: Mon, 1 Jan 2024 17:05:35 -0700 Subject: [PATCH] Warn when permissions not set for PR comment --- src/job-summary.ts | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/job-summary.ts b/src/job-summary.ts index 54118d4..81cb05d 100644 --- a/src/job-summary.ts +++ b/src/job-summary.ts @@ -1,6 +1,7 @@ import * as core from '@actions/core' import * as github from '@actions/github' import {SUMMARY_ENV_VAR} from '@actions/core/lib/summary' +import {RequestError} from '@octokit/request-error' import * as params from './input-params' import {BuildResult} from './build-results' @@ -30,34 +31,48 @@ export async function generateJobSummary(buildResults: BuildResult[], cacheListe } async function addPRComment(jobSummary: string): Promise { - try { - const github_token = params.getGithubToken() + const context = github.context + if (context.payload.pull_request == null) { + core.info('No pull_request trigger: not adding PR comment') + return + } - const context = github.context - if (context.payload.pull_request == null) { - core.info('No pull_request trigger: not adding PR comment') - return - } + const pull_request_number = context.payload.pull_request.number + core.info(`Adding Job Summary as comment to PR #${pull_request_number}.`) - const pull_request_number = context.payload.pull_request.number - core.info(`Adding Job Summary as comment to PR #${pull_request_number}.`) - - const prComment = `

Job Summary for gradle-build-action

+ const prComment = `

Job Summary for gradle-build-action

${github.context.workflow} :: ${github.context.job}
${jobSummary}` - const octokit = github.getOctokit(github_token) + const github_token = params.getGithubToken() + const octokit = github.getOctokit(github_token) + try { await octokit.rest.issues.createComment({ ...context.repo, issue_number: pull_request_number, body: prComment }) } catch (error) { - core.warning(`Failed to generate PR comment: ${String(error)}`) + if (error instanceof RequestError) { + core.warning(buildWarningMessage(error)) + } else { + throw error + } } } +function buildWarningMessage(error: RequestError): string { + const mainWarning = `Failed to generate PR comment.\n${String(error)}` + if (error.message === 'Resource not accessible by integration') { + return `${mainWarning} +Please ensure that the 'pull-requests: write' permission is available for the workflow job. +Note that this permission is never available for a workflow triggered from a repository fork. + ` + } + return mainWarning +} + function renderSummaryTable(results: BuildResult[]): string { if (results.length === 0) { return 'No Gradle build results detected.'