@jeiemgi/cckit 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,301 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # gh-rest-test.sh — the lifecycle verbs work without GraphQL (#396). In rest mode, cckit pr, merge,
4
+ # close, new, effort new, effort pr and effort close send only `gh api repos/…` calls — never
5
+ # `gh api graphql`, `gh pr …` or `gh issue …` — and every board step is skipped with one notice.
6
+ # The default transport is unchanged. Hermetic: a stubbed gh that answers like the REST API (it
7
+ # applies --jq with the real jq), a throwaway repo and a bare remote.
8
+ # Run: bash scripts/lib/gh-rest-test.sh
9
+ # errors: strict — a test runner: rc 1 on any failed assertion
10
+ set -u
11
+ LIB="$(cd "$(dirname "$0")" && pwd)"
12
+ # shellcheck source=/dev/null
13
+ . "$LIB/test-env.sh" && test_env_seal
14
+ unset CLAUDE_CODE_REMOTE CCKIT_GH_TRANSPORT
15
+ fail=0
16
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
17
+ tc() { if grep -qE -- "$2" "$1"; then echo "ok: $3"; else echo "FAIL: $3 (no /$2/ in $1)"; fail=1; fi; }
18
+ command -v jq >/dev/null 2>&1 || { echo "gh-rest-test: jq required" >&2; exit 1; }
19
+ command -v git >/dev/null 2>&1 || { echo "gh-rest-test: git required" >&2; exit 1; }
20
+
21
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
22
+ export GH_LOG="$tmp/gh.log" GH_N="$tmp/n" GH_CLOSED="" GH_HEAD="fix/9-x" GH_BASE="develop" GH_MERGEABLE="true"
23
+ export GH_TITLE="Fix the thing" GH_LABELS='[{"name":"kind:fix"},{"name":"role:tech-lead"}]'
24
+ : > "$GH_LOG"
25
+
26
+ # rest_only <log> <label> — every gh call in <log> is `gh api` on a repos/ endpoint; none is graphql,
27
+ # `gh pr …` or `gh issue …`. (The stub logs one call per line: newlines in a body become \036.)
28
+ rest_only() {
29
+ local bad
30
+ bad="$(grep -vE '^api (-[^ ]+ [^ ]+ )*repos/' "$1" | head -3)"
31
+ if [ -n "$bad" ]; then echo "FAIL: $2 — non-REST gh calls: $bad"; fail=1; else echo "ok: $2"; fi
32
+ if grep -q graphql "$1"; then echo "FAIL: $2 — a graphql call was made"; fail=1; fi
33
+ }
34
+
35
+ mkdir -p "$tmp/bin"
36
+ cat > "$tmp/bin/gh" <<'SH'
37
+ #!/usr/bin/env bash
38
+ # A REST-shaped gh: logs every call; `gh api <endpoint>` answers a fixture, through --jq when given.
39
+ { printf '%s' "$*" | tr '\n' '\036'; echo; } >> "$GH_LOG"
40
+ if [ "$1" != api ]; then # the default (graphql) transport's subcommands
41
+ case "$1 $2" in
42
+ "issue view") printf '{"state":"OPEN","title":"%s","labels":%s,"milestone":{"title":"M1"},"body":"","projectItems":[]}\n' "$GH_TITLE" "$GH_LABELS" ;;
43
+ "issue create") echo "https://github.com/o/r/issues/50" ;;
44
+ "pr create") echo "https://github.com/o/r/pull/7" ;;
45
+ "pr view") printf '{"mergeable":"MERGEABLE","mergeStateStatus":"CLEAN","baseRefName":"%s","headRefName":"%s"}\n' "$GH_BASE" "$GH_HEAD" ;;
46
+ esac
47
+ exit 0
48
+ fi
49
+ shift; ep="$1"; shift
50
+ method=""; jqx=""; fields=""; accept=""
51
+ while [ $# -gt 0 ]; do
52
+ case "$1" in
53
+ -X|--method) method="$2"; shift 2 ;;
54
+ --jq|-q) jqx="$2"; shift 2 ;;
55
+ -f|-F|--raw-field|--field) fields=1; shift 2 ;;
56
+ -H|--header) accept="$2"; shift 2 ;;
57
+ *) shift ;;
58
+ esac
59
+ done
60
+ [ -n "$method" ] || { [ -n "$fields" ] && method=POST || method=GET; }
61
+ # GH_FAIL_EP=<glob>: that endpoint fails the way real gh does — the error JSON on stdout (--jq is
62
+ # not applied), a message on stderr, rc 1.
63
+ case "$ep" in ${GH_FAIL_EP:-//never//})
64
+ printf '{"message":"Not Found","documentation_url":"https://docs.github.com/rest","status":"404"}'
65
+ echo "gh: Not Found (HTTP 404)" >&2; exit 1 ;;
66
+ esac
67
+ next() { n=$(( $(cat "$GH_N" 2>/dev/null || echo 49) + 1 )); echo "$n" > "$GH_N"; echo "$n"; }
68
+ state_of() { case " $GH_CLOSED " in *" $1 "*) echo closed ;; *) echo open ;; esac; }
69
+ json='{}'
70
+ case "$method $ep" in
71
+ "GET repos/o/r/milestones") json='[{"number":2,"title":"M0"},{"number":3,"title":"M1"}]' ;;
72
+ "POST repos/o/r/issues") n="$(next)"; json="{\"number\":$n,\"html_url\":\"https://github.com/o/r/issues/$n\",\"user\":{\"login\":\"me\"}}" ;;
73
+ "GET repos/o/r/issues") json='[{"number":99,"title":"[Effort] 99 · demo effort"},{"number":5,"title":"PR","pull_request":{}}]' ;;
74
+ "GET repos/o/r/issues/"*/sub_issues) json='[{"number":101,"state":"closed"},{"number":102,"state":"closed"}]' ;;
75
+ "GET repos/o/r/issues/"*/timeline)
76
+ sub="${ep#repos/o/r/issues/}"; sub="${sub%/timeline}"
77
+ json="[{\"event\":\"cross-referenced\",\"source\":{\"issue\":{\"number\":6$sub,\"body\":\"mentions #$sub\",\"repository_url\":\"https://api.github.com/repos/o/r\",\"pull_request\":{\"merged_at\":\"2026-01-01\"}}}},
78
+ {\"event\":\"cross-referenced\",\"source\":{\"issue\":{\"number\":5$sub,\"body\":\"Closes #$sub\",\"repository_url\":\"https://api.github.com/repos/o/r\",\"pull_request\":{\"merged_at\":\"2026-01-01\"}}}}]" ;;
79
+ "GET repos/o/r/issues/"*)
80
+ n="${ep##*/}"
81
+ json="{\"number\":$n,\"title\":\"$GH_TITLE\",\"state\":\"$(state_of "$n")\",\"body\":\"## Goal\\nship it\",\"labels\":$GH_LABELS,\"milestone\":{\"number\":3,\"title\":\"M1\"},\"user\":{\"login\":\"me\"}}" ;;
82
+ "POST repos/o/r/pulls") json='{"number":7,"html_url":"https://github.com/o/r/pull/7","user":{"login":"me"}}' ;;
83
+ "GET repos/o/r/pulls") json="[{\"number\":9,\"head\":{\"ref\":\"$GH_HEAD\"},\"state\":\"open\",\"merged_at\":null}]" ;;
84
+ "GET repos/o/r/pulls/"*)
85
+ n="${ep##*/}"
86
+ case "$accept" in *diff*) printf 'diff --git a/src/app.ts b/src/app.ts\n+app\n'; exit 0 ;; esac
87
+ json="{\"number\":$n,\"title\":\"feat: sub $n\",\"mergeable\":$GH_MERGEABLE,\"mergeable_state\":\"clean\",\"base\":{\"ref\":\"$GH_BASE\"},\"head\":{\"ref\":\"$GH_HEAD\"},\"body\":\"Closes #42\\nFixes #43\\nsee #44\",\"state\":\"open\",\"merged_at\":null,\"merge_commit_sha\":\"abc1234def\"}" ;;
88
+ "PUT repos/o/r/pulls/"*/merge) json='{"merged":true}' ;;
89
+ esac
90
+ if [ -n "$jqx" ]; then printf '%s' "$json" | jq -r -c "$jqx"; else printf '%s\n' "$json"; fi
91
+ SH
92
+ chmod +x "$tmp/bin/gh"
93
+ export PATH="$tmp/bin:$PATH" KIT_REPO="o/r" EFFORT_REPO="o/r" KIT_BASE_BRANCH="develop" KIT_WT_INSTALL=0
94
+ unset KIT_CONFIG
95
+
96
+ # shellcheck source=/dev/null
97
+ . "$LIB/gh-rest.sh"
98
+
99
+ # ── the chooser ───────────────────────────────────────────────────────────────────────────────────
100
+ t "default transport is graphql" "$(cckit_gh_transport)" "graphql"
101
+ t "a cloud session defaults to rest" "$(CLAUDE_CODE_REMOTE=true cckit_gh_transport)" "rest"
102
+ t "CCKIT_GH_TRANSPORT=graphql wins in the cloud" "$(CLAUDE_CODE_REMOTE=true CCKIT_GH_TRANSPORT=graphql cckit_gh_transport)" "graphql"
103
+ t "CCKIT_GH_TRANSPORT=rest forces rest locally" "$(CCKIT_GH_TRANSPORT=rest cckit_gh_transport)" "rest"
104
+ t "an unknown value falls to the default" "$(CCKIT_GH_TRANSPORT=soap cckit_gh_transport 2>/dev/null)" "graphql"
105
+ tc <(CCKIT_GH_TRANSPORT=soap cckit_gh_transport 2>&1 >/dev/null) "must be rest or graphql" "an unknown value warns"
106
+
107
+ # ── pure helpers ──────────────────────────────────────────────────────────────────────────────────
108
+ t "closing refs are read from a PR body" \
109
+ "$(printf 'Closes #42\nfixes #43 and resolves #7\nsee #44\nCloses #42\n' | gh_rest_closing_refs | tr '\n' ' ')" "7 42 43 "
110
+
111
+ # ── board steps in rest mode ──────────────────────────────────────────────────────────────────────
112
+ : > "$GH_LOG"
113
+ out="$(CCKIT_GH_TRANSPORT=rest bash -c '
114
+ . "$1/gh-project.sh"
115
+ load_project_ids; echo "rc=$?"
116
+ project_add_item NODE; echo "rc=$?"
117
+ project_set_single_select I F O; echo "rc=$?"
118
+ project_set_text I F T; echo "rc=$?"
119
+ project_find_item_by_issue 5; echo "rc=$?"
120
+ project_issue_status 5; echo "rc=$?"' _ "$LIB" 2>"$tmp/board.err")"
121
+ t "every board function returns 0 in rest mode" "$(printf '%s' "$out" | grep -c '^rc=0$')" "6"
122
+ t "the board functions print nothing on stdout" "$(printf '%s' "$out" | grep -vc '^rc=')" "0"
123
+ t "the notice is printed once" "$(grep -c 'board step skipped' "$tmp/board.err")" "1"
124
+ tc "$tmp/board.err" "^cckit: board step skipped \(GraphQL unavailable; the project's built-in workflows move the card\)$" "the notice wording"
125
+ t "no gh call is made for the board" "$(wc -l < "$GH_LOG" | tr -d ' ')" "0"
126
+
127
+ # ── the task verbs ────────────────────────────────────────────────────────────────────────────────
128
+ git init -q --bare "$tmp/remote.git"
129
+ git clone -q "$tmp/remote.git" "$tmp/work" 2>/dev/null
130
+ cd "$tmp/work" || exit 1
131
+ git config user.email t@t; git config user.name t
132
+ git checkout -q -b develop; git commit -q --allow-empty -m init; git push -q origin develop
133
+ # shellcheck source=/dev/null
134
+ . "$LIB/gh-project.sh"
135
+ # shellcheck source=/dev/null
136
+ . "$LIB/kit-task-ops.sh"
137
+ export KIT_PROJECTS_V2=true CCKIT_GH_TRANSPORT=rest
138
+
139
+ # cckit pr
140
+ git checkout -q -b fix/42-the-thing
141
+ echo x > a.txt; git add a.txt; git commit -q -m wip
142
+ : > "$GH_LOG"
143
+ unset _CCKIT_BOARD_NOTICE_DONE; kto_task_pr 42 "Did the thing." >"$tmp/out" 2>"$tmp/err"; rc=$?
144
+ t "pr: rc 0 over REST" "$rc" "0"
145
+ rest_only "$GH_LOG" "pr: only REST calls"
146
+ tc "$GH_LOG" '^api repos/o/r/issues/42 ' "pr: reads the issue over REST"
147
+ tc "$GH_LOG" '^api repos/o/r/pulls -X POST .*-f title=fix: Fix the thing -f head=fix/42-the-thing -f base=develop' "pr: opens the PR with POST pulls"
148
+ tc "$GH_LOG" '^api repos/o/r/issues/7 -X PATCH -f labels\[\]=kind:fix -f labels\[\]=role:tech-lead -f assignees\[\]=me -F milestone=3' "pr: labels, assignee and milestone in one PATCH"
149
+ tc "$tmp/out" 'https://github.com/o/r/pull/7' "pr: prints the PR url"
150
+ t "pr: the board notice is printed once" "$(grep -c 'board step skipped' "$tmp/err")" "1"
151
+
152
+ # cckit new
153
+ : > "$GH_LOG"
154
+ unset _CCKIT_BOARD_NOTICE_DONE; num="$(kto_task_new "Add a thing" tech-lead task p2 M1 "" "" "Body." 2>"$tmp/err" | tail -1)"; rc=$?
155
+ t "new: rc 0 over REST" "$rc" "0"
156
+ rest_only "$GH_LOG" "new: only REST calls"
157
+ tc "$GH_LOG" '^api repos/o/r/issues -X POST -f title=Add a thing .*-f labels\[\]=kind:task -f labels\[\]=priority:p2 -f labels\[\]=role:tech-lead -F milestone=3' "new: POST issues with labels and the milestone number"
158
+ tc "$GH_LOG" "^api repos/o/r/issues/$num/assignees -X POST -f assignees\[\]=me" "new: assigns the issue to its author"
159
+ t "new: echoes the new number" "$num" "50"
160
+ t "new: no 'NOT added to the board' warning" "$(grep -c 'NOT added' "$tmp/err")" "0"
161
+ t "new: the board notice is printed once" "$(grep -c 'board step skipped' "$tmp/err")" "1"
162
+
163
+ # cckit close
164
+ : > "$GH_LOG"
165
+ unset _CCKIT_BOARD_NOTICE_DONE; kto_task_close 5 "Shipped." >/dev/null 2>"$tmp/err"; rc=$?
166
+ t "close: rc 0 over REST" "$rc" "0"
167
+ rest_only "$GH_LOG" "close: only REST calls"
168
+ tc "$GH_LOG" '^api repos/o/r/issues/5/comments -X POST -f body=Shipped\.$' "close: posts the summary"
169
+ tc "$GH_LOG" '^api repos/o/r/issues/5 -X PATCH -f state=closed -f state_reason=completed$' "close: state_reason completed"
170
+ : > "$GH_LOG"
171
+ kto_task_close 6 "Not worth it." --reason not-planned >/dev/null 2>&1
172
+ tc "$GH_LOG" '^api repos/o/r/issues/6 -X PATCH -f state=closed -f state_reason=not_planned$' "close: not-planned maps to not_planned"
173
+ : > "$GH_LOG"
174
+ kto_task_close 7 "Same work." --duplicate-of 4 >/dev/null 2>&1
175
+ tc "$GH_LOG" '^api repos/o/r/issues/7 -X PATCH -f state=closed -f state_reason=duplicate$' "close: duplicate maps to state_reason duplicate"
176
+ tc "$GH_LOG" 'issues/7/comments -X POST -f body=Same work\..*Duplicate of #4\.$' "close: the comment links the original"
177
+ : > "$GH_LOG"; GH_CLOSED="8"
178
+ kto_task_close 8 "x" >/dev/null 2>&1; rc=$?
179
+ t "close: an already-closed issue is fine" "$rc" "0"
180
+ t "close: it is not closed again" "$(grep -c 'PATCH' "$GH_LOG")" "0"
181
+ GH_CLOSED=""
182
+
183
+ # cckit merge — from the PR's worktree, as in the default-mode suite
184
+ git checkout -q develop
185
+ wt="$tmp/work/.claude/worktrees/fix+9-x"
186
+ git worktree add -q -b fix/9-x "$wt" develop
187
+ git -C "$wt" commit -q --allow-empty -m "the fix"
188
+ git -C "$wt" push -q origin fix/9-x
189
+ : > "$GH_LOG"; GH_CLOSED="43"
190
+ unset _CCKIT_BOARD_NOTICE_DONE; ( cd "$wt" && kto_task_pr_merge ) >"$tmp/out" 2>"$tmp/err"; rc=$?
191
+ t "merge: rc 0 over REST" "$rc" "0"
192
+ rest_only "$GH_LOG" "merge: only REST calls"
193
+ tc "$GH_LOG" '^api repos/o/r/pulls -X GET -f head=o:fix/9-x -f state=open' "merge: finds the open PR for the branch"
194
+ tc "$GH_LOG" '^api repos/o/r/pulls/9/merge -X PUT -f merge_method=squash$' "merge: squash-merges with PUT pulls/9/merge"
195
+ tc "$GH_LOG" '^api repos/o/r/issues/42 -X PATCH -f state=closed -f state_reason=completed$' "merge: closes the linked open issue"
196
+ t "merge: an already-closed linked issue is left" "$(grep -c 'issues/43 -X PATCH' "$GH_LOG")" "0"
197
+ t "merge: a bare mention is not a closing ref" "$(grep -c 'issues/44' "$GH_LOG")" "0"
198
+ t "merge: the worktree is removed" "$([ -d "$wt" ] && echo present || echo gone)" "gone"
199
+ t "merge: the local branch is deleted" "$(git branch --list fix/9-x | wc -l | tr -d ' ')" "0"
200
+ t "merge: the remote branch is kept" "$(git ls-remote --heads origin fix/9-x | wc -l | tr -d ' ')" "1"
201
+ tc "$tmp/out" 'remote branch fix/9-x kept' "merge: says why the remote branch stays"
202
+ : > "$GH_LOG"; GH_HEAD="fix/10-x"
203
+ kto_task_pr_merge 10 --rebase >/dev/null 2>&1
204
+ tc "$GH_LOG" '^api repos/o/r/pulls/10/merge -X PUT -f merge_method=rebase$' "merge: --rebase reaches merge_method"
205
+ : > "$GH_LOG"; GH_MERGEABLE="false"; GH_HEAD="fix/11-x"
206
+ kto_task_pr_merge 11 >/dev/null 2>&1; rc=$?
207
+ t "merge: a conflicting PR from the base is refused" "$rc" "1"
208
+ t "merge: nothing merged on a conflict" "$(grep -c '/merge' "$GH_LOG")" "0"
209
+ GH_MERGEABLE="true"; GH_CLOSED=""; GH_HEAD="fix/9-x"
210
+
211
+ # ── the default transport is unchanged ────────────────────────────────────────────────────────────
212
+ : > "$GH_LOG"
213
+ CLAUDE_CODE_REMOTE=true CCKIT_GH_TRANSPORT=graphql KIT_PROJECTS_V2=false kto_task_close 12 "Done." >/dev/null 2>&1
214
+ tc "$GH_LOG" '^issue close 12 --repo o/r --reason completed --comment Done\.$' "graphql: close still uses gh issue close"
215
+ t "graphql: no REST close" "$(grep -c '^api ' "$GH_LOG")" "0"
216
+
217
+ # ── the effort verbs ──────────────────────────────────────────────────────────────────────────────
218
+ # shellcheck source=/dev/null
219
+ . "$LIB/effort.sh" 2>/dev/null
220
+ # shellcheck source=/dev/null
221
+ . "$LIB/effort-ops.sh"
222
+ GH_TITLE="[Effort] 99 · demo effort"; GH_LABELS='[{"name":"role:tech-lead"},{"name":"kind:task"}]'
223
+ export KIT_BASE_BRANCH="main"
224
+
225
+ # cckit effort new
226
+ : > "$GH_LOG"
227
+ unset _CCKIT_BOARD_NOTICE_DONE; parent="$(effort_new --flow Core "demo effort" "first sub" "second sub" 2>"$tmp/err")"; rc=$?
228
+ t "effort new: rc 0 over REST" "$rc" "0"
229
+ rest_only "$GH_LOG" "effort new: only REST calls"
230
+ tc "$GH_LOG" '^api repos/o/r/labels -X POST -f name=effort ' "effort new: ensures labels over REST"
231
+ tc "$GH_LOG" "^api repos/o/r/issues -X POST -f title=\[Effort\] · \[Core\] demo effort .*-f labels\[\]=effort" "effort new: POST issues for the parent"
232
+ tc "$GH_LOG" "^api repos/o/r/issues/$parent -X PATCH -f title=\[Effort\] $parent · \[Core\] demo effort$" "effort new: renames the parent with its number"
233
+ tc "$GH_LOG" "^api repos/o/r/issues/$parent/labels -X POST -f labels\[\]=slug:demo-effort$" "effort new: adds the slug label"
234
+ t "effort new: parent + 2 subs created" "$(grep -c '^api repos/o/r/issues -X POST' "$GH_LOG")" "3"
235
+ t "effort new: 2 sub-issues linked" "$(grep -c 'sub_issues' "$GH_LOG")" "2"
236
+
237
+ # cckit effort pr + effort close
238
+ git checkout -q -b main; git push -q origin main
239
+ cd "$tmp/work" || exit 1
240
+ effort_start 99 demo >/dev/null 2>&1
241
+ ewt="$tmp/work/.claude/worktrees/effort+99-demo"
242
+ cd "$ewt" || exit 1
243
+ echo work > file.txt; git add file.txt; git commit -q -m "feat: do the thing (#101)"
244
+ : > "$GH_LOG"
245
+ unset _CCKIT_BOARD_NOTICE_DONE; effort_pr 99 --summary "SUMMARY" >"$tmp/out" 2>"$tmp/err"; rc=$?
246
+ t "effort pr: rc 0 over REST" "$rc" "0"
247
+ rest_only "$GH_LOG" "effort pr: only REST calls"
248
+ tc "$GH_LOG" '^api repos/o/r/pulls -X POST -f title=\[#99\] .*-f head=effort/99-demo -f base=main' "effort pr: POST pulls effort/99 → main"
249
+ tc "$GH_LOG" '^api repos/o/r/issues/7 -X PATCH -f labels\[\]=role:tech-lead -f labels\[\]=kind:task .*-F milestone=3' "effort pr: inherits labels + milestone"
250
+ t "effort pr: the board notice is printed once" "$(grep -c 'board step skipped' "$tmp/err")" "1"
251
+
252
+ : > "$GH_LOG"; GH_HEAD="effort/99-demo"; GH_BASE="main"
253
+ unset _CCKIT_BOARD_NOTICE_DONE; close_out="$(effort_close 99 2>&1)"; rc=$?
254
+ cd "$tmp/work" || exit 1
255
+ t "effort close: rc 0 over REST" "$rc" "0"
256
+ rest_only "$GH_LOG" "effort close: only REST calls"
257
+ tc "$GH_LOG" '^api repos/o/r/pulls/9/merge -X PUT -f merge_method=squash$' "effort close: squash-merges over REST"
258
+ tc "$GH_LOG" '^api repos/o/r/issues/101 -X PATCH -f state=closed -f state_reason=completed$' "effort close: closes sub #101"
259
+ tc "$GH_LOG" '^api repos/o/r/issues/99 -X PATCH -f state=closed -f state_reason=completed$' "effort close: closes the parent"
260
+ case "$close_out" in *"remote branch effort/99-demo kept"*) echo "ok: effort close: says why the remote branch stays" ;; *) echo "FAIL: effort close: no remote-branch notice: $close_out"; fail=1 ;; esac
261
+ t "effort close: the board notice is printed once" "$(printf '%s\n' "$close_out" | grep -c 'board step skipped')" "1"
262
+ t "effort close: the local branch is deleted" "$(git branch --list effort/99-demo | wc -l | tr -d ' ')" "0"
263
+
264
+ # wave-style close: no effort branch, the subs shipped as their own merged PRs
265
+ : > "$GH_LOG"
266
+ unset _CCKIT_BOARD_NOTICE_DONE; close_out="$(effort_close 77 2>&1)"; rc=$?
267
+ t "wave close: rc 0 over REST" "$rc" "0"
268
+ rest_only "$GH_LOG" "wave close: only REST calls"
269
+ tc "$GH_LOG" '^api repos/o/r/issues/101/timeline ' "wave close: finds the closing PR in the sub's timeline"
270
+ tc "$GH_LOG" '^api repos/o/r/pulls/5101 -H Accept: application/vnd.github.v3.diff$' "wave close: snapshots the closing PR's diff (not the mention)"
271
+ tc "$GH_LOG" '^api repos/o/r/issues/77 -X PATCH -f state=closed -f state_reason=completed$' "wave close: closes the parent"
272
+ t "wave close: rows match the graphql shape" \
273
+ "$(gh_rest_wave_rows o/r 77 | head -1)" "101|CLOSED|5101|abc1234def|feat: sub 5101"
274
+
275
+ # ── a failed REST read is no data, never the error body ───────────────────────────────────────────
276
+ export GH_FAIL_EP="repos/o/r/issues/404"
277
+ t "failed read: issue state is empty" "$(gh_rest_issue_state o/r 404 2>/dev/null)" ""
278
+ gh_rest_issue_state o/r 404 >/dev/null 2>&1; rc=$?
279
+ t "failed read: issue state returns non-zero" "$rc" "1"
280
+ t "failed read: issue json is empty" "$(gh_rest_issue_json o/r 404 2>/dev/null)" ""
281
+ export GH_FAIL_EP="repos/o/r/issues/404/timeline"
282
+ t "failed read: closing PRs are empty" "$(gh_rest_issue_closing_prs o/r 404 2>/dev/null)" ""
283
+ export GH_FAIL_EP="repos/o/r/pulls"
284
+ t "failed read: no PR for the head" "$(gh_rest_pr_for_head o/r fix/1-x 2>/dev/null)" ""
285
+ export GH_FAIL_EP="repos/o/r/issues/*/sub_issues"
286
+ t "failed read: no sub-issue numbers" "$(gh_rest_sub_issue_numbers o/r 1 2>/dev/null)" ""
287
+ export GH_FAIL_EP="repos/o/r/issues/102/timeline"
288
+ t "failed read: a sub whose timeline fails has no PR" \
289
+ "$(gh_rest_wave_rows o/r 77 2>/dev/null | sed -n 2p)" "102|CLOSED|||"
290
+ : > "$GH_LOG"
291
+ effort_close 77 >/dev/null 2>&1; rc=$?
292
+ t "failed read: wave close refuses" "$rc" "1"
293
+ t "failed read: wave close leaves the parent open" "$(grep -c 'issues/77 -X PATCH' "$GH_LOG")" "0"
294
+ export GH_FAIL_EP="repos/o/r/issues/88"
295
+ effort_start 88 >/dev/null 2>&1
296
+ t "failed read: effort start falls back to the effort slug" \
297
+ "$(git branch --list 'effort/88-*' | tr -d ' *+')" "effort/88-effort"
298
+ unset GH_FAIL_EP
299
+
300
+ [ "$fail" -eq 0 ] && echo "gh-rest: all passed" || echo "gh-rest: FAILURES"
301
+ exit "$fail"
@@ -0,0 +1,289 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # gh-rest.sh — the GitHub transport switch and the REST forms of the lifecycle calls (#396).
4
+ #
5
+ # Cloud agent sessions reach GitHub through a proxy that refuses every GraphQL request (403), and
6
+ # `gh pr …` / `gh issue …` are GraphQL underneath. REST calls to the attached repo still work. So the
7
+ # lifecycle verbs (pr, merge, close, new, effort new|pr|close) pick a transport here:
8
+ #
9
+ # CCKIT_GH_TRANSPORT=rest|graphql forces it
10
+ # CLAUDE_CODE_REMOTE=true defaults it to rest (a cloud session)
11
+ # otherwise graphql — the gh subcommands, unchanged
12
+ #
13
+ # In rest mode every call is `gh api repos/<owner>/<repo>/…`, endpoint first. Projects v2 is
14
+ # GraphQL-only, so board steps are skipped with ONE notice line (exit 0, never a failure).
15
+ #
16
+ # cckit_gh_rest rc 0 iff the transport is rest
17
+ # cckit_gh_transport echo rest|graphql
18
+ # cckit_board_skipped rc 0 iff board steps are skipped (prints the notice once)
19
+ # gh_rest_closing_refs stdin: a PR body → the issue #s it closes (pure)
20
+ # gh_rest_issue_json <repo> <n> {number,title,body,state,labels[{name}],milestone{title,number}}
21
+ # gh_rest_issue_state <repo> <n> OPEN | CLOSED
22
+ # gh_rest_issue_create <repo> <title> <body> <labels-csv> [milestone-title] [assign 1|0] → html_url
23
+ # gh_rest_issue_close <repo> <n> <completed|not_planned|duplicate> [comment]
24
+ # gh_rest_issue_edit_title <repo> <n> <title>
25
+ # gh_rest_issue_set_body <repo> <n> <body>
26
+ # gh_rest_issue_add_labels <repo> <n> <labels-csv>
27
+ # gh_rest_label_create <repo> <name> <color> <description> (idempotent)
28
+ # gh_rest_milestone_number <repo> <title>
29
+ # gh_rest_pr_create <repo> <base> <head> <title> <body> <labels-csv> [milestone-title] → html_url
30
+ # gh_rest_pr_for_head <repo> <branch> the open PR number for a head branch
31
+ # gh_rest_pr_json <repo> <pr> {mergeable,mergeStateStatus,baseRefName,headRefName,body,state}
32
+ # gh_rest_pr_merge <repo> <pr> <squash|merge|rebase>
33
+ # gh_rest_pr_closing_issues <repo> <pr>
34
+ # gh_rest_pr_diff <repo> <pr>
35
+ # gh_rest_sub_issue_numbers <repo> <n>
36
+ # gh_rest_issue_numbers_by_label <repo> <label> open issues only
37
+ # gh_rest_open_issue_titles <repo> "<n>\t<title>" for every open issue
38
+ # gh_rest_issue_closing_prs <repo> <n> merged PRs that close <n> by keyword (timeline)
39
+ # gh_rest_wave_rows <repo> <parent> "sub|STATE|pr|merge-sha|pr-title" per sub (wave close)
40
+ #
41
+ # Not reachable over REST in the cloud: Projects v2 (skipped), and remote branch deletion (the proxy
42
+ # rejects DELETE git/refs) — callers keep the branch and say so.
43
+ # bash 3.2 + zsh compatible. Requires gh, jq.
44
+ # errors: mixed — creates, closes and merges propagate; labels/milestone/assignee are best-effort
45
+
46
+ CCKIT_BOARD_SKIP_NOTICE="cckit: board step skipped (GraphQL unavailable; the project's built-in workflows move the card)"
47
+
48
+ # rc 0 iff the transport is rest. An unknown CCKIT_GH_TRANSPORT warns once and falls to the default.
49
+ cckit_gh_rest() {
50
+ case "${CCKIT_GH_TRANSPORT:-}" in
51
+ rest) return 0 ;;
52
+ graphql) return 1 ;;
53
+ '') : ;;
54
+ *)
55
+ if [ -z "${_CCKIT_TRANSPORT_WARNED:-}" ]; then
56
+ printf "cckit: CCKIT_GH_TRANSPORT must be rest or graphql (got '%s') — using the default\n" \
57
+ "$CCKIT_GH_TRANSPORT" >&2
58
+ _CCKIT_TRANSPORT_WARNED=1
59
+ fi ;;
60
+ esac
61
+ [ "${CLAUDE_CODE_REMOTE:-}" = "true" ]
62
+ }
63
+
64
+ cckit_gh_transport() {
65
+ if cckit_gh_rest; then printf 'rest'; else printf 'graphql'; fi
66
+ }
67
+
68
+ # rc 0 when board steps are skipped (rest mode). Prints the notice at most once per process.
69
+ cckit_board_skipped() {
70
+ cckit_gh_rest || return 1
71
+ if [ -z "${_CCKIT_BOARD_NOTICE_DONE:-}" ]; then
72
+ printf '%s\n' "$CCKIT_BOARD_SKIP_NOTICE" >&2
73
+ _CCKIT_BOARD_NOTICE_DONE=1
74
+ fi
75
+ return 0
76
+ }
77
+
78
+ # --- pure ----------------------------------------------------------------------------------------
79
+
80
+ # A PR body on stdin → the issue numbers it closes, one per line, sorted. REST has no
81
+ # closingIssuesReferences, so the keywords are read from the body (the same regex the graphql path
82
+ # falls back to).
83
+ gh_rest_closing_refs() {
84
+ grep -ioE '(close[sd]?|fix(e[sd])?|resolve[sd]?) +#[0-9]+' | grep -oE '[0-9]+' | sort -un
85
+ }
86
+
87
+ # Split a comma list into `-f <key>[]=<item>` args on stdout, one arg per line (callers read them
88
+ # back with a while loop: no word splitting, so a label with a space survives).
89
+ _ghr_array_args() {
90
+ local key="$1" item
91
+ printf '%s\n' "$2" | tr ',' '\n' | while IFS= read -r item; do
92
+ item="$(printf '%s' "$item" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
93
+ [ -n "$item" ] && printf -- '-f\n%s[]=%s\n' "$key" "$item"
94
+ done
95
+ }
96
+
97
+ # `gh api …` for a read. On failure gh prints the error JSON to stdout and skips --jq, so the output
98
+ # is kept only on rc 0: a failed read is no data (rc 1), never an error body taken as a value.
99
+ _ghr_read() {
100
+ local out
101
+ out="$(gh api "$@")" || return 1
102
+ [ -n "$out" ] && printf '%s\n' "$out"
103
+ return 0
104
+ }
105
+
106
+ # --- issues --------------------------------------------------------------------------------------
107
+
108
+ gh_rest_issue_json() {
109
+ _ghr_read "repos/$1/issues/$2" --jq '{number, title, body: (.body // ""), state: (.state | ascii_upcase),
110
+ labels: [.labels[]? | {name}],
111
+ milestone: (if .milestone then {title: .milestone.title, number: .milestone.number} else null end)}'
112
+ }
113
+
114
+ gh_rest_issue_state() {
115
+ _ghr_read "repos/$1/issues/$2" --jq '.state | ascii_upcase'
116
+ }
117
+
118
+ gh_rest_milestone_number() {
119
+ local tab n t; tab="$(printf '\t')"
120
+ _ghr_read "repos/$1/milestones" -X GET -f state=all -f per_page=100 \
121
+ --jq '.[] | "\(.number)\t\(.title)"' 2>/dev/null \
122
+ | while IFS="$tab" read -r n t; do [ "$t" = "$2" ] && { printf '%s' "$n"; break; }; done
123
+ }
124
+
125
+ # Create an issue with labels + milestone, then assign it to its author (the REST form of
126
+ # `gh issue create --assignee @me`: the token's own login is the author). <assign> 0 skips that.
127
+ # Echoes the html_url.
128
+ gh_rest_issue_create() {
129
+ local repo="$1" title="$2" body="$3" labels="${4:-}" ms="${5:-}" assign="${6:-1}" msnum="" out num url login a
130
+ local tab; tab="$(printf '\t')"
131
+ set -- -f "title=$title" -f "body=$body"
132
+ while IFS= read -r a; do [ -n "$a" ] && set -- "$@" "$a"; done <<EOF
133
+ $(_ghr_array_args labels "$labels")
134
+ EOF
135
+ if [ -n "$ms" ]; then
136
+ msnum="$(gh_rest_milestone_number "$repo" "$ms")"
137
+ if [ -n "$msnum" ]; then set -- "$@" -F "milestone=$msnum"
138
+ else echo "cckit: milestone '$ms' not found in $repo — issue created without it" >&2; fi
139
+ fi
140
+ out="$(gh api "repos/$repo/issues" -X POST "$@" \
141
+ --jq '"\(.number)\t\(.html_url)\t\(.user.login // "")"')" || return 1
142
+ IFS="$tab" read -r num url login <<EOF
143
+ $out
144
+ EOF
145
+ [ -n "$num" ] || return 1
146
+ if [ "$assign" = 1 ] && [ -n "$login" ]; then
147
+ gh api "repos/$repo/issues/$num/assignees" -X POST -f "assignees[]=$login" >/dev/null 2>&1 \
148
+ || echo "cckit: #$num created but not assigned to $login" >&2
149
+ fi
150
+ printf '%s\n' "$url"
151
+ }
152
+
153
+ # Close with a GitHub state_reason (completed | not_planned | duplicate); the comment goes first,
154
+ # as `gh issue close --comment` does.
155
+ gh_rest_issue_close() {
156
+ local repo="$1" n="$2" reason="${3:-completed}" comment="${4:-}"
157
+ reason="$(printf '%s' "$reason" | tr ' -' '__')"
158
+ if [ -n "$comment" ]; then
159
+ gh api "repos/$repo/issues/$n/comments" -X POST -f "body=$comment" >/dev/null || return 1
160
+ fi
161
+ gh api "repos/$repo/issues/$n" -X PATCH -f state=closed -f "state_reason=$reason" >/dev/null
162
+ }
163
+
164
+ gh_rest_issue_edit_title() {
165
+ gh api "repos/$1/issues/$2" -X PATCH -f "title=$3" >/dev/null
166
+ }
167
+
168
+ gh_rest_issue_set_body() {
169
+ gh api "repos/$1/issues/$2" -X PATCH -f "body=$3" >/dev/null
170
+ }
171
+
172
+ gh_rest_issue_add_labels() {
173
+ local repo="$1" n="$2" labels="${3:-}" a
174
+ set --
175
+ while IFS= read -r a; do [ -n "$a" ] && set -- "$@" "$a"; done <<EOF
176
+ $(_ghr_array_args labels "$labels")
177
+ EOF
178
+ [ $# -gt 0 ] || return 0
179
+ gh api "repos/$repo/issues/$n/labels" -X POST "$@" >/dev/null
180
+ }
181
+
182
+ # Idempotent: an existing label answers 422, which is fine.
183
+ gh_rest_label_create() {
184
+ gh api "repos/$1/labels" -X POST -f "name=$2" -f "color=$3" -f "description=$4" >/dev/null 2>&1 || true
185
+ }
186
+
187
+ gh_rest_sub_issue_numbers() {
188
+ _ghr_read "repos/$1/issues/$2/sub_issues" -X GET -f per_page=100 --jq '.[].number'
189
+ }
190
+
191
+ gh_rest_issue_numbers_by_label() {
192
+ _ghr_read "repos/$1/issues" -X GET -f state=open -f "labels=$2" -f per_page=100 \
193
+ --jq '.[] | select(.pull_request | not) | .number'
194
+ }
195
+
196
+ gh_rest_open_issue_titles() {
197
+ _ghr_read "repos/$1/issues" -X GET -f state=open -f per_page=100 --paginate \
198
+ --jq '.[] | select(.pull_request | not) | "\(.number)\t\(.title)"'
199
+ }
200
+
201
+ # --- pull requests -------------------------------------------------------------------------------
202
+
203
+ # Open a PR, then set labels, milestone and assignee (its author — `--assignee @me`) in one PATCH.
204
+ # The PR is the failure that matters; the decoration is best-effort and says so. Echoes the html_url.
205
+ gh_rest_pr_create() {
206
+ local repo="$1" base="$2" head="$3" title="$4" body="$5" labels="${6:-}" ms="${7:-}" msnum out num url login a
207
+ local tab; tab="$(printf '\t')"
208
+ out="$(gh api "repos/$repo/pulls" -X POST -f "title=$title" -f "head=$head" -f "base=$base" -f "body=$body" \
209
+ --jq '"\(.number)\t\(.html_url)\t\(.user.login // "")"')" || return 1
210
+ IFS="$tab" read -r num url login <<EOF
211
+ $out
212
+ EOF
213
+ [ -n "$num" ] || return 1
214
+ set --
215
+ while IFS= read -r a; do [ -n "$a" ] && set -- "$@" "$a"; done <<EOF
216
+ $(_ghr_array_args labels "$labels")
217
+ EOF
218
+ [ -n "$login" ] && set -- "$@" -f "assignees[]=$login"
219
+ if [ -n "$ms" ]; then
220
+ msnum="$(gh_rest_milestone_number "$repo" "$ms")"
221
+ if [ -n "$msnum" ]; then set -- "$@" -F "milestone=$msnum"
222
+ else echo "cckit: milestone '$ms' not found in $repo — PR opened without it" >&2; fi
223
+ fi
224
+ if [ $# -gt 0 ]; then
225
+ gh api "repos/$repo/issues/$num" -X PATCH "$@" >/dev/null 2>&1 \
226
+ || echo "cckit: PR #$num opened, but its labels/milestone/assignee were not set" >&2
227
+ fi
228
+ printf '%s\n' "$url"
229
+ }
230
+
231
+ gh_rest_pr_for_head() {
232
+ _ghr_read "repos/$1/pulls" -X GET -f "head=${1%%/*}:$2" -f state=open --jq '.[0].number // empty'
233
+ }
234
+
235
+ # The fields the merge path reads, in the graphql vocabulary: mergeable MERGEABLE|CONFLICTING|UNKNOWN
236
+ # (REST true|false|null — null while GitHub computes it), mergeStateStatus = REST mergeable_state.
237
+ gh_rest_pr_json() {
238
+ _ghr_read "repos/$1/pulls/$2" --jq '{
239
+ mergeable: (if .mergeable == true then "MERGEABLE" elif .mergeable == false then "CONFLICTING" else "UNKNOWN" end),
240
+ mergeStateStatus: ((.mergeable_state // "unknown") | ascii_upcase),
241
+ baseRefName: .base.ref, headRefName: .head.ref, body: (.body // ""),
242
+ state: (if .merged_at then "MERGED" else (.state | ascii_upcase) end)}'
243
+ }
244
+
245
+ gh_rest_pr_merge() {
246
+ gh api "repos/$1/pulls/$2/merge" -X PUT -f "merge_method=${3:-squash}" >/dev/null
247
+ }
248
+
249
+ gh_rest_pr_closing_issues() {
250
+ _ghr_read "repos/$1/pulls/$2" --jq '.body // ""' 2>/dev/null | gh_rest_closing_refs
251
+ }
252
+
253
+ gh_rest_pr_diff() {
254
+ _ghr_read "repos/$1/pulls/$2" -H 'Accept: application/vnd.github.v3.diff'
255
+ }
256
+
257
+ # The merged PRs that close issue <n> (graphql: closedByPullRequestsReferences, merged only). REST has
258
+ # no such link, so: the PRs in <n>'s timeline that cross-reference it, are merged, live in this repo,
259
+ # and close it by keyword. One number per line, timeline order.
260
+ gh_rest_issue_closing_prs() {
261
+ local repo="$1" n="$2"
262
+ _ghr_read "repos/$repo/issues/$n/timeline" -X GET -f per_page=100 --jq "
263
+ .[] | select(.event == \"cross-referenced\") | .source.issue // empty
264
+ | select(.pull_request.merged_at != null)
265
+ | select((.repository_url // \"\") | endswith(\"/repos/$repo\"))
266
+ | select((.body // \"\") | test(\"(?i)(close[sd]?|fix(e[sd])?|resolve[sd]?) +#$n([^0-9]|\$)\"))
267
+ | .number"
268
+ }
269
+
270
+ # The wave close's per-sub read (graphql: subIssues + closedByPullRequestsReferences): each sub from
271
+ # sub_issues, its first merged closing PR, then that PR's merge commit and title. A failed read leaves
272
+ # the field empty, so the close refuses (fails closed, as the graphql path does).
273
+ gh_rest_wave_rows() {
274
+ local repo="$1" parent="$2" tab sub state pr oid title
275
+ tab="$(printf '\t')"
276
+ _ghr_read "repos/$repo/issues/$parent/sub_issues" -X GET -f per_page=100 \
277
+ --jq '.[] | "\(.number)\t\(.state | ascii_upcase)"' 2>/dev/null \
278
+ | while IFS="$tab" read -r sub state; do
279
+ [ -n "$sub" ] || continue
280
+ oid="" title=""
281
+ pr="$(gh_rest_issue_closing_prs "$repo" "$sub" 2>/dev/null | head -1)"
282
+ if [ -n "$pr" ]; then
283
+ IFS="$tab" read -r oid title <<EOF
284
+ $(_ghr_read "repos/$repo/pulls/$pr" --jq '"\(.merge_commit_sha // "")\t\(.title // "")"' 2>/dev/null)
285
+ EOF
286
+ fi
287
+ printf '%s|%s|%s|%s|%s\n' "$sub" "$state" "$pr" "$oid" "$title"
288
+ done
289
+ }
@@ -23,6 +23,15 @@
23
23
  # Portable: bash 3.2+ AND zsh.
24
24
  # errors: mixed — kit_gc_analyze is read-only and forgiving; the prune path propagates
25
25
 
26
+ # The GitHub transport switch (#396): rest mode routes the gh calls below through gh-rest.sh.
27
+ if ! command -v cckit_gh_rest >/dev/null 2>&1; then
28
+ _ghr_lib="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd)/gh-rest.sh"
29
+ # shellcheck source=/dev/null
30
+ [ -f "$_ghr_lib" ] && . "$_ghr_lib"
31
+ unset _ghr_lib
32
+ fi
33
+ command -v cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
34
+
26
35
  KIT_GC_REPO="${KIT_GC_REPO:-${KIT_REPO:-}}"
27
36
 
28
37
  _kit_gc_root() { git worktree list --porcelain 2>/dev/null | awk '/^worktree /{print $2; exit}'; }
@@ -67,6 +76,11 @@ _kit_gc_require_deps() {
67
76
  # scaled badly exactly when gc matters most (many stale branches). #124.
68
77
  _kit_gc_pr_index() {
69
78
  local repo="${1:-$KIT_GC_REPO}"
79
+ if cckit_gh_rest; then
80
+ gh api "repos/$repo/pulls" -X GET -f state=all -f per_page=100 \
81
+ --jq '.[] | "\(.head.ref)\tPR#\(.number) \(if .merged_at then "MERGED" else (.state | ascii_upcase) end)"' 2>/dev/null || true
82
+ return 0
83
+ fi
70
84
  gh pr list --repo "$repo" --state all --limit 200 --json number,state,headRefName \
71
85
  --jq '.[] | "\(.headRefName)\tPR#\(.number) \(.state)"' 2>/dev/null || true
72
86
  }
@@ -170,12 +184,16 @@ _kit_gc_issue_merged_pr() {
170
184
  while IFS= read -r cand; do
171
185
  [ -n "$cand" ] || continue
172
186
  case "$cand" in *[!0-9]*) continue ;; esac
173
- if [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
187
+ if cckit_gh_rest; then
188
+ # REST: the candidate came from a merged cross-reference that closes <n> (gh_rest_wave_rows).
189
+ result="$cand"; break
190
+ elif [ "$(gh pr view "$cand" --repo "$repo" --json state --jq .state 2>/dev/null)" = "MERGED" ]; then
174
191
  result="$cand"; break
175
192
  fi
176
193
  done <<EOF
177
- $(gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
178
- --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true)
194
+ $(if cckit_gh_rest; then gh_rest_issue_closing_prs "$repo" "$n" 2>/dev/null || true
195
+ else gh issue view "$n" --repo "$repo" --json closedByPullRequestsReferences \
196
+ --jq '.closedByPullRequestsReferences[].number' 2>/dev/null || true; fi)
179
197
  EOF
180
198
  if [ -z "$result" ] && [ "$depth" -eq 0 ]; then
181
199
  parent="$(gh api "repos/$repo/issues/$n/parent" --jq .number 2>/dev/null || true)"