Skip to content

Commit 694467f

Browse files
committed
Testing: Add SpotBugs GitHub Actions workflow
1 parent b51439c commit 694467f

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

.github/workflows/spotbugs.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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_XML="build/reports/spotbugs.xml"
53+
if [ ! -f "$REPORT_XML" ]; then
54+
echo "SpotBugs XML report not found"
55+
exit 1
56+
fi
57+
58+
# Extract warning count from XML report
59+
COUNT=$(python3 -c "
60+
import xml.etree.ElementTree as ET
61+
tree = ET.parse('$REPORT_XML')
62+
root = tree.getroot()
63+
bugs = root.findall('.//BugInstance')
64+
print(len(bugs))
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 from XML
80+
python3 -c "
81+
import xml.etree.ElementTree as ET
82+
tree = ET.parse('$REPORT_XML')
83+
root = tree.getroot()
84+
for bug in root.findall('.//BugInstance'):
85+
bug_type = bug.get('type', 'UNKNOWN')
86+
priority = bug.get('priority', 'N/A')
87+
long_msg = bug.find('LongMessage')
88+
msg = long_msg.text.strip() if long_msg is not None and long_msg.text else ''
89+
src = bug.find('.//SourceLine')
90+
loc = ''
91+
if src is not None:
92+
loc = src.get('sourcefile', '') + ':' + src.get('start', '')
93+
print(f'[P{priority}] {bug_type}')
94+
if msg:
95+
print(f' {msg[:200]}')
96+
if loc:
97+
print(f' at {loc}')
98+
print()
99+
"
100+
101+
exit 1

0 commit comments

Comments
 (0)