fix(post_ci_results.sh): improve CI result reporting and comment handling for Gitea

This commit is contained in:
2026-01-04 00:34:24 -06:00
parent 1dd6d93729
commit e60db20082

View File

@@ -1,19 +1,11 @@
#!/bin/bash #!/bin/bash
set -e set -e
# Required environment variables:
# GITEA_TOKEN
# GITHUB_REPOSITORY
# GITHUB_EVENT_PATH
# GITHUB_SERVER_URL
# GITHUB_RUN_ID
if [ -z "$GITEA_TOKEN" ]; then if [ -z "$GITEA_TOKEN" ]; then
echo "GITEA_TOKEN is not set. Skipping PR comment." echo "GITEA_TOKEN is not set. Skipping PR comment."
exit 0 exit 0
fi fi
# Extract PR number from the event JSON
if [ -f "$GITHUB_EVENT_PATH" ]; then if [ -f "$GITHUB_EVENT_PATH" ]; then
PR_NUMBER=$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH") PR_NUMBER=$(jq -r '.pull_request.number' "$GITHUB_EVENT_PATH")
else else
@@ -36,54 +28,103 @@ fi
RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
# Clean up ANSI escape sequences (colors) from the output if [[ "$GITHUB_SERVER_URL" == *"github.com"* ]] || [ -z "$GITHUB_SERVER_URL" ]; then
# This makes the markdown output readable GITHUB_SERVER_URL="https://git.quad4.io"
RUN_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
sed -i 's/\x1b\[[0-9;]*[mK]//g' "$INPUT_FILE" sed -i 's/\x1b\[[0-9;]*[mK]//g' "$INPUT_FILE"
if [ "$REPORT_TYPE" == "bench" ]; then if [ "$REPORT_TYPE" == "bench" ]; then
HEADER="### Benchmark Summary (automated report)" HEADER="### Benchmark Summary (automated report)"
# Extract from summary start to the end, removing progress lines
# We look for the start of the benchmark summary table
RESULTS=$(sed -n '/==================== BENCHMARK SUMMARY ====================/,$p' "$INPUT_FILE" | grep -v "Progress:") RESULTS=$(sed -n '/==================== BENCHMARK SUMMARY ====================/,$p' "$INPUT_FILE" | grep -v "Progress:")
else else
HEADER="### CI Status Report: $REPORT_TYPE"
# Check for failures
if grep -qiE "FAILED|error|❌|exit status|failure" "$INPUT_FILE"; then if grep -qiE "FAILED|error|❌|exit status|failure" "$INPUT_FILE"; then
# Try to find a meaningful summary HEADER="### ❌ CI Failure: $REPORT_TYPE"
if grep -q "short test summary info" "$INPUT_FILE"; then if grep -q "short test summary info" "$INPUT_FILE"; then
RESULTS=$(sed -n '/=========================== short test summary info ============================/,$p' "$INPUT_FILE") RESULTS=$(sed -n '/=========================== short test summary info ============================/,$p' "$INPUT_FILE")
elif grep -q "Found [0-9]* error" "$INPUT_FILE"; then elif grep -qE "Found [0-9]* error" "$INPUT_FILE"; then
# Ruff/Lint style errors
RESULTS=$(grep -A 20 -B 2 -E "Found [0-9]* error|::error|❌" "$INPUT_FILE") RESULTS=$(grep -A 20 -B 2 -E "Found [0-9]* error|::error|❌" "$INPUT_FILE")
else else
# Extract lines that look like errors, with some context, limited to avoid too long comments
RESULTS=$(grep -Ci 1 -E "FAILED|::error|❌|exit status|failure" "$INPUT_FILE" | tail -n 40) RESULTS=$(grep -Ci 1 -E "FAILED|::error|❌|exit status|failure" "$INPUT_FILE" | tail -n 40)
fi fi
HEADER="### ❌ CI Failure: $REPORT_TYPE" if [ -z "$RESULTS" ]; then
RESULTS=$(tail -n 30 "$INPUT_FILE")
fi
else else
HEADER="### CI Status Report: $REPORT_TYPE"
RESULTS="All checks passed! ✅" RESULTS="All checks passed! ✅"
fi fi
fi fi
if [ -z "$RESULTS" ]; then if [ -z "$RESULTS" ]; then
RESULTS="No detailed results found in $INPUT_FILE" RESULTS="No detailed results found in $INPUT_FILE. Check full logs."
fi fi
# Construct the message MARKER="<!-- GITEA_CI_REPORT -->"
# Note: Gitea/GitHub PR reviews use a JSON body where \n is a literal newline
BODY="${HEADER}\n\n"
BODY="${BODY}\`\`\`text\n${RESULTS}\n\`\`\`\n\n"
BODY="${BODY}🔗 [View Full Run](${RUN_URL})"
# Use jq to construct the JSON payload safely NEW_SECTION=$(cat <<EOF
JSON_PAYLOAD=$(jq -n \ ${HEADER}
--arg body "$BODY" \
'{body: $body, event: "COMMENT"}')
# Post to Gitea \`\`\`text
curl -f -X POST \ ${RESULTS}
-H "Authorization: token $GITEA_TOKEN" \ \`\`\`
-H "Content-Type: application/json" \ EOF
-d "$JSON_PAYLOAD" \ )
"https://git.quad4.io/api/v1/repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/reviews"
COMMENTS_URL="${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments"
EXISTING_COMMENT_JSON=$(curl -s -H "Authorization: token $GITEA_TOKEN" "$COMMENTS_URL" | \
jq -c ".[] | select(.body | contains(\"$MARKER\"))" | tail -n 1)
if [ -n "$EXISTING_COMMENT_JSON" ] && [ "$EXISTING_COMMENT_JSON" != "null" ]; then
COMMENT_ID=$(echo "$EXISTING_COMMENT_JSON" | jq -r .id)
OLD_BODY=$(echo "$EXISTING_COMMENT_JSON" | jq -r .body)
UPDATED_BODY=$(python3 -c "
import sys
import re
old_body = sys.stdin.read()
header = sys.argv[1]
new_section = sys.argv[2]
safe_header = re.escape(header)
pattern = r'(?m)^' + safe_header + r'.*?(?=\n---\n|\Z)'
if re.search(pattern, old_body, re.DOTALL):
updated = re.sub(pattern, new_section, old_body, flags=re.DOTALL)
else:
if '[View Full Run]' in old_body:
updated = old_body.replace('[View Full Run]', f'---\n\n{new_section}\n\n[View Full Run]')
else:
updated = old_body + f'\n\n---\n\n{new_section}'
print(updated.strip())
" "$HEADER" "$NEW_SECTION")
JSON_PAYLOAD=$(jq -n --arg body "$UPDATED_BODY" '{body: $body}')
curl -f -X PATCH \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"${GITHUB_SERVER_URL}/api/v1/repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}"
else
NEW_BODY=$(cat <<EOF
${MARKER}
## CI Status Reports
${NEW_SECTION}
[View Full Run](${RUN_URL})
EOF
)
JSON_PAYLOAD=$(jq -n --arg body "$NEW_BODY" '{body: $body}')
curl -f -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"$COMMENTS_URL"
fi