| # Most of this is inspired by the mypy primer |
| # See: https://github.com/hauntsaninja/mypy_primer |
| # This is the primer job that creates the comment on the PR |
| # It needs to trigger on workflow_run instead of pull_request |
| # as we need repository wide access to create a comment |
| |
| name: Primer / Comment |
| |
| on: |
| workflow_run: |
| workflows: [Primer / Run] |
| types: |
| - completed |
| |
| env: |
| # This needs to be the SAME as in the Main and PR job |
| CACHE_VERSION: 31 |
| |
| permissions: |
| contents: read |
| pull-requests: write |
| |
| jobs: |
| primer-comment: |
| # Skip job if the workflow failed |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| name: Run |
| runs-on: ubuntu-latest |
| steps: |
| - uses: actions/setup-node@v3 |
| with: |
| node-version: 16 |
| - run: npm install @octokit/rest |
| - name: Check out code from GitHub |
| uses: actions/checkout@v3.0.2 |
| - name: Set up Python 3.10 |
| id: python |
| uses: actions/setup-python@v4.2.0 |
| with: |
| python-version: "3.10" |
| |
| # Restore cached Python environment |
| - name: Restore Python virtual environment |
| id: cache-venv |
| uses: actions/cache@v3.0.8 |
| with: |
| path: venv |
| key: |
| ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-primer-${{ |
| env.CACHE_VERSION }} |
| - name: Fail job if Python cache restore failed |
| if: steps.cache-venv.outputs.cache-hit != 'true' |
| run: | |
| echo "Failed to restore Python venv from cache" |
| exit 1 |
| |
| - name: Download outputs |
| uses: actions/github-script@v6 |
| with: |
| script: | |
| // Download workflow pylint output |
| const fs = require('fs'); |
| const artifacts_workflow = await github.rest.actions.listWorkflowRunArtifacts({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| run_id: ${{ github.event.workflow_run.id }}, |
| }); |
| |
| // Get 'main' output |
| const [matchArtifactWorkflowMain] = artifacts_workflow.data.artifacts.filter((artifact) => |
| artifact.name == "primer_output_main"); |
| const downloadOne = await github.rest.actions.downloadArtifact({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| artifact_id: matchArtifactWorkflowMain.id, |
| archive_format: "zip", |
| }); |
| fs.writeFileSync("primer_main_output.zip", Buffer.from(downloadOne.data)); |
| |
| // Get PR output |
| const [matchArtifactWorkflowPR] = artifacts_workflow.data.artifacts.filter((artifact) => |
| artifact.name == "primer_output_pr"); |
| const downloadTwo = await github.rest.actions.downloadArtifact({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| artifact_id: matchArtifactWorkflowPR.id, |
| archive_format: "zip", |
| }); |
| fs.writeFileSync("primer_pr_output.zip", Buffer.from(downloadTwo.data)); |
| |
| // Get PR number |
| const [matchArtifactWorkflowNumber] = artifacts_workflow.data.artifacts.filter((artifact) => |
| artifact.name == "pr_number"); |
| const downloadThree = await github.rest.actions.downloadArtifact({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| artifact_id: matchArtifactWorkflowNumber.id, |
| archive_format: "zip", |
| }); |
| fs.writeFileSync("primer_pr_number.zip", Buffer.from(downloadThree.data)); |
| - run: unzip primer_main_output.zip |
| - run: unzip primer_pr_output.zip |
| - run: unzip primer_pr_number.zip |
| - name: Compare outputs |
| run: | |
| . venv/bin/activate |
| python tests/primer/__main__.py compare \ |
| --commit=${{ github.event.workflow_run.head_sha }} \ |
| --base-file=output_${{ steps.python.outputs.python-version }}_main.txt \ |
| --new-file=output_${{ steps.python.outputs.python-version }}_pr.txt |
| - name: Post comment |
| id: post-comment |
| uses: actions/github-script@v6 |
| with: |
| github-token: ${{ secrets.GITHUB_TOKEN }} |
| script: | |
| const fs = require('fs') |
| const comment = fs.readFileSync('tests/.pylint_primer_tests/comment.txt', { encoding: 'utf8' }) |
| console.log("Comment to post:") |
| console.log(comment) |
| const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" })) |
| await github.rest.issues.createComment({ |
| issue_number: prNumber, |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| body: comment |
| }) |
| return prNumber |
| - name: Hide old comments |
| # Taken from mypy primer |
| # v0.3.0 |
| uses: kanga333/comment-hider@bbdf5b562fbec24e6f60572d8f712017428b92e0 |
| with: |
| github_token: ${{ secrets.GITHUB_TOKEN }} |
| leave_visible: 1 |
| issue_number: ${{ steps.post-comment.outputs.result }} |