[t-mmyx6d0xfp0x] fix: open a pull request when main is protected #1
No files matched your search
@@ -0,0 +1,13 @@
|
||||
name: test
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
jobs:
|
||||
test:
|
||||
runs-on: fritzlab
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- name: run tests
|
||||
run: bash tests/run
|
||||
@@ -4,9 +4,8 @@ Composite Gitea Action that bumps a `FROM <image>:<tag>` line in a target
|
||||
repo's Dockerfile, then commits and pushes. The target repo's CI fires on
|
||||
that push, rebuilding against the new base.
|
||||
|
||||
The only consumer today is `fritzlab/base` triggering a rebuild of
|
||||
`fritzlab/runner` whenever the base image changes. This action is the
|
||||
generalization of that pattern.
|
||||
Consumers: `fritzlab/base` (→ base-dev), `fritzlab/base-dev`
|
||||
(→ runner, base-agent) and `fritzlab/base-agent` (→ agent).
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -36,7 +35,7 @@ generalization of that pattern.
|
||||
| `image` | yes | — | Image to look for in the FROM line. |
|
||||
| `tag` | no | `github.run_number` | New tag to write into FROM. |
|
||||
| `file` | no | `Dockerfile` | File inside target-repo to edit. |
|
||||
| `token` | yes | — | `CI_BOT_TOKEN` with write to target-repo. |
|
||||
| `token` | yes | — | `CI_BOT_TOKEN` with write to target-repo (push a branch, open a pull request). |
|
||||
| `host` | no | `code.fritzlab.net` | Gitea host without protocol. |
|
||||
| `message` | no | `bump <name> to #<tag>` | Commit message override. |
|
||||
|
||||
@@ -49,6 +48,10 @@ generalization of that pattern.
|
||||
4. If no diff (target already on this tag): exit 0 silently.
|
||||
5. Otherwise commit + push to `main` with rebase-on-conflict retry up to
|
||||
3 times.
|
||||
6. If branch protection refuses the push (`Not allowed to push to protected
|
||||
branch main`), push the commit to `cascade/<name>-<tag>` and open a pull
|
||||
request against `main` instead. A re-run reuses that branch and pull request.
|
||||
The bump then lands through the target's normal review.
|
||||
|
||||
## Notes
|
||||
|
||||
@@ -59,3 +62,8 @@ generalization of that pattern.
|
||||
own. There is no end-to-end orchestration: the upstream repo's CI completes
|
||||
the moment cascade-from pushes, regardless of whether the downstream build
|
||||
succeeds.
|
||||
|
||||
## Tests
|
||||
|
||||
`bash tests/run` drives `cascade.sh` against local bare repositories (a
|
||||
pre-receive hook stands in for branch protection) and a stubbed Gitea API.
|
||||
+2
-52
@@ -32,7 +32,7 @@ inputs:
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- name: Clone, edit, push
|
||||
- name: Clone, edit, push (or open a pull request)
|
||||
shell: bash
|
||||
env:
|
||||
TARGET_REPO: ${{ inputs.target-repo }}
|
||||
@@ -43,54 +43,4 @@ runs:
|
||||
HOST: ${{ inputs.host }}
|
||||
MESSAGE: ${{ inputs.message }}
|
||||
RUN_NUMBER: ${{ github.run_number }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
|
||||
TAG="${TAG_INPUT:-$RUN_NUMBER}"
|
||||
NAME="$(basename "$IMAGE")"
|
||||
MSG="${MESSAGE:-bump ${NAME} to #${TAG}}"
|
||||
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
git clone --depth 1 "https://ci-bot:${TOKEN}@${HOST}/${TARGET_REPO}.git" "$WORK"
|
||||
cd "$WORK"
|
||||
git config user.name ci-bot
|
||||
git config user.email ci-bot@fritzlab.net
|
||||
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "FATAL: ${FILE} not found in ${TARGET_REPO}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Escape forward slashes for sed pattern
|
||||
ESC_IMAGE="${IMAGE//\//\\/}"
|
||||
sed -i "s|^FROM ${IMAGE}:.*|FROM ${IMAGE}:${TAG}|" "$FILE"
|
||||
|
||||
if ! grep -q "^FROM ${IMAGE}:${TAG}\b" "$FILE"; then
|
||||
echo "FATAL: sed did not match a FROM line for ${IMAGE} in ${FILE}"
|
||||
grep "^FROM" "$FILE" || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if git diff --quiet; then
|
||||
echo "${TARGET_REPO} already on ${NAME}:${TAG}, skipping"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
git add "$FILE"
|
||||
git commit -m "$MSG"
|
||||
|
||||
ATTEMPTS=0
|
||||
until git push origin main; do
|
||||
ATTEMPTS=$((ATTEMPTS + 1))
|
||||
if [ "$ATTEMPTS" -ge 3 ]; then
|
||||
echo "FATAL: push to ${TARGET_REPO} failed after ${ATTEMPTS} attempts"
|
||||
exit 1
|
||||
fi
|
||||
echo "push rejected, attempt ${ATTEMPTS}; rebasing and retrying"
|
||||
git pull --rebase origin main
|
||||
sleep $((ATTEMPTS * 2))
|
||||
done
|
||||
|
||||
echo "cascaded ${NAME}:${TAG} → ${TARGET_REPO}"
|
||||
run: bash "${{ github.action_path }}/cascade.sh"
|
||||
Executable
+84
@@ -0,0 +1,84 @@
|
||||
#!/usr/bin/env bash
|
||||
# Bump the FROM line in TARGET_REPO and land it on main. Where branch
|
||||
# protection refuses a direct push, propose the same commit as a pull request.
|
||||
# Inputs arrive as environment variables; see action.yaml.
|
||||
set -euo pipefail
|
||||
|
||||
TAG="${TAG_INPUT:-$RUN_NUMBER}"
|
||||
NAME="$(basename "$IMAGE")"
|
||||
MSG="${MESSAGE:-bump ${NAME} to #${TAG}}"
|
||||
BRANCH="cascade/${NAME}-${TAG}"
|
||||
|
||||
WORK="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK"' EXIT
|
||||
|
||||
git clone --depth 1 "https://ci-bot:${TOKEN}@${HOST}/${TARGET_REPO}.git" "$WORK"
|
||||
cd "$WORK"
|
||||
git config user.name ci-bot
|
||||
git config user.email ci-bot@fritzlab.net
|
||||
|
||||
if [ ! -f "$FILE" ]; then
|
||||
echo "FATAL: ${FILE} not found in ${TARGET_REPO}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sed -i "s|^FROM ${IMAGE}:.*|FROM ${IMAGE}:${TAG}|" "$FILE"
|
||||
|
||||
if ! grep -q "^FROM ${IMAGE}:${TAG}\b" "$FILE"; then
|
||||
echo "FATAL: sed did not match a FROM line for ${IMAGE} in ${FILE}"
|
||||
grep "^FROM" "$FILE" || true
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if git diff --quiet; then
|
||||
echo "${TARGET_REPO} already on ${NAME}:${TAG}, skipping"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
git add "$FILE"
|
||||
git commit -m "$MSG"
|
||||
|
||||
# Protected main: push the commit to its own branch and open a pull request,
|
||||
# so the bump waits for review instead of failing the upstream release.
|
||||
propose() {
|
||||
if git ls-remote --exit-code origin "refs/heads/${BRANCH}" >/dev/null; then
|
||||
echo "${BRANCH} already exists in ${TARGET_REPO}"
|
||||
else
|
||||
git push origin "HEAD:refs/heads/${BRANCH}"
|
||||
fi
|
||||
local body status
|
||||
body="$(jq -n --arg title "$MSG" --arg head "$BRANCH" \
|
||||
--arg body "Cascade from ${IMAGE}:${TAG}. ${TARGET_REPO} protects main, so this bump needs review." \
|
||||
'{title: $title, head: $head, base: "main", body: $body}')"
|
||||
status="$(curl -sS -o "$WORK/pull.json" -w '%{http_code}' -X POST \
|
||||
-H "Authorization: token ${TOKEN}" -H 'Content-Type: application/json' \
|
||||
--data "$body" "https://${HOST}/api/v1/repos/${TARGET_REPO}/pulls")"
|
||||
case "$status" in
|
||||
201) echo "proposed ${NAME}:${TAG} → $(jq -r '.html_url' "$WORK/pull.json")" ;;
|
||||
409) echo "a pull request for ${BRANCH} is already open in ${TARGET_REPO}" ;;
|
||||
*)
|
||||
echo "FATAL: opening a pull request in ${TARGET_REPO} answered HTTP ${status}"
|
||||
cat "$WORK/pull.json" || true
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
ATTEMPTS=0
|
||||
until git push origin main 2>"$WORK/push.err"; do
|
||||
cat "$WORK/push.err" >&2
|
||||
if grep -q 'protected branch' "$WORK/push.err"; then
|
||||
propose
|
||||
exit 0
|
||||
fi
|
||||
ATTEMPTS=$((ATTEMPTS + 1))
|
||||
if [ "$ATTEMPTS" -ge 3 ]; then
|
||||
echo "FATAL: push to ${TARGET_REPO} failed after ${ATTEMPTS} attempts"
|
||||
exit 1
|
||||
fi
|
||||
echo "push rejected, attempt ${ATTEMPTS}; rebasing and retrying"
|
||||
git pull --rebase origin main
|
||||
sleep $((ATTEMPTS * 2))
|
||||
done
|
||||
|
||||
echo "cascaded ${NAME}:${TAG} → ${TARGET_REPO}"
|
||||
Executable
+24
@@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
# Gitea API stub for cascade.sh tests: logs each call, answers CURL_STATUS.
|
||||
set -euo pipefail
|
||||
|
||||
out="" data="" url="" method=GET
|
||||
while [ "$#" -gt 0 ]; do
|
||||
case "$1" in
|
||||
-o) out="$2"; shift 2 ;;
|
||||
-X) method="$2"; shift 2 ;;
|
||||
--data) data="$2"; shift 2 ;;
|
||||
-w|-H) shift 2 ;;
|
||||
http*) url="$1"; shift ;;
|
||||
*) shift ;;
|
||||
esac
|
||||
done
|
||||
|
||||
printf '%s %s %s\n' "$method" "$url" "$data" >>"${CURL_LOG}"
|
||||
status="${CURL_STATUS:-201}"
|
||||
if [ "$status" = 201 ]; then
|
||||
printf '{"html_url":"https://code.test/pulls/7"}' >"$out"
|
||||
else
|
||||
printf '{"message":"stub %s"}' "$status" >"$out"
|
||||
fi
|
||||
printf '%s' "$status"
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/usr/bin/env bash
|
||||
# Tests for cascade.sh against local bare repositories. A pre-receive hook
|
||||
# stands in for Gitea branch protection; tests/bin/curl stands in for the API.
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT="$(cd "$(dirname "$0")/.." && pwd)/cascade.sh"
|
||||
TEST_BIN="$(cd "$(dirname "$0")" && pwd)/bin"
|
||||
IMAGE=code.test/fritzlab/base-dev
|
||||
PASS=0
|
||||
FAIL=0
|
||||
ROOT=$(mktemp -d)
|
||||
trap 'rm -rf "${ROOT}"' EXIT
|
||||
|
||||
ok() { echo "PASS [$1]"; PASS=$((PASS + 1)); }
|
||||
bad() { echo "FAIL [$1]: $2"; echo " output: ${OUT}"; FAIL=$((FAIL + 1)); }
|
||||
|
||||
# fresh_remote <protected:0|1> <from-tag>: a bare fritzlab/target on main.
|
||||
fresh_remote() {
|
||||
rm -rf "${ROOT}/remotes" "${ROOT}/seed"
|
||||
mkdir -p "${ROOT}/remotes/fritzlab"
|
||||
git init -q --bare -b main "${ROOT}/remotes/fritzlab/target.git"
|
||||
git init -q -b main "${ROOT}/seed"
|
||||
printf 'FROM %s:%s\nRUN true\n' "${IMAGE}" "$2" >"${ROOT}/seed/Dockerfile"
|
||||
git -C "${ROOT}/seed" add Dockerfile
|
||||
git -C "${ROOT}/seed" -c user.name=t -c user.email=t@test commit -q -m seed
|
||||
git -C "${ROOT}/seed" push -q "${ROOT}/remotes/fritzlab/target.git" main
|
||||
if [ "$1" = 1 ]; then
|
||||
cat >"${ROOT}/remotes/fritzlab/target.git/hooks/pre-receive" <<'HOOK'
|
||||
#!/usr/bin/env bash
|
||||
while read -r _ _ ref; do
|
||||
if [ "$ref" = refs/heads/main ]; then
|
||||
echo "error: Not allowed to push to protected branch main" >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
HOOK
|
||||
chmod +x "${ROOT}/remotes/fritzlab/target.git/hooks/pre-receive"
|
||||
fi
|
||||
: >"${ROOT}/curl.log"
|
||||
}
|
||||
|
||||
# cascade <tag> [CURL_STATUS]: run the action script; sets OUT and RC.
|
||||
cascade() {
|
||||
RC=0
|
||||
OUT=$(cd "${ROOT}" && env PATH="${TEST_BIN}:$PATH" CURL_LOG="${ROOT}/curl.log" \
|
||||
CURL_STATUS="${2:-201}" TARGET_REPO=fritzlab/target IMAGE="${IMAGE}" TAG_INPUT="$1" \
|
||||
FILE=Dockerfile TOKEN=test-token HOST=code.test MESSAGE= RUN_NUMBER=99 \
|
||||
GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="url.file://${ROOT}/remotes/.insteadOf" \
|
||||
GIT_CONFIG_VALUE_0="https://ci-bot:test-token@code.test/" \
|
||||
bash "${SCRIPT}" 2>&1) || RC=$?
|
||||
}
|
||||
|
||||
remote_from() {
|
||||
git --git-dir="${ROOT}/remotes/fritzlab/target.git" show "$1:Dockerfile" 2>/dev/null | head -1
|
||||
}
|
||||
|
||||
fresh_remote 0 20
|
||||
cascade 22
|
||||
if [ "$RC" -eq 0 ] && [ "$(remote_from main)" = "FROM ${IMAGE}:22" ] && [ ! -s "${ROOT}/curl.log" ]; then
|
||||
ok "open main takes the bump directly"
|
||||
else
|
||||
bad "open main takes the bump directly" "rc=${RC} main=$(remote_from main)"
|
||||
fi
|
||||
|
||||
fresh_remote 1 20
|
||||
cascade 22
|
||||
if [ "$RC" -eq 0 ] && [ "$(remote_from main)" = "FROM ${IMAGE}:20" ] \
|
||||
&& [ "$(remote_from cascade/base-dev-22)" = "FROM ${IMAGE}:22" ] \
|
||||
&& grep -q '^POST https://code.test/api/v1/repos/fritzlab/target/pulls ' "${ROOT}/curl.log" \
|
||||
&& grep -q '"head":"cascade/base-dev-22"' <(tr -d ' \n' <"${ROOT}/curl.log") \
|
||||
&& grep -q '"title":"bumpbase-devto#22"' <(tr -d ' \n' <"${ROOT}/curl.log") \
|
||||
&& grep -q 'proposed base-dev:22' <<<"${OUT}"; then
|
||||
ok "protected main gets a pull request"
|
||||
else
|
||||
bad "protected main gets a pull request" "rc=${RC} main=$(remote_from main) log=$(cat "${ROOT}/curl.log")"
|
||||
fi
|
||||
|
||||
cascade 22 409
|
||||
if [ "$RC" -eq 0 ] && grep -q 'cascade/base-dev-22 already exists' <<<"${OUT}" \
|
||||
&& grep -q 'already open' <<<"${OUT}"; then
|
||||
ok "a re-run reuses the open pull request"
|
||||
else
|
||||
bad "a re-run reuses the open pull request" "rc=${RC}"
|
||||
fi
|
||||
|
||||
fresh_remote 1 20
|
||||
cascade 22 403
|
||||
if [ "$RC" -eq 1 ] && grep -q 'answered HTTP 403' <<<"${OUT}"; then
|
||||
ok "a refused pull request fails the step"
|
||||
else
|
||||
bad "a refused pull request fails the step" "rc=${RC}"
|
||||
fi
|
||||
|
||||
fresh_remote 1 22
|
||||
cascade 22
|
||||
if [ "$RC" -eq 0 ] && grep -q 'already on base-dev:22, skipping' <<<"${OUT}" && [ ! -s "${ROOT}/curl.log" ]; then
|
||||
ok "a target already on the tag is left alone"
|
||||
else
|
||||
bad "a target already on the tag is left alone" "rc=${RC}"
|
||||
fi
|
||||
|
||||
echo "${PASS} passed, ${FAIL} failed"
|
||||
[ "${FAIL}" -eq 0 ]
|
||||
Reference in new issue
Block a user