Skip to content

Commit 0e7216b

Browse files
committed
refactor: abstract codecov submission script
- Use CODECOV_TOKEN environment variable instead of hardcoded token - Automatically detect latest commit SHA from git - Automatically detect latest PR number from GitHub API - Support GITHUB_TOKEN for PR detection - Support GITHUB_REPOSITORY and COVERAGE_FILE environment variables - Add error handling and informative output
1 parent 743f0c7 commit 0e7216b

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

scripts/submit-codecov-coverage.sh

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/bin/bash
2+
# Submit code coverage to Codecov
3+
#
4+
# This script automatically detects:
5+
# - Latest commit SHA from git
6+
# - Latest PR number from GitHub API (if running in PR context)
7+
# - Current branch name
8+
#
9+
# Environment variables:
10+
# CODECOV_TOKEN: Codecov upload token (required)
11+
# GITHUB_TOKEN: GitHub token for API access (optional, for PR detection)
12+
# GITHUB_REPOSITORY: Repository in format owner/repo (optional, defaults to git remote)
13+
# COVERAGE_FILE: Coverage file path (defaults to coverage.xml)
14+
15+
set -euo pipefail
16+
17+
# Default values
18+
COVERAGE_FILE="${COVERAGE_FILE:-coverage.xml}"
19+
GITHUB_REPOSITORY="${GITHUB_REPOSITORY:-}"
20+
21+
# Check required environment variable
22+
if [ -z "${CODECOV_TOKEN:-}" ]; then
23+
echo "Error: CODECOV_TOKEN environment variable is required" >&2
24+
exit 1
25+
fi
26+
27+
# Check if coverage file exists
28+
if [ ! -f "$COVERAGE_FILE" ]; then
29+
echo "Error: Coverage file '$COVERAGE_FILE' not found" >&2
30+
exit 1
31+
fi
32+
33+
# Get latest commit SHA
34+
COMMIT_SHA=$(git rev-parse HEAD)
35+
if [ -z "$COMMIT_SHA" ]; then
36+
echo "Error: Could not determine commit SHA" >&2
37+
exit 1
38+
fi
39+
40+
# Get current branch name
41+
BRANCH=$(git rev-parse --abbrev-ref HEAD)
42+
if [ -z "$BRANCH" ]; then
43+
echo "Error: Could not determine branch name" >&2
44+
exit 1
45+
fi
46+
47+
# Get repository name (owner/repo)
48+
if [ -z "$GITHUB_REPOSITORY" ]; then
49+
# Try to get from git remote
50+
GITHUB_REPOSITORY=$(git remote get-url origin 2>/dev/null | sed -E 's|.*github.com[:/]([^/]+/[^/]+)(\.git)?$|\1|' || echo "")
51+
if [ -z "$GITHUB_REPOSITORY" ]; then
52+
echo "Warning: Could not determine repository name. Set GITHUB_REPOSITORY environment variable." >&2
53+
fi
54+
fi
55+
56+
# Try to get PR number from GitHub API if GITHUB_TOKEN is available
57+
PR_NUMBER=""
58+
if [ -n "${GITHUB_TOKEN:-}" ] && [ -n "$GITHUB_REPOSITORY" ]; then
59+
# Get PR number from GitHub API (latest PR for this branch)
60+
PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
61+
"https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?head=${GITHUB_REPOSITORY%%/*}:$BRANCH&state=open" \
62+
| jq -r '.[0].number // empty' 2>/dev/null || echo "")
63+
64+
# If no open PR found, try closed PRs
65+
if [ -z "$PR_NUMBER" ]; then
66+
PR_NUMBER=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
67+
"https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?head=${GITHUB_REPOSITORY%%/*}:$BRANCH&state=all" \
68+
| jq -r '.[0].number // empty' 2>/dev/null || echo "")
69+
fi
70+
fi
71+
72+
# Build codecov command
73+
CODECOV_CMD="codecovcli upload-coverage"
74+
CODECOV_CMD="$CODECOV_CMD -t $CODECOV_TOKEN"
75+
CODECOV_CMD="$CODECOV_CMD --file $COVERAGE_FILE"
76+
CODECOV_CMD="$CODECOV_CMD --branch $BRANCH"
77+
CODECOV_CMD="$CODECOV_CMD --sha $COMMIT_SHA"
78+
CODECOV_CMD="$CODECOV_CMD --report-type coverage"
79+
80+
# Add PR number if available
81+
if [ -n "$PR_NUMBER" ]; then
82+
CODECOV_CMD="$CODECOV_CMD --pr $PR_NUMBER"
83+
echo "Detected PR #$PR_NUMBER for branch $BRANCH"
84+
fi
85+
86+
# Add repository if available
87+
if [ -n "$GITHUB_REPOSITORY" ]; then
88+
CODECOV_CMD="$CODECOV_CMD -r $GITHUB_REPOSITORY"
89+
fi
90+
91+
# Display information
92+
echo "Submitting coverage to Codecov:"
93+
echo " Repository: ${GITHUB_REPOSITORY:-<unknown>}"
94+
echo " Branch: $BRANCH"
95+
echo " Commit: $COMMIT_SHA"
96+
echo " PR: ${PR_NUMBER:-<none>}"
97+
echo " Coverage file: $COVERAGE_FILE"
98+
echo ""
99+
100+
# Execute codecov command
101+
eval "$CODECOV_CMD"
102+

0 commit comments

Comments
 (0)