From 1f74bce383ab22714a00e1e2a805a501df1b2d9e Mon Sep 17 00:00:00 2001 From: "kirill.chalov" Date: Wed, 25 Dec 2024 19:44:46 +0800 Subject: [PATCH] ci[pr-preview]: keep only one comment with PR preview link --- .github/workflows/prw-deploy-preview.yml | 41 ++++++++++++++++++++---- 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/.github/workflows/prw-deploy-preview.yml b/.github/workflows/prw-deploy-preview.yml index 34bd9a98..ee00fc53 100644 --- a/.github/workflows/prw-deploy-preview.yml +++ b/.github/workflows/prw-deploy-preview.yml @@ -66,12 +66,41 @@ jobs: with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | - github.rest.issues.createComment({ - issue_number: ${{ env.PR_NUMBER }}, - owner: context.repo.owner, - repo: context.repo.repo, - body: `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}pr${{ env.PR_NUMBER }}/` - }); + try { + const { data: comments } = await github.rest.issues.listComments({ + issue_number: ${{ env.PR_NUMBER }}, + owner: context.repo.owner, + repo: context.repo.repo + }); + + // Define the comment body + const commentBody = `🎉 A preview for this PR is available at: ${{ env.HUGO_BASEURL }}pr${{ env.PR_NUMBER }}/`; + + // Look for an existing comment containing the specific text + const existingComment = comments.find(comment => + comment.body.includes("🎉 A preview for this PR is available at:") + ); + + if (existingComment) { + // Update the existing comment + await github.rest.issues.updateComment({ + comment_id: existingComment.id, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody + }); + } else { + // Create a new comment + await github.rest.issues.createComment({ + issue_number: ${{ env.PR_NUMBER }}, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody + }); + } + } catch (error) { + core.setFailed(`Failed to manage PR comment: ${error.message}`); + } - name: Invalidate CloudFront cache for PR uses: chetan/invalidate-cloudfront-action@v2