-
Notifications
You must be signed in to change notification settings - Fork 1.2k
259 lines (227 loc) · 11.1 KB
/
PR_summary.yml
File metadata and controls
259 lines (227 loc) · 11.1 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
name: Post PR summary comment
on:
pull_request_target:
# Limit permissions for GITHUB_TOKEN for the entire workflow
permissions:
contents: read
pull-requests: write # Only allow PR comments/labels
# All other permissions are implicitly 'none'
jobs:
build:
name: "post-or-update-summary-comment"
runs-on: ubuntu-latest
if: github.repository == 'leanprover-community/mathlib4'
steps:
- name: Checkout code
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
path: pr-branch
# Checkout the master branch into a subdirectory
- name: Checkout master branch
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
with:
# When testing the scripts, comment out the "ref: master"
ref: master
path: master-branch
- name: Update the merge-conflict label
run: |
cd pr-branch
printf 'PR number: "%s"\n' "${{ github.event.pull_request.number }}"
git config user.name "leanprover-community-mathlib4-bot"
git config user.email "leanprover-community-mathlib4-bot@users.noreply.github.com"
if git merge origin/master --no-ff --no-commit
then
git merge --abort || true
echo "This PR does not have merge conflicts with master."
# we use curl rather than octokit/request-action so that the job won't fail
# (and send an annoying email) if the labels don't exist
curl --request DELETE \
--url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/merge-conflict \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
else
echo "This PR has merge conflicts with master."
# we use curl rather than octokit/request-action so that the job won't fail
# (and send an annoying email) if the labels don't exist
curl --request POST \
--header 'Accept: application/vnd.github+json' \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
--url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
--data '{"labels":["merge-conflict"]}'
fi
- name: Set up Python
uses: actions/setup-python@83679a892e2d95755f2dac6acb0bfd1e9ac5d548 # v6.1.0
with:
python-version: 3.12
- name: Install dependencies
run: |
python -m pip install --upgrade pip
sudo apt-get install -y jq
# If you have additional dependencies, install them here
- name: Get changed and removed/renamed files
run: |
cd pr-branch
git fetch origin ${{ github.base_ref }} # fetch the base branch
# Get the list of all changed files.
echo "Saving the changed files to 'changed_files.txt'..."
git diff --name-only origin/${{ github.base_ref }}... | tee changed_files.txt
# Get all files which were removed or renamed.
echo "Checking for removed files..."
# Shows the `D`eleted files, one per line.
git diff --name-only --diff-filter D origin/${{ github.base_ref }}... | tee removed_files.txt
echo "Checking for renamed files..."
# Shows the `R`enamed files, in human-readable format
# The `awk` pipe
# * extracts into an array the old name as the key and the new name as the value
# * eventually prints "`oldName` was renamed to `newName`" for each key-value pair.
git diff -p --summary --diff-filter=R origin/${{ github.base_ref }}... |
awk '
/^rename from / {
file=$0
gsub(/rename from /, "", file)
oldFile=file
}
/^rename to / {
file=$0
gsub(/rename to /, "", file)
oldNew[oldFile]=file
} END {
for(old in oldNew) {
printf("`%s` was renamed to `%s`\n", old, oldNew[old])
}
}' | tee renamed_files.txt
- name: Compute (re)moved files without deprecation
run: |
cd pr-branch
touch moved_without_deprecation.txt
touch extraneous_deprecated_module.txt
git checkout ${{ github.base_ref }}
while IFS= read -r file
do
if grep ^deprecated_module "${file}" ; then
# shellcheck disable=SC2016
printf '\n⚠️ **warning**: removed file `%s` contains a module deprecation: please add this in a follow-up PR instead\n' "${file}" |
tee -a extraneous_deprecated_module.txt
else
# shellcheck disable=SC2016
printf '\nnote: file `%s` was removed.\nPlease create a follow-up pull request adding a module deprecation. Thanks!\n' "${file}" |
tee -a moved_without_deprecation.txt
fi
done < removed_files.txt
IFS=$'\n'
while IFS= read -r file
do
if grep ^deprecated_module "${file}" ; then
# shellcheck disable=SC2016
printf '\n⚠️ **warning**: renamed file `%s` contains a module deprecation: please add this in a follow-up PR instead\n' "${file}" |
tee -a extraneous_deprecated_module.txt
else
# shellcheck disable=SC2016
printf '\nnote: file `%s` without a module deprecation\nPlease create a follow-up pull request adding one. Thanks!\n' "${file}" |
tee -a moved_without_deprecation.txt
fi
done < renamed_files.txt
# we return to the PR branch, since the next step wants it!
git checkout -
- name: Compute transitive imports
run: |
cd pr-branch
# the checkout dance, to avoid a detached head
git checkout master
git checkout -
currentHash="$(git rev-parse HEAD)"
printf 'currentHash=%s\n' "${currentHash}"
echo "Compute the counts for the HEAD of the PR"
python ../master-branch/scripts/count-trans-deps.py "Mathlib/" > head.json
# Checkout the merge base
git checkout "$(git merge-base master ${{ github.event.pull_request.head.sha }})"
echo "Compute the counts for the merge base"
python ../master-branch/scripts/count-trans-deps.py "Mathlib/" > base.json
# switch back to the current branch: the `declarations_diff` script should be here
git checkout "${currentHash}" --
- name: Post or update the summary comment
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_NAME: ${{ github.head_ref }}
run: |
cd pr-branch
currentHash="$(git rev-parse HEAD)"
PR="${{ github.event.pull_request.number }}"
title="### PR summary"
graphAndHighPercentReports=$(python ../master-branch/scripts/import-graph-report.py base.json head.json changed_files.txt)
echo "Produce import count comment"
importCount=$(
printf '%s\n' "${graphAndHighPercentReports}" | sed '/^Import changes exceeding/Q'
../master-branch/scripts/import_trans_difference.sh
)
echo "Produce high percentage imports"
high_percentages=$(
printf '%s\n' "${graphAndHighPercentReports}" | sed -n '/^Import changes exceeding/,$p'
)
# if there are files with large increase in transitive imports, then we add the `large-import` label
if [ -n "${high_percentages}" ]
then
high_percentages=$'\n\n'"${high_percentages}"
gh pr edit "${PR}" --add-label large-import
else # otherwise, we remove the label
gh pr edit "${PR}" --remove-label large-import
fi
if [ "$(printf '%s' "${importCount}" | wc -l)" -gt 12 ]
then
importCount="$(printf '<details><summary>\n\n%s\n\n</summary>\n\n%s\n\n</details>\n' "#### Import changes for modified files" "${importCount}")"
else
importCount="$(printf '#### Import changes for modified files\n\n%s\n' "${importCount}")"
fi
echo "Compute Declarations' diff comment"
declDiff=$(../master-branch/scripts/declarations_diff.sh)
if [ "$(printf '%s' "${declDiff}" | wc -l)" -gt 15 ]
then
declDiff="$(printf '<details><summary>\n\n%s\n\n</summary>\n\n%s\n\n</details>\n' "#### Declarations diff" "${declDiff}")"
else
declDiff="$(printf '#### Declarations diff\n\n%s\n' "${declDiff}")"
fi
git checkout "${currentHash}" --
hashURL="https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}/commits/${currentHash}"
printf 'hashURL: %s' "${hashURL}"
echo "Compute technical debt changes"
techDebtVar="$(../master-branch/scripts/technical-debt-metrics.sh pr_summary)"
# store in a file, to avoid "long arguments" error.
printf '%s [%s](%s)%s\n\n%s\n\n---\n\n%s\n\n---\n\n%s\n' "${title}" "$(git rev-parse --short HEAD)" "${hashURL}" "${high_percentages}" "${importCount}" "${declDiff}" "${techDebtVar}" > please_merge_master.md
echo "Include any errors about removed or renamed files without deprecation,"
echo "as well as errors about extraneous deprecated_module additions."
if [ -s moved_without_deprecation.txt ]
then
printf '\n\n---\n\n' >> please_merge_master.md
cat moved_without_deprecation.txt >> please_merge_master.md
fi
if [ -s extraneous_deprecated_module.txt ]
then
printf '\n\n---\n\n' >> please_merge_master.md
cat extraneous_deprecated_module.txt >> please_merge_master.md
fi
cat please_merge_master.md
../master-branch/scripts/update_PR_comment.sh please_merge_master.md "${title}" "${PR}"
- name: Update the file-removed label
run: |
cd pr-branch
undeprecatedMoves="$(cat moved_without_deprecation.txt)"
if [ -n "$undeprecatedMoves" ]; then
echo "This PR has undeprecated module (re)movals."
# we use curl rather than octokit/request-action so that the job won't fail
# (and send an annoying email) if the labels don't exist
curl --request POST \
--header 'Accept: application/vnd.github+json' \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
--url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
--data '{"labels":["file-removed"]}'
else
echo "This PR (re)moves no modules without deprecations."
# we use curl rather than octokit/request-action so that the job won't fail
# (and send an annoying email) if the labels don't exist
curl --request DELETE \
--url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/file-removed \
--header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
fi