From 79e8ca0cface7983a272b9291502a91914dd19f8 Mon Sep 17 00:00:00 2001 From: Fabio Niephaus Date: Tue, 8 Nov 2022 18:15:51 +0100 Subject: [PATCH] Ensure creating comments cannot fail job. --- dist/cleanup/index.js | 7 ++++++- dist/main/index.js | 7 ++++++- src/utils.ts | 16 +++++++++++----- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/dist/cleanup/index.js b/dist/cleanup/index.js index d693b2f..b826875 100644 --- a/dist/cleanup/index.js +++ b/dist/cleanup/index.js @@ -74521,7 +74521,12 @@ function createPRComment(content) { throw new Error('Not a PR event.'); } const context = github.context; - yield github.getOctokit(getGitHubToken()).rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: (_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number, body: content })); + try { + yield github.getOctokit(getGitHubToken()).rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: (_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number, body: content })); + } + catch (err) { + core.error(`Failed to create pull request comment. Please make sure this job has 'write' permissions for the 'pull-requests' scope (see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions)? Internal error: ${err}`); + } }); } exports.createPRComment = createPRComment; diff --git a/dist/main/index.js b/dist/main/index.js index 356a576..ef60ccd 100644 --- a/dist/main/index.js +++ b/dist/main/index.js @@ -75268,7 +75268,12 @@ function createPRComment(content) { throw new Error('Not a PR event.'); } const context = github.context; - yield github.getOctokit(getGitHubToken()).rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: (_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number, body: content })); + try { + yield github.getOctokit(getGitHubToken()).rest.issues.createComment(Object.assign(Object.assign({}, context.repo), { issue_number: (_a = context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.number, body: content })); + } + catch (err) { + core.error(`Failed to create pull request comment. Please make sure this job has 'write' permissions for the 'pull-requests' scope (see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions)? Internal error: ${err}`); + } }); } exports.createPRComment = createPRComment; diff --git a/src/utils.ts b/src/utils.ts index 2f1a63f..5ec71ac 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -127,9 +127,15 @@ export async function createPRComment(content: string): Promise { throw new Error('Not a PR event.') } const context = github.context - await github.getOctokit(getGitHubToken()).rest.issues.createComment({ - ...context.repo, - issue_number: context.payload.pull_request?.number as number, - body: content - }) + try { + await github.getOctokit(getGitHubToken()).rest.issues.createComment({ + ...context.repo, + issue_number: context.payload.pull_request?.number as number, + body: content + }) + } catch (err) { + core.error( + `Failed to create pull request comment. Please make sure this job has 'write' permissions for the 'pull-requests' scope (see https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions)? Internal error: ${err}` + ) + } }