| name: Comment with mypy_primer diff |
| |
| on: |
| workflow_run: |
| workflows: |
| - Run mypy_primer |
| types: |
| - completed |
| |
| permissions: |
| contents: read |
| pull-requests: write |
| |
| jobs: |
| comment: |
| name: Comment PR from mypy_primer |
| runs-on: ubuntu-latest |
| steps: |
| - name: Download diffs |
| uses: actions/github-script@v3 |
| with: |
| script: | |
| const fs = require('fs'); |
| const artifacts = await github.actions.listWorkflowRunArtifacts({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| run_id: ${{ github.event.workflow_run.id }}, |
| }); |
| const [matchArtifact] = artifacts.data.artifacts.filter((artifact) => |
| artifact.name == "mypy_primer_diffs"); |
| |
| const download = await github.actions.downloadArtifact({ |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| artifact_id: matchArtifact.id, |
| archive_format: "zip", |
| }); |
| fs.writeFileSync("diff.zip", Buffer.from(download.data)); |
| |
| - run: unzip diff.zip |
| |
| # Based on https://github.com/kanga333/comment-hider |
| - name: Hide old comments |
| uses: actions/github-script@v3 |
| with: |
| github-token: ${{secrets.GITHUB_TOKEN}} |
| script: | |
| const fs = require('fs') |
| |
| const response = await github.issues.listComments({ |
| issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| }) |
| const botCommentIds = response.data |
| .filter(comment => comment.user.login === 'github-actions[bot]') |
| .map(comment => comment.node_id) |
| |
| for (const id of botCommentIds) { |
| const resp = await github.graphql(` |
| mutation { |
| minimizeComment(input: {classifier: OUTDATED, subjectId: "${id}"}) { |
| minimizedComment { |
| isMinimized |
| } |
| } |
| } |
| `) |
| if (resp.errors) { |
| throw new Error(resp.errors) |
| } |
| } |
| |
| - name: Post comment |
| uses: actions/github-script@v3 |
| with: |
| github-token: ${{secrets.GITHUB_TOKEN}} |
| script: | |
| const fs = require('fs') |
| // Keep in sync with shards produced by mypy_primer workflow |
| const data = ( |
| ['diff_0.txt', 'diff_1.txt', 'diff_2.txt'] |
| .map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' })) |
| .join('') |
| .substr(0, 30000) // About 300 lines |
| ) |
| |
| console.log("Diff from mypy_primer:") |
| console.log(data) |
| |
| if (data.trim()) { |
| const body = 'Diff from [mypy_primer](https://github.com/hauntsaninja/mypy_primer), showing the effect of this PR on open source code:\n```diff\n' + data + '```' |
| await github.issues.createComment({ |
| issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), |
| owner: context.repo.owner, |
| repo: context.repo.repo, |
| body |
| }) |
| } |