diff --git a/.github/workflows/pull-requests.yaml b/.github/workflows/pull-requests.yaml index a2fe2102c..be4120ab8 100644 --- a/.github/workflows/pull-requests.yaml +++ b/.github/workflows/pull-requests.yaml @@ -147,7 +147,19 @@ jobs: env: BASE_REF: ${{ github.base_ref }} run: | - git diff --name-only "origin/${BASE_REF}...HEAD" > /tmp/changed.txt + # hack/pr-changed-paths.sh lists a stacked pull request's changes + # against its fork point from main, so the images of the pull requests + # below it are built too. When it cannot, nothing below may trust the + # lists: every image is built, every gated test runs and every package + # keeps its committed refs. + full=false + if ! hack/pr-changed-paths.sh --renames "$BASE_REF" /tmp/changed.txt \ + || ! hack/pr-changed-paths.sh "$BASE_REF" /tmp/changed_norenames.txt; then + echo "::warning title=build-list::could not list the changed paths against the base; building every image and keeping every package's committed refs" + full=true + : > /tmp/changed.txt + : > /tmp/changed_norenames.txt + fi # The docs-only decision reads a RENAME-BLIND list. `git diff # --name-only` applies rename detection and reports a rename as its # new path alone, so moving a code file into docs/ reads as a @@ -167,7 +179,6 @@ jobs: # under-builds on a cross-package rename. That behaviour predates this # line and is not what the docs-only decision is for, so it is left # alone here rather than widened by a side effect. - git diff --name-only --no-renames "origin/${BASE_REF}...HEAD" > /tmp/changed_norenames.txt echo "Changed files:"; sed 's/^/ /' /tmp/changed_norenames.txt # `code` is false when every path is under docs/ or in a CI-only Go # tool that no image builds (cmd/backport-batch, cmd/api-gate): there @@ -175,7 +186,7 @@ jobs: # answers the same question from the PR file list with the same # prefixes, and hack/gate-concurrency-contract.bats holds the two lists # equal. - if grep -qvE '^(docs/|cmd/backport-batch/|cmd/api-gate/)' /tmp/changed_norenames.txt; then + if [ "$full" = true ] || grep -qvE '^(docs/|cmd/backport-batch/|cmd/api-gate/)' /tmp/changed_norenames.txt; then echo "code=true" >> "$GITHUB_OUTPUT" else echo "code=false" >> "$GITHUB_OUTPUT" @@ -187,13 +198,25 @@ jobs: else echo "docs=false" >> "$GITHUB_OUTPUT" fi - backport_batch="$(hack/backport-batch-tests-needed.sh /tmp/changed_norenames.txt)" + if [ "$full" = true ]; then + backport_batch=true + else + backport_batch="$(hack/backport-batch-tests-needed.sh /tmp/changed_norenames.txt)" + fi echo "backport-batch tests needed: $backport_batch" echo "backport_batch=$backport_batch" >> "$GITHUB_OUTPUT" - matrix="$(hack/build-matrix.sh /tmp/changed.txt)" + if [ "$full" = true ]; then + matrix="$(hack/build-matrix.sh FULL)" + else + matrix="$(hack/build-matrix.sh /tmp/changed.txt)" + fi echo "Build matrix: $matrix" echo "matrix=$matrix" >> "$GITHUB_OUTPUT" - talos="$(hack/build-talos-needed.sh /tmp/changed_norenames.txt)" + if [ "$full" = true ]; then + talos=true + else + talos="$(hack/build-talos-needed.sh /tmp/changed_norenames.txt)" + fi echo "Talos build needed: $talos" echo "talos=$talos" >> "$GITHUB_OUTPUT" # Package dirs the PR edited (packages//). Their committed @@ -201,7 +224,11 @@ jobs: # otherwise an upstream image bump in a non-build-unit package would be # silently reverted in e2e/the installer artifact (see # hack/overlay-main-images.sh). Whitespace-separated, no trailing slash. - touched="$(sed -nE 's#^(packages/[^/]+/[^/]+)/.*#\1#p' /tmp/changed.txt | sort -u | tr '\n' ' ')" + if [ "$full" = true ]; then + touched="$(find packages -mindepth 2 -maxdepth 2 -type d | sort | tr '\n' ' ')" + else + touched="$(sed -nE 's#^(packages/[^/]+/[^/]+)/.*#\1#p' /tmp/changed.txt | sort -u | tr '\n' ' ')" + fi echo "Touched packages: $touched" echo "touched=$touched" >> "$GITHUB_OUTPUT" if [ "$matrix" = "[]" ]; then @@ -744,33 +771,16 @@ jobs: # branch, so each base branch has its own artifact to read. # # A base that is neither main nor a line (a stacked PR's feature branch) has - # no artifact, and a `/` in it is not even a valid OCI tag. It reads the - # artifact of whichever of main and the lines it has the fewest commits on - # top of. A tie goes to main. A line with no commit of its own yet is, in - # the graph, main at the cut commit, so a stack on it ties and reads the - # artifact built from main's tip. That artifact carries newer images than - # the line once main moves past the cut. The stack keeps reading it until - # it contains a commit of the line's own. Breaking the tie towards the - # line instead would misroute the far more common branch forked from main - # before the cut, which ties the same way. + # no artifact, and a `/` in it is not even a valid OCI tag. + # hack/pr-artifact-ref.sh picks the artifact such a base reads; the plan + # step lists the changes against the same artifact's line. # # A missing artifact stays a no-op (packages keep their committed refs), which # also covers a line whose first build-release run has not landed yet. - name: Pull base-branch packages tree run: | - ARTIFACT_REF="$BASE_REF" - if [ "$BASE_REF" != "main" ] \ - && ! printf '%s' "$BASE_REF" | grep -qE '^release-[0-9]+\.[0-9]+$'; then - ARTIFACT_REF=main - # A re-run can outlive its base: the payload keeps a branch deleted since. - if git rev-parse --verify --quiet "origin/${BASE_REF}" >/dev/null; then - best="$(git rev-list --count "origin/main..origin/${BASE_REF}")" - for line in $(git for-each-ref --format='%(refname:lstrip=3)' 'refs/remotes/origin/release-*' \ - | grep -E '^release-[0-9]+\.[0-9]+$'); do - n="$(git rev-list --count "origin/${line}..origin/${BASE_REF}")" - if [ "$n" -lt "$best" ]; then best="$n"; ARTIFACT_REF="$line"; fi - done - fi + ARTIFACT_REF="$(hack/pr-artifact-ref.sh "$BASE_REF")" + if [ "$ARTIFACT_REF" != "$BASE_REF" ]; then echo "::notice::Base ${BASE_REF} publishes no packages artifact; overlaying cozystack-packages:${ARTIFACT_REF}." fi rm -rf _out/mainpkgs diff --git a/hack/overlay-main-images_test.bats b/hack/overlay-main-images_test.bats index 68438ffa5..b088c7571 100644 --- a/hack/overlay-main-images_test.bats +++ b/hack/overlay-main-images_test.bats @@ -285,6 +285,8 @@ load test_helper body { sub(/^ /, ""); print }' "$root/.github/workflows/pull-requests.yaml")" [ -n "$script" ] || { echo "cannot extract the pull step's script" >&2; exit 1; } + mkdir -p hack + cp "$root/hack/pr-artifact-ref.sh" hack/ mkdir -p "$w/bin" printf '#!/bin/sh\nprintf "%%s\\n" "$3" > "%s/pulled"\nexit 1\n' "$w" > "$w/bin/flux" chmod +x "$w/bin/flux" diff --git a/hack/pr-artifact-ref.sh b/hack/pr-artifact-ref.sh new file mode 100755 index 000000000..2c4f2eda9 --- /dev/null +++ b/hack/pr-artifact-ref.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# Print the packages artifact a pull request on reads its unbuilt +# images from. Main and the release lines publish their own. Any other base, +# a stacked pull request's feature branch, reads the artifact of whichever of +# main and the lines it has the fewest commits on top of; a tie goes to main. +# A line with no commit of its own yet is main at the cut commit in the graph, +# so a stack on it ties and reads main's, and keeps doing so until it contains +# a commit of the line's own. Breaking the tie towards the line instead would +# misroute the far more common branch forked from main before the cut, which +# ties the same way. A base deleted since the run started reads main's. +set -eu + +if [ "$#" -ne 1 ]; then + echo "usage: pr-artifact-ref.sh " >&2 + exit 2 +fi +base=$1 + +if [ "$base" = main ] || printf '%s\n' "$base" | grep -qE '^release-[0-9]+\.[0-9]+$'; then + printf '%s\n' "$base" + exit 0 +fi +artifact=main +if git rev-parse --verify --quiet "origin/${base}" >/dev/null; then + best="$(git rev-list --count "origin/main..origin/${base}")" + for line in $(git for-each-ref --format='%(refname:lstrip=3)' 'refs/remotes/origin/release-*' \ + | grep -E '^release-[0-9]+\.[0-9]+$'); do + n="$(git rev-list --count "origin/${line}..origin/${base}")" + if [ "$n" -lt "$best" ]; then best="$n"; artifact="$line"; fi + done +fi +printf '%s\n' "$artifact" diff --git a/hack/pr-changed-paths.sh b/hack/pr-changed-paths.sh new file mode 100755 index 000000000..b877492f2 --- /dev/null +++ b/hack/pr-changed-paths.sh @@ -0,0 +1,36 @@ +#!/bin/sh +# Write the list of paths a pull request changes, as its build decisions read +# it. Every package the pull request does not build is filled from the +# packages artifact hack/pr-artifact-ref.sh picks for its base. Main and the +# release lines read their own, so the pull request's own diff is the whole +# question there. A pull request stacked on another one reads the artifact of +# main or a release line, which has none of the stack's changes, so the list +# is taken against the fork point from that artifact's line and the images of +# the lower pull requests are built too. A fork point that cannot be found +# fails, and the caller builds everything. +# +# Usage: pr-changed-paths.sh [--renames] +# The list is rename-blind, both sides of a rename, unless --renames asks for +# git's rename detection. +set -eu + +renames=--no-renames +if [ "${1:-}" = --renames ]; then + renames= + shift +fi +if [ "$#" -ne 2 ]; then + echo "usage: pr-changed-paths.sh [--renames] " >&2 + exit 2 +fi +base=$1 +out=$2 + +artifact="$("$(dirname "$0")/pr-artifact-ref.sh" "$base")" +if [ "$artifact" = "$base" ]; then + range="origin/${base}...HEAD" +else + range="$(git merge-base "origin/${artifact}" HEAD)..HEAD" +fi +# shellcheck disable=SC2086 # an empty $renames is no argument +git diff --name-only $renames "$range" > "$out" diff --git a/hack/pr-changed-paths_test.bats b/hack/pr-changed-paths_test.bats new file mode 100644 index 000000000..161aaa7fb --- /dev/null +++ b/hack/pr-changed-paths_test.bats @@ -0,0 +1,162 @@ +#!/usr/bin/env bats +# Unit tests for hack/pr-changed-paths.sh, the changed-path list a pull request's +# build decisions read, run against small git fixtures. +# +# Run with: bats hack/pr-changed-paths_test.bats + +load test_helper + +setup() { + strict_setup + ROOT="$PWD" + repo=$(mktemp -d) + git -C "$repo" init -q -b main + git -C "$repo" config commit.gpgsign false + mkdir -p "$repo/packages/system/controller" "$repo/hack" + printf 'a\n' > "$repo/packages/system/controller/main.go" + printf 'a\n' > "$repo/hack/x_test.bats" + commit "$repo" main + git -C "$repo" update-ref refs/remotes/origin/main HEAD +} + +teardown() { + rm -rf "$repo" +} + +commit() { + git -C "$1" add -A + git -C "$1" -c user.name=T -c user.email=t@example.org commit -qm "$2" +} + +paths() { + (cd "$repo" && "$ROOT/hack/pr-changed-paths.sh" "$@" "$repo/out.txt") || return + cat "$repo/out.txt" +} + +# The base gains a commit after the pull request forked from it; the +# pull request's list must not include it. +advance() { + git -C "$repo" checkout -q "$1" + mkdir -p "$repo/docs" + printf 'x\n' > "$repo/docs/later.md" + commit "$repo" "$1 later" + git -C "$repo" update-ref "refs/remotes/origin/$1" HEAD + git -C "$repo" checkout -q pr +} + +# A stacked pull request: the parent changes the controller, the child only a test. +stack() { + git -C "$repo" checkout -q -b parent + printf 'b\n' > "$repo/packages/system/controller/main.go" + commit "$repo" parent + git -C "$repo" update-ref refs/remotes/origin/parent HEAD + git -C "$repo" checkout -q -b child + printf 'b\n' > "$repo/hack/x_test.bats" + commit "$repo" child +} + +@test "a pull request on main lists its own changes" { + git -C "$repo" checkout -q -b pr + printf 'b\n' > "$repo/hack/x_test.bats" + commit "$repo" pr + advance main + [ "$(paths main)" = hack/x_test.bats ] +} + +@test "a pull request on a release line lists only what it changed on that line" { + git -C "$repo" checkout -q -b release-1.6 + printf 'b\n' > "$repo/packages/system/controller/main.go" + commit "$repo" line + git -C "$repo" update-ref refs/remotes/origin/release-1.6 HEAD + git -C "$repo" checkout -q -b pr + printf 'b\n' > "$repo/hack/x_test.bats" + commit "$repo" pr + advance release-1.6 + [ "$(paths release-1.6)" = hack/x_test.bats ] +} + +@test "a stacked pull request lists the stack's changes against main as well as its own" { + stack + [ "$(paths parent)" = "$(printf 'hack/x_test.bats\npackages/system/controller/main.go')" ] +} + +@test "a branch only named like a release line counts as stacked" { + git -C "$repo" checkout -q -b release-1.8.1 + printf 'b\n' > "$repo/packages/system/controller/main.go" + commit "$repo" parent + git -C "$repo" update-ref refs/remotes/origin/release-1.8.1 HEAD + git -C "$repo" checkout -q -b pr + printf 'b\n' > "$repo/hack/x_test.bats" + commit "$repo" pr + [ "$(paths release-1.8.1)" = "$(printf 'hack/x_test.bats\npackages/system/controller/main.go')" ] +} + +@test "a stacked pull request whose fork point from main is unknown fails" { + stack + git -C "$repo" update-ref -d refs/remotes/origin/main + if paths parent 2>/dev/null; then + echo "listed paths without a fork point from main" >&2 + false + fi +} + +@test "the rename-blind list keeps both sides of a rename, the other only the new path" { + git -C "$repo" checkout -q -b pr + git -C "$repo" mv packages/system/controller/main.go hack/main.go + commit "$repo" rename + [ "$(paths main)" = "$(printf 'hack/main.go\npackages/system/controller/main.go')" ] + [ "$(paths --renames main)" = hack/main.go ] +} + +@test "a wrong argument count is a usage error" { + rc=0 + (cd "$repo" && "$ROOT/hack/pr-changed-paths.sh" main 2>/dev/null) || rc=$? + [ "$rc" -eq 2 ] +} + +@test "a pull request stacked on a release line lists changes against that line, not against main" { + git -C "$repo" checkout -q -b release-1.6 + printf 'b\n' > "$repo/packages/system/controller/main.go" + commit "$repo" line + git -C "$repo" update-ref refs/remotes/origin/release-1.6 HEAD + git -C "$repo" checkout -q -b parent + mkdir -p "$repo/packages/apps/db" + printf 'a\n' > "$repo/packages/apps/db/values.yaml" + commit "$repo" parent + git -C "$repo" update-ref refs/remotes/origin/parent HEAD + git -C "$repo" checkout -q -b child + printf 'b\n' > "$repo/hack/x_test.bats" + commit "$repo" child + [ "$(paths parent)" = "$(printf 'hack/x_test.bats\npackages/apps/db/values.yaml')" ] +} + +# The plan step must not trust any list when the script fails: it builds every +# image and Talos, runs every gated test and keeps every package's refs. +@test "the plan step trusts none of the lists when they cannot be computed" { + sandbox=$(mktemp -d) + mkdir "$sandbox/hack" + for f in "$ROOT"/hack/*; do ln -s "$f" "$sandbox/hack/"; done + rm "$sandbox/hack/pr-changed-paths.sh" + printf '#!/bin/sh\nexit 1\n' > "$sandbox/hack/pr-changed-paths.sh" + chmod +x "$sandbox/hack/pr-changed-paths.sh" + ln -s "$ROOT/Makefile" "$sandbox/Makefile" + (cd "$ROOT" && find packages -mindepth 1 -maxdepth 2 -type d) | (cd "$sandbox" && xargs mkdir -p) + # The step's scratch files move into the sandbox, so nothing left in /tmp + # by another run can stand in for a list the step failed to write. + yq '.jobs[].steps[]? | select(.id == "p") | .run' "$ROOT/.github/workflows/pull-requests.yaml" \ + | sed "s#/tmp/#$sandbox/#g" > "$sandbox/step.sh" + rc=0 + (cd "$sandbox" && GITHUB_OUTPUT="$sandbox/out" BASE_REF=parent bash -eo pipefail step.sh > "$sandbox/log" 2>&1) || rc=$? + [ "$rc" -eq 0 ] || { cat "$sandbox/log" >&2; false; } + out=$(cat "$sandbox/out") + full=$(cd "$sandbox" && hack/build-matrix.sh FULL) + every=$(cd "$sandbox" && find packages -mindepth 2 -maxdepth 2 -type d | sort | tr '\n' ' ') + rm -rf "$sandbox" + printf '%s\n' "$out" | grep -qx 'code=true' + printf '%s\n' "$out" | grep -qx 'talos=true' + printf '%s\n' "$out" | grep -qx 'backport_batch=true' + printf '%s\n' "$out" | grep -qxF "matrix=$full" + [ -n "$every" ] + printf '%s\n' "$out" | grep -qxF "touched=$every" + printf '%s\n' "$out" | grep -qx 'any=true' +}