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.
118 lines
4.3 KiB
Bash
Executable File
118 lines
4.3 KiB
Bash
Executable File
#!/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 - <<PY
|
|
from pathlib import Path
|
|
import re
|
|
path=Path("scripts/ci.sh")
|
|
text=path.read_text()
|
|
new=re.sub(
|
|
r"(CORE_SHA=\$\{CORE_SHA:-)[0-9a-f]{40}(\})",
|
|
r"\g<1>${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 - <<PY
|
|
import json,os,urllib.request
|
|
api=os.environ.get("MARQDO_GITEA_API","https://git.cflmy.cn/api/v1")
|
|
owner,name,branch,sha,issue="${OWNER}","${NAME}","${BRANCH}","${SOURCE_SHA}","${MARQDO_UPSTREAM_ISSUE}"
|
|
body={
|
|
"title": f"[upstream] pin CORE_SHA to {sha[:12]}",
|
|
"body": f"Dependency pin from Upstream #{issue}.\n\n- CORE_SHA: \`{sha}\`\n- opened by scripts/upstream.sh after local CI lane\n- does not merge automatically",
|
|
"head": branch,
|
|
"base": os.environ.get("CI_COMMIT_BRANCH") or os.environ.get("CI_REPO_DEFAULT_BRANCH") or "master",
|
|
}
|
|
req=urllib.request.Request(
|
|
f"{api}/repos/{owner}/{name}/pulls",
|
|
data=json.dumps(body).encode(),
|
|
method="POST",
|
|
headers={"Authorization":f"token {os.environ['MARQDO_GIT_TOKEN']}","Content-Type":"application/json"},
|
|
)
|
|
with urllib.request.urlopen(req, timeout=60) as resp:
|
|
print(resp.read().decode())
|
|
PY
|
|
fi
|