Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commenting): Add commentingEnabled flag to control PR commenting #184

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
32 changes: 20 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -61195,7 +61196,8 @@ function getConfiguration() {
projectName,
buildNumber,
commentingStage,
githubToken: githubToken()
githubToken: githubToken(),
commentingEnabled
}
};
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export interface PullRequestCommentConfig {
buildNumber: string;
commentingStage: string;
githubToken: string;
commentingEnabled: boolean;
}

export interface Configuration {
Expand Down Expand Up @@ -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,
Expand All @@ -220,6 +223,7 @@ export function getConfiguration(): Configuration {
buildNumber,
commentingStage,
githubToken: githubToken(),
commentingEnabled,
},
};
}
32 changes: 19 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,27 @@ export const main = async (options: Options): Promise<void> => {
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');
}
};

Expand Down
Loading