From 510e875b0b6e18f9aa3c4b4d703699063b9196fe Mon Sep 17 00:00:00 2001 From: marqdo-reconcile Date: Fri, 27 Nov 2026 19:49:41 +0800 Subject: [PATCH] ci: add protected upstream workflow for dependency pin validation Issue #10 CI-08: re-read Upstream task Issues, run this repo's CI lane, and optionally open a CORE_SHA pin PR without trusting release notes. --- .woodpecker/upstream.yml | 18 ++++++ scripts/upstream.sh | 117 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 .woodpecker/upstream.yml create mode 100755 scripts/upstream.sh diff --git a/.woodpecker/upstream.yml b/.woodpecker/upstream.yml new file mode 100644 index 0000000..46e7c99 --- /dev/null +++ b/.woodpecker/upstream.yml @@ -0,0 +1,18 @@ +# Consumer upstream validation (issue #10 ยง8.2 / CI-08). +when: + - event: [manual] + branch: [main, master] + +steps: + upstream: + image: git.cflmy.cn/cflmy/marqdo-ci-rust:3.0.0-rc.5 + environment: + MARQDO_BIN: /opt/marqdo/marqdo-3.0.0-rc.5-x86_64-unknown-linux-gnu/marqdo + MARQDO_STDLIB_DIR: /opt/marqdo/marqdo-3.0.0-rc.5-x86_64-unknown-linux-gnu/stdlib + MARQDO_TEST_BIN: /opt/marqdo/marqdo-3.0.0-rc.5-x86_64-unknown-linux-gnu/marqdo + MARQDO_TOOL_HOME: /tmp/marqdo-tool-home + MARQDO_GIT_TOKEN: + from_secret: GITEA_TOKEN + MARQDO_UPSTREAM_APPLY: "1" + commands: + - sh scripts/upstream.sh diff --git a/scripts/upstream.sh b/scripts/upstream.sh new file mode 100755 index 0000000..94f9b75 --- /dev/null +++ b/scripts/upstream.sh @@ -0,0 +1,117 @@ +#!/bin/sh +# Validate an Upstream task Issue and, when safe, open a dependency pin PR. +# +# Inputs: +# MARQDO_UPSTREAM_ISSUE hint issue number from root reconcile +# MARQDO_GIT_TOKEN read Issues; optional write for pin PR +# MARQDO_UPSTREAM_APPLY set to 1 to push pin branch + open PR +# CI entry: scripts/ci.sh or scripts/test.sh +# +# Trusted inputs are only the Issue fields re-read from Gitea (source_sha/tag). +# Release notes are never executed. +set -eu +ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +cd "$ROOT" + +API_BASE=${MARQDO_GITEA_API:-https://git.cflmy.cn/api/v1} +OWNER=${CI_REPO_OWNER:-} +NAME=${CI_REPO_NAME:-} +if [ -z "$OWNER" ] || [ -z "$NAME" ]; then + case "${CI_REPO:-}" in + */*) OWNER=$(echo "$CI_REPO" | cut -d/ -f1); NAME=$(echo "$CI_REPO" | cut -d/ -f2) ;; + esac +fi + +SOURCE_SHA="" +ISSUE_JSON="" +if [ -n "${MARQDO_UPSTREAM_ISSUE:-}" ] && [ -n "${MARQDO_GIT_TOKEN:-}" ] && [ -n "$OWNER" ] && [ -n "$NAME" ]; then + echo "upstream hint issue=${MARQDO_UPSTREAM_ISSUE}" + ISSUE_JSON=$(curl -fsS -H "Authorization: token ${MARQDO_GIT_TOKEN}" \ + "${API_BASE}/repos/${OWNER}/${NAME}/issues/${MARQDO_UPSTREAM_ISSUE}") + echo "$ISSUE_JSON" | python3 - <<'PY' +import json,sys,re +issue=json.load(sys.stdin) +body=issue.get("body") or "" +if "marqdo:upstream-task key=" not in body: + raise SystemExit("upstream issue missing task marker; refusing to run") +m=re.search(r"upstream source_sha: `([0-9a-f]{40})`", body) +if not m: + raise SystemExit("upstream issue missing source_sha") +open("/tmp/upstream-source-sha","w").write(m.group(1)) +print("validated upstream task issue", issue.get("number"), "sha", m.group(1)) +PY + SOURCE_SHA=$(cat /tmp/upstream-source-sha) +fi + +# Optional CORE_SHA pin (Knowledge / tools that clone a pinned CLI commit). +PIN_CHANGED=0 +if [ -n "$SOURCE_SHA" ] && [ -f scripts/ci.sh ] && grep -q 'CORE_SHA=' scripts/ci.sh; then + CURRENT=$(grep -E '^CORE_SHA=\$\{CORE_SHA:-' scripts/ci.sh | head -1 | sed -E 's/.*:-([0-9a-f]{40})\}.*/\1/') + if [ -n "$CURRENT" ] && [ "$CURRENT" != "$SOURCE_SHA" ]; then + echo "pin CORE_SHA ${CURRENT} -> ${SOURCE_SHA}" + python3 - <${SOURCE_SHA}\2", + text, + count=1, +) +if new==text: + raise SystemExit("failed to rewrite CORE_SHA pin") +path.write_text(new) +PY + PIN_CHANGED=1 + fi +fi + +if [ -x scripts/ci.sh ] || [ -f scripts/ci.sh ]; then + # Prefer the cheap test lane when present; image already supplies MARQDO_BIN. + if grep -q 'lane_test\|scripts/ci.sh test' scripts/ci.sh 2>/dev/null; then + sh scripts/ci.sh test + else + sh scripts/ci.sh + fi +elif [ -x scripts/test.sh ] || [ -f scripts/test.sh ]; then + sh scripts/test.sh +else + echo "upstream.sh: no scripts/ci.sh or scripts/test.sh" >&2 + exit 1 +fi + +if [ "$PIN_CHANGED" = "1" ] && [ "${MARQDO_UPSTREAM_APPLY:-0}" = "1" ]; then + if [ -z "${MARQDO_GIT_TOKEN:-}" ] || [ -z "$OWNER" ] || [ -z "$NAME" ]; then + echo "pin changed but cannot open PR without token/repo" >&2 + exit 1 + fi + BRANCH="upstream/cli-${SOURCE_SHA%????????????????????????????}" + BRANCH="upstream/cli-$(echo "$SOURCE_SHA" | cut -c1-12)" + git config user.email "marqdo-reconcile@users.noreply.cflmy.cn" + git config user.name "marqdo-reconcile" + git checkout -B "$BRANCH" + git add scripts/ci.sh + git commit -m "upstream: pin CORE_SHA to ${SOURCE_SHA}" + git push -u "https://oauth2:${MARQDO_GIT_TOKEN}@git.cflmy.cn/${OWNER}/${NAME}.git" "HEAD:${BRANCH}" + python3 - <