Skip to content

Commit

Permalink
Port over latest GA mypy_primer changes (#10505)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
hauntsaninja and Akuli authored May 20, 2021
1 parent 1e220b1 commit c3a600b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 37 deletions.
45 changes: 8 additions & 37 deletions .github/workflows/mypy_primer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,42 +57,13 @@ jobs:
with:
name: mypy_primer_diffs
path: diff_${{ matrix.shard-index }}.txt

comment:
name: Comment
runs-on: ubuntu-latest
needs: mypy_primer
permissions:
contents: read
pull-requests: write
steps:
- name: Download diffs
uses: actions/download-artifact@v2
- if: ${{ matrix.shard-index }} == 0
name: Save PR number
run: |
echo ${{ github.event.pull_request.number }} | tee pr_number.txt
- if: ${{ matrix.shard-index }} == 0
name: Upload PR number
uses: actions/upload-artifact@v2
with:
name: mypy_primer_diffs

- name: Post comment
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const fs = require('fs')
const data = (
['diff_0.txt', 'diff_1.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: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body
})
}
path: pr_number.txt
99 changes: 99 additions & 0 deletions .github/workflows/mypy_primer_comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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
})
}

0 comments on commit c3a600b

Please sign in to comment.