name: Test
on: 
  workflow_run:
    workflows: ["Generate Preview"]
    types:
      - completed

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: 'Download artifact'
        uses: actions/github-script@v3.1.0
        with:
          script: |
            var artifacts = await github.actions.listWorkflowRunArtifacts({
               owner: context.repo.owner,
               repo: context.repo.repo,
               run_id: ${{ github.event.workflow_run.id }},
            });
            var matchPrArtifact = artifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "pr"
            })[0];
            var matchPreviewArtifact = artifacts.data.artifacts.filter((artifact) => {
              return artifact.name == "preview"
            })[0];
            var downloadPr = await github.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchPrArtifact.id,
               archive_format: 'zip',
            });
            var downloadPreview = await github.actions.downloadArtifact({
               owner: context.repo.owner,
               repo: context.repo.repo,
               artifact_id: matchPreviewArtifact.id,
               archive_format: 'zip',
            });
            var fs = require('fs');
            fs.writeFileSync('${{github.workspace}}/pr.zip', Buffer.from(downloadPr.data));
      - run: |
          unzip pr.zip
          echo "pr_number=$(cat NR)" >> $GITHUB_ENV
