From 1275686863875d7b86e0f38e6362644048679500 Mon Sep 17 00:00:00 2001 From: akash1810 Date: Mon, 16 Dec 2024 15:31:19 +0000 Subject: [PATCH] feat(commenting): Add `commentingEnabled` flag to control PR commenting If a repository builds for multiple Riff-Raff projects, receiving multiple comments (one per Riff-Raff project) can become noisy. This change introduces a flag (`commentingEnabled`) to disable comments entirely. --- README.md | 6 ++++++ action.yaml | 4 ++++ dist/index.js | 32 ++++++++++++++++++++------------ src/config.ts | 4 ++++ src/index.ts | 32 +++++++++++++++++++------------- 5 files changed, 53 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 7c07c79..0c40c3c 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,12 @@ _Default: CODE_ When commenting on a pull request, which stage should be used. Typically a pre-production stage. +### `commentingEnabled` +_Default: true_ + +Whether to comment on the pull request with Riff-Raff deployment links. See also `commentingStage`. + + ## Detailed example To illustrate, given the following file structure: diff --git a/action.yaml b/action.yaml index 61cd7b3..5c29f18 100644 --- a/action.yaml +++ b/action.yaml @@ -38,6 +38,10 @@ inputs: required: false description: When commenting on a pull request, which stage should be used. Typically a pre-production stage. default: CODE + commentingEnabled: + required: false + description: Whether to comment on the pull request with Riff-Raff deployment links. See also `commentingStage`. + default: 'true' runs: using: node20 main: "dist/index.js" diff --git a/dist/index.js b/dist/index.js index 8a7b777..9085ef9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -61180,6 +61180,7 @@ function getConfiguration() { const baseBuildNumber = buildNumberInput ?? envOrUndefined("GITHUB_RUN_NUMBER") ?? "dev"; const buildNumber = offsetBuildNumber(baseBuildNumber, buildNumberOffset); const commentingStage = getInput2("commentingStage") ?? "CODE"; + const commentingEnabled = (getInput2("commentingEnabled") ?? "true") === "true"; return { projectName, roleArn, @@ -61195,7 +61196,8 @@ function getConfiguration() { projectName, buildNumber, commentingStage, - githubToken: githubToken() + githubToken: githubToken(), + commentingEnabled } }; } @@ -61450,19 +61452,25 @@ var main = async (options) => { ); throw err; } - try { - const pullRequestNumber = await getPullRequestNumber(pullRequestComment); - if (pullRequestNumber) { - core4.info(`Commenting on PR ${pullRequestNumber}`); - await commentOnPullRequest(pullRequestNumber, pullRequestComment); - } else { - core4.info( - `Unable to calculate Pull Request number, so cannot add a comment. Event is ${import_github2.context.eventName}` + if (pullRequestComment.commentingEnabled) { + try { + const pullRequestNumber = await getPullRequestNumber(pullRequestComment); + if (pullRequestNumber) { + core4.info(`Commenting on PR ${pullRequestNumber}`); + await commentOnPullRequest(pullRequestNumber, pullRequestComment); + } else { + core4.info( + `Unable to calculate Pull Request number, so cannot add a comment. Event is ${import_github2.context.eventName}` + ); + } + } catch (err) { + core4.error( + "Error commenting on PR. Do you have the correct permissions?" ); + throw err; } - } catch (err) { - core4.error("Error commenting on PR. Do you have the correct permissions?"); - throw err; + } else { + core4.info("commentingEnabled is `false`, skipping comment"); } }; if (require.main === module) { diff --git a/src/config.ts b/src/config.ts index 761031d..1c247ae 100644 --- a/src/config.ts +++ b/src/config.ts @@ -147,6 +147,7 @@ export interface PullRequestCommentConfig { buildNumber: string; commentingStage: string; githubToken: string; + commentingEnabled: boolean; } export interface Configuration { @@ -203,6 +204,8 @@ export function getConfiguration(): Configuration { const buildNumber = offsetBuildNumber(baseBuildNumber, buildNumberOffset); const commentingStage = getInput('commentingStage') ?? 'CODE'; + const commentingEnabled: boolean = + (getInput('commentingEnabled') ?? 'true') === 'true'; return { projectName, @@ -220,6 +223,7 @@ export function getConfiguration(): Configuration { buildNumber, commentingStage, githubToken: githubToken(), + commentingEnabled, }, }; } diff --git a/src/index.ts b/src/index.ts index ae61df8..7de3f9a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -142,21 +142,27 @@ export const main = async (options: Options): Promise => { throw err; } - try { - const pullRequestNumber = await getPullRequestNumber(pullRequestComment); - if (pullRequestNumber) { - core.info(`Commenting on PR ${pullRequestNumber}`); - await commentOnPullRequest(pullRequestNumber, pullRequestComment); - } else { - core.info( - `Unable to calculate Pull Request number, so cannot add a comment. Event is ${context.eventName}`, + if (pullRequestComment.commentingEnabled) { + try { + const pullRequestNumber = await getPullRequestNumber(pullRequestComment); + if (pullRequestNumber) { + core.info(`Commenting on PR ${pullRequestNumber}`); + await commentOnPullRequest(pullRequestNumber, pullRequestComment); + } else { + core.info( + `Unable to calculate Pull Request number, so cannot add a comment. Event is ${context.eventName}`, + ); + } + } catch (err) { + core.error( + 'Error commenting on PR. Do you have the correct permissions?', ); - } - } catch (err) { - core.error('Error commenting on PR. Do you have the correct permissions?'); - // throw to fail the action - throw err; + // throw to fail the action + throw err; + } + } else { + core.info('commentingEnabled is `false`, skipping comment'); } };