11name : Post PR summary comment
22
33on :
4- pull_request :
4+ pull_request_target :
5+
6+ # Limit permissions for GITHUB_TOKEN for the entire workflow
7+ permissions :
8+ contents : read
9+ pull-requests : write # Only allow PR comments/labels
10+ # All other permissions are implicitly 'none'
511
612jobs :
713 build :
@@ -12,11 +18,25 @@ jobs:
1218 - name : Checkout code
1319 uses : actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1420 with :
21+ ref : ${{ github.event.pull_request.head.sha }}
1522 fetch-depth : 0
23+ path : pr-branch
24+
25+ # Checkout the master branch into a subdirectory
26+ - name : Checkout master branch
27+ uses : actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
28+ with :
29+ # When testing the scripts, comment out the "ref: master"
30+ ref : master
31+ path : master-branch
1632
1733 - name : Update the merge-conflict label
1834 run : |
35+ cd pr-branch
1936 printf 'PR number: "%s"\n' "${{ github.event.pull_request.number }}"
37+ git config user.name "leanprover-community-mathlib4-bot"
38+ git config user.email "leanprover-community-mathlib4-bot@users.noreply.github.com"
39+
2040 if git merge origin/master --no-ff --no-commit
2141 then
2242 git merge --abort || true
@@ -27,16 +47,19 @@ jobs:
2747 --url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/merge-conflict \
2848 --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
2949 else
30- echo "This PR has merge conflicts with main ."
50+ echo "This PR has merge conflicts with master ."
3151 # we use curl rather than octokit/request-action so that the job won't fail
3252 # (and send an annoying email) if the labels don't exist
3353 curl --request POST \
34- --url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/merge-conflict \
35- --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
54+ --header 'Accept: application/vnd.github+json' \
55+ --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
56+ --header 'X-GitHub-Api-Version: 2022-11-28' \
57+ --url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
58+ --data '{"labels":["merge-conflict"]}'
3659 fi
3760
3861 - name : Set up Python
39- uses : actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5 .0
62+ uses : actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6 .0
4063 with :
4164 python-version : 3.12
4265
@@ -46,46 +69,109 @@ jobs:
4669 sudo apt-get install -y jq
4770 # If you have additional dependencies, install them here
4871
49- - name : Get changed files
72+ - name : Get changed and removed/renamed files
5073 run : |
74+ cd pr-branch
5175 git fetch origin ${{ github.base_ref }} # fetch the base branch
52- git diff --name-only origin/${{ github.base_ref }}... > changed_files.txt # get the list of changed files
76+
77+ # Get the list of all changed files.
78+ echo "Saving the changed files to 'changed_files.txt'..."
79+ git diff --name-only origin/${{ github.base_ref }}... | tee changed_files.txt
80+
81+ # Get all files which were removed or renamed.
82+ echo "Checking for removed files..."
83+
84+ # Shows the `D`eleted files, one per line.
85+ git diff --name-only --diff-filter D origin/${{ github.base_ref }}... | tee removed_files.txt
86+ echo "Checking for renamed files..."
87+
88+ # Shows the `R`enamed files, in human readable format
89+ # The `awk` pipe
90+ # * extracts into an array the old name as the key and the new name as the value
91+ # * eventually prints "`oldName` was renamed to `newName`" for each key-value pair.
92+ git diff -p --summary --diff-filter=R origin/${{ github.base_ref }}... |
93+ awk '
94+ /^rename from / {
95+ file=$0
96+ gsub(/rename from /, "", file)
97+ oldFile=file
98+ }
99+ /^rename to / {
100+ file=$0
101+ gsub(/rename to /, "", file)
102+ oldNew[oldFile]=file
103+ } END {
104+ for(old in oldNew) {
105+ printf("`%s` was renamed to `%s`\n", old, oldNew[old])
106+ }
107+ }' | tee renamed_files.txt
108+
109+ - name : Compute (re)moved files without deprecation
110+ run : |
111+ cd pr-branch
112+ touch moved_without_deprecation.txt
113+ git checkout ${{ github.base_ref }}
114+ while IFS= read -r file
115+ do
116+ if grep ^deprecated_module "${file}" ; then
117+ printf 'info: removed file %s contains a deprecation\n' "${file}"
118+ else
119+ # shellcheck disable=SC2016
120+ printf '\n⚠️ **warning**: file `%s` was removed without a module deprecation\n' "${file}" |
121+ tee -a moved_without_deprecation.txt
122+ fi
123+ done < removed_files.txt
124+ IFS=$'\n'
125+ while IFS= read -r file
126+ do
127+ # shellcheck disable=SC2016
128+ printf '\n⚠️ **warning**: file %s without a module deprecation\n' "${file}" |
129+ tee -a moved_without_deprecation.txt
130+ done < renamed_files.txt
131+
132+ # we return to the PR branch, since the next step wants it!
133+ git checkout -
53134
54135 - name : Compute transitive imports
55136 run : |
137+ cd pr-branch
56138 # the checkout dance, to avoid a detached head
57139 git checkout master
58140 git checkout -
59141 currentHash="$(git rev-parse HEAD)"
142+ printf 'currentHash=%s\n' "${currentHash}"
60143
61- # Compute the counts for the HEAD of the PR
62- python ./scripts/count-trans-deps.py "Mathlib/" > head.json
144+ echo " Compute the counts for the HEAD of the PR"
145+ python ../master-branch /scripts/count-trans-deps.py "Mathlib/" > head.json
63146
64147 # Checkout the merge base
65- git checkout "$(git merge-base master ${{ github.sha }})"
148+ git checkout "$(git merge-base master ${{ github.event.pull_request.head. sha }})"
66149
67- # Compute the counts for the merge base
68- python ./scripts/count-trans-deps.py "Mathlib/" > base.json
150+ echo " Compute the counts for the merge base"
151+ python ../master-branch /scripts/count-trans-deps.py "Mathlib/" > base.json
69152
70153 # switch back to the current branch: the `declarations_diff` script should be here
71- git checkout "${currentHash}"
154+ git checkout "${currentHash}" --
155+
72156 - name : Post or update the summary comment
73157 env :
74- GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
158+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
75159 BRANCH_NAME : ${{ github.head_ref }}
76160 run : |
161+ cd pr-branch
162+ currentHash="$(git rev-parse HEAD)"
77163 PR="${{ github.event.pull_request.number }}"
78164 title="### PR summary"
79165
80- graphAndHighPercentReports=$(python ./scripts/import-graph-report.py base.json head.json changed_files.txt)
166+ graphAndHighPercentReports=$(python ../master-branch /scripts/import-graph-report.py base.json head.json changed_files.txt)
81167
82- ## Import count comment
168+ echo "Produce import count comment"
83169 importCount=$(
84170 printf '%s\n' "${graphAndHighPercentReports}" | sed '/^Import changes exceeding/Q'
85- ./scripts/import_trans_difference.sh
171+ ../master-branch /scripts/import_trans_difference.sh
86172 )
87173
88- ## High percentage imports
174+ echo "Produce high percentage imports"
89175 high_percentages=$(
90176 printf '%s\n' "${graphAndHighPercentReports}" | sed -n '/^Import changes exceeding/,$p'
91177 )
@@ -105,23 +191,53 @@ jobs:
105191 importCount="$(printf '#### Import changes for modified files\n\n%s\n' "${importCount}")"
106192 fi
107193
108- ## Declarations' diff comment
109- declDiff=$(./scripts/declarations_diff.sh)
194+ echo "Compute Declarations' diff comment"
195+ declDiff=$(../master-branch /scripts/declarations_diff.sh)
110196 if [ "$(printf '%s' "${declDiff}" | wc -l)" -gt 15 ]
111197 then
112198 declDiff="$(printf '<details><summary>\n\n%s\n\n</summary>\n\n%s\n\n</details>\n' "#### Declarations diff" "${declDiff}")"
113199 else
114200 declDiff="$(printf '#### Declarations diff\n\n%s\n' "${declDiff}")"
115201 fi
116- git checkout "${BRANCH_NAME}"
117- currentHash="$(git rev-parse HEAD)"
202+ git checkout "${currentHash}" --
118203 hashURL="https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }}/commits/${currentHash}"
204+ printf 'hashURL: %s' "${hashURL}"
119205
120- ## Technical debt changes
121- techDebtVar="$(./scripts/technical-debt-metrics.sh pr_summary)"
206+ echo "Compute technical debt changes"
207+ techDebtVar="$(../master-branch /scripts/technical-debt-metrics.sh pr_summary)"
122208
123209 # store in a file, to avoid "long arguments" error.
124210 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
125211
212+ echo "Include any errors about removed or renamed files without deprecation."
213+ if [ -s moved_without_deprecation.txt ]
214+ then
215+ printf '\n\n---\n\n' >> please_merge_master.md
216+ cat moved_without_deprecation.txt >> please_merge_master.md
217+ fi
218+
126219 cat please_merge_master.md
127- ./scripts/update_PR_comment.sh please_merge_master.md "${title}" "${PR}"
220+ ../master-branch/scripts/update_PR_comment.sh please_merge_master.md "${title}" "${PR}"
221+
222+ - name : Update the file-removed label
223+ run : |
224+ cd pr-branch
225+ undeprecatedMoves="$(cat moved_without_deprecation.txt)"
226+ if [ -n "$undeprecatedMoves" ]; then
227+ echo "This PR has undeprecated module (re)movals."
228+ # we use curl rather than octokit/request-action so that the job won't fail
229+ # (and send an annoying email) if the labels don't exist
230+ curl --request POST \
231+ --header 'Accept: application/vnd.github+json' \
232+ --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
233+ --header 'X-GitHub-Api-Version: 2022-11-28' \
234+ --url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels \
235+ --data '{"labels":["file-removed"]}'
236+ else
237+ echo "This PR (re)moves no modules without deprecations."
238+ # we use curl rather than octokit/request-action so that the job won't fail
239+ # (and send an annoying email) if the labels don't exist
240+ curl --request DELETE \
241+ --url https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels/file-removed \
242+ --header 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}'
243+ fi
0 commit comments