|
| 1 | +--- |
| 2 | +name: PR Playground Preview |
| 3 | + |
| 4 | +on: |
| 5 | + pull_request: |
| 6 | + types: [opened, synchronize, reopened, edited] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +jobs: |
| 13 | + preview: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout repository |
| 17 | + uses: actions/checkout@v6 |
| 18 | + |
| 19 | + - name: Post or update playground preview comment |
| 20 | + uses: actions/github-script@v8 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + const fs = require('fs'); |
| 24 | + const marker = '<!-- omeka-s-playground-preview -->'; |
| 25 | + const owner = context.repo.owner; |
| 26 | + const repo = context.repo.repo; |
| 27 | + const headRef = context.payload.pull_request.head.ref; |
| 28 | + const prNumber = context.payload.pull_request.number; |
| 29 | +
|
| 30 | + // Read the root blueprint and replace the plugin URL with the PR branch ZIP |
| 31 | + const blueprint = JSON.parse(fs.readFileSync('blueprint.json', 'utf8')); |
| 32 | + const prZipUrl = `https://github.com/${owner}/${repo}/archive/refs/heads/${headRef}.zip`; |
| 33 | + blueprint.plugins = blueprint.plugins.map(url => |
| 34 | + url.includes(`${owner}/${repo}`) ? prZipUrl : url |
| 35 | + ); |
| 36 | +
|
| 37 | + // Encode as base64url |
| 38 | + function toBase64url(str) { |
| 39 | + return Buffer.from(str) |
| 40 | + .toString('base64') |
| 41 | + .replace(/\+/g, '-') |
| 42 | + .replace(/\//g, '_') |
| 43 | + .replace(/=+$/, ''); |
| 44 | + } |
| 45 | +
|
| 46 | + const encoded = toBase64url(JSON.stringify(blueprint)); |
| 47 | + const playgroundUrl = `https://ateeducacion.github.io/omeka-s-playground/?blueprint-data=${encoded}`; |
| 48 | + const imageUrl = 'https://raw.githubusercontent.com/ateeducacion/omeka-s-playground/refs/heads/main/ogimage.png'; |
| 49 | +
|
| 50 | + const body = [ |
| 51 | + marker, |
| 52 | + '## omeka-s Playground Preview', |
| 53 | + '', |
| 54 | + `<a href="${playgroundUrl}">`, |
| 55 | + ` <img src="${imageUrl}" alt="Open this PR in Omeka-S Playground" width="220">`, |
| 56 | + '</a><br>', |
| 57 | + `<small><a href="${playgroundUrl}">Try this PR in your browser</a></small>`, |
| 58 | + '', |
| 59 | + 'This preview loads the plugin directly from the PR branch ZIP in Omeka-S Playground.', |
| 60 | + ].join('\n'); |
| 61 | +
|
| 62 | + // Search for an existing comment with the marker |
| 63 | + const { data: comments } = await github.rest.issues.listComments({ |
| 64 | + owner, |
| 65 | + repo, |
| 66 | + issue_number: prNumber, |
| 67 | + }); |
| 68 | +
|
| 69 | + const existing = comments.find(c => c.body && c.body.includes(marker)); |
| 70 | +
|
| 71 | + if (existing) { |
| 72 | + await github.rest.issues.updateComment({ |
| 73 | + owner, |
| 74 | + repo, |
| 75 | + comment_id: existing.id, |
| 76 | + body, |
| 77 | + }); |
| 78 | + core.info(`Updated existing preview comment #${existing.id}`); |
| 79 | + } else { |
| 80 | + await github.rest.issues.createComment({ |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + issue_number: prNumber, |
| 84 | + body, |
| 85 | + }); |
| 86 | + core.info('Created new preview comment'); |
| 87 | + } |
0 commit comments