|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Builds the content of the docs-live branch. |
| 3 | +# |
| 4 | +# Live docs = the release commit + every docs-only commit on main after it, |
| 5 | +# applied in main order. A docs-only commit that conflicts or leaves a broken |
| 6 | +# link is held ("waiting") until a later release includes it. |
| 7 | +# |
| 8 | +# The release commit is the newest accepted docs-release-* tag: a commit on |
| 9 | +# main that isn't behind the current release, or a hotfix commit branched from |
| 10 | +# the docs-live tip. Every run looks for new tags, so a tag run that was |
| 11 | +# dropped or ran an older version of this workflow is picked up by the next run. |
| 12 | +# |
| 13 | +# Usage: |
| 14 | +# publish-docs.sh auto build from the newest release that builds |
| 15 | +# publish-docs.sh preview <commit> say whether merging <commit> goes live or waits |
| 16 | +# publish-docs.sh check-tag <tag> fail unless the tag can be released |
| 17 | +# publish-docs.sh release print the release commit to build from |
| 18 | +# publish-docs.sh build <release> build the publish commit (no remote writes) |
| 19 | +# |
| 20 | +# build reports through RESULT_FILE (publish_sha=...), REPORT_FILE (markdown) |
| 21 | +# and NEW_WAITING_FILE (one "<sha>\t<reason>" per commit that just started waiting). |
| 22 | + |
| 23 | +set -euo pipefail |
| 24 | + |
| 25 | +DOCS_LIVE_REF="${DOCS_LIVE_REF:-origin/docs-live}" |
| 26 | +MAIN_REF="${MAIN_REF:-origin/main}" |
| 27 | +DOCS_DIR="${DOCS_DIR:-docs}" |
| 28 | +TAG_PATTERN="${TAG_PATTERN:-refs/tags/docs-release-*}" |
| 29 | +LINK_CHECK_CMD="${LINK_CHECK_CMD:-npx --yes mintlify@4.0.393 broken-links}" |
| 30 | +RESULT_FILE="${RESULT_FILE:-/dev/null}" |
| 31 | +REPORT_FILE="${REPORT_FILE:-/dev/null}" |
| 32 | +NEW_WAITING_FILE="${NEW_WAITING_FILE:-/dev/null}" |
| 33 | + |
| 34 | +die() { |
| 35 | + echo "error: $*" >&2 |
| 36 | + exit 1 |
| 37 | +} |
| 38 | + |
| 39 | +is_ancestor() { git merge-base --is-ancestor "$1" "$2"; } |
| 40 | + |
| 41 | +trailer_values() { |
| 42 | + git log -1 --format='%(trailers:key='"$2"',valueonly)' "$1" | sed '/^$/d' |
| 43 | +} |
| 44 | + |
| 45 | +live_tip() { git rev-parse "$DOCS_LIVE_REF^{commit}"; } |
| 46 | + |
| 47 | +# The release recorded on docs-live, or the tip itself before the first run. |
| 48 | +recorded_release() { |
| 49 | + local tip release |
| 50 | + tip=$(live_tip) |
| 51 | + release=$(trailer_values "$tip" Docs-Release | head -n1) |
| 52 | + echo "${release:-$tip}" |
| 53 | +} |
| 54 | + |
| 55 | +# Why a tag can't become the release, or nothing when it can. |
| 56 | +tag_problem() { |
| 57 | + local tag="$1" release live base |
| 58 | + release=$(recorded_release) |
| 59 | + live=$(live_tip) |
| 60 | + if is_ancestor "$tag" "$MAIN_REF"; then |
| 61 | + base=$(git merge-base "$release" "$MAIN_REF") |
| 62 | + is_ancestor "$base" "$tag" || |
| 63 | + echo "it is older than the current release $release, so publishing it would remove live docs. Tag $base or a later commit on main." |
| 64 | + return 0 |
| 65 | + fi |
| 66 | + is_ancestor "$live" "$tag" || |
| 67 | + echo "it is neither on main nor branched from the docs-live tip $live." |
| 68 | +} |
| 69 | + |
| 70 | +check_tag() { |
| 71 | + local tag problem |
| 72 | + tag=$(git rev-parse "$1^{commit}") |
| 73 | + if [[ "$tag" == "$(recorded_release)" ]]; then |
| 74 | + echo "$tag is already the release" >&2 |
| 75 | + return 0 |
| 76 | + fi |
| 77 | + problem=$(tag_problem "$tag") |
| 78 | + [[ -z "$problem" ]] || die "can't release $tag: $problem" |
| 79 | +} |
| 80 | + |
| 81 | +# Newest valid docs-release-* tag that isn't published yet, else the recorded release. |
| 82 | +release() { |
| 83 | + local live release tag sha |
| 84 | + live=$(live_tip) |
| 85 | + release=$(recorded_release) |
| 86 | + is_ancestor "$release" "$MAIN_REF" || is_ancestor "$release" "$live" || |
| 87 | + die "recorded release $release is neither on $MAIN_REF nor in docs-live" |
| 88 | + while IFS= read -r tag; do |
| 89 | + sha=$(git rev-parse "$tag^{commit}") |
| 90 | + is_ancestor "$sha" "$live" && continue |
| 91 | + [[ -z "$(tag_problem "$sha")" ]] || continue |
| 92 | + if [[ "$release" != "$(recorded_release)" ]] && is_ancestor "$sha" "$release"; then |
| 93 | + continue |
| 94 | + fi |
| 95 | + release="$sha" |
| 96 | + done < <(git for-each-ref --sort=creatordate --format='%(refname)' "$TAG_PATTERN") |
| 97 | + echo "$release" |
| 98 | +} |
| 99 | + |
| 100 | +is_docs_only() { |
| 101 | + local sha="$1" parents files path |
| 102 | + parents=$(git rev-list --parents -n1 "$sha" | wc -w) |
| 103 | + [[ "$parents" -eq 2 ]] || return 1 |
| 104 | + files=$(git diff-tree --no-commit-id --name-only -r "$sha") |
| 105 | + [[ -n "$files" ]] || return 1 |
| 106 | + while IFS= read -r path; do |
| 107 | + [[ "$path" == "$DOCS_DIR/"* ]] || return 1 |
| 108 | + done <<<"$files" |
| 109 | +} |
| 110 | + |
| 111 | +WORK="" |
| 112 | +cleanup() { |
| 113 | + if [[ -n "$WORK" ]]; then |
| 114 | + git worktree remove --force "$WORK" >/dev/null 2>&1 || true |
| 115 | + fi |
| 116 | +} |
| 117 | + |
| 118 | +in_work() { |
| 119 | + git -C "$WORK" -c user.name=docs-publish -c user.email=docs-publish@localhost "$@" |
| 120 | +} |
| 121 | + |
| 122 | +links_ok() { |
| 123 | + (cd "$WORK/$DOCS_DIR" && bash -c "$LINK_CHECK_CMD") >&2 |
| 124 | +} |
| 125 | + |
| 126 | +contains() { |
| 127 | + local needle="$1" item |
| 128 | + shift |
| 129 | + for item in "$@"; do |
| 130 | + [[ "$item" == "$needle" ]] && return 0 |
| 131 | + done |
| 132 | + return 1 |
| 133 | +} |
| 134 | + |
| 135 | +# Rebuilds the scratch tree from the release, skipping HELD commits. |
| 136 | +# Sets INCLUDED, CONFLICTED and CONFLICT_FILES. |
| 137 | +replay() { |
| 138 | + local sha files |
| 139 | + INCLUDED=() |
| 140 | + CONFLICTED=() |
| 141 | + CONFLICT_FILES=() |
| 142 | + in_work reset -q --hard "$RELEASE" |
| 143 | + for sha in "${CANDIDATES[@]+"${CANDIDATES[@]}"}"; do |
| 144 | + if contains "$sha" "${ALREADY[@]+"${ALREADY[@]}"}"; then |
| 145 | + INCLUDED+=("$sha") |
| 146 | + continue |
| 147 | + fi |
| 148 | + contains "$sha" "${HELD[@]+"${HELD[@]}"}" && continue |
| 149 | + if in_work cherry-pick --allow-empty --keep-redundant-commits "$sha" >/dev/null 2>&1; then |
| 150 | + INCLUDED+=("$sha") |
| 151 | + continue |
| 152 | + fi |
| 153 | + files=$(in_work diff --name-only --diff-filter=U | paste -sd, -) |
| 154 | + in_work cherry-pick --abort >/dev/null 2>&1 || in_work reset -q --hard |
| 155 | + CONFLICTED+=("$sha") |
| 156 | + CONFLICT_FILES+=("${files:-unknown}") |
| 157 | + done |
| 158 | +} |
| 159 | + |
| 160 | +without() { |
| 161 | + local drop="$1" item |
| 162 | + for item in "${HELD[@]+"${HELD[@]}"}"; do |
| 163 | + [[ "$item" == "$drop" ]] || echo "$item" |
| 164 | + done |
| 165 | +} |
| 166 | + |
| 167 | +# Holds docs-only commits until the links pass, picking from what the last |
| 168 | +# rebuild applied: newest not-yet-live commits first, then live ones. Then gives |
| 169 | +# held commits another try, oldest first, until a full pass changes nothing. |
| 170 | +hold_until_links_pass() { |
| 171 | + local -a previously_live=() |
| 172 | + local sha |
| 173 | + while IFS= read -r sha; do |
| 174 | + [[ -n "$sha" ]] && previously_live+=("$sha") |
| 175 | + done < <(trailer_values "$LIVE" Docs-Included) |
| 176 | + |
| 177 | + while true; do |
| 178 | + local next="" fallback="" i |
| 179 | + for ((i = ${#INCLUDED[@]} - 1; i >= 0; i--)); do |
| 180 | + sha="${INCLUDED[$i]}" |
| 181 | + contains "$sha" "${ALREADY[@]+"${ALREADY[@]}"}" && continue |
| 182 | + if contains "$sha" "${previously_live[@]+"${previously_live[@]}"}"; then |
| 183 | + [[ -n "$fallback" ]] || fallback="$sha" |
| 184 | + else |
| 185 | + next="$sha" |
| 186 | + break |
| 187 | + fi |
| 188 | + done |
| 189 | + next="${next:-$fallback}" |
| 190 | + [[ -n "$next" ]] || die "the docs at release $RELEASE have broken links" |
| 191 | + HELD+=("$next") |
| 192 | + replay |
| 193 | + links_ok && break |
| 194 | + done |
| 195 | + |
| 196 | + local last="${HELD[${#HELD[@]}-1]}" changed=true |
| 197 | + while $changed; do |
| 198 | + changed=false |
| 199 | + for sha in "${CANDIDATES[@]}"; do |
| 200 | + [[ "$sha" != "$last" ]] || continue |
| 201 | + contains "$sha" "${HELD[@]}" || continue |
| 202 | + local -a saved=("${HELD[@]}") rest=() |
| 203 | + while IFS= read -r i; do |
| 204 | + [[ -n "$i" ]] && rest+=("$i") |
| 205 | + done < <(without "$sha") |
| 206 | + HELD=("${rest[@]+"${rest[@]}"}") |
| 207 | + replay |
| 208 | + if links_ok; then |
| 209 | + changed=true |
| 210 | + else |
| 211 | + HELD=("${saved[@]}") |
| 212 | + fi |
| 213 | + done |
| 214 | + done |
| 215 | + replay |
| 216 | +} |
| 217 | + |
| 218 | +build() { |
| 219 | + RELEASE=$(git rev-parse "$1^{commit}") |
| 220 | + LIVE=$(live_tip) |
| 221 | + |
| 222 | + WORK=$(mktemp -d) |
| 223 | + rmdir "$WORK" |
| 224 | + trap cleanup EXIT |
| 225 | + git worktree add -q --detach "$WORK" "$RELEASE" |
| 226 | + |
| 227 | + CANDIDATES=() |
| 228 | + HELD=() |
| 229 | + ALREADY=() |
| 230 | + local sha |
| 231 | + local baseline="" |
| 232 | + if [[ "$RELEASE" == "$(trailer_values "$LIVE" Docs-Release | head -n1)" ]]; then |
| 233 | + baseline=$(trailer_values "$LIVE" Docs-Baseline) |
| 234 | + elif [[ "$RELEASE" != "$LIVE" ]] && is_ancestor "$LIVE" "$RELEASE"; then |
| 235 | + baseline=$(trailer_values "$LIVE" Docs-Included; trailer_values "$LIVE" Docs-Baseline) |
| 236 | + fi |
| 237 | + while IFS= read -r sha; do |
| 238 | + [[ -n "$sha" ]] && ALREADY+=("$sha") |
| 239 | + done <<<"$baseline" |
| 240 | + while IFS= read -r sha; do |
| 241 | + [[ -n "$sha" ]] || continue |
| 242 | + is_docs_only "$sha" && CANDIDATES+=("$sha") |
| 243 | + done < <(git rev-list --reverse "$RELEASE..$MAIN_REF") |
| 244 | + |
| 245 | + replay |
| 246 | + links_ok || hold_until_links_pass |
| 247 | + |
| 248 | + WAITING=() |
| 249 | + REASONS=() |
| 250 | + local i |
| 251 | + for i in "${!CONFLICTED[@]}"; do |
| 252 | + WAITING+=("${CONFLICTED[$i]}") |
| 253 | + REASONS+=("conflict: ${CONFLICT_FILES[$i]}") |
| 254 | + done |
| 255 | + for sha in "${HELD[@]+"${HELD[@]}"}"; do |
| 256 | + WAITING+=("$sha") |
| 257 | + REASONS+=("broken-link") |
| 258 | + done |
| 259 | + |
| 260 | + publish |
| 261 | +} |
| 262 | + |
| 263 | +publish() { |
| 264 | + local tree previous_waiting new_waiting="" i sha short msg |
| 265 | + tree=$(git -C "$WORK" rev-parse 'HEAD^{tree}') |
| 266 | + previous_waiting=$(trailer_values "$LIVE" Docs-Waiting | awk '{print $1}') |
| 267 | + |
| 268 | + short=$(git rev-parse --short "$RELEASE") |
| 269 | + msg="docs: publish $short + ${#INCLUDED[@]} docs-only commit(s)"$'\n\n'"Docs-Release: $RELEASE" |
| 270 | + for sha in "${ALREADY[@]+"${ALREADY[@]}"}"; do |
| 271 | + msg+=$'\n'"Docs-Baseline: $sha" |
| 272 | + done |
| 273 | + for sha in "${INCLUDED[@]+"${INCLUDED[@]}"}"; do |
| 274 | + contains "$sha" "${ALREADY[@]+"${ALREADY[@]}"}" || msg+=$'\n'"Docs-Included: $sha" |
| 275 | + done |
| 276 | + for i in "${!WAITING[@]}"; do |
| 277 | + msg+=$'\n'"Docs-Waiting: ${WAITING[$i]} ${REASONS[$i]}" |
| 278 | + if ! grep -qx "${WAITING[$i]}" <<<"$previous_waiting"; then |
| 279 | + new_waiting+="${WAITING[$i]}"$'\t'"${REASONS[$i]}"$'\n' |
| 280 | + fi |
| 281 | + done |
| 282 | + |
| 283 | + write_report |
| 284 | + |
| 285 | + local live_release this_waiting |
| 286 | + live_release=$(trailer_values "$LIVE" Docs-Release | head -n1) |
| 287 | + this_waiting=$(printf '%s\n' "${WAITING[@]+"${WAITING[@]}"}" | sed '/^$/d' | sort) |
| 288 | + if [[ "$tree" == "$(git rev-parse "$LIVE^{tree}")" && |
| 289 | + "$RELEASE" == "${live_release:-$LIVE}" && |
| 290 | + "$this_waiting" == "$(sed '/^$/d' <<<"$previous_waiting" | sort)" ]]; then |
| 291 | + echo "publish_sha=" >>"$RESULT_FILE" |
| 292 | + echo "nothing to publish" >&2 |
| 293 | + return 0 |
| 294 | + fi |
| 295 | + |
| 296 | + local -a parents=(-p "$LIVE") |
| 297 | + if is_ancestor "$LIVE" "$RELEASE"; then |
| 298 | + parents=(-p "$RELEASE") |
| 299 | + elif ! is_ancestor "$RELEASE" "$LIVE"; then |
| 300 | + parents+=(-p "$RELEASE") |
| 301 | + fi |
| 302 | + |
| 303 | + local commit |
| 304 | + commit=$(git -c user.name=docs-publish -c user.email=docs-publish@users.noreply.github.com \ |
| 305 | + commit-tree "$tree" "${parents[@]}" -m "$msg") |
| 306 | + echo "publish_sha=$commit" >>"$RESULT_FILE" |
| 307 | + if [[ -n "$live_release" ]]; then |
| 308 | + printf '%s' "$new_waiting" >>"$NEW_WAITING_FILE" |
| 309 | + fi |
| 310 | + echo "$commit" |
| 311 | +} |
| 312 | + |
| 313 | +write_report() { |
| 314 | + local sha i |
| 315 | + { |
| 316 | + echo "### Docs publish" |
| 317 | + echo |
| 318 | + echo "Release commit: \`$RELEASE\` $(git log -1 --format=%s "$RELEASE")" |
| 319 | + echo |
| 320 | + echo "**Docs-only commits published since the release:** ${#INCLUDED[@]}" |
| 321 | + for sha in "${INCLUDED[@]+"${INCLUDED[@]}"}"; do |
| 322 | + echo "- \`${sha:0:9}\` $(git log -1 --format=%s "$sha")" |
| 323 | + done |
| 324 | + echo |
| 325 | + echo "**Waiting for the next docs release:** ${#WAITING[@]}" |
| 326 | + for i in "${!WAITING[@]}"; do |
| 327 | + echo "- \`${WAITING[$i]:0:9}\` $(git log -1 --format=%s "${WAITING[$i]}") (${REASONS[$i]})" |
| 328 | + done |
| 329 | + } >>"$REPORT_FILE" |
| 330 | +} |
| 331 | + |
| 332 | +# Builds from the newest accepted release. If that release can't be built (for |
| 333 | +# example a tag with broken links), falls back to the recorded release so |
| 334 | +# docs-only changes keep publishing, and warns. |
| 335 | +auto() { |
| 336 | + local release recorded |
| 337 | + release=$(release) |
| 338 | + recorded=$(recorded_release) |
| 339 | + if "$0" build "$release"; then |
| 340 | + return 0 |
| 341 | + fi |
| 342 | + [[ "$release" != "$recorded" ]] || die "the recorded release $recorded can't be built" |
| 343 | + echo "::warning::Release $release can't be published (see the error above). Published docs-only changes on top of $recorded instead." |
| 344 | + "$0" build "$recorded" |
| 345 | +} |
| 346 | + |
| 347 | +# Predicts what merging a commit does, treating it as the tip of main. Prints one line: |
| 348 | +# "live", "waiting<TAB><reason>" or "tag" (not docs-only, waits for a release tag). |
| 349 | +preview() { |
| 350 | + local sha out publish reason |
| 351 | + sha=$(git rev-parse "$1^{commit}") |
| 352 | + if ! is_docs_only "$sha"; then |
| 353 | + echo "tag" |
| 354 | + return 0 |
| 355 | + fi |
| 356 | + out=$(MAIN_REF="$sha" RESULT_FILE=/dev/null REPORT_FILE=/dev/null NEW_WAITING_FILE=/dev/null "$0" auto 2>/dev/null) || |
| 357 | + die "couldn't build the docs to preview" |
| 358 | + publish=$(grep -E '^[0-9a-f]{40}$' <<<"$out" | tail -n1 || true) |
| 359 | + if [[ -z "$publish" ]]; then |
| 360 | + echo "live" |
| 361 | + return 0 |
| 362 | + fi |
| 363 | + reason=$(trailer_values "$publish" Docs-Waiting | awk -v s="$sha" '$1 == s { $1 = ""; sub(/^ /, ""); print; exit }') |
| 364 | + if [[ -n "$reason" ]]; then |
| 365 | + printf 'waiting\t%s\n' "$reason" |
| 366 | + else |
| 367 | + echo "live" |
| 368 | + fi |
| 369 | +} |
| 370 | + |
| 371 | +case "${1:-}" in |
| 372 | +auto) auto ;; |
| 373 | +preview) |
| 374 | + [[ $# -eq 2 ]] || die "usage: $0 preview <commit>" |
| 375 | + preview "$2" |
| 376 | + ;; |
| 377 | +check-tag) |
| 378 | + [[ $# -eq 2 ]] || die "usage: $0 check-tag <tag>" |
| 379 | + check_tag "$2" |
| 380 | + ;; |
| 381 | +release) release ;; |
| 382 | +build) |
| 383 | + [[ $# -eq 2 ]] || die "usage: $0 build <release>" |
| 384 | + build "$2" |
| 385 | + ;; |
| 386 | +*) die "usage: $0 auto | preview <commit> | check-tag <tag> | release | build <release>" ;; |
| 387 | +esac |
0 commit comments