Skip to content

Commit dc9cd57

Browse files
committed
Testing: Add SpotBugs GitHub Actions workflow
1 parent 52afad5 commit dc9cd57

1 file changed

Lines changed: 97 additions & 0 deletions

File tree

.github/workflows/spotbugs.yml

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

0 commit comments

Comments
 (0)