|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------- |
| 4 | +# generate_comment_body.sh |
| 5 | +# |
| 6 | +# Purpose: |
| 7 | +# Produce a markdown-formatted body for a GitHub PR comment that summarises |
| 8 | +# compilation warnings/errors and failed CTest results for a single matrix |
| 9 | +# target. |
| 10 | +# |
| 11 | +# Expected positional arguments: |
| 12 | +# $1 compilation_errors_warnings.out – file with compiler output |
| 13 | +# $2 cmake-output.log – file with CTest output |
| 14 | +# $3 bake_target – matrix target name (e.g. vt-build-…) |
| 15 | +# $4 job_status – “success”, “failure”, “cancelled”, … |
| 16 | +# |
| 17 | +# Environment variables (read-only) – provided by GitHub Actions: |
| 18 | +# GITHUB_REPOSITORY, GITHUB_RUN_ID, GITHUB_RUN_ATTEMPT, GITHUB_SHA |
| 19 | +# |
| 20 | +# Output: |
| 21 | +# The assembled markdown comment is printed to STDOUT. |
| 22 | +# ----------------------------------------------------------------------------- |
| 23 | + |
| 24 | +set -euo pipefail |
| 25 | + |
| 26 | +compilation_errors_warnings_out="$1" |
| 27 | +cmake_output_log="$2" |
| 28 | +bake_target="$3" |
| 29 | +job_status="$4" |
| 30 | + |
| 31 | +newline=$'\n' |
| 32 | +max_comment_size=2000 |
| 33 | + |
| 34 | +[[ "$job_status" == "success" || "$job_status" == "failure" ]] && succeeded=1 || succeeded=0 |
| 35 | + |
| 36 | +if [[ -f "$compilation_errors_warnings_out" && -s "$compilation_errors_warnings_out" ]]; then |
| 37 | + warnings_errors=$(<"$compilation_errors_warnings_out") |
| 38 | +else |
| 39 | + warnings_errors="" |
| 40 | +fi |
| 41 | + |
| 42 | +tests_failures="" |
| 43 | +if [[ -f "$cmake_output_log" && -s "$cmake_output_log" ]]; then |
| 44 | + tests_failures=$(sed -n '/The following tests FAILED:/,$p' "$cmake_output_log") |
| 45 | + tests_failures=${tests_failures//$'\t'/ } |
| 46 | +fi |
| 47 | + |
| 48 | +if (( succeeded )); then |
| 49 | + [[ -z "$warnings_errors" ]] && warnings_errors='Compilation – successful' |
| 50 | + [[ -z "$tests_failures" ]] && tests_failures='Testing – passed' |
| 51 | +else |
| 52 | + [[ -z "$warnings_errors" && -z "$tests_failures" ]] && \ |
| 53 | + warnings_errors='Build failed for unknown reason. Check build logs' |
| 54 | +fi |
| 55 | + |
| 56 | +val="${warnings_errors}${newline}${newline}${tests_failures}" |
| 57 | +if (( ${#val} > max_comment_size )); then |
| 58 | + val="${val:0:max_comment_size}${newline}${newline} ==> And there is more. Read log. <==" |
| 59 | +fi |
| 60 | + |
| 61 | +build_link=$(gh api "repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}/jobs" | |
| 62 | + jq -r --arg target "$bake_target" '.jobs | map(select(.name | contains($target))) | .[0].html_url') |
| 63 | + |
| 64 | +commit_date=$(date -u -d "$(gh api "repos/${GITHUB_REPOSITORY}/commits/${GITHUB_SHA}" --jq '.commit.committer.date')" '+%Y-%m-%d %H:%M:%S %Z') |
| 65 | + |
| 66 | +comment_body=$(cat <<EOF |
| 67 | +Build for $GITHUB_SHA ($commit_date) |
| 68 | +
|
| 69 | +\`\`\` |
| 70 | +$val |
| 71 | +\`\`\` |
| 72 | +
|
| 73 | +[View job log]($build_link) |
| 74 | +EOF |
| 75 | +) |
| 76 | + |
| 77 | +printf '%s\n' "$comment_body" |
0 commit comments