|
| 1 | +name: Update pkg.pr.new comment |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ['Publish Any Commit'] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +permissions: |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + build: |
| 14 | + name: 'Update comment' |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - uses: GitHubSecurityLab/actions-permissions/monitor@v1 |
| 18 | + - name: Download artifact |
| 19 | + uses: actions/download-artifact@v4 |
| 20 | + with: |
| 21 | + name: output |
| 22 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + run-id: ${{ github.event.workflow_run.id }} |
| 24 | + |
| 25 | + - run: ls -R . |
| 26 | + - name: 'Post or update comment' |
| 27 | + uses: actions/github-script@v6 |
| 28 | + with: |
| 29 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + script: | |
| 31 | + const fs = require('fs'); |
| 32 | + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); |
| 33 | +
|
| 34 | + const bot_comment_identifier = `<!-- pkg.pr.new comment -->`; |
| 35 | +
|
| 36 | + const body = (number) => `${bot_comment_identifier} |
| 37 | +
|
| 38 | + \`\`\` |
| 39 | + ${output.packages.map((p) => `pnpm add https://pkg.pr.new/${p.name}@${number}`).join('\n')} |
| 40 | + \`\`\` |
| 41 | + `; |
| 42 | +
|
| 43 | + async function find_bot_comment(issue_number) { |
| 44 | + if (!issue_number) return null; |
| 45 | + const comments = await github.rest.issues.listComments({ |
| 46 | + owner: context.repo.owner, |
| 47 | + repo: context.repo.repo, |
| 48 | + issue_number: issue_number, |
| 49 | + }); |
| 50 | + return comments.data.find((comment) => |
| 51 | + comment.body.includes(bot_comment_identifier) |
| 52 | + ); |
| 53 | + } |
| 54 | +
|
| 55 | + async function create_or_update_comment(issue_number) { |
| 56 | + if (!issue_number) { |
| 57 | + console.log('No issue number provided. Cannot post or update comment.'); |
| 58 | + return; |
| 59 | + } |
| 60 | +
|
| 61 | + const existing_comment = await find_bot_comment(issue_number); |
| 62 | + if (existing_comment) { |
| 63 | + await github.rest.issues.updateComment({ |
| 64 | + owner: context.repo.owner, |
| 65 | + repo: context.repo.repo, |
| 66 | + comment_id: existing_comment.id, |
| 67 | + body: body(issue_number), |
| 68 | + }); |
| 69 | + } else { |
| 70 | + await github.rest.issues.createComment({ |
| 71 | + issue_number: issue_number, |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + body: body(issue_number), |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | +
|
| 79 | + async function log_publish_info() { |
| 80 | + console.log('\n' + '='.repeat(50)); |
| 81 | + console.log('Publish Information'); |
| 82 | + console.log('='.repeat(50)); |
| 83 | + console.log('\nPublished Packages:'); |
| 84 | + console.log(output.packages.map((p) => `${p.name} - pnpm add https://pkg.pr.new/${p.name}@${p.url.replace(/^.+@([^@]+)$/, '$1')}`).join('\n')); |
| 85 | + console.log('\n' + '='.repeat(50)); |
| 86 | + } |
| 87 | +
|
| 88 | + if (output.event_name === 'pull_request') { |
| 89 | + if (output.number) { |
| 90 | + await create_or_update_comment(output.number); |
| 91 | + } |
| 92 | + } else if (output.event_name === 'push') { |
| 93 | + const pull_requests = await github.rest.pulls.list({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + state: 'open', |
| 97 | + head: `${context.repo.owner}:${output.ref.replace('refs/heads/', '')}`, |
| 98 | + }); |
| 99 | +
|
| 100 | + if (pull_requests.data.length > 0) { |
| 101 | + await create_or_update_comment(pull_requests.data[0].number); |
| 102 | + } else { |
| 103 | + console.log( |
| 104 | + 'No open pull request found for this push. Logging publish information to console:' |
| 105 | + ); |
| 106 | + await log_publish_info(); |
| 107 | + } |
| 108 | + } |
0 commit comments