Skip to content

Commit 8af7e98

Browse files
committed
Testing: add GitHub Actions workflow to run SpotBugs on pull requests
1 parent 15a8edd commit 8af7e98

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

.github/workflows/spotbugs.yml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
name: SpotBugs Static Analysis
2+
3+
on:
4+
pull_request:
5+
branches: [ '*' ]
6+
paths:
7+
- '**/*.java'
8+
- 'build.xml'
9+
- 'spotbugs-exclude.xml'
10+
11+
jobs:
12+
spotbugs:
13+
runs-on: ubuntu-latest
14+
name: Run SpotBugs
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Set up Java 17
21+
uses: actions/setup-java@v4
22+
with:
23+
distribution: 'temurin'
24+
java-version: '17'
25+
26+
- name: Set up Ant
27+
run: |
28+
sudo apt-get update
29+
sudo apt-get install -y ant
30+
31+
- name: Download JUnit
32+
run: |
33+
mkdir -p /tmp/junit
34+
wget -q -P /tmp/junit \
35+
"https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.jar"
36+
echo "JUNIT_HOME=/tmp/junit" >> $GITHUB_ENV
37+
38+
- name: Download and set up SpotBugs
39+
run: |
40+
SPOTBUGS_VERSION="4.9.3"
41+
wget -q "https://github.com/spotbugs/spotbugs/releases/download/${SPOTBUGS_VERSION}/spotbugs-${SPOTBUGS_VERSION}.tgz"
42+
tar xzf "spotbugs-${SPOTBUGS_VERSION}.tgz"
43+
echo "SPOTBUGS_HOME=${GITHUB_WORKSPACE}/spotbugs-${SPOTBUGS_VERSION}" >> $GITHUB_ENV
44+
45+
- name: Run SpotBugs
46+
run: |
47+
ant spotbugs
48+
49+
- name: Check for SpotBugs warnings
50+
if: always()
51+
run: |
52+
REPORT="build/reports/spotbugs.html"
53+
if [ ! -f "$REPORT" ]; then
54+
echo "SpotBugs report not found"
55+
exit 1
56+
fi
57+
58+
# Extract warning count from report
59+
COUNT=$(python3 -c "
60+
import re
61+
with open('$REPORT', 'r') as f:
62+
content = f.read()
63+
m = re.search(r'<b>Total Warnings</b>.*?<b>(\d+)</b>', content, re.DOTALL)
64+
print(m.group(1) if m else '0')
65+
")
66+
67+
if [ "$COUNT" -eq 0 ]; then
68+
echo "=================================="
69+
echo "SpotBugs: 0 warnings found"
70+
echo "=================================="
71+
exit 0
72+
fi
73+
74+
echo "=================================="
75+
echo "SpotBugs: $COUNT warning(s) found"
76+
echo "=================================="
77+
echo ""
78+
79+
# Print each warning
80+
python3 -c "
81+
import re
82+
with open('$REPORT', 'r') as f:
83+
content = f.read()
84+
pattern = r'priority-(\d)\">([\w]+)</span>.*?</td>\s*<td>(.*?)</td>.*?Bug type (\w+).*?</p>'
85+
warnings = re.findall(pattern, content, re.DOTALL)
86+
for pri, code, desc, full_type in warnings:
87+
d = re.sub(r'<[^>]+>', '', desc).strip()
88+
loc = ''
89+
m2 = re.search(r'At ([\w.]+:\[line \d+\])', d)
90+
print(f'[P{pri}] {full_type}')
91+
print(f' {d[:200]}')
92+
print()
93+
"
94+
95+
exit 1

0 commit comments

Comments
 (0)