-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnftest_coverage_comment.sh
More file actions
120 lines (102 loc) · 3.87 KB
/
nftest_coverage_comment.sh
File metadata and controls
120 lines (102 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env bash
# See the NOTICE file distributed with this work for additional information
# regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail
FILE=$1
if [[ ! -s "$FILE" ]]; then
echo "Coverage file $FILE is empty. Exiting."
exit 1
fi
COVERAGE_THRESHOLD=${COVERAGE_THRESHOLD:-90}
MARKER="<!-- nf-test-coverage-report -->"
: "${GITHUB_TOKEN:?Missing GITHUB_TOKEN}"
: "${GITHUB_REPOSITORY:?Missing GITHUB_REPOSITORY}"
: "${GITHUB_EVENT_PATH:?Missing GITHUB_EVENT_PATH}"
# Get PR number
PR_NUMBER=$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH")
if [[ -z "$PR_NUMBER" || "$PR_NUMBER" == "null" ]]; then
echo "Not running on a pull request. Exiting."
exit 0
fi
# ---- Parse nf-test coverage totals ----
COVERAGE_LINE=$(grep -E '(COVERAGE:|Status:)' "$FILE" | tail -1 || true)
if [[ -z "$COVERAGE_LINE" ]]; then
echo "ERROR: Could not find coverage line in $FILE"
cat "$FILE"
exit 1
fi
if echo "$COVERAGE_LINE" | grep -q 'COVERAGE:'; then
TOTAL_COVERAGE=$(echo "$COVERAGE_LINE" | grep -oE '[0-9]+(\.[0-9]+)?%' | tr -d '%' || true)
HIT_FILES=$(echo "$COVERAGE_LINE" | grep -oE '[0-9]+ of' | grep -oE '[0-9]+' || true)
TOTAL_FILES=$(echo "$COVERAGE_LINE" | grep -oE 'of [0-9]+' | grep -oE '[0-9]+' || true)
else
# New format: "Status: 26 of 64 modules and workflows are covered ... (40.62%)"
TOTAL_COVERAGE=$(echo "$COVERAGE_LINE" | grep -oE '\([0-9]+(\.[0-9]+)?%\)' | tr -d '()%' || true)
HIT_FILES=$(echo "$COVERAGE_LINE" | grep -oE '[0-9]+ of' | grep -oE '[0-9]+' || true)
TOTAL_FILES=$(echo "$COVERAGE_LINE" | grep -oE 'of [0-9]+' | grep -oE '[0-9]+' || true)
fi
if [[ -z "$TOTAL_COVERAGE" || \
-z "$HIT_FILES" || \
-z "$TOTAL_FILES" || "$TOTAL_FILES" -eq 0 ]];
then
echo "Could not parse coverage percentage from: $COVERAGE_LINE"
exit 1
fi
COVERAGE_OK=$(awk -v cov="$TOTAL_COVERAGE" -v th="$COVERAGE_THRESHOLD" \
'BEGIN { print (cov >= th) ? "yes" : "no" }')
if [[ "$COVERAGE_OK" == "yes" ]]; then
STATUS_ICON="✅"
STATUS_TEXT="Coverage meets threshold"
else
STATUS_ICON="❌"
STATUS_TEXT="Coverage below ${COVERAGE_THRESHOLD}% threshold"
fi
# ---- Comment body ----
COMMENT_BODY=$(cat <<EOF
$MARKER
### 🧪 nf-test Coverage Report
**Total coverage:** **${TOTAL_COVERAGE}%**
**Lines:** $HIT_FILES / $TOTAL_FILES
**Status:** $STATUS_ICON $STATUS_TEXT
EOF
)
# ---- GitHub API ----
API_URL="https://api.github.com"
COMMENTS_URL="$API_URL/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments"
AUTH_HEADER="Authorization: Bearer $GITHUB_TOKEN"
EXISTING_COMMENT_ID=$(curl -s \
-H "$AUTH_HEADER" \
-H "Accept: application/vnd.github+json" \
"$COMMENTS_URL" \
| jq -r ".[] | select(.body | contains(\"$MARKER\")) | .id")
if [[ -n "$EXISTING_COMMENT_ID" ]]; then
echo "Updating existing nf-test coverage comment"
curl -s -X PATCH \
-H "$AUTH_HEADER" \
-H "Accept: application/vnd.github+json" \
"$API_URL/repos/$GITHUB_REPOSITORY/issues/comments/$EXISTING_COMMENT_ID" \
-d "$(jq -nc --arg body "$COMMENT_BODY" '{body: $body}')"
else
echo "Creating nf-test coverage comment"
curl -s -X POST \
-H "$AUTH_HEADER" \
-H "Accept: application/vnd.github+json" \
"$COMMENTS_URL" \
-d "$(jq -nc --arg body "$COMMENT_BODY" '{body: $body}')"
fi
# ---- Fail CI if coverage too low ----
if [[ "$COVERAGE_OK" != "yes" ]]; then
echo "Coverage ${TOTAL_COVERAGE}% is below threshold ${COVERAGE_THRESHOLD}%"
exit 1
fi