|
| 1 | +name: Package Preview Publish |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +jobs: |
| 8 | + publish-preview: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + |
| 11 | + steps: |
| 12 | + - name: Checkout code |
| 13 | + uses: actions/checkout@v4 |
| 14 | + |
| 15 | + - name: Enable corepack |
| 16 | + run: corepack enable |
| 17 | + |
| 18 | + - name: Setup Node.js |
| 19 | + uses: actions/setup-node@v4 |
| 20 | + with: |
| 21 | + node-version: 20 |
| 22 | + cache: "npm" |
| 23 | + |
| 24 | + - name: Install dependencies |
| 25 | + run: npm install |
| 26 | + |
| 27 | + - name: Build package |
| 28 | + run: npm run build |
| 29 | + |
| 30 | + - name: Publish preview with pkg.pr.new |
| 31 | + run: npx pkg-pr-new publish --json output.json --comment=off |
| 32 | + |
| 33 | + - name: Comment PR with install instructions |
| 34 | + uses: actions/github-script@v6 |
| 35 | + with: |
| 36 | + script: | |
| 37 | + const fs = require('fs'); |
| 38 | + const output = JSON.parse(fs.readFileSync('output.json', 'utf8')); |
| 39 | + if (!output.packages || output.packages.length === 0) { |
| 40 | + core.setFailed('No packages published by pkg.pr.new'); |
| 41 | + return; |
| 42 | + } |
| 43 | + const pkg = output.packages[0]; |
| 44 | + const installCmd = `npm i ${pkg.url}`; |
| 45 | + const badge = `[](https://pkg.pr.new/~/${context.repo.owner}/${context.repo.repo})`; |
| 46 | + const body = `### 🚀 Package Preview Available! |
| 47 | +
|
| 48 | + ${badge} |
| 49 | +
|
| 50 | + --- |
| 51 | +
|
| 52 | + **Install this PR's preview build with npm:** |
| 53 | +
|
| 54 | + \`\`\`sh |
| 55 | + ${installCmd} |
| 56 | + \`\`\` |
| 57 | +
|
| 58 | + - 📦 [Preview Package on pkg.pr.new](${pkg.url}) |
| 59 | + - 🔗 [View this commit on GitHub](https://github.com/${context.repo.owner}/${context.repo.repo}/commit/${pkg.commit}) |
| 60 | +
|
| 61 | + --- |
| 62 | + <sub>Preview powered by [pkg.pr.new](https://pkg.pr.new) — try new features instantly, no NPM release needed!</sub> |
| 63 | + `; |
| 64 | +
|
| 65 | + const botCommentIdentifier = '### 🚀 Package Preview Available!'; |
| 66 | +
|
| 67 | + async function findBotComment(issueNumber) { |
| 68 | + if (!issueNumber) return null; |
| 69 | + const comments = await github.rest.issues.listComments({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + issue_number: issueNumber, |
| 73 | + }); |
| 74 | + return comments.data.find((comment) => |
| 75 | + comment.body.includes(botCommentIdentifier) |
| 76 | + ); |
| 77 | + } |
| 78 | +
|
| 79 | + async function createOrUpdateComment(issueNumber) { |
| 80 | + if (!issueNumber) { |
| 81 | + console.log('No issue number provided. Cannot post or update comment.'); |
| 82 | + return; |
| 83 | + } |
| 84 | +
|
| 85 | + const existingComment = await findBotComment(issueNumber); |
| 86 | + if (existingComment) { |
| 87 | + await github.rest.issues.updateComment({ |
| 88 | + owner: context.repo.owner, |
| 89 | + repo: context.repo.repo, |
| 90 | + comment_id: existingComment.id, |
| 91 | + body: body, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + await github.rest.issues.createComment({ |
| 95 | + issue_number: issueNumber, |
| 96 | + owner: context.repo.owner, |
| 97 | + repo: context.repo.repo, |
| 98 | + body: body, |
| 99 | + }); |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + if (context.eventName === 'pull_request') { |
| 104 | + if (context.issue.number) { |
| 105 | + await createOrUpdateComment(context.issue.number); |
| 106 | + } |
| 107 | + } |
| 108 | +
|
| 109 | +permissions: |
| 110 | + contents: read |
| 111 | + pull-requests: write |
0 commit comments