Skip to content

Commit

Permalink
Support duplicate action comments on PRs
Browse files Browse the repository at this point in the history
The action may be run multiple times as requested. This isn't supported by
the PR comment status update as it presumes a single run of a v2 style
workspace. To support this case we now dedupe comments on workflow and
job name, setting the job name in the PR to differentiate runs.
  • Loading branch information
emcfarlane committed Jul 18, 2024
1 parent 1bf85d6 commit 1c1623c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
13 changes: 8 additions & 5 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45607,9 +45607,12 @@ async function downloadBuf(version) {
// See the License for the specific language governing permissions and
// limitations under the License.

// commentTag is the tag used to identify the comment. This is a non-visible
// string injected into the comment body.
const commentTag = "<!-- Buf results -->";

// commentTag returns the tag used to identify the comment. This is a non-visible
// string injected into the comment body. It is unique to the workflow and job.
function commentTag() {
return `<!-- buf ${lib_github.context.workflow}:${lib_github.context.job} -->`;
}
// findCommentOnPR finds the comment on the PR that contains the Buf results.
// If the comment is found, it returns the comment ID. If the comment is not
// found, it returns undefined.
Expand All @@ -45625,7 +45628,7 @@ async function findCommentOnPR(context, github) {
repo: repo,
issue_number: prNumber,
});
const previousComment = comments.find((comment) => comment.body?.includes(commentTag));
const previousComment = comments.find((comment) => comment.body?.includes(commentTag()));
if (previousComment) {
core.info(`Found previous comment ${previousComment.id}`);
return previousComment.id;
Expand Down Expand Up @@ -45758,7 +45761,7 @@ async function main() {
// Comment on the PR with the summary, if requested.
if (inputs.pr_comment) {
const commentID = await findCommentOnPR(lib_github.context, github);
await commentOnPR(lib_github.context, github, commentID, `The latest Buf updates on your PR.\n\n${summary.stringify()}`);
await commentOnPR(lib_github.context, github, commentID, `The latest Buf updates from ${lib_github.context.job} on your PR.\n\n${summary.stringify()}`);
}
// Write the summary to a file defined by GITHUB_STEP_SUMMARY.
// NB: Write empties the buffer and must be after the comment.
Expand Down
11 changes: 7 additions & 4 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
// limitations under the License.

import * as core from "@actions/core";
import { context } from "@actions/github";
import { Context } from "@actions/github/lib/context";
import { GitHub } from "@actions/github/lib/utils";

// commentTag is the tag used to identify the comment. This is a non-visible
// string injected into the comment body.
const commentTag = "<!-- Buf results -->";
// commentTag returns the tag used to identify the comment. This is a non-visible
// string injected into the comment body. It is unique to the workflow and job.
function commentTag(): string {
return `<!-- buf ${context.workflow}:${context.job} -->`;
}

// findCommentOnPR finds the comment on the PR that contains the Buf results.
// If the comment is found, it returns the comment ID. If the comment is not
Expand All @@ -39,7 +42,7 @@ export async function findCommentOnPR(
issue_number: prNumber,
});
const previousComment = comments.find((comment) =>
comment.body?.includes(commentTag),
comment.body?.includes(commentTag()),
);
if (previousComment) {
core.info(`Found previous comment ${previousComment.id}`);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function main() {
context,
github,
commentID,
`The latest Buf updates on your PR.\n\n${summary.stringify()}`,
`The latest Buf updates from ${context.job} on your PR.\n\n${summary.stringify()}`,
);
}
// Write the summary to a file defined by GITHUB_STEP_SUMMARY.
Expand Down

0 comments on commit 1c1623c

Please sign in to comment.