@jeiemgi/cckit 0.10.0 → 0.12.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.
Files changed (193) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/AGENTS.md +136 -127
  3. package/README.md +66 -43
  4. package/bin/cckit +151 -181
  5. package/cckit.config.json +1 -4
  6. package/package.json +1 -1
  7. package/scripts/advisor-test.sh +55 -0
  8. package/scripts/autopilot-run-test.sh +20 -12
  9. package/scripts/autopilot-run.sh +9 -1
  10. package/scripts/autopilot-test.sh +29 -13
  11. package/scripts/autopilot.sh +4 -4
  12. package/scripts/benchmark/verb-timings-test.sh +4 -0
  13. package/scripts/benchmark/verb-timings.sh +16 -5
  14. package/scripts/capture-project-ids.sh +23 -1
  15. package/scripts/check.sh +25 -0
  16. package/scripts/content-skills-test.sh +1 -1
  17. package/scripts/contribute.sh +8 -3
  18. package/scripts/copilot-loop-test.sh +66 -81
  19. package/scripts/exec-targets-test.sh +20 -0
  20. package/scripts/force-push-fence-test.sh +44 -0
  21. package/scripts/force-push-fence.baseline +1 -0
  22. package/scripts/force-push-fence.sh +51 -0
  23. package/scripts/forge-fence-test.sh +69 -0
  24. package/scripts/forge-fence.baseline +2 -0
  25. package/scripts/forge-fence.sh +76 -0
  26. package/scripts/forge-report-test.sh +100 -0
  27. package/scripts/forge-report.sh +176 -0
  28. package/scripts/init-guided-test.sh +94 -82
  29. package/scripts/init.sh +12 -7
  30. package/scripts/install-test.sh +26 -0
  31. package/scripts/install.sh +7 -0
  32. package/scripts/instruction-budget-baseline.txt +1 -1
  33. package/scripts/kit-doctor-test.sh +60 -12
  34. package/scripts/kit-doctor.sh +170 -77
  35. package/scripts/lib/agent-resolve-test.sh +2 -0
  36. package/scripts/lib/agent-resolve.sh +15 -8
  37. package/scripts/lib/board-view.sh +3 -3
  38. package/scripts/lib/captain.sh +37 -8
  39. package/scripts/lib/content-kinds-gc-test.sh +22 -21
  40. package/scripts/lib/copilot-test.sh +20 -11
  41. package/scripts/lib/copilot.sh +19 -13
  42. package/scripts/lib/effort-close-subs-test.sh +96 -0
  43. package/scripts/lib/effort-dispatch-test.sh +5 -3
  44. package/scripts/lib/effort-metrics-test.sh +7 -12
  45. package/scripts/lib/effort-metrics.sh +17 -46
  46. package/scripts/lib/effort-ops-test.sh +261 -167
  47. package/scripts/lib/effort-ops.sh +449 -171
  48. package/scripts/lib/effort-parity-test.sh +57 -62
  49. package/scripts/lib/effort-plan-test.sh +56 -54
  50. package/scripts/lib/effort-plan.sh +14 -7
  51. package/scripts/lib/effort-review-test.sh +107 -0
  52. package/scripts/lib/effort-review.sh +93 -0
  53. package/scripts/lib/effort-slug-test.sh +34 -14
  54. package/scripts/lib/effort-slug.sh +13 -13
  55. package/scripts/lib/effort.sh +58 -80
  56. package/scripts/lib/forge-github.sh +260 -0
  57. package/scripts/lib/forge-none.sh +24 -0
  58. package/scripts/lib/forge-test.sh +186 -0
  59. package/scripts/lib/forge.sh +133 -0
  60. package/scripts/lib/gh-board-add-test.sh +118 -0
  61. package/scripts/lib/gh-project-test.sh +151 -31
  62. package/scripts/lib/gh-project.sh +139 -11
  63. package/scripts/lib/gh-rest-test.sh +63 -118
  64. package/scripts/lib/gh-rest.sh +275 -52
  65. package/scripts/lib/git-core-test.sh +128 -0
  66. package/scripts/lib/grill-record-test.sh +49 -0
  67. package/scripts/lib/grill-record.sh +88 -0
  68. package/scripts/lib/handoff.sh +1 -1
  69. package/scripts/lib/init-github.sh +38 -13
  70. package/scripts/lib/kinds-test.sh +13 -16
  71. package/scripts/lib/kit-brief-test.sh +51 -10
  72. package/scripts/lib/kit-brief.sh +103 -18
  73. package/scripts/lib/kit-gc-test.sh +90 -109
  74. package/scripts/lib/kit-gc.sh +19 -22
  75. package/scripts/lib/kit-hotfix.sh +18 -16
  76. package/scripts/lib/kit-interview.sh +6 -0
  77. package/scripts/lib/kit-lib-test.sh +1 -1
  78. package/scripts/lib/kit-lib.sh +7 -7
  79. package/scripts/lib/kit-msg.sh +3 -3
  80. package/scripts/lib/kit-status.sh +19 -14
  81. package/scripts/lib/kit-task-ops-test.sh +20 -1
  82. package/scripts/lib/kit-task-ops.sh +177 -94
  83. package/scripts/lib/kto-hotfix-test.sh +53 -58
  84. package/scripts/lib/kto-task-close-test.sh +28 -27
  85. package/scripts/lib/kto-task-new-test.sh +55 -48
  86. package/scripts/lib/kto-task-pr-merge-test.sh +77 -41
  87. package/scripts/lib/kto-task-pr-test.sh +30 -34
  88. package/scripts/lib/light-task-run-test.sh +19 -15
  89. package/scripts/lib/light-task-test.sh +34 -29
  90. package/scripts/lib/light-task.sh +24 -13
  91. package/scripts/lib/loose-issues.sh +65 -0
  92. package/scripts/lib/naming-test.sh +112 -0
  93. package/scripts/lib/naming-verbs-test.sh +126 -0
  94. package/scripts/lib/naming.sh +172 -0
  95. package/scripts/lib/next-test.sh +27 -22
  96. package/scripts/lib/next.sh +13 -7
  97. package/scripts/lib/orchestration-runtime-test.sh +11 -8
  98. package/scripts/lib/plan-machine.sh +22 -16
  99. package/scripts/lib/plan-next.sh +4 -4
  100. package/scripts/lib/pr-evidence-test.sh +63 -74
  101. package/scripts/lib/pr-evidence.sh +15 -12
  102. package/scripts/lib/qa-lane-test.sh +59 -27
  103. package/scripts/lib/qa-lane.sh +14 -6
  104. package/scripts/lib/review-page-test.sh +34 -25
  105. package/scripts/lib/review-page.sh +10 -4
  106. package/scripts/lib/spec-lint-test.sh +63 -0
  107. package/scripts/lib/spec-lint.sh +95 -0
  108. package/scripts/lib/sprint-test.sh +96 -0
  109. package/scripts/lib/sprint.sh +216 -0
  110. package/scripts/lib/stage-receipt-test.sh +169 -19
  111. package/scripts/lib/stage-receipt.sh +195 -10
  112. package/scripts/lib/test-forge-shim.sh +115 -0
  113. package/scripts/lib/verbs.sh +105 -0
  114. package/scripts/lib/verbs.tsv +92 -0
  115. package/scripts/lib/version-bump-test.sh +20 -0
  116. package/scripts/lib/version-bump.sh +4 -2
  117. package/scripts/lib/worker-brief-test.sh +1 -1
  118. package/scripts/lib/worker-brief.sh +1 -1
  119. package/scripts/lib/worktree-issue-test.sh +24 -19
  120. package/scripts/lib/worktree-issue.sh +10 -10
  121. package/scripts/lib/worktree-start-test.sh +13 -16
  122. package/scripts/lib/worktree-start.sh +57 -7
  123. package/scripts/loose-issues-test.sh +102 -0
  124. package/scripts/loose-issues.baseline +5 -0
  125. package/scripts/marketing-walkthrough-test.sh +25 -30
  126. package/scripts/night-grant.sh +14 -8
  127. package/scripts/night-run.mjs +1 -1
  128. package/scripts/orchestrate-test.sh +27 -31
  129. package/scripts/orchestrate.sh +9 -3
  130. package/scripts/portable-test.sh +37 -36
  131. package/scripts/publish.sh +9 -3
  132. package/scripts/setup-labels.sh +23 -12
  133. package/scripts/setup-milestones.sh +8 -2
  134. package/scripts/status-test.sh +39 -33
  135. package/scripts/status.sh +12 -6
  136. package/scripts/task-sync-test.sh +75 -32
  137. package/scripts/task-sync.sh +97 -6
  138. package/scripts/terms-check-test.sh +191 -0
  139. package/scripts/terms-check.sh +242 -0
  140. package/scripts/verbs-gen.sh +43 -0
  141. package/scripts/verbs-help-test.sh +61 -0
  142. package/scripts/verbs-ratchet-test.sh +69 -0
  143. package/scripts/verbs-ratchet.sh +34 -0
  144. package/scripts/verbs-test.sh +120 -0
  145. package/scripts/verbs.baseline +5 -0
  146. package/skills/kit-add/SKILL.md +2 -1
  147. package/skills/kit-advisor/SKILL.md +75 -0
  148. package/skills/kit-advisor/references/close.md +53 -0
  149. package/skills/kit-advisor/references/grill.md +69 -0
  150. package/skills/kit-advisor/references/orchestrate.md +56 -0
  151. package/skills/kit-advisor/references/plan.md +64 -0
  152. package/skills/kit-advisor/references/review.md +47 -0
  153. package/skills/kit-advisor/references/step-report.md +61 -0
  154. package/skills/kit-annotate/SKILL.md +1 -0
  155. package/skills/kit-autopilot/SKILL.md +2 -1
  156. package/skills/kit-cleanup/SKILL.md +3 -2
  157. package/skills/kit-contribute/SKILL.md +1 -0
  158. package/skills/kit-customize/SKILL.md +1 -0
  159. package/skills/kit-dev/SKILL.md +1 -0
  160. package/skills/kit-digest/SKILL.md +1 -0
  161. package/skills/kit-docs-writer/SKILL.md +4 -3
  162. package/skills/kit-effort-close/SKILL.md +18 -9
  163. package/skills/kit-effort-new/SKILL.md +12 -8
  164. package/skills/kit-effort-pr/SKILL.md +8 -7
  165. package/skills/kit-effort-start/SKILL.md +1 -0
  166. package/skills/kit-gc/SKILL.md +1 -0
  167. package/skills/kit-next/SKILL.md +8 -7
  168. package/skills/kit-onboard/SKILL.md +1 -0
  169. package/skills/kit-page/SKILL.md +1 -0
  170. package/skills/kit-plan-next/SKILL.md +11 -11
  171. package/skills/kit-security-sweep/SKILL.md +1 -0
  172. package/skills/kit-ship/SKILL.md +1 -0
  173. package/skills/kit-status/SKILL.md +6 -5
  174. package/skills/kit-task-close/SKILL.md +1 -0
  175. package/skills/kit-task-new/SKILL.md +1 -0
  176. package/skills/kit-task-pr/SKILL.md +1 -0
  177. package/skills/kit-task-pr-auto/SKILL.md +2 -1
  178. package/skills/kit-task-pr-merge/SKILL.md +4 -3
  179. package/skills/kit-task-start/SKILL.md +1 -0
  180. package/skills/kit-task-sync/SKILL.md +3 -0
  181. package/skills/kit-ux-audit/SKILL.md +1 -0
  182. package/skills/routing.yaml +124 -0
  183. package/templates/hooks/kit-mail-check.sh.tmpl +1 -1
  184. package/templates/instructions.md.tmpl +3 -3
  185. package/templates/rules/authoring.md +7 -3
  186. package/templates/rules/delegation-brief.md +4 -0
  187. package/templates/rules/effort-model.md +34 -31
  188. package/templates/rules/gate-and-review.md +1 -1
  189. package/templates/rules/merge-safety.md +9 -7
  190. package/templates/rules/naming-and-ids.md +20 -24
  191. package/templates/rules/plan-next.md +5 -5
  192. package/templates/skills/campaign/SKILL.md +2 -2
  193. package/scripts/lib/engine-adapter.sh +0 -93
@@ -0,0 +1,260 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # forge-github.sh — the GitHub provider of the forge port (E537.2): REST over curl with a token from
4
+ # forge_token. No gh subcommand is run here; `gh` is used only by forge_token to read its token.
5
+ # CCKIT_FORGE_API the API base (default https://api.github.com); tests point it at a shim
6
+ # errors: strict — an HTTP error is rc 1 with the API message on stderr; jq is required for JSON
7
+ _fg_api_base() { printf '%s' "${CCKIT_FORGE_API:-https://api.github.com}"; }
8
+
9
+ # _fg_curl <method> <path> <body-or-empty> [accept] [headers-file] [extra-headers]: one request. The
10
+ # body on stdout, rc 1 on HTTP >= 400 (the API's message on stderr) or a transport failure. <path>
11
+ # may be a full URL. [extra-headers] is newline-separated `Name: value` lines.
12
+ _fg_curl() {
13
+ # `rpath`, not `path`: in zsh `path` is the array tied to PATH, and a local by that name hides
14
+ # every command from the function (the delegation brief's zsh gotcha).
15
+ local method="$1" rpath="$2" body="${3:-}" accept="${4:-application/vnd.github+json}" hdr="${5:-}" xh="${6:-}" token out code url line
16
+ token="$(forge_token)" || { echo "cckit: no forge token (GH_TOKEN, GITHUB_TOKEN, or gh auth login)" >&2; return 1; }
17
+ case "$rpath" in http://*|https://*) url="$rpath" ;; *) url="$(_fg_api_base)/${rpath#/}" ;; esac
18
+ set -- -sS -X "$method" -H "Authorization: Bearer $token" -H "Accept: $accept" -w '\n%{http_code}'
19
+ while IFS= read -r line; do [ -n "$line" ] && set -- "$@" -H "$line"; done <<EOF
20
+ $xh
21
+ EOF
22
+ [ -n "$hdr" ] && set -- "$@" -D "$hdr"
23
+ if [ -n "$body" ]; then set -- "$@" -H "Content-Type: application/json" --data-binary "$body"; fi
24
+ out="$(curl "$@" "$url")" || return 1
25
+ code="${out##*$'\n'}"; out="${out%$'\n'*}"
26
+ case "$code" in
27
+ 2*) [ -n "$out" ] && printf '%s\n' "$out"; return 0 ;;
28
+ *) printf 'cckit: GitHub answered %s on %s %s: %s\n' "$code" "$method" "${rpath#/}" \
29
+ "$(printf '%s' "$out" | jq -r '.message // .' 2>/dev/null | sed -n 1p)" >&2; return 1 ;;
30
+ esac
31
+ }
32
+
33
+ # forge_api <method> <path> [json-body]: the response body on stdout; rc 1 on an HTTP error.
34
+ forge_api() { _fg_curl "$1" "$2" "${3:-}"; }
35
+
36
+ # forge_graphql <query> [k=v …]: POST /graphql with the query and string variables; a value of the
37
+ # form N:<int> is an Int variable. The whole response (data and errors) on stdout, as gh api graphql
38
+ # prints it. Unavailable in a cloud session (the proxy refuses GraphQL): see cckit_board_skipped.
39
+ forge_graphql() {
40
+ local query="$1" vars='{}' kv k v; shift
41
+ for kv in "$@"; do
42
+ k="${kv%%=*}"; v="${kv#*=}"
43
+ case "$v" in
44
+ N:*) vars="$(printf '%s' "$vars" | jq --arg k "$k" --argjson v "${v#N:}" '.[$k] = $v')" ;;
45
+ *) vars="$(printf '%s' "$vars" | jq --arg k "$k" --arg v "$v" '.[$k] = $v')" ;;
46
+ esac
47
+ done
48
+ _fg_curl POST graphql "$(jq -n --arg q "$query" --argjson v "$vars" '{query: $q, variables: $v}')"
49
+ }
50
+
51
+ # forge_rest <endpoint> [gh api flags…]: the `gh api` argument form over curl, so a call site moves
52
+ # with its arguments unchanged. Understood: -X|--method, -f|--raw-field k=v (string), -F|--field k=v
53
+ # (true|false|null|integer typed, @file reads the file, k[]=v appends), -q|--jq <filter> (applied
54
+ # with jq -r, per page), --paginate (follows Link rel=next), -H|--header 'Name: value' (Accept
55
+ # replaces the default; any other header is sent as given),
56
+ # --input <file|-> (a raw JSON body), --silent. Fields go to the query string on GET and DELETE and
57
+ # to a JSON body otherwise; fields with no method mean POST. `graphql` as the endpoint posts the
58
+ # query= and variables as forge_graphql does. rc 1 on an HTTP error with nothing on stdout.
59
+ forge_rest() {
60
+ local ep="" method="" filter="" paginate=0 accept="application/vnd.github+json" input="" silent=0 xh=""
61
+ local body='{}' has_field=0 q="" k v raw typed arr
62
+ while [ $# -gt 0 ]; do
63
+ case "$1" in
64
+ -X|--method) method="$2"; shift 2 ;;
65
+ -q|--jq) filter="$2"; shift 2 ;;
66
+ --paginate) paginate=1; shift ;;
67
+ --silent) silent=1; shift ;;
68
+ -H|--header)
69
+ case "$2" in
70
+ [Aa]ccept:*) accept="$(printf '%s' "${2#*:}" | sed 's/^ *//')" ;;
71
+ *:*) xh="$xh${xh:+
72
+ }$2" ;;
73
+ esac
74
+ shift 2 ;;
75
+ --input) input="$2"; shift 2 ;;
76
+ -f|--raw-field|-F|--field)
77
+ raw="$2"; k="${raw%%=*}"; v="${raw#*=}"; has_field=1
78
+ typed=0; [ "$1" = "-F" ] || [ "$1" = "--field" ] && typed=1
79
+ if [ "$typed" = 1 ]; then
80
+ case "$v" in
81
+ @*) v="$(cat "${v#@}")"; typed=0 ;;
82
+ true|false|null) ;;
83
+ ''|*[!0-9-]*) typed=0 ;;
84
+ esac
85
+ fi
86
+ arr=0; case "$k" in *'[]') k="${k%\[\]}"; arr=1 ;; esac
87
+ if [ "$ep" = graphql ] && [ "$k" != query ]; then
88
+ # a GraphQL variable: typed integers travel as Int
89
+ if [ "$typed" = 1 ]; then q="$q${q:+ }$k=N:$v"; else q="$q${q:+ }$k=$v"; fi
90
+ elif [ "$ep" = graphql ]; then
91
+ body="$(printf '%s' "$body" | jq --arg v "$v" '.query = $v')"
92
+ elif [ "$typed" = 1 ]; then
93
+ if [ "$arr" = 1 ]; then body="$(printf '%s' "$body" | jq --arg k "$k" --argjson v "$v" '.[$k] += [$v]')"
94
+ else body="$(printf '%s' "$body" | jq --arg k "$k" --argjson v "$v" '.[$k] = $v')"; fi
95
+ else
96
+ if [ "$arr" = 1 ]; then body="$(printf '%s' "$body" | jq --arg k "$k" --arg v "$v" '.[$k] += [$v]')"
97
+ else body="$(printf '%s' "$body" | jq --arg k "$k" --arg v "$v" '.[$k] = $v')"; fi
98
+ fi
99
+ shift 2 ;;
100
+ -*) shift ;;
101
+ *) if [ -z "$ep" ]; then ep="$1"; fi; shift ;;
102
+ esac
103
+ done
104
+ [ -n "$ep" ] || { echo "forge_rest: an endpoint is required" >&2; return 2; }
105
+ # gh's placeholders for the repository of the current directory's origin remote.
106
+ case "$ep" in *'{owner}'*|*'{repo}'*)
107
+ local _r; _r="$(forge_repo_from_git)" || { echo "forge_rest: {owner}/{repo} needs an origin remote on GitHub" >&2; return 1; }
108
+ ep="$(printf '%s' "$ep" | sed -e "s#{owner}#${_r%%/*}#g" -e "s#{repo}#${_r##*/}#g")" ;;
109
+ esac
110
+ if [ "$ep" = graphql ]; then
111
+ local query; query="$(printf '%s' "$body" | jq -r '.query // ""')"
112
+ # shellcheck disable=SC2086
113
+ out="$(forge_graphql "$query" $q)" || return 1
114
+ [ "$silent" = 1 ] && return 0
115
+ if [ -n "$filter" ]; then printf '%s\n' "$out" | jq -r "$filter"; else printf '%s\n' "$out"; fi
116
+ return 0
117
+ fi
118
+ [ -n "$method" ] || { if [ "$has_field" = 1 ] || [ -n "$input" ]; then method=POST; else method=GET; fi; }
119
+ local send="" url="${ep#/}" out hdr next
120
+ if [ -n "$input" ]; then
121
+ if [ "$input" = "-" ]; then send="$(cat)"; else send="$(cat "$input")"; fi
122
+ elif [ "$has_field" = 1 ]; then
123
+ case "$method" in
124
+ GET|DELETE)
125
+ local qs; qs="$(printf '%s' "$body" | jq -r 'to_entries | map("\(.key)=\(.value|tostring|@uri)") | join("&")')"
126
+ case "$url" in *\?*) url="$url&$qs" ;; *) url="$url?$qs" ;; esac ;;
127
+ *) send="$body" ;;
128
+ esac
129
+ fi
130
+ hdr="$(mktemp)"; local frc=0
131
+ while :; do
132
+ out="$(_fg_curl "$method" "$url" "$send" "$accept" "$hdr" "$xh")" || { rm -f "$hdr"; return 1; }
133
+ if [ "$silent" != 1 ]; then
134
+ # A --jq that fails (a non-JSON 200 body) is a failed read, rc 1, not an empty answer.
135
+ if [ -n "$filter" ]; then printf '%s\n' "$out" | jq -r "$filter" || frc=1; else printf '%s\n' "$out"; fi
136
+ fi
137
+ [ "$paginate" = 1 ] || break
138
+ next="$(tr -d '\r' < "$hdr" | sed -nE 's/^[Ll]ink: .*<([^>]+)>; rel="next".*/\1/p' | sed -n 1p)"
139
+ [ -n "$next" ] || break
140
+ url="$next"
141
+ done
142
+ rm -f "$hdr"
143
+ return "$frc"
144
+ }
145
+
146
+ # _fg_json <method> <path> <body-or-empty> <jq-filter>: the call, then the filter. The call is
147
+ # captured first so an HTTP error is rc 1 whether or not the caller set pipefail.
148
+ _fg_json() {
149
+ local out
150
+ out="$(forge_api "$1" "$2" "$3")" || return 1
151
+ printf '%s\n' "$out" | jq "$4"
152
+ }
153
+ _fg_jq_r() { # same, raw output
154
+ local out
155
+ out="$(forge_api "$1" "$2" "$3")" || return 1
156
+ printf '%s\n' "$out" | jq -r "$4"
157
+ }
158
+
159
+ _fg_labels_json() { # csv → ["a","b"]
160
+ printf '%s' "${1:-}" | tr ',' '\n' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | sed '/^$/d' | jq -R . | jq -sc .
161
+ }
162
+ _fg_milestone_number() { # <repo> <title> → number or empty
163
+ [ -n "${2:-}" ] || return 0
164
+ local out
165
+ out="$(forge_api GET "repos/$1/milestones?state=all&per_page=100")" || return 1
166
+ printf '%s\n' "$out" | jq -r --arg t "$2" '.[] | select(.title==$t) | .number' | sed -n 1p
167
+ }
168
+
169
+ forge_issue_view() {
170
+ _fg_json GET "repos/$1/issues/$2" "" '{number, title, body: (.body // ""), state: (.state | ascii_upcase),
171
+ labels: [.labels[]? | {name}],
172
+ milestone: (if .milestone then {title: .milestone.title, number: .milestone.number} else null end),
173
+ url: .html_url}'
174
+ }
175
+
176
+ forge_issue_create() {
177
+ local repo="$1" title="$2" body="$3" labels="${4:-}" ms="${5:-}" msnum payload
178
+ msnum="$(_fg_milestone_number "$repo" "$ms")" || return 1
179
+ payload="$(jq -n --arg t "$title" --arg b "$body" --argjson l "$(_fg_labels_json "$labels")" --arg m "$msnum" \
180
+ '{title: $t, body: $b, labels: $l} + (if $m != "" then {milestone: ($m|tonumber)} else {} end)')"
181
+ _fg_jq_r POST "repos/$repo/issues" "$payload" .html_url
182
+ }
183
+
184
+ forge_issue_close() {
185
+ local repo="$1" n="$2" reason="${3:-completed}" comment="${4:-}"
186
+ if [ -n "$comment" ]; then forge_issue_comment "$repo" "$n" "$comment" >/dev/null || return 1; fi
187
+ forge_api PATCH "repos/$repo/issues/$n" "$(jq -n --arg r "$reason" '{state: "closed", state_reason: $r}')" >/dev/null
188
+ }
189
+
190
+ forge_issue_comment() {
191
+ _fg_jq_r POST "repos/$1/issues/$2/comments" "$(jq -n --arg b "$3" '{body: $b}')" .html_url
192
+ }
193
+
194
+ forge_issue_label() {
195
+ forge_api POST "repos/$1/issues/$2/labels" "$(jq -n --argjson l "$(_fg_labels_json "$3")" '{labels: $l}')" >/dev/null
196
+ }
197
+
198
+ forge_issue_list() {
199
+ local repo="$1" state="${2:-open}" labels="${3:-}" q
200
+ q="state=$state&per_page=100"
201
+ [ -n "$labels" ] && q="$q&labels=$(printf '%s' "$labels" | sed 's/ //g')"
202
+ _fg_json GET "repos/$repo/issues?$q" "" '[.[] | select(.pull_request == null) | {number, title, state: (.state|ascii_upcase), labels: [.labels[]? | {name}]}]'
203
+ }
204
+
205
+ forge_pr_open() {
206
+ local repo="$1" base="$2" head="$3" title="$4" body="$5" labels="${6:-}" out num
207
+ out="$(forge_api POST "repos/$repo/pulls" "$(jq -n --arg t "$title" --arg b "$body" --arg base "$base" --arg head "$head" '{title: $t, body: $b, base: $base, head: $head}')")" || return 1
208
+ num="$(printf '%s' "$out" | jq -r .number)"
209
+ if [ -n "$labels" ]; then forge_issue_label "$repo" "$num" "$labels" || true; fi
210
+ printf '%s' "$out" | jq -r .html_url
211
+ }
212
+
213
+ forge_pr_view() {
214
+ local repo="$1" ref="$2" n
215
+ n="$ref"
216
+ case "$ref" in
217
+ *[!0-9]*) n="$(_fg_jq_r GET "repos/$repo/pulls?state=all&head=${repo%%/*}:$ref&per_page=1" "" '.[0].number // empty')" || return 1
218
+ [ -n "$n" ] || { echo "cckit: no pull request from $ref in $repo" >&2; return 1; } ;;
219
+ esac
220
+ _fg_json GET "repos/$repo/pulls/$n" "" '{number, title, body: (.body // ""), state: (.state|ascii_upcase),
221
+ base: .base.ref, head: .head.ref, head_sha: .head.sha, mergeable, merged, url: .html_url}'
222
+ }
223
+
224
+ forge_pr_merge() {
225
+ forge_api PUT "repos/$1/pulls/$2/merge" "$(jq -n --arg m "${3:-squash}" '{merge_method: $m}')" >/dev/null
226
+ }
227
+
228
+ forge_pr_list() {
229
+ local repo="$1" state="${2:-open}" head="${3:-}" q="state=$state&per_page=100"
230
+ [ -n "$head" ] && q="$q&head=${repo%%/*}:$head"
231
+ _fg_json GET "repos/$repo/pulls?$q" "" '[.[] | {number, title, state: (.state|ascii_upcase), head: .head.ref, base: .base.ref, url: .html_url}]'
232
+ }
233
+
234
+ # --- the gh CLI, inside the layer -----------------------------------------------------------------
235
+ # gh is OPTIONAL: it is how a person gets a token interactively, and nothing else here needs it.
236
+ forge_cli_present() { command -v gh >/dev/null 2>&1; }
237
+ forge_cli_version() { gh --version 2>/dev/null | sed -n 1p | awk '{print $3}'; }
238
+ # forge_login: an interactive sign-in. With gh installed it runs `gh auth login`; without, it says
239
+ # how to provide a token. rc 1 when no token resolves afterwards.
240
+ forge_login() {
241
+ if forge_cli_present; then gh auth login --git-credential-helper || return 1
242
+ else
243
+ echo "cckit: no gh installed — put a token in GH_TOKEN (a fine-grained token with Issues, Pull requests and Contents on the repository), or install gh and run gh auth login" >&2
244
+ return 1
245
+ fi
246
+ forge_token >/dev/null 2>&1
247
+ }
248
+ # forge_login_scope <scope>: add an OAuth scope to gh's token (`gh auth refresh -s <scope>`). rc 1
249
+ # without gh, where the token in GH_TOKEN is the user's to re-issue.
250
+ forge_login_scope() {
251
+ forge_cli_present || { echo "cckit: scope '$1' is added to a gh token with gh auth refresh -s $1; a GH_TOKEN is re-issued with it" >&2; return 1; }
252
+ gh auth refresh -s "$1"
253
+ }
254
+ # forge_token_scopes: the token's OAuth scopes (X-OAuth-Scopes), comma-separated; empty for a
255
+ # fine-grained token, which carries none.
256
+ forge_token_scopes() {
257
+ local token; token="$(forge_token)" || return 1
258
+ curl -sS -I -H "Authorization: Bearer $token" "$(_fg_api_base)/user" 2>/dev/null \
259
+ | tr -d '\r' | sed -nE 's/^[Xx]-[Oo][Aa]uth-[Ss]copes: *//p' | sed -n 1p
260
+ }
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # forge-none.sh — the forge provider when there is none (E537.2). Every forge_* call refuses with one
4
+ # line naming the verb that needs a forge (forge_need), rc 1. Nothing is silent and nothing returns 0.
5
+ # errors: strict — every function is rc 1
6
+ _forge_none() { forge_need; }
7
+ forge_api() { _forge_none; }
8
+ forge_rest() { _forge_none; }
9
+ forge_graphql() { _forge_none; }
10
+ forge_issue_view() { _forge_none; }
11
+ forge_issue_create() { _forge_none; }
12
+ forge_issue_close() { _forge_none; }
13
+ forge_issue_comment() { _forge_none; }
14
+ forge_issue_label() { _forge_none; }
15
+ forge_issue_list() { _forge_none; }
16
+ forge_pr_open() { _forge_none; }
17
+ forge_pr_view() { _forge_none; }
18
+ forge_pr_merge() { _forge_none; }
19
+ forge_pr_list() { _forge_none; }
20
+ forge_cli_present() { command -v gh >/dev/null 2>&1; }
21
+ forge_cli_version() { gh --version 2>/dev/null | sed -n 1p | awk '{print $3}'; }
22
+ forge_login() { if command -v gh >/dev/null 2>&1; then gh auth login --git-credential-helper; else echo "cckit: no gh installed — put a token in GH_TOKEN, or install gh and run gh auth login" >&2; return 1; fi; }
23
+ forge_login_scope() { _forge_none; }
24
+ forge_token_scopes() { _forge_none; }
@@ -0,0 +1,186 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # forge-test.sh — E537.2: the forge port. Provider selection (env, config, token, default), the none
4
+ # provider refuses every call with one line naming the verb, and the github provider speaks REST
5
+ # over curl with a bearer token and never runs a gh subcommand. Hermetic: a curl shim on PATH logs
6
+ # every request and answers fixtures; a gh shim stands in for an installed gh.
7
+ # Run: bash scripts/lib/forge-test.sh
8
+ # errors: strict — a test runner: rc 1 on any failed assertion
9
+ set -u
10
+ LIB="$(cd "$(dirname "$0")" && pwd)"
11
+ # shellcheck source=/dev/null
12
+ . "$LIB/test-env.sh" && test_env_seal
13
+ unset GH_TOKEN GITHUB_TOKEN CCKIT_FORGE CCKIT_VERB KIT_CONFIG
14
+ fail=0
15
+ t() { if [ "$2" = "$3" ]; then echo "ok: $1"; else echo "FAIL: $1 -> got '[$2]' want '[$3]'"; fail=1; fi; }
16
+ tc() { if grep -qE -- "$2" "$1"; then echo "ok: $3"; else echo "FAIL: $3 (no /$2/ in $1)"; fail=1; fi; }
17
+ command -v jq >/dev/null 2>&1 || { echo "forge-test: jq required" >&2; exit 1; }
18
+
19
+ tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
20
+ mkdir -p "$tmp/bin" "$tmp/proj"
21
+ export CURL_LOG="$tmp/curl.log"; : > "$CURL_LOG"
22
+ export GH_SHIM_TOKEN="" # what the gh shim answers to `gh auth token` (empty = not signed in)
23
+
24
+ cat > "$tmp/bin/curl" <<'SH'
25
+ #!/usr/bin/env bash
26
+ # logs: METHOD URL BODY AUTH, answers a fixture by URL, ends with the http code like -w '\n%{http_code}'
27
+ method=GET; url=""; body=""; auth=""
28
+ while [ $# -gt 0 ]; do
29
+ case "$1" in
30
+ -X) method="$2"; shift 2 ;;
31
+ -H) case "$2" in Authorization:*) auth="$2" ;; esac; shift 2 ;;
32
+ --data-binary) body="$2"; shift 2 ;;
33
+ -w|-o) shift 2 ;;
34
+ -*) shift ;;
35
+ *) url="$1"; shift ;;
36
+ esac
37
+ done
38
+ { printf '%s %s %s %s\n' "$method" "$url" "$(printf '%s' "$body" | tr '\n' ' ')" "$auth"; } >> "$CURL_LOG"
39
+ code=200
40
+ case "$method $url" in
41
+ "GET "*"/milestones?"*) out='[{"number":4,"title":"v1"},{"number":9,"title":"v2"}]' ;;
42
+ "GET "*"/issues/404") code=404; out='{"message":"Not Found"}' ;;
43
+ "GET "*"/issues/7") out='{"number":7,"title":"Seven","body":null,"state":"open","labels":[{"name":"kind:fix"}],"milestone":{"title":"v2","number":9},"html_url":"https://x/issues/7"}' ;;
44
+ "GET "*"/issues?"*) out='[{"number":7,"title":"Seven","state":"open","labels":[]},{"number":8,"title":"A PR","state":"open","labels":[],"pull_request":{}}]' ;;
45
+ "POST "*"/issues/"*"/comments") out='{"html_url":"https://x/issues/7#c1"}' ;;
46
+ "POST "*"/issues/"*"/labels") out='[]' ;;
47
+ "POST "*"/issues") out='{"number":12,"html_url":"https://x/issues/12"}' ;;
48
+ "PATCH "*"/issues/"*) out='{"state":"closed"}' ;;
49
+ "POST "*"/pulls") out='{"number":30,"html_url":"https://x/pull/30"}' ;;
50
+ "GET "*"/pulls?"*"head="*) out='[{"number":30}]' ;;
51
+ "GET "*"/pulls?"*) out='[{"number":30,"title":"P","state":"open","head":{"ref":"fix/1-x"},"base":{"ref":"develop"},"html_url":"https://x/pull/30"}]' ;;
52
+ "GET "*"/pulls/30") out='{"number":30,"title":"P","body":"b","state":"open","base":{"ref":"develop"},"head":{"ref":"fix/1-x"},"mergeable":true,"merged":false,"html_url":"https://x/pull/30"}' ;;
53
+ "PUT "*"/pulls/30/merge") out='{"merged":true}' ;;
54
+ "POST "*"/graphql") out='{"data":{"ok":true}}' ;;
55
+ *) code=500; out='{"message":"unexpected '"$method $url"'"}' ;;
56
+ esac
57
+ printf '%s\n%s' "$out" "$code"
58
+ SH
59
+ cat > "$tmp/bin/gh" <<'SH'
60
+ #!/usr/bin/env bash
61
+ case "$*" in
62
+ "auth token") [ -n "${GH_SHIM_TOKEN:-}" ] && { printf '%s\n' "$GH_SHIM_TOKEN"; exit 0; }; exit 1 ;;
63
+ *) echo "gh shim: a gh subcommand was run: $*" >&2; exit 97 ;;
64
+ esac
65
+ SH
66
+ chmod +x "$tmp/bin/curl" "$tmp/bin/gh"
67
+ export PATH="$tmp/bin:$PATH"
68
+
69
+ # run <env…> -- <function> <args…>: in a fresh bash with the port loaded; stdout to $tmp/out, stderr to $tmp/err
70
+ run() {
71
+ local envs=()
72
+ while [ $# -gt 0 ] && [ "$1" != "--" ]; do envs+=("$1"); shift; done; shift
73
+ env "${envs[@]+"${envs[@]}"}" bash -c '. "$0/forge.sh"; forge_load; "$@"' "$LIB" "$@" >"$tmp/out" 2>"$tmp/err"
74
+ }
75
+
76
+ # --- provider selection -------------------------------------------------------------------------
77
+ t "CCKIT_FORGE=none wins" "$(CCKIT_FORGE=none bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "none"
78
+ t "CCKIT_FORGE=github wins" "$(CCKIT_FORGE=github bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "github"
79
+ echo '{"forge":{"provider":"github"}}' > "$tmp/proj/cckit.config.json"
80
+ t "config forge.provider is read" "$(cd "$tmp/proj" && bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "github"
81
+ echo '{"forge":{"provider":"none"}}' > "$tmp/proj/cckit.config.json"
82
+ t "config none even with a token" "$(cd "$tmp/proj" && GH_TOKEN=t bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "none"
83
+ echo '{}' > "$tmp/proj/cckit.config.json"
84
+ t "unset: a GH_TOKEN selects github" "$(cd "$tmp/proj" && GH_TOKEN=t bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "github"
85
+ t "unset: a GITHUB_TOKEN selects github" "$(cd "$tmp/proj" && GITHUB_TOKEN=t bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "github"
86
+ t "unset: a signed-in gh selects github" "$(cd "$tmp/proj" && GH_SHIM_TOKEN=ghs bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "github"
87
+ t "unset: no token anywhere selects none" "$(cd "$tmp/proj" && bash -c '. "$0/forge.sh"; forge_provider' "$LIB")" "none"
88
+ echo '{"forge":{"provider":"gitlab"}}' > "$tmp/proj/cckit.config.json"
89
+ out="$(cd "$tmp/proj" && bash -c '. "$0/forge.sh"; forge_provider' "$LIB" 2>"$tmp/err")"
90
+ t "an unknown provider falls to the default" "$out" "none"
91
+ tc "$tmp/err" "forge.provider must be github or none" "an unknown provider warns once"
92
+ echo '{}' > "$tmp/proj/cckit.config.json"
93
+
94
+ # --- none refuses, naming the verb --------------------------------------------------------------
95
+ cd "$tmp/proj"
96
+ run CCKIT_FORGE=none CCKIT_VERB=pr -- forge_issue_view o/r 7; rc=$?
97
+ t "none: forge_issue_view is rc 1" "$rc" "1"
98
+ t "none: nothing on stdout" "$(cat "$tmp/out")" ""
99
+ tc "$tmp/err" "^cckit pr: needs a forge \(GitHub\)" "none: the one line names the verb from CCKIT_VERB"
100
+ tc "$tmp/err" "git steps of pr run without one" "none: the line says the git steps still run"
101
+ run CCKIT_FORGE=none -- forge_need merge; rc=$?
102
+ t "forge_need <verb> rc 1 without a forge" "$rc" "1"
103
+ tc "$tmp/err" "^cckit merge: needs a forge" "forge_need names the verb it was given"
104
+ run CCKIT_FORGE=none -- forge_pr_merge o/r 30; t "none: forge_pr_merge rc 1" "$?" "1"
105
+ run CCKIT_FORGE=none -- forge_api GET repos/o/r; t "none: forge_api rc 1" "$?" "1"
106
+ t "none: no curl request was made" "$(wc -l < "$CURL_LOG" | tr -d ' ')" "0"
107
+ run CCKIT_FORGE=github GH_TOKEN=t -- forge_need pr; t "forge_need rc 0 with a forge" "$?" "0"
108
+ run CCKIT_FORGE=github GH_TOKEN=t -- forge_available; t "forge_available rc 0 with a forge" "$?" "0"
109
+
110
+ # --- github: REST over curl ----------------------------------------------------------------------
111
+ G=(CCKIT_FORGE=github GH_TOKEN=tok-1 CCKIT_FORGE_API=https://api.test)
112
+ run "${G[@]}" -- forge_issue_view o/r 7; t "issue_view rc" "$?" "0"
113
+ t "issue_view shape" "$(jq -c '{number,title,body,state,labels,milestone,url}' "$tmp/out")" \
114
+ '{"number":7,"title":"Seven","body":"","state":"OPEN","labels":[{"name":"kind:fix"}],"milestone":{"title":"v2","number":9},"url":"https://x/issues/7"}'
115
+ tc "$CURL_LOG" "^GET https://api.test/repos/o/r/issues/7 Authorization: Bearer tok-1$" "issue_view: GET with the bearer token, no body"
116
+
117
+ : > "$CURL_LOG"
118
+ run "${G[@]}" -- forge_issue_create o/r "T" "B" "kind:fix, role:docs" v2; t "issue_create rc" "$?" "0"
119
+ t "issue_create prints the url" "$(cat "$tmp/out")" "https://x/issues/12"
120
+ tc "$CURL_LOG" "^GET https://api.test/repos/o/r/milestones" "issue_create resolves the milestone by title"
121
+ t "issue_create payload" "$(grep '^POST .*/issues ' "$CURL_LOG" | sed -E 's/^POST [^ ]+ (.*) Authorization.*/\1/' | jq -c .)" \
122
+ '{"title":"T","body":"B","labels":["kind:fix","role:docs"],"milestone":9}'
123
+
124
+ : > "$CURL_LOG"
125
+ run "${G[@]}" -- forge_issue_close o/r 7 not_planned "bye"; t "issue_close rc" "$?" "0"
126
+ t "issue_close: comment then PATCH" "$(awk '{print $1}' "$CURL_LOG" | tr '\n' ' ')" "POST PATCH "
127
+ t "issue_close payload" "$(grep '^PATCH' "$CURL_LOG" | sed -E 's/^PATCH [^ ]+ (.*) Authorization.*/\1/' | jq -c .)" '{"state":"closed","state_reason":"not_planned"}'
128
+
129
+ : > "$CURL_LOG"
130
+ run "${G[@]}" -- forge_issue_comment o/r 7 "hello"; t "issue_comment prints the url" "$(cat "$tmp/out")" "https://x/issues/7#c1"
131
+ run "${G[@]}" -- forge_issue_label o/r 7 "a,b"; t "issue_label rc" "$?" "0"
132
+ t "issue_label payload" "$(grep '/labels ' "$CURL_LOG" | sed -E 's/^POST [^ ]+ (.*) Authorization.*/\1/' | jq -c .)" '{"labels":["a","b"]}'
133
+ run "${G[@]}" -- forge_issue_list o/r open "kind:fix"; t "issue_list drops pull requests" "$(jq -c '[.[].number]' "$tmp/out")" "[7]"
134
+ tc "$CURL_LOG" "issues\?state=open&per_page=100&labels=kind:fix" "issue_list passes state and labels"
135
+
136
+ : > "$CURL_LOG"
137
+ run "${G[@]}" -- forge_pr_open o/r develop fix/1-x "T" "B" "size:S"; t "pr_open rc" "$?" "0"
138
+ t "pr_open prints the url" "$(cat "$tmp/out")" "https://x/pull/30"
139
+ t "pr_open: POST pulls then labels" "$(awk '{print $1, $2}' "$CURL_LOG" | sed 's#https://api.test##' | tr '\n' ';')" "POST /repos/o/r/pulls;POST /repos/o/r/issues/30/labels;"
140
+ run "${G[@]}" -- forge_pr_view o/r 30; t "pr_view by number" "$(jq -c '{number,base,head,mergeable,merged}' "$tmp/out")" '{"number":30,"base":"develop","head":"fix/1-x","mergeable":true,"merged":false}'
141
+ : > "$CURL_LOG"
142
+ run "${G[@]}" -- forge_pr_view o/r fix/1-x; t "pr_view by head branch" "$(jq -r .number "$tmp/out")" "30"
143
+ tc "$CURL_LOG" "pulls\?state=all&head=o:fix/1-x" "pr_view resolves the head as owner:branch"
144
+ run "${G[@]}" -- forge_pr_merge o/r 30 merge; t "pr_merge rc" "$?" "0"
145
+ t "pr_merge payload" "$(grep '^PUT' "$CURL_LOG" | sed -E 's/^PUT [^ ]+ (.*) Authorization.*/\1/' | jq -c .)" '{"merge_method":"merge"}'
146
+ run "${G[@]}" -- forge_pr_list o/r open; t "pr_list shape" "$(jq -c '.[0] | {number,head,base}' "$tmp/out")" '{"number":30,"head":"fix/1-x","base":"develop"}'
147
+
148
+ run "${G[@]}" -- forge_issue_view o/r 404; t "an HTTP error is rc 1" "$?" "1"
149
+ t "an HTTP error leaves stdout empty" "$(cat "$tmp/out")" ""
150
+ tc "$tmp/err" "GitHub answered 404 on GET repos/o/r/issues/404: Not Found" "an HTTP error names code, call and message"
151
+ run CCKIT_FORGE=github -- forge_api GET repos/o/r; t "github with no token is rc 1" "$?" "1"
152
+ tc "$tmp/err" "no forge token" "github with no token says so"
153
+ t "no gh subcommand ran in the github provider" "$(grep -c 'gh shim: a gh subcommand' "$tmp/err" "$CURL_LOG" | awk -F: '{s+=$2} END{print s}')" "0"
154
+
155
+ # --- forge_rest: the gh api argument form ---------------------------------------------------------
156
+ : > "$CURL_LOG"
157
+ run "${G[@]}" -- forge_rest repos/o/r/issues/7 --jq .title; t "rest: a bare endpoint is GET, --jq applies" "$(cat "$tmp/out")" "Seven"
158
+ tc "$CURL_LOG" "^GET https://api.test/repos/o/r/issues/7 " "rest: GET with no body"
159
+ : > "$CURL_LOG"
160
+ run "${G[@]}" -- forge_rest repos/o/r/issues -X POST -f title=T -f 'labels[]=a' -f 'labels[]=b' -F milestone=9 -F draft=true --jq .number; t "rest: POST fields rc" "$?" "0"
161
+ t "rest: -f strings, -f k[] arrays, -F typed, in argument order" "$(sed -E 's/^POST [^ ]+ (.*) Authorization.*/\1/' "$CURL_LOG" | jq -c .)" '{"title":"T","labels":["a","b"],"milestone":9,"draft":true}'
162
+ t "rest: --jq applies to the response" "$(cat "$tmp/out")" "12"
163
+ : > "$CURL_LOG"
164
+ run "${G[@]}" -- forge_rest repos/o/r/issues -f state=open -f per_page=100 -X GET --jq 'length'; t "rest: GET fields go to the query string" "$(grep -oE 'issues\?[^ ]*' "$CURL_LOG")" "issues?state=open&per_page=100"
165
+ : > "$CURL_LOG"
166
+ run "${G[@]}" -- forge_rest repos/o/r/issues/7 -f body=x; t "rest: fields with no method mean POST" "$(awk '{print $1}' "$CURL_LOG")" "POST"
167
+ run "${G[@]}" -- forge_rest repos/o/r/issues/404; t "rest: an HTTP error is rc 1" "$?" "1"
168
+ t "rest: an HTTP error prints nothing" "$(cat "$tmp/out")" ""
169
+ : > "$CURL_LOG"
170
+ run "${G[@]}" -- forge_rest repos/o/r/pulls/30 -H 'Accept: application/vnd.github.v3.diff'; t "rest: -H Accept passes through" "$(grep -c 'pulls/30' "$CURL_LOG")" "1"
171
+ : > "$CURL_LOG"
172
+ run "${G[@]}" -- forge_rest graphql -f query='query($o:String!,$n:Int!){x}' -F o=octo -F n=7 --jq '.data'; t "rest graphql: rc" "$?" "0"
173
+ t "rest graphql: POST graphql with query and typed variables" "$(sed -E 's/^POST [^ ]+ (.*) Authorization.*/\1/' "$CURL_LOG" | jq -c .)" '{"query":"query($o:String!,$n:Int!){x}","variables":{"o":"octo","n":7}}'
174
+ : > "$CURL_LOG"
175
+ run "${G[@]}" -- forge_graphql 'mutation{y}' project=P item=I; t "forge_graphql variables" "$(sed -E 's/^POST [^ ]+ (.*) Authorization.*/\1/' "$CURL_LOG" | jq -c .variables)" '{"project":"P","item":"I"}'
176
+ run CCKIT_FORGE=none -- forge_rest repos/o/r; t "none: forge_rest rc 1" "$?" "1"
177
+ run CCKIT_FORGE=none -- forge_graphql 'query{x}'; t "none: forge_graphql rc 1" "$?" "1"
178
+
179
+ # --- the layer is the port: no gh subcommand in forge-github.sh; forge-report leaves the layer out
180
+ t "forge-github.sh runs no gh subcommand but auth (login, refresh, token)" "$(grep -vE '^[[:space:]]*#' "$LIB/forge-github.sh" | grep -cE '(^|[;&|({`]|\$\(|if|then|do|exec)[[:space:]]*gh (api|pr|issue|repo|label|run|release)')" "0"
181
+ t "forge-report lists the layer and keeps it out of the ratchet" \
182
+ "$(bash "$LIB/../forge-report.sh" --files | awk '$1 ~ /scripts\/lib\/forge/ {print $1}' | sort | tr '\n' ' ')" \
183
+ "scripts/lib/forge-github.sh scripts/lib/forge-none.sh scripts/lib/forge.sh "
184
+
185
+ [ "$fail" -eq 0 ] && echo "ALL OK (forge)"
186
+ exit "$fail"
@@ -0,0 +1,133 @@
1
+ #!/usr/bin/env bash
2
+ # shellcheck shell=bash
3
+ # forge.sh — the forge port (E537.2). cckit's core needs only git; a forge (today: GitHub) is an
4
+ # optional provider behind these functions. Callers use forge_* and never `gh` or the API URL.
5
+ #
6
+ # forge_provider github | none — CCKIT_FORGE, else forge.provider in the
7
+ # project config, else github when a token resolves, else none
8
+ # forge_repo_from_git [dir] owner/name of the origin remote, or rc 1
9
+ # forge_token the token: GH_TOKEN, GITHUB_TOKEN, else `gh auth token` when gh
10
+ # is installed; empty when none resolves
11
+ # forge_load source forge-<provider>.sh once (every forge_* below needs it)
12
+ # forge_need [verb] rc 0 with a forge; without one, ONE line naming the verb that
13
+ # needs it (CCKIT_VERB when the verb is not given), rc 1
14
+ # forge_available rc 0 iff the provider is not none (no message)
15
+ # forge_note_none <verb> <what> the git-only path says what it skipped, once per verb
16
+ # forge_compare_url <base> <branch> the GitHub compare page for a pushed branch, or rc 1
17
+ # forge_api <method> <path> [json] raw REST call, the body on stdout; rc 1 on an HTTP error
18
+ # forge_rest <endpoint> [forge_rest flags] the `gh api` argument form (-X -f -F --jq --paginate -H --input)
19
+ # forge_graphql <query> [k=v …] POST /graphql; a value N:<int> is an Int variable
20
+ # forge_issue_view <repo> <n> {number,title,body,state,labels[{name}],milestone{title,number},url}
21
+ # forge_issue_create <repo> <title> <body> [labels-csv] [milestone-title] → html_url
22
+ # forge_issue_close <repo> <n> [completed|not_planned] [comment]
23
+ # forge_issue_comment <repo> <n> <body> → html_url
24
+ # forge_issue_label <repo> <n> <labels-csv> add labels
25
+ # forge_issue_list <repo> [open|closed|all] [labels-csv] → [{number,title,state,labels[{name}]}]
26
+ # forge_pr_open <repo> <base> <head> <title> <body> [labels-csv] → html_url
27
+ # forge_pr_view <repo> <n|head-branch> → {number,title,body,state,base,head,mergeable,merged,url}
28
+ # forge_pr_merge <repo> <n> [squash|merge|rebase]
29
+ # forge_pr_list <repo> [open|closed|all] [head-branch] → [{number,title,state,head,base,url}]
30
+ # forge_cli_present · forge_cli_version · forge_login · forge_login_scope <s> · forge_token_scopes
31
+ # gh, inside the layer: the optional way a person gets a token
32
+ #
33
+ # <repo> is owner/name. Every function of the none provider refuses through forge_need.
34
+ # bash 3.2 + zsh compatible. Requires jq for the JSON forms.
35
+ # errors: strict — a refused forge call is rc 1 with its one line on stderr; HTTP errors propagate
36
+ _forge_self="${BASH_SOURCE[0]:-$0}"
37
+ FORGE_LIB="$(cd "$(dirname "$_forge_self")" && pwd)"
38
+ # shellcheck source=/dev/null
39
+ . "$FORGE_LIB/config-path.sh"
40
+
41
+ # forge_repo_from_git [dir]: owner/name of the origin remote (https or ssh GitHub URL), or rc 1.
42
+ forge_repo_from_git() {
43
+ local url
44
+ url="$(git -C "${1:-.}" remote get-url origin 2>/dev/null)" || return 1
45
+ url="${url%.git}"; url="${url%/}"
46
+ case "$url" in
47
+ git@*:*) printf '%s' "${url#*:}" ;;
48
+ *://*) url="${url#*://}"; url="${url#*/}"; printf '%s' "$url" ;;
49
+ *) return 1 ;;
50
+ esac
51
+ }
52
+
53
+ forge_token() {
54
+ if [ -n "${GH_TOKEN:-}" ]; then printf '%s' "$GH_TOKEN"; return 0; fi
55
+ if [ -n "${GITHUB_TOKEN:-}" ]; then printf '%s' "$GITHUB_TOKEN"; return 0; fi
56
+ if command -v gh >/dev/null 2>&1; then gh auth token 2>/dev/null && return 0; fi
57
+ return 1
58
+ }
59
+
60
+ # The configured provider, or empty when the config says nothing.
61
+ _forge_configured() {
62
+ local cfg
63
+ cfg="$(kit_config_path 2>/dev/null)" || return 0
64
+ [ -f "$cfg" ] && command -v jq >/dev/null 2>&1 || return 0
65
+ jq -r '.forge.provider // empty' "$cfg" 2>/dev/null
66
+ }
67
+
68
+ forge_provider() {
69
+ local p
70
+ p="${CCKIT_FORGE:-}"
71
+ [ -n "$p" ] || p="$(_forge_configured)"
72
+ case "$p" in
73
+ github|none) printf '%s' "$p"; return 0 ;;
74
+ '') ;;
75
+ *) printf "cckit: forge.provider must be github or none (got '%s') — using the default\n" "$p" >&2 ;;
76
+ esac
77
+ if forge_token >/dev/null 2>&1; then printf 'github'; else printf 'none'; fi
78
+ }
79
+
80
+ forge_load() {
81
+ [ -n "${_FORGE_LOADED:-}" ] && return 0
82
+ local p; p="$(forge_provider)"
83
+ # shellcheck source=/dev/null
84
+ . "$FORGE_LIB/forge-$p.sh"
85
+ _FORGE_LOADED="$p"
86
+ }
87
+
88
+ forge_available() { [ "$(forge_provider)" != "none" ]; }
89
+
90
+ # forge_note_none <verb> <what>: the git-only path's one line, printed ONCE per verb and process:
91
+ # `cckit <verb>: no forge — <what>`. A forge-less step is never silent; it is also never a wall of
92
+ # repeats when a verb loops over branches.
93
+ forge_note_none() {
94
+ case " ${_FORGE_NONE_SAID:-} " in *" $1 "*) return 0 ;; esac
95
+ _FORGE_NONE_SAID="${_FORGE_NONE_SAID:-} $1"
96
+ printf 'cckit %s: no forge — %s\n' "$1" "$2" >&2
97
+ }
98
+
99
+ # forge_compare_url <base> <branch> [dir]: the forge's compare page for a pushed branch, built from
100
+ # the origin remote when it is a GitHub URL; empty (rc 1) otherwise. What `cckit pr` prints when it
101
+ # has pushed but cannot open the pull request itself.
102
+ forge_compare_url() {
103
+ local repo; repo="$(forge_repo_from_git "${3:-.}")" || return 1
104
+ local url; url="$(git -C "${3:-.}" remote get-url origin 2>/dev/null)"
105
+ case "$url" in *github.com*) printf 'https://github.com/%s/compare/%s...%s' "$repo" "$1" "$2" ;; *) return 1 ;; esac
106
+ }
107
+
108
+ forge_need() {
109
+ forge_available && return 0
110
+ local verb="${1:-${CCKIT_VERB:-}}"
111
+ if [ -n "$verb" ] && [ "$verb" != cckit ]; then
112
+ printf 'cckit %s: needs a forge (GitHub) and none is configured. Set forge.provider to github and a token in GH_TOKEN, or sign in with gh. The git steps of %s run without one.\n' "$verb" "$verb" >&2
113
+ else
114
+ printf 'cckit: needs a forge (GitHub) and none is configured. Set forge.provider to github and a token in GH_TOKEN, or sign in with gh. The git steps still run without one.\n' >&2
115
+ fi
116
+ return 1
117
+ }
118
+
119
+ # Lazy port: each forge_* below loads the provider on first use, then calls the provider's own
120
+ # definition (forge_load redefines the name, and bash resolves the inner call at call time). A
121
+ # caller that sourced forge.sh needs no forge_load of its own.
122
+ _forge_lazy() {
123
+ local fn="$1"; shift
124
+ forge_load || return 1
125
+ typeset -f "$fn" >/dev/null 2>&1 || { echo "cckit: forge provider '$_FORGE_LOADED' has no $fn" >&2; return 1; }
126
+ "$fn" "$@"
127
+ }
128
+ for _fn in forge_api forge_rest forge_graphql forge_issue_view forge_issue_create forge_issue_close \
129
+ forge_issue_comment forge_issue_label forge_issue_list forge_pr_open forge_pr_view forge_pr_merge forge_pr_list \
130
+ forge_cli_present forge_cli_version forge_login forge_login_scope forge_token_scopes; do
131
+ eval "$_fn() { _forge_lazy $_fn \"\$@\"; }"
132
+ done
133
+ unset _fn