|
| 1 | +name: Comment with mypy_primer diff |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: |
| 6 | + - Run mypy_primer |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + comment: |
| 16 | + name: Comment PR from mypy_primer |
| 17 | + runs-on: ubuntu-latest |
| 18 | + steps: |
| 19 | + - name: Download diffs |
| 20 | + uses: actions/github-script@v3 |
| 21 | + with: |
| 22 | + script: | |
| 23 | + const fs = require('fs'); |
| 24 | + const artifacts = await github.actions.listWorkflowRunArtifacts({ |
| 25 | + owner: context.repo.owner, |
| 26 | + repo: context.repo.repo, |
| 27 | + run_id: ${{ github.event.workflow_run.id }}, |
| 28 | + }); |
| 29 | + const [matchArtifact] = artifacts.data.artifacts.filter((artifact) => |
| 30 | + artifact.name == "mypy_primer_diffs"); |
| 31 | +
|
| 32 | + const download = await github.actions.downloadArtifact({ |
| 33 | + owner: context.repo.owner, |
| 34 | + repo: context.repo.repo, |
| 35 | + artifact_id: matchArtifact.id, |
| 36 | + archive_format: "zip", |
| 37 | + }); |
| 38 | + fs.writeFileSync("diff.zip", Buffer.from(download.data)); |
| 39 | +
|
| 40 | + - run: unzip diff.zip |
| 41 | + |
| 42 | + # Based on https://github.com/kanga333/comment-hider |
| 43 | + - name: Hide old comments |
| 44 | + uses: actions/github-script@v3 |
| 45 | + with: |
| 46 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 47 | + script: | |
| 48 | + const fs = require('fs') |
| 49 | +
|
| 50 | + const response = await github.issues.listComments({ |
| 51 | + issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), |
| 52 | + owner: context.repo.owner, |
| 53 | + repo: context.repo.repo, |
| 54 | + }) |
| 55 | + const botCommentIds = response.data |
| 56 | + .filter(comment => comment.user.login === 'github-actions[bot]') |
| 57 | + .map(comment => comment.node_id) |
| 58 | +
|
| 59 | + for (const id of botCommentIds) { |
| 60 | + const resp = await github.graphql(` |
| 61 | + mutation { |
| 62 | + minimizeComment(input: {classifier: OUTDATED, subjectId: "${id}"}) { |
| 63 | + minimizedComment { |
| 64 | + isMinimized |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + `) |
| 69 | + if (resp.errors) { |
| 70 | + throw new Error(resp.errors) |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + - name: Post comment |
| 75 | + uses: actions/github-script@v3 |
| 76 | + with: |
| 77 | + github-token: ${{secrets.GITHUB_TOKEN}} |
| 78 | + script: | |
| 79 | + const fs = require('fs') |
| 80 | + // Keep in sync with shards produced by mypy_primer workflow |
| 81 | + const data = ( |
| 82 | + ['diff_0.txt', 'diff_1.txt', 'diff_2.txt'] |
| 83 | + .map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' })) |
| 84 | + .join('') |
| 85 | + .substr(0, 30000) // About 300 lines |
| 86 | + ) |
| 87 | +
|
| 88 | + console.log("Diff from mypy_primer:") |
| 89 | + console.log(data) |
| 90 | +
|
| 91 | + if (data.trim()) { |
| 92 | + 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 + '```' |
| 93 | + await github.issues.createComment({ |
| 94 | + issue_number: fs.readFileSync("pr_number.txt", { encoding: "utf8" }), |
| 95 | + owner: context.repo.owner, |
| 96 | + repo: context.repo.repo, |
| 97 | + body |
| 98 | + }) |
| 99 | + } |
0 commit comments