|
| 1 | +name: Publish PR |
| 2 | +run-name: ${{ github.event.workflow_run.display_title }} |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_run: |
| 6 | + workflows: ['Render PR'] |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +env: |
| 10 | + # Must match ./render-pr.yml |
| 11 | + ARTIFACT_NAME: ${{ vars.ARTIFACT_NAME || 'result' }} |
| 12 | + |
| 13 | +jobs: |
| 14 | + publish: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + if: > |
| 17 | + ${{ |
| 18 | + !github.event.repository.fork |
| 19 | + && github.event.workflow_run.event == 'pull_request' |
| 20 | + && github.event.workflow_run.conclusion == 'success' |
| 21 | + }} |
| 22 | + steps: |
| 23 | + - uses: actions/checkout@v4 |
| 24 | + - uses: ljharb/actions/node/install@main |
| 25 | + name: 'nvm install lts/* && npm install' |
| 26 | + with: |
| 27 | + node-version: lts/* |
| 28 | + - name: Print event info |
| 29 | + uses: actions/github-script@v7 |
| 30 | + with: |
| 31 | + script: 'console.log(${{ toJson(github.event) }});' |
| 32 | + - name: Download zipball |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const { owner, repo } = context.repo; |
| 37 | + const run_id = ${{ github.event.workflow_run.id }}; |
| 38 | + const name = process.env.ARTIFACT_NAME; |
| 39 | + const listArtifactsQuery = { owner, repo, run_id, name }; |
| 40 | + const listArtifactsResponse = await github.rest.actions.listWorkflowRunArtifacts(listArtifactsQuery); |
| 41 | + const { total_count, artifacts } = listArtifactsResponse.data; |
| 42 | + if (total_count !== 1) { |
| 43 | + const summary = artifacts?.map(({ name, size_in_bytes, url }) => ({ name, size_in_bytes, url })); |
| 44 | + const detail = JSON.stringify(summary ?? []); |
| 45 | + throw new RangeError(`Expected 1 ${name} artifact, got ${total_count} ${detail}`); |
| 46 | + } |
| 47 | + const artifact_id = artifacts[0].id; |
| 48 | + console.log(`downloading artifact ${artifact_id}`); |
| 49 | + const downloadResponse = await github.rest.actions.downloadArtifact({ |
| 50 | + owner, |
| 51 | + repo, |
| 52 | + artifact_id, |
| 53 | + archive_format: 'zip', |
| 54 | + }); |
| 55 | + const fs = require('fs'); |
| 56 | + fs.writeFileSync('${{ github.workspace }}/result.zip', Buffer.from(downloadResponse.data)); |
| 57 | + - name: Provide result directory |
| 58 | + run: rm -rf result && mkdir -p result |
| 59 | + - run: unzip -o result.zip -d result |
| 60 | + - run: ls result |
| 61 | + - name: Extract PR data |
| 62 | + run: | |
| 63 | + cd result |
| 64 | + awk -v ok=1 ' |
| 65 | + NR == 1 && match($0, /^[1-9][0-9]* [0-9a-fA-F]{7,}$/) { |
| 66 | + print "PR=" $1; |
| 67 | + print "COMMIT=" $2; |
| 68 | + next; |
| 69 | + } |
| 70 | + { ok = 0; } |
| 71 | + END { exit !ok; } |
| 72 | + ' pr-data.txt >> "$GITHUB_ENV" |
| 73 | + rm pr-data.txt |
| 74 | + - name: Insert preview warning |
| 75 | + run: | |
| 76 | + tmp="$(mktemp -u XXXXXXXX.json)" |
| 77 | + export REPO_URL="https://github.com/$GITHUB_REPOSITORY" |
| 78 | + jq -n ' |
| 79 | + def repo_link($args): $args as [$path, $contents] |
| 80 | + | (env.REPO_URL + ($path // "")) as $url |
| 81 | + | "<a href=\"\($url | @html)\">\($contents // $url)</a>"; |
| 82 | + { |
| 83 | + SUMMARY: "PR #\(env.PR)", |
| 84 | + REPO_LINK: repo_link([]), |
| 85 | + PR_LINK: repo_link(["/pull/" + env.PR, "PR #\(env.PR)"]), |
| 86 | + COMMIT_LINK: ("commit " + repo_link(["/commit/" + env.COMMIT, "<code>\(env.COMMIT)</code>"])), |
| 87 | + } |
| 88 | + ' > "$tmp" |
| 89 | + find result -name '*.html' -exec \ |
| 90 | + node scripts/insert_warning.mjs scripts/pr_preview_warning.html "$tmp" '{}' '+' |
| 91 | + - name: Publish to gh-pages |
| 92 | + uses: JamesIves/github-pages-deploy-action@v4 |
| 93 | + with: |
| 94 | + branch: gh-pages |
| 95 | + folder: result |
| 96 | + target-folder: pr/${{ env.PR }} |
| 97 | + - name: Determine gh-pages url |
| 98 | + id: get-pages-url |
| 99 | + run: | |
| 100 | + gh_pages_url="https://$(printf '%s' "$GITHUB_REPOSITORY" \ |
| 101 | + | sed 's#/#.github.io/#; s#^tc39.github.io/#tc39.es/#')" |
| 102 | + echo "url=$gh_pages_url" >> $GITHUB_OUTPUT |
| 103 | + - name: Provide PR comment |
| 104 | + uses: phulsechinmay/[email protected] |
| 105 | + with: |
| 106 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 107 | + ISSUE_ID: ${{ env.PR }} |
| 108 | + message: > |
| 109 | + The rendered spec for this PR is available at |
| 110 | + ${{ steps.get-pages-url.outputs.url }}/pr/${{ env.PR }}. |
0 commit comments