summaryrefslogtreecommitdiff
path: root/.github/workflows/primer_comment.yaml
blob: 52374a680fae5ca775cbf0bc12ba1e9a5a73c7ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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: 2
  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.3.0
      - name: Set up Python ${{ env.DEFAULT_PYTHON }}
        id: python
        uses: actions/setup-python@v4.5.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.2.5
        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.0
        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.0
        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 }}