diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml index 0cf3c885d3..92a72b40ff 100644 --- a/.github/workflows/cr.yml +++ b/.github/workflows/cr.yml @@ -26,4 +26,85 @@ jobs: run: npm run build - name: Publish packages - run: npx pkg-pr-new publish './packages/circuit-ui' './packages/design-tokens' './packages/icons' + run: npx pkg-pr-new publish './packages/circuit-ui' './packages/design-tokens' './packages/icons' --json output.json --comment=off + + - 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"