Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hack/automation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Workflows run them with a token of the `ai-openshell-sdk-go` GitHub App (`ai-ope
| `core-tags.sh` | `core:tags` | List ai-openshell/openshell release tags newer than the pin (`buf.gen.yaml`, `inputs[0].tag`), semver-sorted with pre-releases below their stable version. |
| `open-bump-pr.sh <tag> [--dry-run]` | `core:bump` | Open the update proposal for one core tag: branch `core/<tag>`, pin change, regenerated bindings, proto diff in the body, labels `core-bump` plus `stable` or `prerelease`. Closes older pre-release proposals. |
| `bump-pr-body.md` | | PR body template for `open-bump-pr.sh`. |
| `fill-template.sh <template> KEY=value...` | | Print a template with its `{{KEY}}` placeholders filled (`fill` in `template.jq`): values are passed to jq as arguments, inserted as is and never scanned again, and a placeholder without an argument fails. Used by `open-bump-pr.sh` and `hack/migration/port-pr.sh`. |
| `fill-template-test.sh` | | Offline tests for `fill-template.sh`; part of `workflows:check`. |
| `core-registry.sh` | | Sourced by `start-gateway.sh` and `open-bump-pr.sh`: `core_registry <version>` prints the registry that holds a core release's images, `quay.io/ai-openshell` for releases since the move and `ghcr.io/nvidia/openshell` for older ones. |
| `merge-bump-pr.sh [--dry-run] [--auto-merge on\|off]` | `core:merge` | Merge stable proposals that change no proto: one commit with a provenance check from the bump App, exactly the pin bump, green checks, no `hold` label. Off unless `CORE_AUTO_MERGE` is `on`. |
| `pin-bump.sh` | | Sourced by `open-bump-pr.sh` and `merge-bump-pr.sh`: `pin_bump_edit <tag> <kind>` makes the pin and README edits of a bump, `pin_bump_tree <base> <tag> <kind>` prints the tree a bump commit with only those edits has, `older <a> <b>` compares release tags. |
Expand All @@ -20,6 +22,7 @@ Workflows run them with a token of the `ai-openshell-sdk-go` GitHub App (`ai-ope
| `rpc-issues.jq` | | The planner for `rpc-issues.sh`: input document in, actions and rendered bodies out; no network. |
| `rpc-rows.jq` | | Turns core's buf image and proto sources into the planner's input rows (signatures, comments, messages, links). |
| `rpc-marker.jq` | | The hidden issue marker, parsed by `rpc-issues.jq` and `update-drift-issue.sh` (jq module). |
| `template.jq` | | `fill`: the single-pass `{{KEY}}` template filler shared by `rpc-issues.jq` and `fill-template.sh` (jq module). |
| `rpc-issue-body.md` | | Issue body template for `rpc-issues.jq`. |
| `rpc-coverage.sh` | | The coverage rule shared by `rpc-issues.sh` and `drift-check.sh` (sourced, not run). |
| `rpc-issues-lib.sh` | | Exceptions list parser and helpers for listing the bot's issues (sourced, not run). |
Expand Down
110 changes: 110 additions & 0 deletions hack/automation/fill-template-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0

# fill-template-test.sh: offline tests for fill-template.sh, the renderer of
# the bump and port PR bodies.
#
# Usage: fill-template-test.sh
#
# Run from the root of an sdk-go clone; part of workflows:check. Checks that
# placeholder text inside a value stays literal (no environment variable such
# as GH_TOKEN leaks into the body, and a value naming its own placeholder
# does not loop), that several placeholders on one line are all filled, that
# an unknown placeholder fails, and that the real templates render without
# leftover placeholders. Exits 1 on any failure.
set -euo pipefail

fill=hack/automation/fill-template.sh
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

fail=0
ok() { echo "ok $1"; }
bad() { echo "FAIL $1: $2"; fail=1; }

# A reintroduced rescanning loop must fail the test, not hang it.
run() {
if command -v timeout >/dev/null; then
timeout 10 "$fill" "$@"
else
perl -e 'alarm 10; exec @ARGV or die "exec: $!"' "$fill" "$@" # macOS has no timeout
fi
}

# Placeholder text in a value is inserted literally, even for a variable
# that is set in the environment and for the value's own placeholder.
printf 'a {{PROTO_DIFF}} b\n' >"$tmp/one.md"
diff=$'+ // {{GH_TOKEN}} {{NEW}}\n+ // {{PROTO_DIFF}}'
if out=$(GH_TOKEN=ghs_SECRET run "$tmp/one.md" PROTO_DIFF="$diff" NEW=v9.9.9); then
want=$'a + // {{GH_TOKEN}} {{NEW}}\n+ // {{PROTO_DIFF}} b'
if [ "$out" = "$want" ]; then ok literal-values; else bad literal-values "got: $out"; fi
case $out in *ghs_SECRET* | *v9.9.9*) bad no-leak "a value was expanded: $out" ;; *) ok no-leak ;; esac
else
bad literal-values "exit $?"
fi

# Several placeholders on one line, special characters in values, and lines
# without placeholders.
printf '{{OLD}}...{{NEW}} {{OLD}}\nplain {line} {{lower}}\n' >"$tmp/two.md"
out=$(run "$tmp/two.md" OLD='v1 & \1' NEW='v2')
want=$'v1 & \\1...v2 v1 & \\1\nplain {line} {{lower}}'
if [ "$out" = "$want" ]; then ok several-placeholders; else bad several-placeholders "got: $out"; fi

# Backslashes in a value arrive unchanged: values must not pass through
# anything that processes escape sequences (such as awk -v).
out=$(run "$tmp/one.md" PROTO_DIFF='a\tb\\c\n')
if [ "$out" = 'a a\tb\\c\n b' ]; then ok backslashes; else bad backslashes "got: $out"; fi

# A template without placeholders comes out byte for byte, including a last
# line without a newline, and no arguments are needed.
printf 'line one\n\nlast line {not a placeholder}' >"$tmp/plain.md"
if run "$tmp/plain.md" >"$tmp/plain.out" && cmp -s "$tmp/plain.md" "$tmp/plain.out"; then
ok same-bytes
else
bad same-bytes "output differs: $(od -c "$tmp/plain.out" | head -3)"
fi

# A placeholder without a KEY=value argument stops the run.
printf 'x {{NEW}} {{GH_TOKEN}}\n' >"$tmp/bad.md"
if GH_TOKEN=ghs_SECRET run "$tmp/bad.md" NEW=v1 >"$tmp/out" 2>"$tmp/err"; then
bad unknown-placeholder "accepted {{GH_TOKEN}}"
elif grep -q 'unknown placeholder {{GH_TOKEN}}' "$tmp/err"; then
ok unknown-placeholder
else
bad unknown-placeholder "stderr: $(cat "$tmp/err")"
fi

# Malformed arguments are refused before anything is rendered, by the
# argument check rather than by a later unknown placeholder.
if run "$tmp/one.md" 'proto_diff=x' >"$tmp/out" 2>"$tmp/err"; then
bad bad-argument "accepted a lowercase key"
elif grep -q 'expected KEY=value' "$tmp/err"; then
ok bad-argument
else
bad bad-argument "stderr: $(cat "$tmp/err")"
fi

# Each caller passes exactly the keys its template uses. The keys are read
# from the caller's fill-template.sh call, so a key dropped from or renamed
# in a caller fails here instead of in the scheduled automation.
caller_keys() { # <script>: the KEY= names of its fill-template.sh call
awk '/fill-template\.sh" "/ { on = 1 } on { print } on && />"\$body"$/ { exit }' "$1" |
{ grep -oE '(^|[[:space:]])[A-Z_]+=' || true; } | tr -d ' =\t' | sort -u # no call: empty, reported below
}
template_keys() { grep -oE '\{\{[A-Z_]+\}\}' "$1" | tr -d '{}' | sort -u; }
check_caller() { # <name> <script> <template>
local keys args=() k out
keys=$(caller_keys "$2")
if [ -z "$keys" ]; then bad "$1" "no fill-template.sh call found in $2"; return; fi
if [ "$keys" != "$(template_keys "$3")" ]; then
bad "$1" "$2 passes [$(tr '\n' ' ' <<<"$keys")], $3 uses [$(template_keys "$3" | tr '\n' ' ')]"
return
fi
for k in $keys; do args+=("$k=value-of-$k"); done
if out=$(run "$3" "${args[@]}") && ! grep -q '{{' <<<"$out"; then ok "$1"; else bad "$1" "render failed or placeholders left"; fi
Comment thread
SDAChess marked this conversation as resolved.
Resolved
}
check_caller bump-pr-body hack/automation/open-bump-pr.sh hack/automation/bump-pr-body.md
check_caller port-pr-body hack/migration/port-pr.sh hack/migration/port-pr-body.md

[ "$fail" -eq 0 ] && echo "fill-template tests passed"
exit "$fail"
36 changes: 36 additions & 0 deletions hack/automation/fill-template.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# fill-template.sh: print a template with its {{KEY}} placeholders filled.
#
# Usage: fill-template.sh <template> KEY=value...
#
# Each KEY=value argument names a placeholder and its value, so the keys are
# listed exactly once. The values reach jq as --arg arguments, never through
# the environment, and are inserted as is: no escape processing, and no
# second scan for placeholders (fill in template.jq). A placeholder in the
# template without an argument stops the run with an error. Used by
# open-bump-pr.sh and hack/migration/port-pr.sh; tested by
# fill-template-test.sh.
set -euo pipefail

die() { echo "ERROR: $*" >&2; exit 1; }
[ $# -ge 1 ] || die "usage: $0 <template> KEY=value..."
tmpl=$1
shift
[ -f "$tmpl" ] || die "template $tmpl is missing"

args=()
for kv in "$@"; do
key=${kv%%=*}
[ "$key" != "$kv" ] && [[ $key =~ ^[A-Z_]+$ ]] || die "expected KEY=value with KEY in A-Z and _, got '${kv:0:40}'"
args+=(--arg "$key" "${kv#*=}")
done

here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# -R -s: the whole template as one string; -j: print it without adding a
# newline, so the output keeps the template's own line endings.
# ${args[@]+...}: bash 3.2 (macOS) treats an empty array as unset under -u.
jq -L "$here" -Rsj ${args[@]+"${args[@]}"} 'include "template"; fill($ARGS.named)' <"$tmpl" ||
die "could not fill $tmpl"
16 changes: 6 additions & 10 deletions hack/automation/open-bump-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,12 @@ fixtures=$(cat "$work/fixtures.txt")
# A fresh tag's images may not be pushed yet; new releases go to Quay.
registry=$(core_registry "${tag#v}") || registry=quay.io/ai-openshell
body="$work/body.md"
OLD="$old" NEW="$tag" KIND="$kind" COMPARE_URL="$compare_url" IMAGE="$registry/gateway:${tag#v}" \
GENERATED="${generated:-no generated files changed}" FIXTURES="${fixtures:-- no fixture changes}" \
PROTO_DIFF="$proto_section" \
awk '{
while (match($0, /\{\{[A-Z_]+\}\}/)) {
key = substr($0, RSTART + 2, RLENGTH - 4)
$0 = substr($0, 1, RSTART - 1) ENVIRON[key] substr($0, RSTART + RLENGTH)
}
print
}' "$body_tmpl" >"$body"
# Values are inserted as is and never scanned again, so upstream text such as
# "{{GH_TOKEN}}" in the proto diff stays literal (fill-template.sh).
"$here/fill-template.sh" "$body_tmpl" \
OLD="$old" NEW="$tag" KIND="$kind" COMPARE_URL="$compare_url" IMAGE="$registry/gateway:${tag#v}" \
GENERATED="${generated:-no generated files changed}" FIXTURES="${fixtures:-- no fixture changes}" \
PROTO_DIFF="$proto_section" >"$body"

echo "Proposal for $tag ($kind), pin $old -> $tag:"
git log --format=' %h %an <%ae> %s' "$base..$branch"
Expand Down
6 changes: 1 addition & 5 deletions hack/automation/rpc-issues.jq
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# whose last close or reopen actor the plan depends on.

include "rpc-marker";
include "template";

def base_labels: ["help wanted", "rpc-coverage"];
def readiness_labels: ["rpc:blocked", "rpc:ready"];
Expand Down Expand Up @@ -64,11 +65,6 @@ def issue_status:
elif .last_actor_is_bot == true then "bot-closed"
else "human-closed" end;

# Replace the {{KEY}} placeholders of a template with the values of an
# object in one pass, so text from core that contains "{{...}}" stays as is.
def fill($vals):
gsub("\\{\\{(?<k>[A-Z_]+)\\}\\}"; $vals[.k] // "{{\(.k)}}");

def normalize: gsub("\r"; "") | sub("\\s+$"; "");

# Group buildability: the least ready state of its methods.
Expand Down
12 changes: 12 additions & 0 deletions hack/automation/template.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
#
# template.jq: fill the {{KEY}} placeholders of a template, shared by
# rpc-issues.jq and fill-template.sh. Load it with
# `jq -L hack/automation 'include "template"; ...'`.

# fill($vals): the input string with every {{KEY}} replaced by $vals[KEY], in
# one pass. Inserted text is never scanned again, so a value that contains
# "{{...}}" (text from core, say) stays as is. A placeholder that $vals has
# no value for is an error, so a typo in a template cannot render as empty.
def fill($vals):
gsub("\\{\\{(?<k>[A-Z_]+)\\}\\}"; $vals[.k] // error("unknown placeholder {{\(.k)}}"));
9 changes: 6 additions & 3 deletions hack/migration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ The migration is tracked in NVIDIA/OpenShell#5031.
| `message-rules.txt` | `git filter-repo --replace-message` rules: bare `#N` becomes `NVIDIA/OpenShell#N`. |
| `verify-tree.sh <core-clone> <freeze-sha> <sdk-go-clone> <commit>` | Prove the imported tree is byte-identical to `sdk/go` at the freeze commit, compare commit, author, and sign-off counts, and reject bare references. |
| `port-pr.sh <core-pr-number>` | Port an open NVIDIA/OpenShell PR into this repository with its original commits and authorship (branch `port/<n>`, label `port`). |
| `port-pr-body.md` | PR body template used by `port-pr.sh`. |
| `port-pr-body.md` | PR body template used by `port-pr.sh`, filled by `hack/automation/fill-template.sh`. |
| `port-pr-test.sh` | Offline test of `port-pr.sh` against a synthetic upstream PR that replaces the renderer; part of `workflows:check`. |
| `FREEZE` | The recorded freeze commit (`sha=<sha>`) and the stable core tag at the freeze (`tag=<tag>`). |

## Import procedure
Expand All @@ -24,6 +25,8 @@ The migration is tracked in NVIDIA/OpenShell#5031.
## Porting procedure

1. From the root of a clean clone: `hack/migration/port-pr.sh <core-pr-number>`. It builds `port/<n>` from `origin/main` and prints the PR body. Nothing is pushed yet.
2. On a conflict, resolve it and `git add` the files. Then run `port-pr.sh --fix-paths <n>`, `git am --continue`, and `port-pr.sh --continue <n>`.
3. Review the branch and the body, then run `port-pr.sh --continue --push <n>` to push and open the PR.
2. On a conflict, resolve it and `git add` the files. Then run `--fix-paths <n>`, `git am --continue`, and `--continue <n>` with the trusted copy of the script whose path `port-pr.sh` prints.
3. Review the branch and the body, then run the trusted copy with `--continue --push <n>` to push and open the PR.

After `git am`, the working tree holds the upstream PR's files, and a PR can change any script in it. `port-pr.sh` therefore copies itself and the files it runs later (`port-pr-body.md`, `hack/automation/fill-template.sh`, `template.jq`) to `$(git rev-parse --git-path port-src/<n>)/trusted/` before it applies anything, renders the PR body with that copy, and runs `--fix-paths` and `--continue` only from it.
4. Port only the top PR of a stack. A PR that contains another PR's commits brings them along.
117 changes: 117 additions & 0 deletions hack/migration/port-pr-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0

# port-pr-test.sh: offline test of port-pr.sh against a synthetic upstream PR.
#
# Usage: port-pr-test.sh
#
# Run from the root of an sdk-go clone; part of workflows:check. Builds a
# throwaway SDK clone and a throwaway core repository whose PR replaces
# hack/automation/fill-template.sh with a script that writes a marker file
# and puts {{GH_TOKEN}} into a commit subject. gh and go are stubbed, and core
# is fetched from the local repository, so nothing touches the network.
# Checks that no code from the PR runs during the port, that the PR body keeps
# the upstream text literally, that --continue from the patched working tree
# is refused, and that --continue from the trusted copy finishes the port.
# Exits 1 on any failure.
set -euo pipefail

repo=$(pwd -P)
tmp=$(mktemp -d)
trap '[ -n "${KEEP:-}" ] || rm -rf "$tmp"' EXIT # KEEP=1 keeps it for debugging
sdk=$tmp/sdk core=$tmp/core bin=$tmp/bin marker=$tmp/pwned

fail=0
ok() { echo "ok $1"; }
bad() { echo "FAIL $1: $2"; fail=1; }

gitq() { git -c user.name=Test -c user.email=test@example.com -c init.defaultBranch=main "$@"; }

# The SDK clone: the migration and template files of this checkout.
toolkit=(hack/migration/port-pr.sh hack/migration/port-pr-body.md hack/migration/FREEZE
hack/automation/fill-template.sh hack/automation/template.jq)
mkdir -p "$sdk"
for f in "${toolkit[@]}"; do
mkdir -p "$sdk/$(dirname "$f")"
cp "$repo/$f" "$sdk/$f"
done
printf 'module github.com/ai-openshell/sdk-go\n\ngo 1.22\n' >"$sdk/go.mod"
printf 'package sdk\n' >"$sdk/sdk.go"
gitq -C "$sdk" init -q
gitq -C "$sdk" add -A
gitq -C "$sdk" commit -q -m "sdk base"

# Core with sdk/go holding the same renderer, and a PR that replaces it.
mkdir -p "$core/sdk/go/hack/automation"
printf 'module github.com/NVIDIA/OpenShell/sdk/go\n\ngo 1.22\n' >"$core/sdk/go/go.mod"
printf 'package sdk\n' >"$core/sdk/go/sdk.go"
cp "$repo/hack/automation/fill-template.sh" "$core/sdk/go/hack/automation/fill-template.sh"
gitq -C "$core" init -q
gitq -C "$core" add -A
gitq -C "$core" commit -q -m "core base"
printf '#!/usr/bin/env bash\ntouch %q\n' "$marker" >"$core/sdk/go/hack/automation/fill-template.sh"
printf 'package sdk\n\n// Added by the PR.\nfunc Added() {}\n' >"$core/sdk/go/added.go"
gitq -C "$core" add -A
gitq -C "$core" commit -q -m 'feat: add Added {{GH_TOKEN}} {{AUTHOR}}'
head=$(git -C "$core" rev-parse HEAD)
git -C "$core" update-ref refs/pull/1/head "$head"
git -C "$core" reset -q --hard HEAD~1

# gh answers the PR metadata; go is never needed with --no-build.
mkdir -p "$bin"
cat >"$bin/gh" <<EOF
#!/usr/bin/env bash
printf '%s\n' '{"state":"OPEN","baseRefName":"main","headRefOid":"$head","title":"feat: add Added","url":"https://example.com/pr/1","author":{"login":"someone"}}'
EOF
printf '#!/usr/bin/env bash\nexit 0\n' >"$bin/go"
chmod +x "$bin/gh" "$bin/go"

port() { # runs in the SDK clone with the stubs first on PATH
(cd "$sdk" && PATH="$bin:$PATH" GH_TOKEN=ghs_SECRET \
GIT_AUTHOR_NAME=Test GIT_AUTHOR_EMAIL=test@example.com \
GIT_COMMITTER_NAME=Test GIT_COMMITTER_EMAIL=test@example.com "$@")
}

# 1. A full port: the PR's replacement renderer must not run.
if port hack/migration/port-pr.sh --base main --core "$core" --no-build 1 >"$tmp/run.out" 2>&1; then
ok port-runs
else
bad port-runs "exit $?: $(tail -n 3 "$tmp/run.out")"
fi
if [ -e "$marker" ]; then bad trusted-renderer "the PR's fill-template.sh ran"; else ok trusted-renderer; fi
if grep -q 'Added() {}' "$sdk/added.go" 2>/dev/null; then ok patches-applied; else bad patches-applied "added.go missing"; fi
body=$(cd "$sdk" && cat "$(git rev-parse --git-path port-src/1)/body.md" 2>/dev/null || true)
case $body in
*'{{GH_TOKEN}} {{AUTHOR}}'*) ok body-literal ;;
*) bad body-literal "upstream subject not literal in the body" ;;
esac
case $body in *ghs_SECRET*) bad body-no-secret "token in the body" ;; *) ok body-no-secret ;; esac

# 2. --continue from the patched working tree is refused.
if port hack/migration/port-pr.sh --base main --continue 1 >"$tmp/cont.out" 2>&1; then
bad continue-refused "the working-tree script ran --continue"
elif grep -q 'not trusted' "$tmp/cont.out"; then
ok continue-refused
else
bad continue-refused "$(tail -n 2 "$tmp/cont.out")"
fi

if port hack/migration/port-pr.sh --fix-paths 1 >"$tmp/fix.out" 2>&1; then
bad fix-paths-refused "the working-tree script ran --fix-paths"
elif grep -q 'not trusted' "$tmp/fix.out"; then
ok fix-paths-refused
else
bad fix-paths-refused "$(tail -n 2 "$tmp/fix.out")"
fi

# 3. --continue from the trusted copy finishes the port.
trusted=$(cd "$sdk" && cd "$(git rev-parse --git-path port-src/1)/trusted" 2>/dev/null && pwd -P || true)
if [ -n "$trusted" ] && port "$trusted/hack/migration/port-pr.sh" --base main --continue --no-build 1 >"$tmp/trusted.out" 2>&1; then
ok continue-trusted
else
bad continue-trusted "$(tail -n 3 "$tmp/trusted.out" 2>/dev/null)"
fi
if [ -e "$marker" ]; then bad trusted-renderer-continue "the PR's fill-template.sh ran"; else ok trusted-renderer-continue; fi

[ "$fail" -eq 0 ] && echo "port-pr tests passed"
exit "$fail"
Loading