|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Report job of fork-follow.yml (tests: fork-follow.test.sh). Writes the run's summary and exits |
| 3 | +# non-zero when the run needs a person, so trouble shows as a failed run even without issues. |
| 4 | +# |
| 5 | +# What was pushed and published is read back from the repository (branches, tags, releases), not |
| 6 | +# inferred from job results, so the summary never claims a state it did not see. When the |
| 7 | +# repository has Issues enabled it also opens or comments on one issue, "Fork cannot follow |
| 8 | +# upstream", and closes it once a run succeeds; with Issues disabled it says so and opens none. |
| 9 | +# |
| 10 | +# Environment (from the run): REPOSITORY, SERIES_BRANCH, RUN_URL, ACTION, PLAN_RESULT, |
| 11 | +# RELEASE_RESULT, ADVANCE_RESULT, BASE_TAG, PREVIOUS_TAG, PREVIOUS_HEAD, CANDIDATE, RELEASED_TAG, |
| 12 | +# DROPPED, DISABLED, CANCELLED (from the workflow sweeps), SWEEP_RESULT, REPORTS (directory of the jobs' reason |
| 13 | +# files), ORIGIN (git URL of this fork, default GitHub). Summary to $GITHUB_STEP_SUMMARY, else stdout. |
| 14 | +set -uo pipefail |
| 15 | + |
| 16 | +REPOSITORY="${REPOSITORY:-${GITHUB_REPOSITORY:?REPOSITORY or GITHUB_REPOSITORY}}" |
| 17 | +ORIGIN="${ORIGIN:-https://github.com/$REPOSITORY.git}" |
| 18 | +SERIES_BRANCH="${SERIES_BRANCH:-main}" |
| 19 | +SUMMARY="${GITHUB_STEP_SUMMARY:-/dev/stdout}" |
| 20 | +ACTION="${ACTION:-}" |
| 21 | +REPORTS="${REPORTS:-}" |
| 22 | +title="Fork cannot follow upstream" |
| 23 | +short() { [[ -n "$1" ]] && echo "\`${1:0:12}\`" || echo "(none)"; } |
| 24 | + |
| 25 | +failed=false |
| 26 | +for result in "${PLAN_RESULT:-}" "${RELEASE_RESULT:-skipped}" "${ADVANCE_RESULT:-skipped}"; do |
| 27 | + [[ "$result" == success || "$result" == skipped ]] || failed=true |
| 28 | +done |
| 29 | +[[ "${PLAN_RESULT:-}" == success ]] || failed=true |
| 30 | +[[ "$ACTION" != stage ]] || failed=true |
| 31 | +[[ "${SWEEP_RESULT:-success}" != failure ]] || failed=true |
| 32 | + |
| 33 | +# The sweeps' lists arrive joined with ','; drop the empty parts and repeats. |
| 34 | +tidy() { tr ',' '\n' <<< "$1" | sed '/^$/d' | awk '!seen[$0]++' | paste -sd, -; } |
| 35 | +DISABLED="$(tidy "${DISABLED:-}")" |
| 36 | +CANCELLED="$(tidy "${CANCELLED:-}")" |
| 37 | + |
| 38 | +state=() |
| 39 | +row() { state+=("| $1 | $2 |"); } |
| 40 | + |
| 41 | +# --- what the repository holds now ----------------------------------------------------------- |
| 42 | +if refs="$(git ls-remote "$ORIGIN" "refs/heads/$SERIES_BRANCH" refs/heads/fork-follow/candidate 'refs/tags/*' 2>&1)"; then |
| 43 | + at() { awk -v ref="$1" '$2 == ref { print $1 }' <<< "$refs"; } |
| 44 | + series_now="$(at "refs/heads/$SERIES_BRANCH")" |
| 45 | + candidate_now="$(at refs/heads/fork-follow/candidate)" |
| 46 | + if [[ -z "${CANDIDATE:-}" ]]; then |
| 47 | + row "Series branch \`$SERIES_BRANCH\`" "at $(short "$series_now")" |
| 48 | + elif [[ "$series_now" == "$CANDIDATE" && "${PREVIOUS_HEAD:-$CANDIDATE}" != "$CANDIDATE" ]]; then |
| 49 | + row "Series branch \`$SERIES_BRANCH\`" "moved to $(short "$CANDIDATE") (was $(short "$PREVIOUS_HEAD"))" |
| 50 | + elif [[ "$series_now" == "$CANDIDATE" ]]; then |
| 51 | + row "Series branch \`$SERIES_BRANCH\`" "at $(short "$series_now"), unchanged" |
| 52 | + else |
| 53 | + row "Series branch \`$SERIES_BRANCH\`" "**not moved**: at $(short "$series_now")" |
| 54 | + fi |
| 55 | + row "Candidate branch \`fork-follow/candidate\`" "$([[ -n "$candidate_now" ]] && echo "at $(short "$candidate_now")" || echo "absent")" |
| 56 | + backups="$(awk '$2 ~ "^refs/tags/fork-follow/pre-" && $2 !~ /\^\{\}$/ { sub("refs/tags/", "", $2); print $2 }' <<< "$refs" | paste -sd, -)" |
| 57 | + [[ -z "$backups" ]] || row "Backups of earlier series (tags)" "${backups//,/, }" |
| 58 | + # Fork release tags that point at the candidate (lightweight, or annotated and peeled). |
| 59 | + tags="" |
| 60 | + if [[ -n "${CANDIDATE:-}" ]]; then |
| 61 | + tags="$(awk -v sha="$CANDIDATE" '$1 == sha { t = $2; sub("refs/tags/", "", t); sub(/\^\{\}$/, "", t); print t }' <<< "$refs" \ |
| 62 | + | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+-fork\.[0-9]+$' | sort -uV)" |
| 63 | + fi |
| 64 | + if [[ -z "${CANDIDATE:-}" ]]; then |
| 65 | + row "Release" "none: the plan did not pick a commit" |
| 66 | + elif [[ -z "$tags" ]]; then |
| 67 | + row "Release" "**none published** for $(short "$CANDIDATE")" |
| 68 | + else |
| 69 | + for tag in $tags; do |
| 70 | + if draft="$(gh api "repos/$REPOSITORY/releases/tags/$tag" --jq .draft 2>&1)"; then |
| 71 | + if [[ "$draft" == false ]]; then |
| 72 | + if [[ "$tag" == "${RELEASED_TAG:-}" && "$ACTION" != release ]]; then |
| 73 | + row "Release" "$tag, published earlier" |
| 74 | + else |
| 75 | + row "Release" "$tag **published** at $(short "$CANDIDATE")" |
| 76 | + fi |
| 77 | + else |
| 78 | + row "Release" "$tag tag exists, release is still a draft (not offered to updaters)" |
| 79 | + failed=true |
| 80 | + fi |
| 81 | + elif [[ "$draft" == *"Not Found"* || "$draft" == *"404"* ]]; then |
| 82 | + row "Release" "tag $tag exists without a release" |
| 83 | + failed=true |
| 84 | + else |
| 85 | + row "Release" "unknown: could not read the release of $tag" |
| 86 | + failed=true |
| 87 | + fi |
| 88 | + done |
| 89 | + fi |
| 90 | +else |
| 91 | + row "Repository state" "unknown: could not read the branches and tags of $REPOSITORY" |
| 92 | + failed=true |
| 93 | +fi |
| 94 | +[[ -z "${DISABLED:-}" ]] || row "Upstream workflows disabled" "${DISABLED//,/, }" |
| 95 | +[[ -z "${CANCELLED:-}" ]] || row "Their runs cancelled" "${CANCELLED//,/, }" |
| 96 | +[[ -z "${DROPPED:-}" ]] || row "Patches dropped (empty on $BASE_TAG: upstream has the change)" "${DROPPED//;/, }" |
| 97 | + |
| 98 | +reasons="" |
| 99 | +if [[ -n "$REPORTS" && -d "$REPORTS" ]]; then |
| 100 | + for file in "$REPORTS"/*.md; do |
| 101 | + [[ -f "$file" ]] && reasons+="$(cat "$file")"$'\n\n' |
| 102 | + done |
| 103 | +fi |
| 104 | + |
| 105 | +case "$ACTION" in |
| 106 | + none) what="Nothing to do: the series is released${RELEASED_TAG:+ as $RELEASED_TAG}." ;; |
| 107 | + release) what="Release the series on ${BASE_TAG:-?} as it is." ;; |
| 108 | + rebase) what="Rebase the series from ${PREVIOUS_TAG:-?} onto ${BASE_TAG:-?}, release it, then move \`$SERIES_BRANCH\`." ;; |
| 109 | + advance) what="Move \`$SERIES_BRANCH\` onto ${BASE_TAG:-?}: ${RELEASED_TAG:-?} already has the rebased series." ;; |
| 110 | + stage) what="Rebase onto ${BASE_TAG:-?} and push the candidate; its release needs its own workflow files." ;; |
| 111 | + *) what="The plan did not finish." ;; |
| 112 | +esac |
| 113 | + |
| 114 | +body="$(mktemp)" |
| 115 | +{ |
| 116 | + if [[ "$failed" == true ]]; then |
| 117 | + echo "## $title" |
| 118 | + else |
| 119 | + echo "## Fork follows upstream" |
| 120 | + fi |
| 121 | + echo |
| 122 | + echo "Plan: $what Run: ${RUN_URL:-?}" |
| 123 | + echo |
| 124 | + if [[ "$failed" == true ]]; then |
| 125 | + if [[ -n "$reasons" ]]; then |
| 126 | + printf '%s' "$reasons" |
| 127 | + else |
| 128 | + echo "A job failed without writing a reason (results: plan ${PLAN_RESULT:-?}, release ${RELEASE_RESULT:-skipped}, advance ${ADVANCE_RESULT:-skipped}); see the run." |
| 129 | + echo |
| 130 | + fi |
| 131 | + fi |
| 132 | + echo "| What | State now |" |
| 133 | + echo "|---|---|" |
| 134 | + printf '%s\n' "${state[@]}" |
| 135 | +} > "$body" |
| 136 | + |
| 137 | +# --- the issue, when the repository has Issues --------------------------------------------------- |
| 138 | +note="" |
| 139 | +if ! has_issues="$(gh api "repos/$REPOSITORY" --jq .has_issues 2>&1)"; then |
| 140 | + note="Could not read whether $REPOSITORY has Issues, so no issue was opened or closed." |
| 141 | + failed=true |
| 142 | +elif [[ "$has_issues" != true ]]; then |
| 143 | + note="Issues are disabled on $REPOSITORY, so no issue was opened or closed; this summary and the run's status are the report." |
| 144 | +elif ! issue="$(gh issue list --repo "$REPOSITORY" --state open --search "\"$title\" in:title" \ |
| 145 | + --json number,title --jq ".[] | select(.title == \"$title\") | .number" | head -n1)"; then |
| 146 | + note="Could not list the issues of $REPOSITORY, so no issue was opened or closed." |
| 147 | + failed=true |
| 148 | +elif [[ "$failed" == true && -n "$issue" ]]; then |
| 149 | + gh issue comment "$issue" --repo "$REPOSITORY" --body-file "$body" > /dev/null \ |
| 150 | + && note="Commented on issue #$issue." || { note="Could not comment on issue #$issue."; } |
| 151 | +elif [[ "$failed" == true ]]; then |
| 152 | + if url="$(gh issue create --repo "$REPOSITORY" --title "$title" --body-file "$body")"; then |
| 153 | + note="Opened $url." |
| 154 | + else |
| 155 | + note="Could not open an issue." |
| 156 | + fi |
| 157 | +elif [[ -n "$issue" ]]; then |
| 158 | + gh issue close "$issue" --repo "$REPOSITORY" --comment "$(cat "$body")" > /dev/null \ |
| 159 | + && note="Closed issue #$issue." || { note="Could not close issue #$issue."; failed=true; } |
| 160 | +fi |
| 161 | + |
| 162 | +{ |
| 163 | + cat "$body" |
| 164 | + [[ -z "$note" ]] || printf '\n%s\n' "$note" |
| 165 | +} >> "$SUMMARY" |
| 166 | +rm -f "$body" |
| 167 | +[[ "$failed" != true ]] |
0 commit comments