-
-
Notifications
You must be signed in to change notification settings - Fork 33
102 lines (90 loc) · 3.67 KB
/
mypy_primer_comment.yml
File metadata and controls
102 lines (90 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
name: mypy_primer comment
permissions:
contents: read
pull-requests: write
on: # zizmor: ignore[dangerous-triggers] -- workflow_run is needed to post PR comments from fork PRs
workflow_run:
workflows: [mypy_primer run]
types: [completed]
jobs:
comment:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
timeout-minutes: 2
steps:
- uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20
name: download pr-number
with:
name: pr-number
path: ${{ runner.temp }}/artifacts
run_id: ${{ github.event.workflow_run.id }}
- name: parse pr-number
id: pr-number
run: |
if [[ -f ${{ runner.temp }}/artifacts/pr-number ]]
then
echo "pr-number=$(<${{ runner.temp }}/artifacts/pr-number)" >> "$GITHUB_OUTPUT"
fi
- uses: dawidd6/action-download-artifact@8305c0f1062bb0d184d09ef4493ecb9288447732 # v20
name: download mypy_primer.diff
id: download-mypy_primer-diff
if: steps.pr-number.outputs.pr-number
with:
name: mypy_primer-diff
path: ${{ runner.temp }}/artifacts
run_id: ${{ github.event.workflow_run.id }}
- name: generate comment content
id: generate-comment
if: ${{ steps.download-mypy_primer-diff.outputs.found_artifact == 'true' }}
run: |
# Guard against malicious mypy_primer results that symlink to a secret
# file on this runner
if [[ -L ${{ runner.temp }}/artifacts/mypy_primer.diff ]]
then
echo "Error: mypy_primer.diff cannot be a symlink"
exit 1
fi
# Note this identifier is used to find the comment to update on
# subsequent runs
echo '<!-- generated-comment mypy_primer -->' >> comment.md
echo '## `mypy_primer` results' >> comment.md
if [ -s "${{ runner.temp }}/artifacts/mypy_primer.diff" ]; then
echo '<details>' >> comment.md
echo '<summary>⚠️ Changes were detected when running mypy on open source projects</summary>' >> comment.md
echo '' >> comment.md
echo '```diff' >> comment.md
cat ${{ runner.temp }}/artifacts/mypy_primer.diff >> comment.md
echo '```' >> comment.md
echo '</details>' >> comment.md
else
echo '✅ No ecosystem changes detected' >> comment.md
fi
echo 'comment<<EOF' >> "$GITHUB_OUTPUT"
cat comment.md >> "$GITHUB_OUTPUT"
echo 'EOF' >> "$GITHUB_OUTPUT"
- name: create or update comment
if: steps.generate-comment.outcome == 'success'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ steps.pr-number.outputs.pr-number }}
run: |
# Find existing comment by the bot containing the marker
COMMENT_ID=$(
gh api \
"repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
--paginate \
--jq '.[] | select(.user.login == "github-actions[bot]") | select(.body | contains("<!-- generated-comment mypy_primer -->")) | .id' \
| head -n1
)
if [[ -n "$COMMENT_ID" ]]; then
# Update existing comment
gh api \
--method PATCH \
"repos/${{ github.repository }}/issues/comments/${COMMENT_ID}" \
--field "body=@comment.md"
else
# Create new comment
gh pr comment "${PR_NUMBER}" \
--repo "${{ github.repository }}" \
--body-file comment.md
fi