|
| 1 | +name: "Pull Request Workflow Run" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: # this action runs in a trusted context, but passed info from the "Pull Request" untrusted workflow |
| 5 | + workflows: ["Pull Request"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +permissions: |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +env: |
| 13 | + PR_DIR: "_pr" |
| 14 | + PR_ZIP: "_pr.zip" |
| 15 | + PREVIEW_DIR: "_preview-templates" |
| 16 | + |
| 17 | +jobs: |
| 18 | + preview: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 21 | + env: |
| 22 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + TEMPLATES_API_CLIENT_ID: ${{ secrets.TEMPLATES_API_CLIENT_ID }} |
| 24 | + TEMPLATES_API_CLIENT_SECRET: ${{ secrets.TEMPLATES_API_CLIENT_SECRET }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + - uses: ./.github/actions/setup |
| 28 | + |
| 29 | + - name: "Download pull request info" |
| 30 | + uses: actions/github-script@v7 |
| 31 | + with: |
| 32 | + script: | |
| 33 | + var artifacts = await github.rest.actions.listWorkflowRunArtifacts({ |
| 34 | + owner: context.repo.owner, |
| 35 | + repo: context.repo.repo, |
| 36 | + run_id: ${{github.event.workflow_run.id }}, |
| 37 | + }); |
| 38 | + var matchArtifact = artifacts.data.artifacts.filter((artifact) => { |
| 39 | + return artifact.name == "pr" |
| 40 | + })[0]; |
| 41 | + var download = await github.rest.actions.downloadArtifact({ |
| 42 | + owner: context.repo.owner, |
| 43 | + repo: context.repo.repo, |
| 44 | + artifact_id: matchArtifact.id, |
| 45 | + archive_format: 'zip', |
| 46 | + }); |
| 47 | + var fs = require('fs'); |
| 48 | + var path = require('path'); |
| 49 | + fs.writeFileSync(path.join('${{github.workspace}}', process.env.PR_ZIP), Buffer.from(download.data)); |
| 50 | +
|
| 51 | + - name: "Load PR info into environment variables" |
| 52 | + run: mkdir $PR_DIR && unzip $PR_ZIP -d $PR_DIR && cat $PR_DIR/env >> $GITHUB_ENV |
| 53 | + # |
| 54 | + # Note: Checking out the source of the PR means checking out untrusted code. |
| 55 | + # We shouldn't run any tests or package installs past this point. |
| 56 | + # Simply checking out to fetch the files is reasonable, however. |
| 57 | + # https://securitylab.github.com/resources/github-actions-preventing-pwn-requests/ |
| 58 | + # |
| 59 | + - uses: actions/checkout@v4 |
| 60 | + with: |
| 61 | + repository: ${{ env.PR_REPOSITORY }} |
| 62 | + ref: ${{ env.PR_REF }} |
| 63 | + path: ${{ env.PREVIEW_DIR }} |
| 64 | + persist-credentials: "false" |
| 65 | + |
| 66 | + - name: "Make preview" |
| 67 | + run: | |
| 68 | + pnpm -w preview $PREVIEW_DIR \ |
| 69 | + --repoFullName $PR_REPOSITORY \ |
| 70 | + --branch $PR_REF \ |
| 71 | + --pr $PR_ID |
0 commit comments