Repository navigation
201 lines (174 loc) · 6.35 KB
/
Copy pathcpd.yml
File metadata and controls
201 lines (174 loc) · 6.35 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
name: Copy/Paste Detection
on:
pull_request:
branches:
- main
paths:
- "**/*.rs"
- "!target/**"
- ".github/workflows/cpd.yml"
permissions:
contents: read
pull-requests: write
concurrency:
group: cpd-bounded-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
env:
PMD_VERSION: 7.26.0
CPD_TOKENS: "100"
COMMENT_MARKER: <!-- cpd-report -->
jobs:
cpd:
name: Copy/Paste Detection
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Checkout
uses: actions/checkout@v7
with:
fetch-depth: 0
persist-credentials: false
- name: Determine changed Rust files
id: changed
shell: bash
run: |
set -euo pipefail
BASE_SHA=$(git merge-base "origin/${GITHUB_BASE_REF}" HEAD)
git diff --name-only --diff-filter=d "$BASE_SHA" HEAD -- "*.rs" \
| grep -Ev '(^target/|(^|/)generated(/|$))' \
> changed-files.txt || true
COUNT=$(wc -l < changed-files.txt | tr -d " ")
echo "count=$COUNT" >> "$GITHUB_OUTPUT"
echo "Changed non-generated Rust files ($COUNT):"
cat changed-files.txt || true
- name: Setup Java
if: steps.changed.outputs.count != '0'
uses: actions/setup-java@v6
with:
distribution: temurin
java-version: "21"
- name: Setup PMD
if: steps.changed.outputs.count != '0'
shell: bash
run: |
set -euo pipefail
curl -fL "https://github.com/pmd/pmd/releases/download/pmd_releases%2F${PMD_VERSION}/pmd-dist-${PMD_VERSION}-bin.zip" -o pmd.zip
unzip -q pmd.zip
rm pmd.zip
- name: Run CPD
id: cpd
shell: bash
run: |
set -uo pipefail
if [ "${{ steps.changed.outputs.count }}" = "0" ]; then
: > cpd-report.md
echo "duplications=0" >> "$GITHUB_OUTPUT"
echo "No changed Rust files."
exit 0
fi
# PMD CPD exit codes:
# 0 - no duplications
# 4 - duplications found
# 5 - recoverable errors, for example a file failed to lex
set +e
"pmd-bin-${PMD_VERSION}/bin/pmd" cpd \
--language rust \
--minimum-tokens "${CPD_TOKENS}" \
--file-list changed-files.txt \
--format markdown \
> cpd-report.md 2> cpd-stderr.log
STATUS=$?
set -e
if [ "$STATUS" -ne 0 ] && [ "$STATUS" -ne 4 ] && [ "$STATUS" -ne 5 ]; then
echo "PMD CPD errored (status $STATUS):"
cat cpd-stderr.log
exit "$STATUS"
fi
if [ -s cpd-stderr.log ]; then
echo "=== PMD stderr ==="
STDERR_BYTES=$(wc -c < cpd-stderr.log | tr -d " ")
head -c 20000 cpd-stderr.log
echo ""
if [ "$STDERR_BYTES" -gt 20000 ]; then
echo "... PMD stderr truncated from ${STDERR_BYTES} bytes."
fi
fi
DUP_COUNT=$(grep -c "^Found a " cpd-report.md || true)
REPORT_BYTES=$(wc -c < cpd-report.md | tr -d " ")
REPORT_TRUNCATED=0
if [ "$REPORT_BYTES" -gt 50000 ]; then
head -c 50000 cpd-report.md > cpd-report.preview.md
mv cpd-report.preview.md cpd-report.md
REPORT_TRUNCATED=1
fi
sed -i "s|${GITHUB_WORKSPACE}/||g" cpd-report.md
awk '
BEGIN { open = 0 }
/^```$/ {
if (open == 0) { print "```rust"; open = 1 }
else { print "```"; open = 0 }
next
}
{ print }
END {
if (open != 0) { print "```" }
}
' cpd-report.md > cpd-report.tagged.md
mv cpd-report.tagged.md cpd-report.md
if [ "$REPORT_TRUNCATED" -eq 1 ]; then
{
echo ""
echo "_(report preview truncated from ${REPORT_BYTES} bytes)_"
} >> cpd-report.md
fi
echo "duplications=${DUP_COUNT:-0}" >> "$GITHUB_OUTPUT"
PREVIEW_BYTES=$(wc -c < cpd-report.md | tr -d " ")
echo "CPD found ${DUP_COUNT:-0} duplication(s); report ${REPORT_BYTES} bytes, preview ${PREVIEW_BYTES} bytes."
- name: Build comment body
shell: bash
env:
CHANGED_COUNT: ${{ steps.changed.outputs.count }}
DUP_COUNT: ${{ steps.cpd.outputs.duplications }}
run: |
set -euo pipefail
{
echo "${COMMENT_MARKER}"
echo "## Copy/Paste Detection"
echo ""
if [ "${DUP_COUNT:-0}" = "0" ]; then
echo "No duplications found in ${CHANGED_COUNT} changed non-generated Rust file(s) (threshold: ${CPD_TOKENS} tokens)."
else
echo "Found **${DUP_COUNT}** duplication(s) across ${CHANGED_COUNT} changed non-generated Rust file(s) (threshold: ${CPD_TOKENS} tokens)."
echo ""
echo "<details>"
echo "<summary>Show duplications</summary>"
echo ""
cat cpd-report.md
echo ""
echo "</details>"
fi
} > comment-body.md
SIZE=$(wc -c < comment-body.md)
if [ "$SIZE" -gt 60000 ]; then
head -c 60000 comment-body.md > comment-body.trunc.md
printf "\n\n_(report truncated; full output in workflow logs)_\n" >> comment-body.trunc.md
mv comment-body.trunc.md comment-body.md
fi
cat comment-body.md
- name: Post sticky PR comment
if: github.event.pull_request.head.repo.full_name == github.repository
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}
shell: bash
run: |
set -euo pipefail
BODY=$(cat comment-body.md)
COMMENT_ID=$(gh api "repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" \
--paginate -q ".[] | select(.body | startswith(\"${COMMENT_MARKER}\")) | .id" | head -1)
if [ -n "$COMMENT_ID" ]; then
gh api "repos/${{ github.repository }}/issues/comments/${COMMENT_ID}" \
-X PATCH -f body="$BODY"
else
gh pr comment "$PR_NUMBER" --body "$BODY"
fi