|
1 | | -name: 'Java SDK - Unit Testing' |
2 | | - |
3 | | -# This workflow runs ONLY unit tests (excludes integration tests ending with IT.java) |
4 | | -# Integration tests require network access and valid .env credentials |
| 1 | +name: Java SDK - Coverage Check |
5 | 2 |
|
6 | 3 | on: |
7 | 4 | pull_request: |
8 | 5 | branches: |
9 | 6 | - development |
10 | 7 | - staging |
11 | | - - main |
| 8 | + - master |
12 | 9 |
|
13 | 10 | jobs: |
14 | | - coverage: |
15 | | - name: Unit Test Coverage Check |
| 11 | + test-and-coverage: |
16 | 12 | runs-on: ubuntu-latest |
17 | | - permissions: |
18 | | - contents: read |
19 | | - pull-requests: write |
20 | | - checks: write |
21 | | - |
| 13 | + |
22 | 14 | steps: |
23 | | - - name: Checkout code |
| 15 | + - name: Checkout Repository |
24 | 16 | uses: actions/checkout@v4 |
25 | | - |
26 | | - - name: Set up JDK 8 |
| 17 | + |
| 18 | + - name: Set up JDK 17 |
27 | 19 | uses: actions/setup-java@v4 |
28 | 20 | with: |
29 | | - java-version: '8' |
30 | 21 | distribution: 'temurin' |
31 | | - cache: 'maven' |
32 | | - |
33 | | - - name: Run unit tests (excluding integration tests) |
34 | | - run: | |
35 | | - echo "Running unit tests only (excluding *IT.java files)..." |
36 | | - echo "Note: Surefire plugin has skipTests=true by default, overriding with -DskipTests=false" |
37 | | - mvn clean test -DskipTests=false -Dtest='Test*' -Dgpg.skip=true |
38 | | - continue-on-error: false |
39 | | - |
40 | | - - name: Verify JaCoCo reports generated |
| 22 | + java-version: '17' |
| 23 | + cache: maven |
| 24 | + |
| 25 | + - name: Run Tests and Generate JaCoCo Report |
| 26 | + working-directory: contentstack-java |
| 27 | + run: mvn clean test -Dtest='Test*' jacoco:report -Dgpg.skip=true |
| 28 | + |
| 29 | + - name: Verify Coverage Thresholds |
| 30 | + working-directory: contentstack-java |
41 | 31 | run: | |
42 | | - echo "Checking for JaCoCo reports..." |
43 | | - echo "Current directory: $(pwd)" |
44 | | - echo "Target directory contents:" |
45 | | - ls -la target/ || echo "No target directory found" |
46 | | - if [ -d "target/jacoco-ut" ]; then |
47 | | - echo "JaCoCo directory contents:" |
48 | | - ls -lh target/jacoco-ut/ |
49 | | - fi |
50 | | - if [ ! -f "target/jacoco-ut/jacoco.xml" ]; then |
51 | | - echo "❌ Error: jacoco.xml not found in target/jacoco-ut/" |
52 | | - echo "This usually means tests didn't run or JaCoCo plugin isn't configured correctly" |
| 32 | + echo "Checking JaCoCo coverage thresholds..." |
| 33 | + INSTRUCTION_COVERAGE=$(grep -oPm1 "(?<=<counter type=\"INSTRUCTION\" missed=\")\d+\" covered=\"\d+\"" target/site/jacoco/jacoco.xml | sed 's/" covered="/ /' | awk '{print $2/($1+$2)*100}') |
| 34 | + BRANCH_COVERAGE=$(grep -oPm1 "(?<=<counter type=\"BRANCH\" missed=\")\d+\" covered=\"\d+\"" target/site/jacoco/jacoco.xml | sed 's/" covered="/ /' | awk '{print $2/($1+$2)*100}') |
| 35 | + echo "Instruction Coverage: $INSTRUCTION_COVERAGE%" |
| 36 | + echo "Branch Coverage: $BRANCH_COVERAGE%" |
| 37 | +
|
| 38 | + MIN_INSTRUCTION=90 |
| 39 | + MIN_BRANCH=80 |
| 40 | +
|
| 41 | + if (( ${INSTRUCTION_COVERAGE%.*} < MIN_INSTRUCTION )); then |
| 42 | + echo "❌ Instruction coverage below $MIN_INSTRUCTION%" |
53 | 43 | exit 1 |
54 | 44 | fi |
55 | | - if [ ! -f "target/jacoco-ut/jacoco.csv" ]; then |
56 | | - echo "⚠️ Warning: jacoco.csv not found (badge generation will be skipped)" |
57 | | - fi |
58 | | - echo "✅ JaCoCo XML report found in target/jacoco-ut/" |
59 | | - if: always() |
60 | | - |
61 | | - - name: Generate JaCoCo Badge |
62 | | - id: jacoco |
63 | | - uses: cicirello/jacoco-badge-generator@v2 |
64 | | - if: hashFiles('target/jacoco-ut/jacoco.csv') != '' |
65 | | - continue-on-error: true |
66 | | - with: |
67 | | - jacoco-csv-file: target/jacoco-ut/jacoco.csv |
68 | | - badges-directory: .github/badges |
69 | | - generate-branches-badge: true |
70 | | - generate-summary: true |
71 | | - |
72 | | - - name: Check coverage thresholds |
73 | | - id: coverage-check |
74 | | - if: hashFiles('target/jacoco-ut/jacoco.xml') != '' |
75 | | - run: | |
76 | | - echo "Checking coverage thresholds (unit tests only)..." |
77 | | - # Extract coverage percentages from JaCoCo XML report (using sed for cross-platform compatibility) |
78 | | - INSTRUCTION_COVERAGE=$(sed -n 's/.*type="INSTRUCTION".*covered="\([0-9]*\)".*/\1/p' target/jacoco-ut/jacoco.xml | tail -1) |
79 | | - INSTRUCTION_MISSED=$(sed -n 's/.*type="INSTRUCTION".*missed="\([0-9]*\)".*/\1/p' target/jacoco-ut/jacoco.xml | tail -1) |
80 | | - BRANCH_COVERAGE=$(sed -n 's/.*type="BRANCH".*covered="\([0-9]*\)".*/\1/p' target/jacoco-ut/jacoco.xml | tail -1) |
81 | | - BRANCH_MISSED=$(sed -n 's/.*type="BRANCH".*missed="\([0-9]*\)".*/\1/p' target/jacoco-ut/jacoco.xml | tail -1) |
82 | | - |
83 | | - # Calculate percentages |
84 | | - INSTRUCTION_TOTAL=$((INSTRUCTION_COVERAGE + INSTRUCTION_MISSED)) |
85 | | - BRANCH_TOTAL=$((BRANCH_COVERAGE + BRANCH_MISSED)) |
86 | | - |
87 | | - if [ $INSTRUCTION_TOTAL -gt 0 ]; then |
88 | | - INSTRUCTION_PCT=$((INSTRUCTION_COVERAGE * 100 / INSTRUCTION_TOTAL)) |
89 | | - else |
90 | | - INSTRUCTION_PCT=0 |
91 | | - fi |
92 | | - |
93 | | - if [ $BRANCH_TOTAL -gt 0 ]; then |
94 | | - BRANCH_PCT=$((BRANCH_COVERAGE * 100 / BRANCH_TOTAL)) |
95 | | - else |
96 | | - BRANCH_PCT=0 |
97 | | - fi |
98 | | - |
99 | | - echo "instruction_pct=$INSTRUCTION_PCT" >> $GITHUB_OUTPUT |
100 | | - echo "branch_pct=$BRANCH_PCT" >> $GITHUB_OUTPUT |
101 | | - |
102 | | - # Check thresholds |
103 | | - THRESHOLD_MET=true |
104 | | - MESSAGES="" |
105 | | - |
106 | | - if [ $INSTRUCTION_PCT -lt 90 ]; then |
107 | | - THRESHOLD_MET=false |
108 | | - MESSAGES="${MESSAGES}❌ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" |
109 | | - else |
110 | | - MESSAGES="${MESSAGES}✅ Overall instruction coverage is ${INSTRUCTION_PCT}% (threshold: 90%)\n" |
111 | | - fi |
112 | | - |
113 | | - if [ $BRANCH_PCT -lt 80 ]; then |
114 | | - THRESHOLD_MET=false |
115 | | - MESSAGES="${MESSAGES}❌ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" |
116 | | - else |
117 | | - MESSAGES="${MESSAGES}✅ Branch coverage is ${BRANCH_PCT}% (threshold: 80%)\n" |
118 | | - fi |
119 | | - |
120 | | - echo "threshold_met=$THRESHOLD_MET" >> $GITHUB_OUTPUT |
121 | | - echo -e "$MESSAGES" |
122 | | - echo "messages<<EOF" >> $GITHUB_OUTPUT |
123 | | - echo -e "$MESSAGES" >> $GITHUB_OUTPUT |
124 | | - echo "EOF" >> $GITHUB_OUTPUT |
125 | | - |
126 | | - if [ "$THRESHOLD_MET" = "false" ]; then |
| 45 | +
|
| 46 | + if (( ${BRANCH_COVERAGE%.*} < MIN_BRANCH )); then |
| 47 | + echo "❌ Branch coverage below $MIN_BRANCH%" |
127 | 48 | exit 1 |
128 | 49 | fi |
129 | | - |
130 | | - - name: Add coverage comment to PR |
131 | | - uses: madrapps/jacoco-report@v1.6.1 |
132 | | - if: always() |
133 | | - with: |
134 | | - paths: | |
135 | | - ${{ github.workspace }}/target/jacoco-ut/jacoco.xml |
136 | | - token: ${{ secrets.GITHUB_TOKEN }} |
137 | | - min-coverage-overall: 90 |
138 | | - min-coverage-changed-files: 80 |
139 | | - title: '📊 Unit Test Coverage Report' |
140 | | - update-comment: true |
141 | | - |
142 | | - - name: Upload JaCoCo coverage report |
| 50 | +
|
| 51 | + echo "✅ Coverage thresholds met." |
| 52 | +
|
| 53 | + - name: Upload JaCoCo HTML Report (Artifact) |
143 | 54 | uses: actions/upload-artifact@v4 |
144 | | - if: always() |
145 | 55 | with: |
146 | 56 | name: jacoco-report |
147 | | - path: target/jacoco-ut/ |
148 | | - |
149 | | - - name: Fail if coverage thresholds not met |
150 | | - if: steps.coverage-check.outputs.threshold_met == 'false' |
151 | | - run: | |
152 | | - echo "Coverage thresholds not met:" |
153 | | - echo "${{ steps.coverage-check.outputs.messages }}" |
154 | | - exit 1 |
155 | | -
|
| 57 | + path: contentstack-java/target/site/jacoco/ |
0 commit comments