Skip to content

Commit c3a600b

Browse files
hauntsaninjaAkuli
andauthored
Port over latest GA mypy_primer changes (#10505)
This is mostly the same as typeshed. The biggest difference is that we don't post the "no effect" message. As a side effect, we also have to run the comment hider first. Co-authored-by: hauntsaninja <> Co-authored-by: Akuli <[email protected]>
1 parent 1e220b1 commit c3a600b

File tree

2 files changed

+107
-37
lines changed

2 files changed

+107
-37
lines changed

.github/workflows/mypy_primer.yml

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -57,42 +57,13 @@ jobs:
5757
with:
5858
name: mypy_primer_diffs
5959
path: diff_${{ matrix.shard-index }}.txt
60-
61-
comment:
62-
name: Comment
63-
runs-on: ubuntu-latest
64-
needs: mypy_primer
65-
permissions:
66-
contents: read
67-
pull-requests: write
68-
steps:
69-
- name: Download diffs
70-
uses: actions/download-artifact@v2
60+
- if: ${{ matrix.shard-index }} == 0
61+
name: Save PR number
62+
run: |
63+
echo ${{ github.event.pull_request.number }} | tee pr_number.txt
64+
- if: ${{ matrix.shard-index }} == 0
65+
name: Upload PR number
66+
uses: actions/upload-artifact@v2
7167
with:
7268
name: mypy_primer_diffs
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-
const data = (
81-
['diff_0.txt', 'diff_1.txt']
82-
.map(fileName => fs.readFileSync(fileName, { encoding: 'utf8' }))
83-
.join('')
84-
.substr(0, 30000) // About 300 lines
85-
)
86-
87-
console.log("Diff from mypy_primer:")
88-
console.log(data)
89-
90-
if (data.trim()) {
91-
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 + '```'
92-
await github.issues.createComment({
93-
issue_number: context.issue.number,
94-
owner: context.repo.owner,
95-
repo: context.repo.repo,
96-
body
97-
})
98-
}
69+
path: pr_number.txt
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)