# 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: 3 KEY_PREFIX: venv-primer DEFAULT_PYTHON: "3.11" 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: - name: Check out code from GitHub uses: actions/checkout@v3.5.2 - name: Set up Python ${{ env.DEFAULT_PYTHON }} id: python uses: actions/setup-python@v4.6.0 with: python-version: ${{ env.DEFAULT_PYTHON }} check-latest: true # Restore cached Python environment - name: Restore Python virtual environment id: cache-venv uses: actions/cache@v3.3.1 with: path: venv fail-on-cache-miss: true key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-${{ env.KEY_PREFIX }}-${{ env.CACHE_VERSION }}-${{ hashFiles('pyproject.toml', 'requirements_test.txt', 'requirements_test_min.txt', 'requirements_test_pre_commit.txt') }} - name: Download outputs uses: actions/github-script@v6.4.1 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.4.1 with: 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 uses: kanga333/comment-hider@c12bb20b48aeb8fc098e35967de8d4f8018fffdf # v0.4.0 with: github_token: ${{ secrets.GITHUB_TOKEN }} leave_visible: 1 issue_number: ${{ steps.post-comment.outputs.result }}