From b6c5d042d5633333538a694a2ff20801fdb133ac Mon Sep 17 00:00:00 2001 From: FredrikOseberg Date: Tue, 10 Dec 2024 08:47:14 +0100 Subject: [PATCH] fix: only comment if comment has not been added already --- .github/workflows/core-feature-alert.yml | 53 +++++++++++++++++------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/.github/workflows/core-feature-alert.yml b/.github/workflows/core-feature-alert.yml index 68a41240af..1ad410de74 100644 --- a/.github/workflows/core-feature-alert.yml +++ b/.github/workflows/core-feature-alert.yml @@ -18,25 +18,48 @@ jobs: id: pr-creator run: echo "PR_CREATOR=${{ github.event.pull_request.user.login }}" >> $GITHUB_ENV - - name: Post a notification about core feature changes + - name: Post a notification about core feature changes if not already commented uses: actions/github-script@v6 with: script: | const prCreator = process.env.PR_CREATOR; - github.rest.issues.createComment({ + const commentBody = `@${prCreator}, core features have been modified in this pull request. Please review!`; + + const { data: comments } = await github.rest.issues.listComments({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.payload.pull_request.number, - body: `@${prCreator}, core features have been modified in this pull request. Please review!` - }) - # - name: Add reviewers to the PR - # uses: actions/github-script@v6 - # with: - # script: | - # const reviewers = ['FredrikOseberg']; - # github.rest.pulls.requestReviewers({ - # owner: context.repo.owner, - # repo: context.repo.repo, - # pull_number: context.payload.pull_request.number, - # reviewers: reviewers, - # }); \ No newline at end of file + }); + + const alreadyCommented = comments.some(comment => comment.body === commentBody); + + if (!alreadyCommented) { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + body: commentBody, + }); + console.log('Posted a new comment.'); + } else { + console.log('Comment already exists, skipping.'); + } + - name: Add reviewers to the PR if they are not the creator + uses: actions/github-script@v6 + with: + script: | + const prCreator = process.env.PR_CREATOR; + const allReviewers = ['FredrikOseberg', 'gastonfournier']; + const reviewersToAdd = allReviewers.filter(reviewer => reviewer !== prCreator); + + if (reviewersToAdd.length > 0) { + github.rest.pulls.requestReviewers({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.payload.pull_request.number, + reviewers: reviewersToAdd, + }); + console.log(`Added reviewers: ${reviewersToAdd.join(', ')}`); + } else { + console.log(`No reviewers to add as all match the PR creator.`); + } \ No newline at end of file