Skip to content

Commit 745bb5a

Browse files
sharkdpAlexWaygood
authored andcommitted
[red-knot] mypy_primer: comment on PRs (#16599)
## Summary Add a new pipeline to comment on PRs if there is a mypy_primer diff result. ## Test Plan Not yet, I'm afraid I will have to merge this first to have the pipeline available on main.
1 parent e4e1c58 commit 745bb5a

File tree

2 files changed

+113
-8
lines changed

2 files changed

+113
-8
lines changed

.github/workflows/mypy_primer.yaml

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,27 @@ jobs:
5959
6060
echo "Running mypy_primer"
6161
62-
uvx --from "git+https://github.com/astral-sh/mypy_primer.git@add-red-knot-support" mypy_primer \
63-
--repo ruff \
64-
--type-checker knot \
65-
--old base_commit \
66-
--new "$GITHUB_SHA" \
67-
--project-selector '/(mypy_primer|black|pyp|git-revise|zipp|arrow)$' \
68-
--output concise \
69-
--debug
62+
(
63+
uvx --from "git+https://github.com/astral-sh/mypy_primer.git@add-red-knot-support" mypy_primer \
64+
--repo ruff \
65+
--type-checker knot \
66+
--old base_commit \
67+
--new "$GITHUB_SHA" \
68+
--project-selector '/(mypy_primer|black|pyp|git-revise|zipp|arrow)$' \
69+
--output concise \
70+
--debug | tee mypy_primer.diff
71+
) || [ $? -eq 1 ]
72+
73+
echo ${{ github.event.number }} > pr-number
74+
75+
- name: Upload diff
76+
uses: actions/upload-artifact@v4
77+
with:
78+
name: mypy_primer_diff
79+
path: mypy_primer.diff
80+
81+
- name: Upload pr-number
82+
uses: actions/upload-artifact@v4
83+
with:
84+
name: pr-number
85+
path: pr-number
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
name: PR comment (mypy_primer)
2+
3+
on: # zizmor: ignore[dangerous-triggers]
4+
workflow_run:
5+
workflows: [Run mypy_primer]
6+
types: [completed]
7+
workflow_dispatch:
8+
inputs:
9+
workflow_run_id:
10+
description: The mypy_primer workflow that triggers the workflow run
11+
required: true
12+
13+
jobs:
14+
comment:
15+
runs-on: ubuntu-24.04
16+
permissions:
17+
pull-requests: write
18+
steps:
19+
- uses: dawidd6/action-download-artifact@v8
20+
name: Download PR number
21+
with:
22+
name: pr-number
23+
run_id: ${{ github.event.workflow_run.id || github.event.inputs.workflow_run_id }}
24+
if_no_artifact_found: ignore
25+
allow_forks: true
26+
27+
- name: Parse pull request number
28+
id: pr-number
29+
run: |
30+
if [[ -f pr-number ]]
31+
then
32+
echo "pr-number=$(<pr-number)" >> "$GITHUB_OUTPUT"
33+
fi
34+
35+
- uses: dawidd6/action-download-artifact@v8
36+
name: "Download mypy_primer results"
37+
id: download-mypy_primer_diff
38+
if: steps.pr-number.outputs.pr-number
39+
with:
40+
name: mypy_primer_diff
41+
workflow: mypy_primer.yaml
42+
pr: ${{ steps.pr-number.outputs.pr-number }}
43+
path: pr/mypy_primer_diff
44+
workflow_conclusion: completed
45+
if_no_artifact_found: ignore
46+
allow_forks: true
47+
48+
- name: Generate comment content
49+
id: generate-comment
50+
if: steps.download-mypy_primer_diff.outputs.found_artifact == 'true'
51+
run: |
52+
# Guard against malicious mypy_primer results that symlink to a secret
53+
# file on this runner
54+
if [[ -L pr/mypy_primer_diff/mypy_primer.diff ]]
55+
then
56+
echo "Error: mypy_primer.diff cannot be a symlink"
57+
exit 1
58+
fi
59+
60+
# Note this identifier is used to find the comment to update on
61+
# subsequent runs
62+
echo '<!-- generated-comment mypy_primer -->' >> comment.txt
63+
64+
echo '## `mypy_primer` results' >> comment.txt
65+
echo '```diff' >> comment.txt
66+
cat pr/mypy_primer_diff/mypy_primer.diff >> comment.txt
67+
echo '```' >> comment.txt
68+
69+
echo 'comment<<EOF' >> "$GITHUB_OUTPUT"
70+
cat comment.txt >> "$GITHUB_OUTPUT"
71+
echo 'EOF' >> "$GITHUB_OUTPUT"
72+
73+
- name: Find existing comment
74+
uses: peter-evans/find-comment@v3
75+
if: steps.generate-comment.outcome == 'success'
76+
id: find-comment
77+
with:
78+
issue-number: ${{ steps.pr-number.outputs.pr-number }}
79+
comment-author: "github-actions[bot]"
80+
body-includes: "<!-- generated-comment mypy_primer -->"
81+
82+
- name: Create or update comment
83+
if: steps.find-comment.outcome == 'success'
84+
uses: peter-evans/create-or-update-comment@v4
85+
with:
86+
comment-id: ${{ steps.find-comment.outputs.comment-id }}
87+
issue-number: ${{ steps.pr-number.outputs.pr-number }}
88+
body-path: comment.txt
89+
edit-mode: replace

0 commit comments

Comments
 (0)