|
| 1 | +# Most of this is inspired by the mypy primer |
| 2 | +# See: https://github.com/hauntsaninja/mypy_primer |
| 3 | +# This is the primer job that creates the comment on the PR |
| 4 | +# It needs to trigger on workflow_run instead of pull_request |
| 5 | +# as we need repository wide access to create a comment |
| 6 | + |
| 7 | +name: Primer / Comment |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_run: |
| 11 | + workflows: [Primer / Run] |
| 12 | + types: |
| 13 | + - completed |
| 14 | + |
| 15 | +env: |
| 16 | + CACHE_VERSION: 1 |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + pull-requests: write |
| 21 | + |
| 22 | +jobs: |
| 23 | + primer-comment: |
| 24 | + name: Run |
| 25 | + runs-on: ubuntu-latest |
| 26 | + steps: |
| 27 | + - uses: actions/setup-node@v1 |
| 28 | + with: |
| 29 | + version: 12 |
| 30 | + - run: npm install @octokit/rest |
| 31 | + - name: Check out code from GitHub |
| 32 | + |
| 33 | + - name: Set up Python 3.8 |
| 34 | + id: python |
| 35 | + |
| 36 | + with: |
| 37 | + python-version: 3.8 |
| 38 | + |
| 39 | + # Restore cached Python environment |
| 40 | + - name: Generate partial Python venv restore key |
| 41 | + id: generate-python-key |
| 42 | + run: >- |
| 43 | + echo "::set-output name=key::venv-${{ env.CACHE_VERSION }}-${{ |
| 44 | + hashFiles('setup.cfg', 'requirements_test.txt', 'requirements_test_min.txt') |
| 45 | + }}" |
| 46 | + - name: Restore Python virtual environment |
| 47 | + id: cache-venv |
| 48 | + |
| 49 | + with: |
| 50 | + path: venv |
| 51 | + key: >- |
| 52 | + ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ |
| 53 | + steps.generate-python-key.outputs.key }} |
| 54 | + restore-keys: | |
| 55 | + ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ env.CACHE_VERSION }}- |
| 56 | + - name: Create Python virtual environment |
| 57 | + if: steps.cache-venv.outputs.cache-hit != 'true' |
| 58 | + run: | |
| 59 | + python -m venv venv |
| 60 | + . venv/bin/activate |
| 61 | + python -m pip install -U pip setuptools wheel |
| 62 | + pip install -U -r requirements_test.txt |
| 63 | +
|
| 64 | + - name: Download outputs |
| 65 | + uses: actions/github-script@v6 |
| 66 | + with: |
| 67 | + script: | |
| 68 | + // Download workflow pylint output |
| 69 | + const fs = require('fs'); |
| 70 | + const artifacts_workflow = await github.rest.actions.listWorkflowRunArtifacts({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + run_id: ${{ github.event.workflow_run.id }}, |
| 74 | + }); |
| 75 | +
|
| 76 | + // Get 'main' output |
| 77 | + const [matchArtifactWorkflowMain] = artifacts_workflow.data.artifacts.filter((artifact) => |
| 78 | + artifact.name == "primer_output_main"); |
| 79 | + const downloadOne = await github.rest.actions.downloadArtifact({ |
| 80 | + owner: context.repo.owner, |
| 81 | + repo: context.repo.repo, |
| 82 | + artifact_id: matchArtifactWorkflowMain.id, |
| 83 | + archive_format: "zip", |
| 84 | + }); |
| 85 | + fs.writeFileSync("primer_main_output.zip", Buffer.from(downloadOne.data)); |
| 86 | +
|
| 87 | + // Get PR output |
| 88 | + const [matchArtifactWorkflowPR] = artifacts_workflow.data.artifacts.filter((artifact) => |
| 89 | + artifact.name == "primer_output_pr"); |
| 90 | + const downloadTwo = await github.rest.actions.downloadArtifact({ |
| 91 | + owner: context.repo.owner, |
| 92 | + repo: context.repo.repo, |
| 93 | + artifact_id: matchArtifactWorkflowPR.id, |
| 94 | + archive_format: "zip", |
| 95 | + }); |
| 96 | + fs.writeFileSync("primer_pr_output.zip", Buffer.from(downloadTwo.data)); |
| 97 | +
|
| 98 | + // Get PR number |
| 99 | + const [matchArtifactWorkflowNumber] = artifacts_workflow.data.artifacts.filter((artifact) => |
| 100 | + artifact.name == "pr_number"); |
| 101 | + const downloadThree = await github.rest.actions.downloadArtifact({ |
| 102 | + owner: context.repo.owner, |
| 103 | + repo: context.repo.repo, |
| 104 | + artifact_id: matchArtifactWorkflowNumber.id, |
| 105 | + archive_format: "zip", |
| 106 | + }); |
| 107 | + fs.writeFileSync("primer_pr_number.zip", Buffer.from(downloadThree.data)); |
| 108 | + - run: unzip primer_main_output.zip |
| 109 | + - run: unzip primer_pr_output.zip |
| 110 | + - run: unzip primer_pr_number.zip |
| 111 | + - name: Compare outputs |
| 112 | + run: | |
| 113 | + . venv/bin/activate |
| 114 | + python tests/primer/primer_tool.py compare --base-file=output_main.txt --new-file=output_pr.txt |
| 115 | + - name: Post comment |
| 116 | + id: post-comment |
| 117 | + uses: actions/github-script@v6 |
| 118 | + with: |
| 119 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 120 | + script: | |
| 121 | + const fs = require('fs') |
| 122 | + const comment = fs.readFileSync('tests/.pylint_primer_tests/comment.txt', { encoding: 'utf8' }) |
| 123 | + console.log("Comment to post:") |
| 124 | + console.log(comment) |
| 125 | + const prNumber = parseInt(fs.readFileSync("pr_number.txt", { encoding: "utf8" })) |
| 126 | + await github.rest.issues.createComment({ |
| 127 | + issue_number: prNumber, |
| 128 | + owner: context.repo.owner, |
| 129 | + repo: context.repo.repo, |
| 130 | + body: comment |
| 131 | + }) |
| 132 | + return prNumber |
| 133 | + - name: Hide old comments |
| 134 | + # Taken from mypy primer |
| 135 | + # v0.3.0 |
| 136 | + uses: kanga333/comment-hider@bbdf5b562fbec24e6f60572d8f712017428b92e0 |
| 137 | + with: |
| 138 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 139 | + leave_visible: 1 |
| 140 | + issue_number: ${{ steps.post-comment.outputs.result }} |
0 commit comments