Skip to content

Commit 2e75436

Browse files
Refactor unit testing workflow: rename job to 'coverage', update JDK setup to version 9 with Zulu distribution, streamline coverage extraction from JaCoCo HTML report, and enhance PR comment formatting for coverage summary.
1 parent c6cc34b commit 2e75436

File tree

1 file changed

+28
-53
lines changed

1 file changed

+28
-53
lines changed

.github/workflows/unit-testing.yml

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,60 @@ on:
88
- master
99

1010
jobs:
11-
test-and-coverage:
11+
coverage:
1212
runs-on: ubuntu-latest
13-
permissions:
14-
contents: read
15-
pull-requests: write
1613

1714
steps:
18-
- name: Checkout Repository
15+
- name: Checkout repository
1916
uses: actions/checkout@v4
2017

21-
- name: Set up JDK 8 (Temurin)
18+
- name: Set up JDK 9
2219
uses: actions/setup-java@v4
2320
with:
24-
distribution: 'temurin'
25-
java-version: '8'
26-
cache: maven
21+
java-version: '9'
22+
distribution: 'zulu'
2723

28-
- name: Ensure Tests Are Enabled in pom.xml
24+
- name: Enable tests in pom.xml
2925
run: |
30-
echo "🔧 Temporarily enabling tests in pom.xml..."
31-
if [[ "$OSTYPE" == "darwin"* ]]; then
32-
sed -i '' 's/<skipTests>true<\/skipTests>/<skipTests>false<\/skipTests>/g' pom.xml || true
33-
else
34-
sed -i 's/<skipTests>true<\/skipTests>/<skipTests>false<\/skipTests>/g' pom.xml || true
35-
fi
36-
echo "✅ Tests enabled."
26+
echo "🔧 Ensuring tests are enabled in pom.xml..."
27+
sed -i 's/<skipTests>true<\/skipTests>/<skipTests>false<\/skipTests>/g' pom.xml || true
3728
38-
- name: Run Tests and Generate JaCoCo Report
29+
- name: Run tests and generate JaCoCo report
3930
run: mvn clean test -Dtest='Test*' jacoco:report -Dgpg.skip=true
31+
working-directory: contentstack-java
4032

41-
- name: Extract and Check Coverage Thresholds
42-
id: coverage
33+
- name: 📊 Extract coverage from JaCoCo HTML report
34+
id: extract_coverage
35+
working-directory: contentstack-java
4336
run: |
44-
echo "Checking JaCoCo coverage thresholds..."
37+
echo "Extracting coverage summary from JaCoCo HTML report..."
4538
46-
if [ ! -f target/site/jacoco/jacoco.xml ]; then
47-
echo "❌ JaCoCo XML report not found."
39+
if [ ! -f target/site/jacoco/index.html ]; then
40+
echo "❌ JaCoCo HTML report not found!"
4841
exit 1
4942
fi
5043
51-
INSTRUCTION=$(grep -oPm1 '(?<=<counter type="INSTRUCTION" missed=")[0-9]+" covered="[0-9]+"' target/site/jacoco/jacoco.xml | sed 's/" covered="/ /' | awk '{printf "%.2f", ($2/($1+$2))*100}')
52-
BRANCH=$(grep -oPm1 '(?<=<counter type="BRANCH" missed=")[0-9]+" covered="[0-9]+"' target/site/jacoco/jacoco.xml | sed 's/" covered="/ /' | awk '{printf "%.2f", ($2/($1+$2))*100}')
53-
54-
echo "Instruction Coverage: $INSTRUCTION%"
55-
echo "Branch Coverage: $BRANCH%"
56-
57-
echo "instruction=$INSTRUCTION" >> $GITHUB_OUTPUT
58-
echo "branch=$BRANCH" >> $GITHUB_OUTPUT
59-
60-
MIN_INSTRUCTION=90
61-
MIN_BRANCH=80
62-
63-
if (( ${INSTRUCTION%.*} < MIN_INSTRUCTION )); then
64-
echo "❌ Instruction coverage below $MIN_INSTRUCTION%"
65-
exit 1
66-
fi
44+
INSTRUCTION=$(grep -A2 "Total" target/site/jacoco/index.html | grep -m1 "%" | sed -E 's/.*>([0-9]+\.[0-9]+)%<.*/\1/')
45+
BRANCH=$(grep -A3 "Total" target/site/jacoco/index.html | grep -m2 "%" | tail -n1 | sed -E 's/.*>([0-9]+\.[0-9]+)%<.*/\1/')
6746
68-
if (( ${BRANCH%.*} < MIN_BRANCH )); then
69-
echo "❌ Branch coverage below $MIN_BRANCH%"
70-
exit 1
71-
fi
47+
echo "📘 Instruction Coverage: ${INSTRUCTION:-0}"
48+
echo "🌿 Branch Coverage: ${BRANCH:-0}"
7249
73-
echo "✅ Coverage thresholds met."
50+
echo "instruction=${INSTRUCTION:-0}" >> $GITHUB_OUTPUT
51+
echo "branch=${BRANCH:-0}" >> $GITHUB_OUTPUT
7452
75-
- name: Comment Coverage Summary on PR
76-
if: always()
53+
- name: 💬 Post coverage summary as PR comment
7754
uses: marocchino/sticky-pull-request-comment@v2
7855
with:
79-
header: "🧪 JaCoCo Coverage Report"
56+
header: 🧪 JaCoCo Coverage Report
8057
message: |
8158
**Coverage Summary**
82-
- 📘 Instruction Coverage: `${{ steps.coverage.outputs.instruction }}%`
83-
- 🌿 Branch Coverage: `${{ steps.coverage.outputs.branch }}%`
84-
85-
✅ Minimum thresholds: 90% instruction, 80% branch
59+
- 📘 Instruction Coverage: `${{ steps.extract_coverage.outputs.instruction }}%`
60+
- 🌿 Branch Coverage: `${{ steps.extract_coverage.outputs.branch }}%`
8661
87-
- name: Upload JaCoCo HTML Report
88-
if: always()
62+
- name: 📦 Upload JaCoCo HTML report as artifact
8963
uses: actions/upload-artifact@v4
9064
with:
9165
name: jacoco-report
9266
path: target/site/jacoco/
67+
if-no-files-found: warn

0 commit comments

Comments
 (0)