@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.
@@ -20,9 +20,9 @@
20
20
  # kto_compose_issue_body <user-body> <plan> <blocked> <role-slug> echo an issue body
21
21
  # kto_closing_issue_numbers <pr-num> echo the issue #s a merged PR closes
22
22
  # kto_task_pr <num> <summary> [commit-msg] commit+push+open PR (board → In Review)
23
- # kto_task_pr_merge [pr-num] squash-merge + close issues + switch develop + GC
23
+ # kto_task_pr_merge [pr-num] [--squash|--merge|--rebase] merge (squash default) + close issues + GC + update the PR base
24
24
  # kto_task_new <title> <role> <kind> <prio> [ms] [plan] [blocked] <body> create issue + board fields (ms may be empty)
25
- # kto_task_close <num> <summary> [pr-num] close issue + board → Done
25
+ # kto_task_close <num> <summary> [pr-num] [--reason R] [--duplicate-of N] [--superseded-by N] close + reason + board → Done
26
26
  # errors: mixed — git/gh ops propagate; board + label side effects are best-effort
27
27
 
28
28
  # --- repo resolution -----------------------------------------------------------------------------
@@ -37,6 +37,19 @@ if [[ -z "${KIT_REPO:-}" ]]; then
37
37
  unset _kto_root
38
38
  fi
39
39
 
40
+ # The GitHub transport switch (#396): in rest mode every gh call below is `gh api repos/…`.
41
+ if ! type cckit_gh_rest >/dev/null 2>&1; then
42
+ if [ -n "${BASH_SOURCE:-}" ]; then _kto_self="${BASH_SOURCE[0]}"
43
+ elif [ -n "${ZSH_VERSION:-}" ]; then eval '_kto_self="${(%):-%x}"'
44
+ else _kto_self="$0"; fi
45
+ _kto_self="$(cd "$(dirname "$_kto_self")" 2>/dev/null && pwd)/gh-rest.sh"
46
+ # shellcheck source=gh-rest.sh
47
+ [ -f "$_kto_self" ] && . "$_kto_self"
48
+ unset _kto_self
49
+ fi
50
+ type cckit_gh_rest >/dev/null 2>&1 || cckit_gh_rest() { return 1; }
51
+ type cckit_board_skipped >/dev/null 2>&1 || cckit_board_skipped() { return 1; }
52
+
40
53
  kto_repo() {
41
54
  printf '%s' "${KIT_TASK_REPO:-${KIT_REPO:-}}"
42
55
  }
@@ -48,8 +61,11 @@ kto_base_branch() {
48
61
 
49
62
  # True (rc 0) iff board updates should run. Off when KIT_PROJECTS_V2 is explicitly "false" — keeps
50
63
  # the op portable to a project with no Projects v2 board (the plugin's KIT_PROJECTS_V2 toggle).
64
+ # In rest mode (#396) the board is GraphQL-only: skipped with one notice, never a failure.
51
65
  kto_board_enabled() {
52
- [[ "${KIT_PROJECTS_V2:-true}" != "false" ]]
66
+ [[ "${KIT_PROJECTS_V2:-true}" != "false" ]] || return 1
67
+ cckit_board_skipped && return 1
68
+ return 0
53
69
  }
54
70
 
55
71
  # --- pure parsers (network-free; covered by the self-test) ---------------------------------------
@@ -178,6 +194,7 @@ kto_compose_issue_body() {
178
194
  kto_closing_issue_numbers() {
179
195
  local pr="$1" repo issues body
180
196
  repo="$(kto_repo)"
197
+ if cckit_gh_rest; then gh_rest_pr_closing_issues "$repo" "$pr"; return 0; fi
181
198
  issues=$(gh pr view "$pr" --repo "$repo" --json closingIssuesReferences \
182
199
  --jq '.closingIssuesReferences[].number' 2>/dev/null)
183
200
  if [[ -z "$issues" ]]; then
@@ -215,7 +232,8 @@ kto_task_pr() {
215
232
  [[ -n "$num" ]] || { echo "✗ kto_task_pr: branch '$branch' doesn't match <kind>/<N>-<slug> and no issue number passed" >&2; return 1; }
216
233
  [[ -n "$summary" ]] || { echo "✗ kto_task_pr: a Summary is required (never invent one)" >&2; return 1; }
217
234
 
218
- meta=$(gh issue view "$num" --repo "$repo" --json title,labels,milestone) || return 1
235
+ if cckit_gh_rest; then meta=$(gh_rest_issue_json "$repo" "$num") || return 1
236
+ else meta=$(gh issue view "$num" --repo "$repo" --json title,labels,milestone) || return 1; fi
219
237
  title=$(printf '%s' "$meta" | jq -r .title)
220
238
  labels=$(printf '%s' "$meta" | jq -r '[.labels[].name] | join(",")')
221
239
  milestone=$(printf '%s' "$meta" | jq -r '.milestone.title // ""')
@@ -242,6 +260,10 @@ kto_task_pr() {
242
260
 
243
261
  git push -u origin "$branch" || return 1
244
262
 
263
+ if cckit_gh_rest; then
264
+ url=$(gh_rest_pr_create "$repo" "$(kto_base_branch)" "$branch" "$pr_title" \
265
+ "$(kto_compose_pr_body "$num" "$summary" "$role" "$title")" "$labels" "$milestone") || return 1
266
+ else
245
267
  url=$(gh pr create --repo "$repo" \
246
268
  --base "$(kto_base_branch)" --head "$branch" \
247
269
  --title "$pr_title" \
@@ -249,6 +271,7 @@ kto_task_pr() {
249
271
  --label "$labels" \
250
272
  ${milestone:+--milestone "$milestone"} \
251
273
  --assignee "@me") || return 1
274
+ fi
252
275
  echo "✓ $url"
253
276
 
254
277
  # Board: add the PR (In Review) + move the issue to In Review. Best-effort — never fail the op.
@@ -269,13 +292,32 @@ kto_task_pr() {
269
292
  fi
270
293
  }
271
294
 
272
- # kit task pr-merge — squash-merge the open PR for the current branch (rebase-fix conflicts onto
273
- # develop first), close its linked issues (GitHub native auto-close only fires on main), switch the
274
- # main checkout to develop + pull, then clean up the merged branch + worktree.
275
- # Args: [pr-num] (defaults to the open PR for the current branch)
295
+ # kit task pr-merge — merge a PR (squash by default; --merge or --rebase), rebase-fix conflicts onto
296
+ # the PR's base first, close its linked issues (GitHub native auto-close only fires on the default
297
+ # branch), then clean up the PR's head branch + worktree and bring the PR's base up to date.
298
+ # Args: [<pr-num>] [--squash|--merge|--rebase] (the PR defaults to the open PR for the current branch)
299
+ #
300
+ # The PR's base and head come from GitHub, never from the current checkout (#376): the base may be an
301
+ # effort/<N> branch, and a sub PR merged from the effort worktree must not GC the effort branch.
276
302
  kto_task_pr_merge() {
277
- local input_pr="${1:-}"
278
- local repo branch base pr_num mergeable merge_state issues n state wt main_wt
303
+ local input_pr="" method="squash" a
304
+ for a in "$@"; do
305
+ case "$a" in
306
+ --squash) method="squash" ;;
307
+ --merge) method="merge" ;;
308
+ --rebase) method="rebase" ;;
309
+ -*) echo "✗ kto_task_pr_merge: unknown flag '$a' — usage: cckit merge [<pr>] [--squash|--merge|--rebase]" >&2; return 2 ;;
310
+ *)
311
+ # Refuse anything but a PR number (#366). `gh pr merge --help` exits 0, so a flag taken as
312
+ # the number used to "merge", then run the cleanup on the base branch.
313
+ if [[ -n "$input_pr" || ! "$a" =~ ^#?[0-9]+$ ]]; then
314
+ echo "✗ kto_task_pr_merge: '$a' is not a PR number — usage: cckit merge [<pr>] [--squash|--merge|--rebase]" >&2; return 2
315
+ fi
316
+ input_pr="${a#\#}" ;;
317
+ esac
318
+ done
319
+
320
+ local repo branch base pr_num meta merge_state pr_base head issues n state wt main_wt base_wt common
279
321
  repo="$(kto_repo)"
280
322
  base="$(kto_base_branch)"
281
323
  branch="$(git rev-parse --abbrev-ref HEAD)"
@@ -284,86 +326,111 @@ kto_task_pr_merge() {
284
326
  echo "✗ Working tree is dirty — commit or stash changes first" >&2; return 1
285
327
  fi
286
328
 
287
- # Refuse anything but a PR number. `gh pr merge --help` exits 0, so a flag taken as the number
288
- # used to "merge", then run the cleanup below on the base branch (#366).
289
- if [[ -n "$input_pr" && ! "$input_pr" =~ ^#?[0-9]+$ ]]; then
290
- echo "✗ kto_task_pr_merge: '$input_pr' is not a PR number — usage: cckit merge [<pr>]" >&2; return 2
291
- fi
292
- pr_num="${input_pr#\#}"
329
+ pr_num="$input_pr"
293
330
  if [[ -z "$pr_num" ]]; then
294
- pr_num=$(gh pr list --repo "$repo" --head "$branch" --state open --json number --jq '.[0].number // empty')
331
+ if cckit_gh_rest; then pr_num=$(gh_rest_pr_for_head "$repo" "$branch")
332
+ else pr_num=$(gh pr list --repo "$repo" --head "$branch" --state open --json number --jq '.[0].number // empty'); fi
295
333
  [[ -z "$pr_num" ]] && { echo "✗ No open PR found for branch $branch" >&2; return 1; }
296
334
  fi
297
- echo "→ PR #$pr_num on branch $branch"
298
335
 
299
- mergeable=$(gh pr view "$pr_num" --repo "$repo" --json mergeable,mergeStateStatus \
300
- --jq '{mergeable:.mergeable, status:.mergeStateStatus}')
301
- merge_state=$(printf '%s' "$mergeable" | jq -r .mergeable)
302
- echo " mergeable=$merge_state status=$(printf '%s' "$mergeable" | jq -r .status)"
336
+ if cckit_gh_rest; then meta=$(gh_rest_pr_json "$repo" "$pr_num") || return 1
337
+ else meta=$(gh pr view "$pr_num" --repo "$repo" --json mergeable,mergeStateStatus,baseRefName,headRefName) || return 1; fi
338
+ merge_state=$(printf '%s' "$meta" | jq -r '.mergeable // ""')
339
+ pr_base=$(printf '%s' "$meta" | jq -r '.baseRefName // empty'); pr_base="${pr_base:-$base}"
340
+ head=$(printf '%s' "$meta" | jq -r '.headRefName // empty'); head="${head:-$branch}"
341
+ echo "→ PR #$pr_num: $head → $pr_base ($method)"
342
+ echo " mergeable=$merge_state status=$(printf '%s' "$meta" | jq -r '.mergeStateStatus // ""')"
343
+
344
+ # Branches that are never rebased, force-pushed or deleted here: the repo base, the PR's base
345
+ # (it may be an effort branch), and the release branches (#366).
346
+ _kto_protected() { case "$1" in "$base"|"$pr_base"|main|master|develop|HEAD) return 0 ;; esac; return 1; }
303
347
 
304
348
  if [[ "$merge_state" == "CONFLICTING" ]]; then
305
- case "$branch" in "$base"|main|master|develop|HEAD)
306
- echo "✗ PR #$pr_num conflicts — rebase it from its own worktree (never rebase $branch here)" >&2; return 1 ;;
307
- esac
308
- echo "→ Conflicts detected — rebasing onto $base..."
309
- git fetch origin "$base"
310
- if git rebase "origin/$base"; then
311
- git push --force-with-lease origin "$branch"
349
+ if [[ "$branch" != "$head" ]] || _kto_protected "$branch"; then
350
+ echo "✗ PR #$pr_num conflicts — rebase $head onto $pr_base from its own worktree (not from $branch)" >&2; return 1
351
+ fi
352
+ echo "→ Conflicts detected — rebasing onto $pr_base..."
353
+ git fetch origin "$pr_base"
354
+ if git rebase "origin/$pr_base"; then
355
+ git push --force-with-lease origin "$head"
312
356
  echo "✓ Rebased and pushed — re-checking mergeability..."
313
357
  else
314
- echo "✗ Rebase had conflicts that need manual resolution — resolve, then run pr-merge again" >&2
358
+ echo "✗ Rebase had conflicts that need manual resolution — resolve, then run cckit merge again" >&2
315
359
  git rebase --abort 2>/dev/null || true
316
360
  return 1
317
361
  fi
318
362
  fi
319
363
 
320
- if ! gh pr merge "$pr_num" --repo "$repo" --squash 2>&1; then
364
+ if cckit_gh_rest; then
365
+ if ! gh_rest_pr_merge "$repo" "$pr_num" "$method" 2>&1; then
366
+ echo "✗ Merge failed — check PR status: https://github.com/$repo/pull/$pr_num" >&2
367
+ return 1
368
+ fi
369
+ elif ! gh pr merge "$pr_num" --repo "$repo" "--$method" 2>&1; then
321
370
  echo "✗ Merge failed — check PR status: https://github.com/$repo/pull/$pr_num" >&2
322
371
  return 1
323
372
  fi
324
- echo "✓ PR #$pr_num merged"
373
+ echo "✓ PR #$pr_num merged ($method)"
325
374
 
326
375
  # Close linked issues (replaces issue-close-on-merge.yml; native auto-close only fires on main).
327
376
  issues="$(kto_closing_issue_numbers "$pr_num")"
328
377
  for n in $issues; do
329
- state=$(gh issue view "$n" --repo "$repo" --json state --jq .state 2>/dev/null)
330
- if [[ "$state" == "OPEN" ]]; then
378
+ if cckit_gh_rest; then state=$(gh_rest_issue_state "$repo" "$n" 2>/dev/null)
379
+ else state=$(gh issue view "$n" --repo "$repo" --json state --jq .state 2>/dev/null); fi
380
+ if [[ "$state" == "OPEN" ]] && cckit_gh_rest; then
381
+ gh_rest_issue_close "$repo" "$n" completed \
382
+ "Closed automatically on merge of #$pr_num to $pr_base (cckit merge; GitHub native auto-close only fires on the default branch)." \
383
+ && echo "✓ Closed issue #$n"
384
+ elif [[ "$state" == "OPEN" ]]; then
331
385
  gh issue close "$n" --repo "$repo" \
332
- --comment "Closed automatically on merge of #$pr_num to $base (kit task pr-merge; GitHub native auto-close only fires on the default branch)." \
386
+ --comment "Closed automatically on merge of #$pr_num to $pr_base (cckit merge; GitHub native auto-close only fires on the default branch)." \
333
387
  && echo "✓ Closed issue #$n"
334
388
  elif [[ -n "$state" ]]; then
335
389
  echo " issue #$n already $state — skipped"
336
390
  fi
337
391
  done
338
392
 
339
- # Locate the branch's worktree + the main checkout, then clean up FROM the main checkout (#371).
340
- # Run from the PR's own worktree, removing that worktree deleted our cwd, and every git call after
341
- # it died with "Unable to read current working directory" — leaving the local branch behind. The
342
- # main checkout = the parent of the shared git dir; fall back to the first `worktree list` entry.
343
- local common
344
- wt=$(git worktree list --porcelain | awk -v b="refs/heads/$branch" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
393
+ # Clean up FROM the main checkout (#371): run from the PR's own worktree, removing it deletes our
394
+ # cwd. Main checkout = parent of the shared git dir; fall back to the first `worktree list` entry.
395
+ wt=$(git worktree list --porcelain | awk -v b="refs/heads/$head" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
396
+ base_wt=$(git worktree list --porcelain | awk -v b="refs/heads/$pr_base" '/^worktree /{p=$2} /^branch /{if($2==b) print p}')
345
397
  common="$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)"
346
398
  main_wt=""
347
399
  [[ -n "$common" && "$(basename "$common")" == ".git" ]] && main_wt="$(dirname "$common")"
348
400
  [[ -n "$main_wt" ]] || main_wt=$(git worktree list --porcelain | awk '/^worktree /{print $2; exit}')
349
401
  cd "$main_wt" || { echo "✗ cannot reach the main checkout $main_wt for cleanup" >&2; return 1; }
350
- git -C "$main_wt" checkout "$base" 2>/dev/null || true
351
- git -C "$main_wt" pull origin "$base" 2>/dev/null || true
352
402
 
353
- if [[ -n "$wt" && "$wt" != "$main_wt" ]]; then
403
+ if [[ -n "$wt" && "$wt" != "$main_wt" ]] && ! _kto_protected "$head"; then
354
404
  git -C "$main_wt" worktree remove "$wt" --force && echo "✓ Removed worktree $wt"
355
405
  fi
356
- # Never delete the base (or a release) branch: merging a PR from the hub leaves $branch = base.
357
406
  # -D, not -d: a squash merge leaves the branch "not fully merged" by ancestry.
358
- case "$branch" in
359
- "$base"|main|master|develop|HEAD)
360
- echo " (on $branch — no branch cleanup; run cckit merge from the PR's worktree to GC it)" ;;
361
- *)
362
- git -C "$main_wt" branch -D "$branch" 2>/dev/null && echo "✓ Deleted local branch $branch"
363
- git -C "$main_wt" push origin --delete "$branch" 2>/dev/null && echo "✓ Deleted remote branch $branch" || echo " (remote branch already gone)" ;;
364
- esac
407
+ if _kto_protected "$head"; then
408
+ echo " ($head is protected — no branch cleanup)"
409
+ else
410
+ git -C "$main_wt" branch -D "$head" 2>/dev/null && echo "✓ Deleted local branch $head"
411
+ if cckit_gh_rest; then
412
+ # The cloud proxy rejects branch deletion (#396): keep it and say so.
413
+ echo " (remote branch $head kept — branch deletion is unavailable here; delete it on GitHub)"
414
+ else
415
+ git -C "$main_wt" push origin --delete "$head" 2>/dev/null && echo "✓ Deleted remote branch $head" || echo " (remote branch already gone)"
416
+ fi
417
+ fi
365
418
  git -C "$main_wt" worktree prune
366
- echo "✓ On $base, up to date — merged branch + worktree cleaned"
419
+
420
+ # Bring the PR's base up to date where it lives. The repo base: the main checkout switches to it and
421
+ # pulls. An effort/<N> base is checked out in its own worktree (git allows one checkout per branch),
422
+ # so that worktree fast-forwards; with no worktree, the local ref is fast-forwarded by fetch.
423
+ if [[ "$pr_base" == "$base" ]]; then
424
+ git -C "$main_wt" checkout "$pr_base" 2>/dev/null || true
425
+ git -C "$main_wt" pull --ff-only origin "$pr_base" 2>/dev/null || true
426
+ echo "✓ On $pr_base in $main_wt, up to date — merged branch + worktree cleaned"
427
+ elif [[ -n "$base_wt" ]]; then
428
+ git -C "$base_wt" pull --ff-only origin "$pr_base" 2>/dev/null || true
429
+ echo "✓ $pr_base updated in $base_wt — continue there (merged branch + worktree cleaned)"
430
+ else
431
+ git -C "$main_wt" fetch origin "$pr_base:$pr_base" 2>/dev/null || git -C "$main_wt" fetch origin "$pr_base" 2>/dev/null || true
432
+ echo "✓ $pr_base fetched (no worktree has it checked out) — merged branch + worktree cleaned"
433
+ fi
367
434
  }
368
435
 
369
436
  # kit task new — create a GitHub issue with labels + milestone, add it to the board, set Role +
@@ -383,9 +450,13 @@ kto_task_new() {
383
450
  labels="kind:${kind},priority:${prio},role:${role_slug}"
384
451
  body="$(kto_compose_issue_body "$body_user" "$plan" "$blocked" "$role_slug")"
385
452
 
453
+ if cckit_gh_rest; then
454
+ url=$(gh_rest_issue_create "$repo" "$title" "$body" "$labels" "$milestone") || return 1
455
+ else
386
456
  url=$(gh issue create --repo "$repo" \
387
457
  --title "$title" --body "$body" \
388
458
  --label "$labels" ${milestone:+--milestone "$milestone"} --assignee "@me") || return 1
459
+ fi
389
460
  num=$(basename "$url")
390
461
  echo "✓ $url"
391
462
 
@@ -407,16 +478,62 @@ kto_task_new() {
407
478
  printf '%s' "$num"
408
479
  }
409
480
 
410
- # kit task close — set the board Status to Done and close the issue (with a summary comment).
411
- # Plan-badge flip stays in the skill (it edits a repo file → needs a worktree/PR). Args: <num> <summary> [pr-num]
481
+ # kit task close — set the board Status to Done and close the issue with a summary comment and a
482
+ # close reason (#377). Plan-badge flip stays in the skill (it edits a repo file → needs a worktree/PR).
483
+ # Args: <num> <summary> [pr-num] [--reason completed|not-planned|duplicate] [--duplicate-of <N>]
484
+ # [--superseded-by <N>]
485
+ # --duplicate-of N GitHub state_reason duplicate, linked to #N (implies --reason duplicate)
486
+ # --superseded-by N state_reason not planned + "Superseded by #N" in the comment
487
+ # The summary may be omitted with --duplicate-of / --superseded-by: it defaults to "Duplicate of #N."
488
+ # / "Superseded by #N.". Flags may come anywhere; the closed set is the close-reason vocabulary.
412
489
  kto_task_close() {
413
- local num="${1:-}" summary="${2:-}"
414
- local repo issue_json state item
415
- repo="$(kto_repo)"
490
+ local num="" summary="" reason="" dup="" sup="" a pos=0
491
+ while [[ $# -gt 0 ]]; do
492
+ a="$1"
493
+ case "$a" in
494
+ --reason) reason="${2:-}"; shift 2 ;;
495
+ --reason=*) reason="${a#--reason=}"; shift ;;
496
+ --duplicate-of) dup="${2:-}"; shift 2 ;;
497
+ --superseded-by) sup="${2:-}"; shift 2 ;;
498
+ -*) echo "✗ kto_task_close: unknown flag '$a' — usage: cckit close <issue> [\"<summary>\"] [--reason completed|not-planned|duplicate] [--duplicate-of N] [--superseded-by N]" >&2; return 2 ;;
499
+ *)
500
+ pos=$((pos+1))
501
+ case "$pos" in 1) num="$a" ;; 2) summary="$a" ;; *) : ;; esac # 3rd positional = legacy pr-num
502
+ shift ;;
503
+ esac
504
+ done
505
+ dup="${dup#\#}"; sup="${sup#\#}"
416
506
  [[ -n "$num" ]] || { echo "✗ kto_task_close: issue number required" >&2; return 1; }
507
+ [[ -z "$dup" || "$dup" =~ ^[0-9]+$ ]] || { echo "✗ kto_task_close: --duplicate-of needs an issue number" >&2; return 2; }
508
+ [[ -z "$sup" || "$sup" =~ ^[0-9]+$ ]] || { echo "✗ kto_task_close: --superseded-by needs an issue number" >&2; return 2; }
509
+ [[ -n "$dup" && -n "$sup" ]] && { echo "✗ kto_task_close: --duplicate-of and --superseded-by are exclusive" >&2; return 2; }
510
+
511
+ # The close-reason set (completed · not planned · duplicate · superseded) → GitHub's state_reason.
512
+ local gh_reason
513
+ case "$reason" in
514
+ ""|completed) gh_reason="completed" ;;
515
+ not-planned|"not planned"|not_planned) gh_reason="not planned" ;;
516
+ duplicate) gh_reason="duplicate" ;;
517
+ *) echo "✗ kto_task_close: --reason must be completed, not-planned or duplicate (got '$reason')" >&2; return 2 ;;
518
+ esac
519
+ if [[ -n "$dup" ]]; then
520
+ [[ -z "$reason" || "$gh_reason" == "duplicate" ]] || { echo "✗ kto_task_close: --duplicate-of implies --reason duplicate" >&2; return 2; }
521
+ gh_reason="duplicate"; summary="${summary:-Duplicate of #$dup.}"
522
+ fi
523
+ if [[ -n "$sup" ]]; then
524
+ [[ -z "$reason" || "$gh_reason" == "not planned" ]] || { echo "✗ kto_task_close: --superseded-by implies --reason not-planned" >&2; return 2; }
525
+ gh_reason="not planned"
526
+ if [[ -z "$summary" ]]; then summary="Superseded by #$sup."
527
+ else case "$summary" in *"#$sup"*) : ;; *) summary="$summary"$'\n\n'"Superseded by #$sup." ;; esac
528
+ fi
529
+ fi
530
+ [[ "$gh_reason" == "duplicate" && -z "$dup" ]] && { echo "✗ kto_task_close: --reason duplicate needs --duplicate-of <N>" >&2; return 2; }
417
531
  [[ -n "$summary" ]] || { echo "✗ kto_task_close: a summary is required" >&2; return 1; }
418
532
 
419
- issue_json=$(gh issue view "$num" --repo "$repo" --json state,projectItems) || return 1
533
+ local repo issue_json state item
534
+ repo="$(kto_repo)"
535
+ if cckit_gh_rest; then issue_json=$(gh_rest_issue_json "$repo" "$num") || return 1
536
+ else issue_json=$(gh issue view "$num" --repo "$repo" --json state,projectItems) || return 1; fi
420
537
  state=$(printf '%s' "$issue_json" | jq -r .state)
421
538
 
422
539
  if kto_board_enabled && type load_project_ids >/dev/null 2>&1 && load_project_ids 2>/dev/null; then
@@ -426,8 +543,20 @@ kto_task_close() {
426
543
  echo "✓ Board Status → Done for #$num" || true
427
544
  fi
428
545
 
429
- if [[ "$state" == "OPEN" ]]; then
430
- gh issue close "$num" --repo "$repo" --comment "$summary" && echo "✓ Closed issue #$num"
546
+ if [[ "$state" == "OPEN" ]] && cckit_gh_rest; then
547
+ # REST has no duplicate link (#396): state_reason duplicate, and the comment names the original.
548
+ if [[ -n "$dup" ]]; then
549
+ case "$summary" in *"#$dup"*) : ;; *) summary="$summary"$'\n\n'"Duplicate of #$dup." ;; esac
550
+ fi
551
+ gh_rest_issue_close "$repo" "$num" "$gh_reason" "$summary" || return 1
552
+ echo "✓ Closed issue #$num ($gh_reason${dup:+ of #$dup}${sup:+, superseded by #$sup})"
553
+ elif [[ "$state" == "OPEN" ]]; then
554
+ if [[ -n "$dup" ]]; then
555
+ gh issue close "$num" --repo "$repo" --duplicate-of "$dup" --comment "$summary" || return 1
556
+ else
557
+ gh issue close "$num" --repo "$repo" --reason "$gh_reason" --comment "$summary" || return 1
558
+ fi
559
+ echo "✓ Closed issue #$num ($gh_reason${dup:+ of #$dup}${sup:+, superseded by #$sup})"
431
560
  else
432
561
  echo " issue #$num already $state — board status updated only"
433
562
  fi
@@ -0,0 +1,83 @@
1
+ #!/usr/bin/env bash
2
+ # kto-task-close-test.sh — `cckit close` closes with a reason from the close-reason set, maps it to
3
+ # GitHub's state_reason, still posts the summary and still sets the board to Done (#377).
4
+ # Hermetic: stubbed gh and board helpers. Run: bash scripts/lib/kto-task-close-test.sh
5
+ # errors: strict — a test runner: rc 1 on any failed assertion
6
+ set -u
7
+ LIB="$(cd "$(dirname "$0")" && pwd)"
8
+ ROOT="$(cd "$LIB/../.." && pwd)"
9
+ fail=0
10
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
11
+ tc() { if grep -qE -- "$2" "$1"; then echo "ok: $3"; else echo "FAIL: $3 (no /$2/ in $1)"; fail=1; fi; }
12
+ command -v jq >/dev/null 2>&1 || { echo "kto-task-close-test: jq required" >&2; exit 1; }
13
+
14
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
15
+ export GH_LOG="$tmp/gh.log" GH_STATE="OPEN"
16
+ mkdir -p "$tmp/bin"
17
+ cat > "$tmp/bin/gh" <<'SH'
18
+ #!/usr/bin/env bash
19
+ echo "$*" >> "$GH_LOG"
20
+ case "$1 $2" in
21
+ "issue view") printf '{"state":"%s","projectItems":[]}\n' "$GH_STATE" ;;
22
+ *) exit 0 ;;
23
+ esac
24
+ SH
25
+ chmod +x "$tmp/bin/gh"
26
+ export PATH="$tmp/bin:$PATH" KIT_REPO="o/r" KIT_PROJECTS_V2="true"
27
+ unset KIT_CONFIG
28
+
29
+ # shellcheck source=/dev/null
30
+ . "$LIB/kit-task-ops.sh"
31
+ # Board stubs: the close must still move the item to Done whatever the reason.
32
+ BOARD_LOG="$tmp/board.log"
33
+ load_project_ids() { STATUS_FIELD_ID="SF"; STATUS_OPT_DONE="DONE"; return 0; }
34
+ project_find_item_by_issue() { echo "ITEM-$1"; }
35
+ project_set_single_select() { echo "$*" >> "$BOARD_LOG"; }
36
+
37
+ run() { : > "$GH_LOG"; : > "$BOARD_LOG"; kto_task_close "$@" >/dev/null 2>"$tmp/err"; echo $?; }
38
+ closed() { grep -E '^issue close ' "$GH_LOG" | head -1; }
39
+
40
+ t "default close succeeds" "$(run 5 "Shipped in #9.")" 0
41
+ tc "$GH_LOG" '^issue close 5 --repo o/r --reason completed --comment Shipped in #9\.$' "default reason is completed, with the summary"
42
+ tc "$BOARD_LOG" '^ITEM-5 SF DONE$' "the board goes to Done"
43
+
44
+ t "--reason not-planned succeeds" "$(run 6 "Measured; not worth it." --reason not-planned)" 0
45
+ tc "$GH_LOG" '^issue close 6 --repo o/r --reason not planned --comment Measured; not worth it\.$' "not-planned maps to GitHub's 'not planned'"
46
+ tc "$BOARD_LOG" '^ITEM-6 SF DONE$' "not planned still sets Done"
47
+
48
+ t "--duplicate-of succeeds without a summary" "$(run 7 --duplicate-of 4)" 0
49
+ tc "$GH_LOG" '^issue close 7 --repo o/r --duplicate-of 4 --comment Duplicate of #4\.$' "--duplicate-of links the original and defaults the comment"
50
+ tc "$BOARD_LOG" '^ITEM-7 SF DONE$' "a duplicate still sets Done"
51
+ t "--reason duplicate + --duplicate-of" "$(run 7 "Same as #4." --reason duplicate --duplicate-of '#4')" 0
52
+ tc "$GH_LOG" '^issue close 7 --repo o/r --duplicate-of 4 --comment Same as #4\.$' "an explicit summary is kept; '#4' is accepted"
53
+
54
+ t "--superseded-by succeeds" "$(run 8 "Folded into the new effort." --superseded-by 12)" 0
55
+ tc "$GH_LOG" '^issue close 8 --repo o/r --reason not planned --comment Folded into the new effort\.$' "superseded closes as not planned"
56
+ tc "$GH_LOG" '^Superseded by #12\.$' "and the comment says Superseded by #12"
57
+ t "--superseded-by alone defaults the comment" "$(run 8 --superseded-by 12)" 0
58
+ tc "$GH_LOG" '^issue close 8 --repo o/r --reason not planned --comment Superseded by #12\.$' "default superseded comment"
59
+
60
+ t "--reason completed is accepted explicitly" "$(run 9 "Done." --reason completed)" 0
61
+ tc "$GH_LOG" '^issue close 9 --repo o/r --reason completed ' "explicit completed"
62
+
63
+ # refusals: nothing is closed and the board is not touched
64
+ t "an unknown reason is refused (rc 2)" "$(run 10 "x" --reason wontfix)" 2
65
+ t "an unknown flag is refused (rc 2)" "$(run 10 "x" --force)" 2
66
+ t "duplicate without --duplicate-of (rc 2)" "$(run 10 "x" --reason duplicate)" 2
67
+ t "--duplicate-of contradicting --reason (rc 2)" "$(run 10 "x" --reason completed --duplicate-of 4)" 2
68
+ t "--duplicate-of + --superseded-by (rc 2)" "$(run 10 "x" --duplicate-of 4 --superseded-by 5)" 2
69
+ t "a non-number --duplicate-of (rc 2)" "$(run 10 "x" --duplicate-of abc)" 2
70
+ t "no summary and no default (rc 1)" "$(run 10)" 1
71
+ t "refusals closed nothing" "$(grep -c 'issue close' "$GH_LOG")" 0
72
+ t "refusals left the board alone" "$(wc -l < "$BOARD_LOG" | tr -d ' ')" 0
73
+
74
+ # an already-closed issue: board only, no second close
75
+ GH_STATE="CLOSED"
76
+ t "an already-closed issue is fine" "$(run 11 "x" --reason not-planned)" 0
77
+ t "it is not closed again" "$(grep -c '^issue close' "$GH_LOG")" 0
78
+ tc "$BOARD_LOG" '^ITEM-11 SF DONE$' "but the board still goes to Done"
79
+
80
+ t "the dispatcher answers --help" "$("$ROOT/bin/cckit" close --help | cut -c1-19)" "cckit close <issue>"
81
+
82
+ [ "$fail" -eq 0 ] && echo "kto-task-close: all passed" || echo "kto-task-close: FAILURES"
83
+ exit "$fail"
@@ -1,5 +1,7 @@
1
1
  #!/usr/bin/env bash
2
- # kto-task-pr-merge-test.sh — kto_task_pr_merge never acts on the base branch (#366).
2
+ # kto-task-pr-merge-test.sh — kto_task_pr_merge never acts on the base branch (#366), cleans up from
3
+ # the PR's own worktree (#371), and merges by squash, merge commit or rebase into develop or an
4
+ # effort branch (#376).
3
5
  # A flag taken as the PR number used to "merge" (gh pr merge --help exits 0) and then run the branch
4
6
  # cleanup on the base branch; a conflicting PR merged from the hub would rebase + force-push it.
5
7
  # Hermetic: stubbed gh, throwaway repo + bare remote. Run: bash scripts/lib/kto-task-pr-merge-test.sh
@@ -12,13 +14,14 @@ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]'
12
14
  command -v jq >/dev/null 2>&1 || { echo "kto-task-pr-merge-test: jq required" >&2; exit 1; }
13
15
 
14
16
  tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
15
- export GH_LOG="$tmp/gh.log" GH_MERGEABLE="MERGEABLE"; : > "$GH_LOG"
17
+ export GH_LOG="$tmp/gh.log" GH_MERGEABLE="MERGEABLE" GH_BASE="develop" GH_HEAD=""; : > "$GH_LOG"
16
18
  mkdir -p "$tmp/bin"
17
19
  cat > "$tmp/bin/gh" <<'SH'
18
20
  #!/usr/bin/env bash
19
21
  echo "$*" >> "$GH_LOG"
20
22
  case "$1 $2" in
21
- "pr view") echo "{\"mergeable\":\"$GH_MERGEABLE\",\"status\":\"CLEAN\"}" ;;
23
+ "pr view") printf '{"mergeable":"%s","mergeStateStatus":"CLEAN","baseRefName":"%s","headRefName":"%s"}\n' \
24
+ "$GH_MERGEABLE" "${GH_BASE:-develop}" "${GH_HEAD:-}" ;;
22
25
  *) exit 0 ;;
23
26
  esac
24
27
  SH
@@ -39,14 +42,31 @@ kto_task_pr_merge --help >/dev/null 2>&1; rc=$?
39
42
  t "a flag is refused as a PR number (rc 2)" "$rc" "2"
40
43
  t "nothing was merged on a refused flag" "$(grep -c 'pr merge' "$GH_LOG")" "0"
41
44
  t "the dispatcher answers --help with usage" "$("$ROOT/bin/cckit" merge --help | cut -c1-18)" "cckit merge [<pr>]"
45
+ # #376: a bad flag, or a second PR number, is refused with usage and merges nothing
46
+ kto_task_pr_merge 7 --fast >/dev/null 2>"$tmp/errf"; rc=$?
47
+ t "an unknown flag is refused (rc 2)" "$rc" "2"
48
+ t "the refusal prints the usage" "$(grep -c 'usage: cckit merge \[<pr>\] \[--squash|--merge|--rebase\]' "$tmp/errf")" "1"
49
+ kto_task_pr_merge 7 8 >/dev/null 2>&1; rc=$?
50
+ t "two PR numbers are refused (rc 2)" "$rc" "2"
51
+ t "nothing was merged on a refused flag" "$(grep -c 'pr merge' "$GH_LOG")" "0"
42
52
 
43
- : > "$GH_LOG"
53
+ : > "$GH_LOG"; GH_HEAD="fix/7-x"
44
54
  kto_task_pr_merge 7 >/dev/null 2>&1
45
55
  t "a numeric PR merges" "$(grep -c 'pr merge 7' "$GH_LOG")" "1"
56
+ t "the default method is still squash" "$(grep -c 'pr merge 7 --repo o/r --squash' "$GH_LOG")" "1"
57
+ # #376: --merge (a merge commit) and --rebase reach gh unchanged
58
+ : > "$GH_LOG"; GH_HEAD="fix/10-x"
59
+ kto_task_pr_merge 10 --merge >/dev/null 2>&1; rc=$?
60
+ t "--merge succeeds" "$rc" "0"
61
+ t "--merge asks GitHub for a merge commit" "$(grep -c 'pr merge 10 --repo o/r --merge' "$GH_LOG")" "1"
62
+ : > "$GH_LOG"; GH_HEAD="fix/13-x"
63
+ kto_task_pr_merge --rebase 13 >/dev/null 2>&1; rc=$?
64
+ t "--rebase succeeds (flag before the number)" "$rc" "0"
65
+ t "--rebase asks GitHub for a rebase merge" "$(grep -c 'pr merge 13 --repo o/r --rebase' "$GH_LOG")" "1"
46
66
  t "merging from the base branch keeps it" "$(git branch --list develop | tr -d ' *')" "develop"
47
67
  t "and keeps it on origin" "$(git ls-remote --heads origin develop | wc -l | tr -d ' ')" "1"
48
68
 
49
- : > "$GH_LOG"; GH_MERGEABLE="CONFLICTING"
69
+ : > "$GH_LOG"; GH_MERGEABLE="CONFLICTING"; GH_HEAD="fix/8-x"
50
70
  before="$(git rev-parse develop)"
51
71
  kto_task_pr_merge 8 >/dev/null 2>&1; rc=$?
52
72
  t "a conflicting PR from the base branch is refused" "$rc" "1"
@@ -54,7 +74,7 @@ t "the base branch was not rebased" "$(git rev-parse develop)" "$be
54
74
  t "nothing was merged on a conflict refusal" "$(grep -c 'pr merge' "$GH_LOG")" "0"
55
75
 
56
76
  # ── #371: merged from the PR's OWN worktree, the cleanup still finishes ─────────────────────────
57
- : > "$GH_LOG"; GH_MERGEABLE="MERGEABLE"
77
+ : > "$GH_LOG"; GH_MERGEABLE="MERGEABLE"; GH_HEAD="fix/9-x"
58
78
  wt="$tmp/work/.claude/worktrees/fix+9-x"
59
79
  git worktree add -q -b fix/9-x "$wt" develop
60
80
  git -C "$wt" commit -q --allow-empty -m "the fix"
@@ -68,5 +88,42 @@ t "its remote branch is deleted" "$(git ls-remote --heads origi
68
88
  t "no 'Unable to read current working directory'" "$(grep -c 'current working directory' "$tmp/err9")" "0"
69
89
  t "the base branch survives" "$(git branch --list develop | tr -d ' *')" "develop"
70
90
 
91
+ # ── #376: a sub PR whose base is an effort branch ────────────────────────────────────────────────
92
+ git checkout -q develop
93
+ ewt="$tmp/work/.claude/worktrees/effort+5-e"
94
+ git worktree add -q -b effort/5-e "$ewt" develop
95
+ git -C "$ewt" commit -q --allow-empty -m "effort start"; git -C "$ewt" push -q origin effort/5-e
96
+ swt="$tmp/work/.claude/worktrees/sub+5a-x"
97
+ git worktree add -q -b sub/5a-x "$swt" effort/5-e
98
+ git -C "$swt" commit -q --allow-empty -m "sub a"; git -C "$swt" push -q origin sub/5a-x
99
+ # what GitHub's merge does to the effort branch: a new commit on origin/effort/5-e
100
+ git clone -q "$tmp/remote.git" "$tmp/gh-side" 2>/dev/null
101
+ git -C "$tmp/gh-side" checkout -q effort/5-e
102
+ git -C "$tmp/gh-side" -c user.email=t@t -c user.name=t commit -q --allow-empty -m "Merge pull request #11"
103
+ git -C "$tmp/gh-side" push -q origin effort/5-e
104
+
105
+ : > "$GH_LOG"; GH_BASE="effort/5-e"; GH_HEAD="sub/5a-x"
106
+ ( cd "$swt" && kto_task_pr_merge 11 --merge ) >/dev/null 2>&1; rc=$?
107
+ t "a sub PR into an effort branch merges (rc 0)" "$rc" "0"
108
+ t "with a merge commit" "$(grep -c 'pr merge 11 --repo o/r --merge' "$GH_LOG")" "1"
109
+ t "the sub worktree is removed" "$([ -d "$swt" ] && echo present || echo gone)" "gone"
110
+ t "the sub branch is deleted" "$(git branch --list sub/5a-x | wc -l | tr -d ' ')" "0"
111
+ t "the effort branch survives" "$(git branch --list effort/5-e | tr -d ' *+')" "effort/5-e"
112
+ t "the effort worktree survives" "$([ -d "$ewt" ] && echo present || echo gone)" "present"
113
+ t "the effort worktree is pulled to the merge" "$(git -C "$ewt" rev-parse HEAD)" "$(git -C "$tmp/gh-side" rev-parse HEAD)"
114
+ t "the main checkout stays on develop" "$(git rev-parse --abbrev-ref HEAD)" "develop"
115
+
116
+ # Run from the EFFORT worktree with a sub PR number: the current branch is the PR's base, and it
117
+ # must never be treated as the branch to clean up (before #376 this deleted effort/5-e).
118
+ git branch -q sub/5b-y effort/5-e; git push -q origin sub/5b-y
119
+ : > "$GH_LOG"; GH_HEAD="sub/5b-y"
120
+ ( cd "$ewt" && kto_task_pr_merge 12 ) >/dev/null 2>&1; rc=$?
121
+ t "merging a sub PR from the effort worktree: rc 0" "$rc" "0"
122
+ t "the effort branch still survives" "$(git branch --list effort/5-e | tr -d ' *+')" "effort/5-e"
123
+ t "the effort worktree still survives" "$([ -d "$ewt" ] && echo present || echo gone)" "present"
124
+ t "the sub branch (not the cwd branch) is deleted" "$(git branch --list sub/5b-y | wc -l | tr -d ' ')" "0"
125
+ t "and on origin" "$(git ls-remote --heads origin sub/5b-y | wc -l | tr -d ' ')" "0"
126
+ t "effort/5-e is still on origin" "$(git ls-remote --heads origin effort/5-e | wc -l | tr -d ' ')" "1"
127
+
71
128
  [ "$fail" -eq 0 ] && echo "kto-task-pr-merge: all passed" || echo "kto-task-pr-merge: FAILURES"
72
129
  exit "$fail"
@@ -7,16 +7,16 @@ LIB="$(cd "$(dirname "$0")" && pwd)"
7
7
  fail=0
8
8
  t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
9
9
 
10
- out="$(KIT_CONFIG=/nonexistent CCKIT_OUTPUT=json EFFORT_REPO=x/y KEEP_ME=1 bash -c '
10
+ out="$(KIT_CONFIG=/nonexistent CCKIT_OUTPUT=json EFFORT_REPO=x/y CLAUDE_CODE_REMOTE=true KEEP_ME=1 bash -c '
11
11
  . "$1/test-env.sh"
12
12
  test_env_unset_args | paste -sd" " -
13
13
  test_env_seal
14
- echo "${KIT_CONFIG-unset} ${CCKIT_OUTPUT-unset} ${EFFORT_REPO-unset} ${KEEP_ME-unset}"' _ "$LIB")"
14
+ echo "${KIT_CONFIG-unset} ${CCKIT_OUTPUT-unset} ${EFFORT_REPO-unset} ${CLAUDE_CODE_REMOTE-unset} ${KEEP_ME-unset}"' _ "$LIB")"
15
15
  args="$(printf '%s\n' "$out" | sed -n 1p)"
16
16
  after="$(printf '%s\n' "$out" | sed -n 2p)"
17
17
  case " $args " in *" -u KIT_CONFIG "*) echo "ok: unset args name KIT_CONFIG" ;; *) echo "FAIL: unset args: $args"; fail=1 ;; esac
18
18
  case " $args " in *" -u KEEP_ME "*) echo "FAIL: unset args name a non-kit variable: $args"; fail=1 ;; *) echo "ok: unset args leave other variables alone" ;; esac
19
- t "seal unsets KIT_*, CCKIT_*, EFFORT_* and keeps the rest" "$after" "unset unset unset 1"
19
+ t "seal unsets KIT_*, CCKIT_*, EFFORT_*, CLAUDE_CODE_REMOTE and keeps the rest" "$after" "unset unset unset unset 1"
20
20
 
21
21
  # The runner seals too: a test run under scripts/test.sh never sees the caller's KIT_CONFIG.
22
22
  if [ -n "${KIT_CONFIG:-}" ]; then echo "FAIL: KIT_CONFIG leaked into this test"; fail=1; else echo "ok: no KIT_CONFIG reached this test"; fi