Meta: Optimize PR warning insertion #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish PR | |
run-name: ${{ github.event.workflow_run.display_title }} | |
on: | |
workflow_run: | |
workflows: ['Render PR'] | |
types: [completed] | |
jobs: | |
publish: | |
runs-on: ubuntu-latest | |
if: > | |
${{ | |
!github.event.repository.fork && | |
github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' | |
}} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: '[node LTS] npm install' | |
uses: ljharb/actions/node/install@main | |
with: | |
node-version: lts/* | |
- name: Print event info | |
uses: actions/github-script@v7 | |
with: | |
script: 'console.log(${{ toJson(github.event) }});' | |
- name: Download zipball | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const { owner, repo } = context.repo; | |
const run_id = ${{ github.event.workflow_run.id }}; | |
const listArtifactsQuery = { owner, repo, run_id, name: 'result' }; | |
const listArtifactsResponse = | |
await github.rest.actions.listWorkflowRunArtifacts(listArtifactsQuery); | |
const { total_count, artifacts } = listArtifactsResponse.data; | |
if (total_count !== 1) { | |
const summary = artifacts?.map(artifact => { | |
const { name, size_in_bytes, url } = artifact; | |
return { name, size_in_bytes, url }; | |
}); | |
const repr = value => JSON.stringify(value); | |
throw Error(`Expected 1 artifact, got ${total_count} ${repr(summary ?? [])}`); | |
} | |
const artifact_id = artifacts[0].id; | |
console.log(`downloading artifact ${artifact_id}`); | |
const downloadResponse = await github.rest.actions.downloadArtifact({ | |
owner, | |
repo, | |
artifact_id, | |
archive_format: 'zip', | |
}); | |
const fs = require('fs'); | |
fs.writeFileSync('${{ github.workspace }}/result.zip', Buffer.from(downloadResponse.data)); | |
- name: Provide result directory | |
run: rm -rf result && mkdir -p result | |
- run: unzip -o result.zip -d result | |
- run: ls result | |
- name: Extract PR data | |
id: extract-pr-data | |
run: | | |
cd result | |
awk -v ok=1 ' | |
NR == 1 && match($0, /^[1-9][0-9]* [0-9a-fA-F]{7,}$/) { | |
print "number=" $1; | |
print "commit=" $2; | |
next; | |
} | |
{ ok = 0; } | |
END { exit !ok; } | |
' pr-data.txt >> $GITHUB_OUTPUT | |
rm pr-data.txt | |
- name: Insert preview warning | |
env: | |
PR: ${{ steps.extract-pr-data.outputs.number }} | |
COMMIT: ${{ steps.extract-pr-data.outputs.commit }} | |
run: | | |
tmp="$(mktemp -u XXXXXXXX.json)" | |
repo_url="https://github.com/$GITHUB_REPOSITORY" | |
jq -n --arg repo_url "$repo_url" --arg PR "$PR" --arg commit "$COMMIT" ' | |
def repo_link($args): $args as [$path, $contents] | |
| ($repo_url + ($path // "")) as $url | |
| "<a href=\"\($url | @html)\">\($contents // $url)</a>"; | |
{ | |
SUMMARY: "PR #\($PR)", | |
REPO_LINK: repo_link([]), | |
PR_LINK: repo_link(["/pull/" + $PR, "PR #\($PR)"]), | |
COMMIT_LINK: ("commit " + repo_link(["/commit/" + $commit, "<code>\($commit)</code>"])), | |
} | |
' > "$tmp" | |
find result -name '*.html' -exec \ | |
node scripts/insert_warning.mjs scripts/pr_preview_warning.html "$tmp" '{}' '+' | |
- name: Publish to gh-pages | |
uses: JamesIves/[email protected] | |
with: | |
branch: gh-pages | |
folder: result | |
target-folder: pr/${{ steps.extract-pr-data.outputs.number }} | |
- name: Determine gh-pages url | |
id: get-pages-url | |
run: | | |
gh_pages_url="https://$(printf '%s' "$GITHUB_REPOSITORY" \ | |
| sed 's#/#.github.io/#; s#^tc39.github.io/#tc39.es/#')" | |
echo "url=$gh_pages_url" >> $GITHUB_OUTPUT | |
- name: Provide PR comment | |
uses: phulsechinmay/[email protected] | |
with: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ISSUE_ID: ${{ steps.extract-pr-data.outputs.number }} | |
message: > | |
The rendered spec for this PR is available at | |
${{ steps.get-pages-url.outputs.url }}/pr/${{ steps.extract-pr-data.outputs.number }}. |