Repository navigation
fix(automation): fill the bump PR body in a single pass #77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
5e2917c
fix(automation): fill the bump PR body in a single pass
rhuss 6b7a1af
fix(automation): share the template renderer and test it
rhuss 8c938ec
refactor(automation): fill templates with jq instead of awk
rhuss c51986a
fix(migration): run port-pr.sh helpers from a trusted copy
rhuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| 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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)}}")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.