From 0ce4b559f213944b426eb5cb687d5b22ba51bef7 Mon Sep 17 00:00:00 2001 From: Paolo Ricciuti Date: Tue, 5 Nov 2024 14:07:54 +0100 Subject: [PATCH] chore: add custom comment with link to playground for `pkg.pr.new` (#14151) * chore: add custom comment with link to playground for `pkg.pr.new` * chore: small updates to the script output * chore: refine comment a bit, include multiple packages * tweak comment * chore: two steps workflow * chore: update workflow --------- Co-authored-by: Rich Harris --- .github/workflows/pkg.pr.new-comment.yml | 107 +++++++++++++++++++++++ .github/workflows/pkg.pr.new.yml | 18 +++- 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/pkg.pr.new-comment.yml diff --git a/.github/workflows/pkg.pr.new-comment.yml b/.github/workflows/pkg.pr.new-comment.yml new file mode 100644 index 0000000000..b6ad1272f0 --- /dev/null +++ b/.github/workflows/pkg.pr.new-comment.yml @@ -0,0 +1,107 @@ +name: Update pkg.pr.new comment + +on: + workflow_run: + workflows: ['Publish Any Commit'] + types: + - completed + +jobs: + build: + name: 'Update comment' + runs-on: ubuntu-latest + steps: + - name: Download artifact + uses: actions/download-artifact@v4 + with: + name: output.json + path: output.json + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + - 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 bot_comment_identifier = ``; + + const body = (number) => `${bot_comment_identifier} + + [Playground](https://svelte.dev/playground?version=pr-${number}) + + \`\`\` + ${output.packages.map((p) => `pnpm add https://pkg.pr.new/${p.name}@${number}`).join('\n')} + \`\`\` + `; + + async function find_bot_comment(issue_number) { + if (!issue_number) return null; + const comments = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: issue_number, + }); + return comments.data.find((comment) => + comment.body.includes(bot_comment_identifier) + ); + } + + async function create_or_update_comment(issue_number) { + if (!issue_number) { + console.log('No issue number provided. Cannot post or update comment.'); + return; + } + + const existing_comment = await find_bot_comment(issue_number); + if (existing_comment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existing_comment.id, + body: body(issue_number), + }); + } else { + await github.rest.issues.createComment({ + issue_number: issue_number, + owner: context.repo.owner, + repo: context.repo.repo, + body: body(issue_number), + }); + } + } + + async function log_publish_info() { + console.log('\n' + '='.repeat(50)); + console.log('Publish Information'); + console.log('='.repeat(50)); + console.log('\nPublished Packages:'); + console.log(packages); + console.log('\nPlayground URL:'); + console.log(`\nhttps://svelte.dev/playground?version=commit-${output.sha.substring(0, 7)}`) + console.log('\n' + '='.repeat(50)); + } + + if (output.event_name === 'pull_request') { + if (context.issue.number) { + await create_or_update_comment(context.issue.number); + } + } else if (output.event_name === 'push') { + const pull_requests = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${context.repo.owner}:${output.ref.replace('refs/heads/', '')}`, + }); + + if (pull_requests.data.length > 0) { + await create_or_update_comment(pull_requests.data[0].number); + } else { + console.log( + 'No open pull request found for this push. Logging publish information to console:' + ); + await log_publish_info(); + } + } diff --git a/.github/workflows/pkg.pr.new.yml b/.github/workflows/pkg.pr.new.yml index 76736782a0..ccb05ec93b 100644 --- a/.github/workflows/pkg.pr.new.yml +++ b/.github/workflows/pkg.pr.new.yml @@ -21,4 +21,20 @@ jobs: - name: Build run: pnpm build - - run: pnpx pkg-pr-new publish --compact --no-template './packages/svelte' + - run: pnpx pkg-pr-new publish --comment=off --json output.json --compact --no-template './packages/svelte' + - name: Add metadata to output + 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')); + output.number = context.issue.number; + output.event_name = context.eventName; + output.ref = context.ref; + fs.writeFileSync('output.json', JSON.stringify(output), 'utf8'); + - name: Upload output + uses: actions/upload-artifact@v4 + with: + name: output.json + path: output.json