Skip to content

Commit 67de428

Browse files
committed
added graceful handling of failures
1 parent 39cbd93 commit 67de428

File tree

1 file changed

+75
-16
lines changed

1 file changed

+75
-16
lines changed

.github/workflows/pr-code-coverage.yml

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,29 @@ jobs:
2424
echo "Waiting for Azure DevOps build for PR #$PR_NUMBER ..."
2525
2626
for i in {1..100}; do
27-
BUILD_INFO=$(curl -s "$API_URL" | jq -c --arg PR "$PR_NUMBER" '[.value[] | select(.triggerInfo["pr.number"]==$PR)][0] // empty')
28-
if [[ -n "$BUILD_INFO" && "$BUILD_INFO" != "null" ]]; then
29-
STATUS=$(echo "$BUILD_INFO" | jq -r .status)
30-
RESULT=$(echo "$BUILD_INFO" | jq -r .result)
31-
BUILD_ID=$(echo "$BUILD_INFO" | jq -r .id)
32-
WEB_URL=$(echo "$BUILD_INFO" | jq -r ._links.web.href)
27+
echo "Attempt $i/100: Checking build status..."
28+
29+
# Fetch API response with error handling
30+
API_RESPONSE=$(curl -s "$API_URL")
31+
32+
# Check if response is valid JSON
33+
if ! echo "$API_RESPONSE" | jq . >/dev/null 2>&1; then
34+
echo "❌ Invalid JSON response from Azure DevOps API"
35+
echo "Response received: $API_RESPONSE"
36+
echo "This usually indicates the Azure DevOps pipeline has failed or API is unavailable"
37+
exit 1
38+
fi
39+
40+
# Parse build info safely
41+
BUILD_INFO=$(echo "$API_RESPONSE" | jq -c --arg PR "$PR_NUMBER" '[.value[]? | select(.triggerInfo["pr.number"]?==$PR)] | .[0] // empty' 2>/dev/null)
42+
43+
if [[ -n "$BUILD_INFO" && "$BUILD_INFO" != "null" && "$BUILD_INFO" != "empty" ]]; then
44+
STATUS=$(echo "$BUILD_INFO" | jq -r '.status // "unknown"')
45+
RESULT=$(echo "$BUILD_INFO" | jq -r '.result // "unknown"')
46+
BUILD_ID=$(echo "$BUILD_INFO" | jq -r '.id // "unknown"')
47+
WEB_URL=$(echo "$BUILD_INFO" | jq -r '._links.web.href // "unknown"')
48+
49+
echo "Found build: ID=$BUILD_ID, Status=$STATUS, Result=$RESULT"
3350
3451
if [[ "$STATUS" == "completed" ]]; then
3552
if [[ "$RESULT" == "succeeded" ]]; then
@@ -38,13 +55,24 @@ jobs:
3855
echo "BUILD_ID=$BUILD_ID" >> $GITHUB_ENV
3956
break
4057
else
41-
echo "❌ Build $BUILD_ID failed"
58+
echo "❌ Azure DevOps build $BUILD_ID failed with result: $RESULT"
59+
echo "🔗 Build URL: $WEB_URL"
60+
echo "This coverage workflow cannot proceed when the main build fails."
4261
exit 1
4362
fi
63+
else
64+
echo "⏳ Build $BUILD_ID is still $STATUS..."
4465
fi
66+
else
67+
echo "⏳ No build found for PR #$PR_NUMBER yet... (attempt $i/100)"
68+
fi
69+
70+
if [[ $i -eq 100 ]]; then
71+
echo "❌ Timeout: No build found for PR #$PR_NUMBER after 100 attempts"
72+
echo "This may indicate the Azure DevOps pipeline was not triggered or failed to start"
73+
exit 1
4574
fi
4675
47-
echo "⏳ Build not ready yet... retrying in 20s"
4876
sleep 20
4977
done
5078
@@ -54,15 +82,34 @@ jobs:
5482
ARTIFACTS_URL="https://dev.azure.com/SqlClientDrivers/public/_apis/build/builds/$BUILD_ID/artifacts?api-version=7.1-preview.5"
5583
5684
echo "📥 Fetching artifacts for build $BUILD_ID..."
57-
ARTIFACTS=$(curl -s "$ARTIFACTS_URL")
85+
86+
# Fetch artifacts with error handling
87+
ARTIFACTS_RESPONSE=$(curl -s "$ARTIFACTS_URL")
88+
89+
# Check if response is valid JSON
90+
if ! echo "$ARTIFACTS_RESPONSE" | jq . >/dev/null 2>&1; then
91+
echo "❌ Invalid JSON response from artifacts API"
92+
echo "Response received: $ARTIFACTS_RESPONSE"
93+
echo "This indicates the Azure DevOps build may not have completed successfully or artifacts are not available"
94+
exit 1
95+
fi
5896
5997
# Find the coverage report artifact
60-
COVERAGE_ARTIFACT=$(echo "$ARTIFACTS" | jq -r '.value[] | select(.name | test("Code Coverage Report")) | .resource.downloadUrl')
98+
COVERAGE_ARTIFACT=$(echo "$ARTIFACTS_RESPONSE" | jq -r '.value[]? | select(.name | test("Code Coverage Report")) | .resource.downloadUrl // empty' 2>/dev/null)
6199
62-
if [[ -n "$COVERAGE_ARTIFACT" && "$COVERAGE_ARTIFACT" != "null" ]]; then
100+
if [[ -n "$COVERAGE_ARTIFACT" && "$COVERAGE_ARTIFACT" != "null" && "$COVERAGE_ARTIFACT" != "empty" ]]; then
63101
echo "📊 Downloading coverage report..."
64-
curl -L "$COVERAGE_ARTIFACT" -o coverage-report.zip
65-
unzip -q coverage-report.zip
102+
if ! curl -L "$COVERAGE_ARTIFACT" -o coverage-report.zip --fail --silent; then
103+
echo "❌ Failed to download coverage report from Azure DevOps"
104+
echo "This indicates the coverage artifacts may not be available or accessible"
105+
exit 1
106+
fi
107+
108+
if ! unzip -q coverage-report.zip; then
109+
echo "❌ Failed to extract coverage report zip file"
110+
echo "The downloaded artifact may be corrupted"
111+
exit 1
112+
fi
66113
67114
# Find the main index.html file
68115
INDEX_FILE=$(find . -name "index.html" -path "*/Code Coverage Report*" | head -1)
@@ -90,9 +137,16 @@ jobs:
90137
echo "COVERED_LINES=$COVERED_LINES"
91138
echo "TOTAL_LINES=$TOTAL_LINES"
92139
140+
# Validate that we got the essential data
141+
if [[ -z "$OVERALL_PERCENTAGE" ]]; then
142+
echo "❌ Could not extract coverage percentage from the report"
143+
echo "The coverage report format may have changed or be incomplete"
144+
exit 1
145+
fi
146+
93147
echo "COVERAGE_PERCENTAGE=$OVERALL_PERCENTAGE" >> $GITHUB_ENV
94-
echo "COVERED_LINES=$COVERED_LINES" >> $GITHUB_ENV
95-
echo "TOTAL_LINES=$TOTAL_LINES" >> $GITHUB_ENV
148+
echo "COVERED_LINES=${COVERED_LINES:-N/A}" >> $GITHUB_ENV
149+
echo "TOTAL_LINES=${TOTAL_LINES:-N/A}" >> $GITHUB_ENV
96150
97151
# Extract top files with low coverage - improved approach
98152
echo "📋 Extracting file-level coverage..."
@@ -112,16 +166,21 @@ jobs:
112166
fi
113167
114168
echo "LOW_COVERAGE_FILES<<EOF" >> $GITHUB_ENV
115-
echo "$LOW_COVERAGE_FILES" >> $GITHUB_ENV
169+
echo "${LOW_COVERAGE_FILES:-No detailed file data available}" >> $GITHUB_ENV
116170
echo "EOF" >> $GITHUB_ENV
117171
118172
echo "✅ Coverage data extracted successfully"
119173
else
120174
echo "❌ Could not find index.html in coverage report"
175+
echo "Available files in the coverage report:"
176+
find . -name "*.html" | head -10 || echo "No HTML files found"
121177
exit 1
122178
fi
123179
else
124180
echo "❌ Could not find coverage report artifact"
181+
echo "Available artifacts from the build:"
182+
echo "$ARTIFACTS_RESPONSE" | jq -r '.value[]?.name // "No artifacts found"' 2>/dev/null || echo "Could not parse artifacts list"
183+
echo "This indicates the Azure DevOps build may not have generated coverage reports"
125184
exit 1
126185
fi
127186

0 commit comments

Comments
 (0)