Skip to content

Commit d92e842

Browse files
noreplyclaude
andcommitted
fork: follow each stable upstream release with the patch series
fork-follow.yml runs daily. When upstream has a newer stable tag it rebases the series onto it (patches upstream now has drop out as empty), runs the schema check, tests, builds and publishes through fork-release.yml, then moves the series branch with a lease. When the series has commits no fork release has, it releases them. A conflict, schema change, upstream commits inside the series, a red test or a failed build open or update one issue and publish nothing. Pushing a rebase needs a FORK_PUSH_TOKEN secret, because GITHUB_TOKEN may not push commits that change workflow files. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e9afd0c commit d92e842

2 files changed

Lines changed: 278 additions & 0 deletions

File tree

‎.github/scripts/fork-follow.sh‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env bash
2+
# Decides what the follow-upstream workflow does next, and rebases the fork's patch series onto a
3+
# newer stable upstream tag when there is one. Run in a full clone with the series checked out.
4+
# Writes key=value lines to $GITHUB_OUTPUT (stdout without it); a failure's reason goes to $REPORT.
5+
set -euo pipefail
6+
7+
: "${UPSTREAM:?owner/name of the upstream repository}"
8+
REPORT="${REPORT:-fork-follow-report.md}"
9+
OUTPUT="${GITHUB_OUTPUT:-/dev/stdout}"
10+
logs="${RUNNER_TEMP:-$(mktemp -d)}"
11+
upstream_url="${UPSTREAM_URL:-https://github.com/$UPSTREAM.git}"
12+
13+
fail() {
14+
printf '%s\n' "$@" > "$REPORT"
15+
cat "$REPORT" >&2
16+
exit 1
17+
}
18+
19+
stable_tags() {
20+
git ls-remote --tags --refs "$upstream_url" 'v*' \
21+
| sed -n 's|.*refs/tags/\(v[0-9]\+\.[0-9]\+\.[0-9]\+\)$|\1|p' | sort -rV
22+
}
23+
24+
fetch_upstream_tag() {
25+
git fetch --quiet --no-tags "$upstream_url" "+refs/tags/$1:refs/upstream-tags/$1"
26+
git rev-parse "refs/upstream-tags/$1^{commit}"
27+
}
28+
29+
head="$(git rev-parse HEAD)"
30+
target_tag="$(stable_tags | sed -n 1p)"
31+
[[ -n "$target_tag" ]] || fail "$UPSTREAM has no stable release tag."
32+
33+
# The series sits on the newest stable upstream tag it contains.
34+
base_tag=""
35+
while read -r tag; do
36+
commit="$(fetch_upstream_tag "$tag")"
37+
if git merge-base --is-ancestor "$commit" HEAD; then
38+
base_tag="$tag"
39+
base="$commit"
40+
break
41+
fi
42+
done < <(stable_tags)
43+
[[ -n "$base_tag" ]] || fail "The series ($head) contains no stable $UPSTREAM tag."
44+
45+
# Every commit between that tag and the series head must be the fork's own. Upstream commits there
46+
# mean the branch is not a patch series (for example it still equals upstream's default branch).
47+
git fetch --quiet --no-tags "$upstream_url" "+HEAD:refs/upstream-head"
48+
series_count="$(git rev-list --count "$base..HEAD")"
49+
own_count="$(git rev-list --count "$base..HEAD" --not refs/upstream-head)"
50+
if [[ "$series_count" != "$own_count" ]]; then
51+
fail "The series holds $((series_count - own_count)) upstream commits after $base_tag." \
52+
"Reset it to $base_tag plus the fork's own commits; nothing was rebased or published."
53+
fi
54+
55+
node .github/scripts/fork-schema-check.cjs "$base" HEAD > "$logs/schema-check.log" 2>&1 \
56+
|| fail "The series changes the database schema:" '```' "$(cat "$logs/schema-check.log")" '```'
57+
58+
released_head() {
59+
local version="${1#v}" last
60+
last="$(git ls-remote --tags --refs origin "v$version-fork.*" \
61+
| sed -n "s|.*refs/tags/v$version-fork\.\([0-9]\+\)$|\1|p" | sort -n | tail -n1)"
62+
[[ -z "$last" ]] || git ls-remote origin "refs/tags/v$version-fork.$last^{}" "refs/tags/v$version-fork.$last" \
63+
| sort -k2 | tail -n1 | cut -f1
64+
}
65+
66+
if [[ "$target_tag" == "$base_tag" ]]; then
67+
if [[ "$(released_head "$base_tag")" == "$head" ]]; then
68+
action=none
69+
else
70+
action=release
71+
fi
72+
{
73+
echo "action=$action"
74+
echo "base_tag=$base_tag"
75+
echo "candidate=$head"
76+
} >> "$OUTPUT"
77+
exit 0
78+
fi
79+
80+
# A newer upstream release: replay the series onto it. Patches upstream now has become empty and
81+
# are dropped; the release notes list the series, so a dropped patch simply no longer appears.
82+
target="$(fetch_upstream_tag "$target_tag")"
83+
before="$(git log --format=%s "$base..HEAD")"
84+
if ! git -c user.name=t3code-fork -c [email protected] \
85+
rebase --empty=drop --onto "$target" "$base" > "$logs/rebase.log" 2>&1; then
86+
stopped="$(git log -1 --format='%h %s' REBASE_HEAD 2>/dev/null || echo unknown)"
87+
conflicts="$(git diff --name-only --diff-filter=U | sed 's/^/- /')"
88+
git rebase --abort || true
89+
fail "Rebasing the series from $base_tag onto $target_tag stopped at: $stopped" "" \
90+
"Conflicting files:" "$conflicts" "" "Nothing was published; the last fork release stays current."
91+
fi
92+
node .github/scripts/fork-schema-check.cjs "$target" HEAD > "$logs/schema-check.log" 2>&1 \
93+
|| fail "After rebasing onto $target_tag the series changes the database schema:" '```' "$(cat "$logs/schema-check.log")" '```'
94+
dropped="$(comm -23 <(sort <<<"$before") <(git log --format=%s "$target..HEAD" | sort) | paste -sd ';' -)"
95+
{
96+
echo "action=rebase"
97+
echo "base_tag=$target_tag"
98+
echo "previous_tag=$base_tag"
99+
echo "previous_head=$head"
100+
echo "candidate=$(git rev-parse HEAD)"
101+
echo "dropped=$dropped"
102+
} >> "$OUTPUT"

‎.github/workflows/fork-follow.yml‎

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
name: Fork follow upstream
2+
3+
# Daily: keeps this fork's patch series on upstream's newest stable release and publishes it.
4+
# - Upstream has a newer stable tag: rebase the series onto it, test, build, publish, then move the
5+
# series branch to the result.
6+
# - Same tag, but the series has commits no fork release has: test, build and publish them.
7+
# - Anything fails (conflict, schema change, red test, failed build): open or update one issue and
8+
# publish nothing, so the last fork release stays current.
9+
#
10+
# Settings: variable FORK_SERIES_BRANCH (default main). Secret FORK_PUSH_TOKEN: a token that may push
11+
# this repository's contents and workflow files (GITHUB_TOKEN may not push commits that change
12+
# .github/workflows); only a rebase needs it.
13+
on:
14+
schedule:
15+
- cron: "23 6 * * *"
16+
workflow_dispatch:
17+
18+
concurrency:
19+
group: fork-follow
20+
cancel-in-progress: false
21+
22+
permissions:
23+
contents: read
24+
25+
env:
26+
SERIES_BRANCH: ${{ vars.FORK_SERIES_BRANCH || 'main' }}
27+
28+
jobs:
29+
plan:
30+
name: Plan
31+
runs-on: ubuntu-latest
32+
timeout-minutes: 15
33+
outputs:
34+
action: ${{ steps.plan.outputs.action }}
35+
base_tag: ${{ steps.plan.outputs.base_tag }}
36+
candidate: ${{ steps.plan.outputs.candidate }}
37+
previous_head: ${{ steps.plan.outputs.previous_head }}
38+
dropped: ${{ steps.plan.outputs.dropped }}
39+
steps:
40+
- name: Checkout the series
41+
uses: actions/checkout@v6
42+
with:
43+
ref: ${{ env.SERIES_BRANCH }}
44+
fetch-depth: 0
45+
filter: blob:none
46+
token: ${{ secrets.FORK_PUSH_TOKEN || github.token }}
47+
48+
- id: plan
49+
name: Plan and rebase
50+
env:
51+
GH_TOKEN: ${{ github.token }}
52+
REPORT: ${{ runner.temp }}/fork-follow-report.md
53+
run: |
54+
set -euo pipefail
55+
UPSTREAM="$(gh api "repos/$GITHUB_REPOSITORY" --jq '.parent.full_name // empty')"
56+
if [[ -z "$UPSTREAM" ]]; then
57+
echo "$GITHUB_REPOSITORY is not a fork, so there is no upstream to follow." > "$REPORT"
58+
exit 1
59+
fi
60+
export UPSTREAM
61+
bash .github/scripts/fork-follow.sh
62+
63+
- name: Push the rebased series as a candidate
64+
if: steps.plan.outputs.action == 'rebase'
65+
env:
66+
HAS_PUSH_TOKEN: ${{ secrets.FORK_PUSH_TOKEN != '' }}
67+
REPORT: ${{ runner.temp }}/fork-follow-report.md
68+
run: |
69+
set -euo pipefail
70+
if [[ "$HAS_PUSH_TOKEN" != "true" ]]; then
71+
echo "Rebasing onto ${{ steps.plan.outputs.base_tag }} needs the FORK_PUSH_TOKEN secret (contents and workflows write)." > "$REPORT"
72+
exit 1
73+
fi
74+
git push --force origin "HEAD:refs/heads/fork-follow/candidate"
75+
76+
- name: Keep the failure report
77+
if: failure()
78+
uses: actions/upload-artifact@v7
79+
with:
80+
name: fork-follow-report
81+
path: ${{ runner.temp }}/fork-follow-report.md
82+
if-no-files-found: ignore
83+
retention-days: 7
84+
85+
release:
86+
name: Release
87+
needs: [plan]
88+
if: needs.plan.outputs.action == 'release' || needs.plan.outputs.action == 'rebase'
89+
permissions:
90+
actions: read
91+
contents: write
92+
uses: ./.github/workflows/fork-release.yml
93+
secrets: inherit
94+
with:
95+
ref: ${{ needs.plan.outputs.candidate }}
96+
publish: true
97+
98+
advance:
99+
name: Move the series branch
100+
needs: [plan, release]
101+
if: needs.plan.outputs.action == 'rebase' && needs.release.result == 'success'
102+
runs-on: ubuntu-latest
103+
timeout-minutes: 10
104+
steps:
105+
- name: Checkout
106+
uses: actions/checkout@v6
107+
with:
108+
ref: ${{ needs.plan.outputs.candidate }}
109+
fetch-depth: 0
110+
filter: blob:none
111+
token: ${{ secrets.FORK_PUSH_TOKEN }}
112+
113+
# The lease refuses if the branch moved since the plan (a fork PR merged meanwhile); the next
114+
# run rebases again with that change, and the release just published stays valid.
115+
- name: Move the series branch to the released commit
116+
run: |
117+
set -euo pipefail
118+
git push \
119+
--force-with-lease="refs/heads/$SERIES_BRANCH:${{ needs.plan.outputs.previous_head }}" \
120+
origin "${{ needs.plan.outputs.candidate }}:refs/heads/$SERIES_BRANCH"
121+
git push origin --delete fork-follow/candidate || true
122+
123+
report:
124+
name: Report
125+
needs: [plan, release, advance]
126+
if: always() && needs.plan.outputs.action != 'none'
127+
runs-on: ubuntu-latest
128+
timeout-minutes: 5
129+
permissions:
130+
issues: write
131+
steps:
132+
- name: Download the failure report
133+
if: contains(needs.*.result, 'failure')
134+
continue-on-error: true
135+
uses: actions/download-artifact@v8
136+
with:
137+
name: fork-follow-report
138+
path: report
139+
140+
- name: Open, update or close the follow issue
141+
env:
142+
GH_TOKEN: ${{ github.token }}
143+
FAILED: ${{ contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled') }}
144+
BASE_TAG: ${{ needs.plan.outputs.base_tag }}
145+
DROPPED: ${{ needs.plan.outputs.dropped }}
146+
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
147+
run: |
148+
set -euo pipefail
149+
title="Fork cannot follow upstream"
150+
issue="$(gh issue list --repo "$GITHUB_REPOSITORY" --state open --search "\"$title\" in:title" \
151+
--json number,title --jq ".[] | select(.title == \"$title\") | .number" | head -n1)"
152+
if [[ "$FAILED" == "true" ]]; then
153+
{
154+
echo "Run: $RUN_URL"
155+
echo
156+
if [[ -f report/fork-follow-report.md ]]; then
157+
cat report/fork-follow-report.md
158+
else
159+
echo "A test, build or publish job failed${BASE_TAG:+ for $BASE_TAG}; see the run. Nothing was published."
160+
fi
161+
} > body.md
162+
if [[ -n "$issue" ]]; then
163+
gh issue comment "$issue" --repo "$GITHUB_REPOSITORY" --body-file body.md
164+
else
165+
gh issue create --repo "$GITHUB_REPOSITORY" --title "$title" --body-file body.md
166+
fi
167+
exit 0
168+
fi
169+
note="Published the fork release on $BASE_TAG: $RUN_URL"
170+
if [[ -n "$DROPPED" ]]; then
171+
note="$note"$'\n\n'"Dropped because upstream now has them: ${DROPPED//;/, }"
172+
fi
173+
echo "$note" >> "$GITHUB_STEP_SUMMARY"
174+
if [[ -n "$issue" ]]; then
175+
gh issue close "$issue" --repo "$GITHUB_REPOSITORY" --comment "$note"
176+
fi

0 commit comments

Comments
 (0)