From ad9d555acbdce8c68bcf4f7eecd38399b96dd538 Mon Sep 17 00:00:00 2001 From: sirineJ <112706079+sirineJ@users.noreply.github.com> Date: Thu, 26 Dec 2024 11:02:05 +0100 Subject: [PATCH] add post CR comment --- .github/workflows/cr.yml | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml index 0cf3c885d3..6c9e9d6df3 100644 --- a/.github/workflows/cr.yml +++ b/.github/workflows/cr.yml @@ -27,3 +27,84 @@ jobs: - name: Publish packages run: npx pkg-pr-new publish './packages/circuit-ui' './packages/design-tokens' './packages/icons' + + - name: Post or update comment + uses: actions/github-script@v6 + with: + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); + + const packages = output.packages + .map((p) => `- ${p.name}: ${p.url}`) + .join('\n'); + const templates = output.templates + .map((t) => `- [${t.name}](${t.url})`) + .join('\n'); + + const sha = context.payload.pull_request.head.sha + + const commitUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${sha}`; + + const body = `## Packages were published 🚀 + + ### Published Packages: + + ${packages} + + ### Templates: + + ${templates} + + [View Commit](${commitUrl})`; + + const botCommentIdentifier = '## Packages were published'; + + async function findBotComment(issueNumber) { + if (!issueNumber) return null; + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issueNumber, + }); + return comments.data.find((comment) => + comment.body.includes(botCommentIdentifier) + ); + } + + async function createOrUpdateComment(issueNumber) { + if (!issueNumber) { + console.log('No issue number provided. Cannot post or update comment.'); + return; + } + + const existingComment = await findBotComment(issueNumber); + if (existingComment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existingComment.id, + body: body, + }); + } else { + await github.rest.issues.createComment({ + issue_number: issueNumber, + owner: context.repo.owner, + repo: context.repo.repo, + body: body, + }); + } + } + if (context.issue.number) { + await createOrUpdateComment(context.issue.number); + } + + - name: Delete label + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + PR_NUMBER: + ${{ github.event.pull_request.number }} + run: | + echo "Pull Request Number: $PR_NUMBER" + gh pr edit $PR_NUMBER --remove-label "preview"