From adff70d02565a2612aad76444c5f58eb76ac8172 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Thu, 29 Feb 2024 10:51:37 -0500 Subject: [PATCH] Update course-schedule PR comments in-place (#1856) Instead of always adding a new comment, just update the existing comment if one exists. This fixes #1834. --- .github/workflows/course-schedule-comment.yml | 29 +++++++++++++++++-- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/.github/workflows/course-schedule-comment.yml b/.github/workflows/course-schedule-comment.yml index 9d2b1973a71..6059c671ee2 100644 --- a/.github/workflows/course-schedule-comment.yml +++ b/.github/workflows/course-schedule-comment.yml @@ -60,9 +60,32 @@ jobs: var pr_number = Number(fs.readFileSync('pr-number')); var upstream = fs.readFileSync('upstream-schedule').toString(); var schedule = fs.readFileSync('schedule').toString(); - if (upstream != schedule) { - schedule = schedule + "# New Course Schedule\n"; - schedule = schedule + "This PR changes the course schedule. The new schedule is shown below."; + schedule = "\n" + + "# Changes to Course Schedule\n" + + "This PR changes the course schedule. The new schedule is shown below.\n\n" + + schedule; + + // Look for existing comments + var existing_comment; + for await ({ data: comments } of github.paginate.iterator(github.rest.issues.listComments, { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr_number + })) { + existing_comment = comments.find((c) => c.body.includes("")); + if (existing_comment) { + break; + } + } + + if (existing_comment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existing_comment.id, + body: schedule, + }); + } else if (upstream != schedule) { await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo,