@pecot/workflow-mcp 0.0.0-stage → 5.0.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 (244) hide show
  1. package/.claude-plugin/plugin.json +8 -0
  2. package/LICENSE +21 -0
  3. package/README.md +126 -2
  4. package/bin/pecot-workflow-mcp.mjs +10 -0
  5. package/cli/dist/cli.d.ts +26 -0
  6. package/cli/dist/cli.js +222 -0
  7. package/cli/dist/commands/cell-table.d.ts +3 -0
  8. package/cli/dist/commands/cell-table.js +495 -0
  9. package/cli/dist/commands/command-binding.d.ts +26 -0
  10. package/cli/dist/commands/command-binding.js +266 -0
  11. package/cli/dist/commands/command-definition.d.ts +136 -0
  12. package/cli/dist/commands/command-definition.js +101 -0
  13. package/cli/dist/commands/command-help.d.ts +8 -0
  14. package/cli/dist/commands/command-help.js +173 -0
  15. package/cli/dist/commands/command-stop.d.ts +19 -0
  16. package/cli/dist/commands/command-stop.js +30 -0
  17. package/cli/dist/commands/dependabot.d.ts +3 -0
  18. package/cli/dist/commands/dependabot.js +165 -0
  19. package/cli/dist/commands/directory-option.d.ts +7 -0
  20. package/cli/dist/commands/directory-option.js +48 -0
  21. package/cli/dist/commands/failure.d.ts +13 -0
  22. package/cli/dist/commands/failure.js +33 -0
  23. package/cli/dist/commands/fuzzy.d.ts +7 -0
  24. package/cli/dist/commands/fuzzy.js +79 -0
  25. package/cli/dist/commands/github-client.d.ts +12 -0
  26. package/cli/dist/commands/github-client.js +23 -0
  27. package/cli/dist/commands/global-options.d.ts +25 -0
  28. package/cli/dist/commands/global-options.js +83 -0
  29. package/cli/dist/commands/help-layout.d.ts +40 -0
  30. package/cli/dist/commands/help-layout.js +117 -0
  31. package/cli/dist/commands/help.d.ts +8 -0
  32. package/cli/dist/commands/help.js +55 -0
  33. package/cli/dist/commands/input-schema.d.ts +14 -0
  34. package/cli/dist/commands/input-schema.js +56 -0
  35. package/cli/dist/commands/issue-board.d.ts +38 -0
  36. package/cli/dist/commands/issue-board.js +62 -0
  37. package/cli/dist/commands/issue-comment.d.ts +3 -0
  38. package/cli/dist/commands/issue-comment.js +196 -0
  39. package/cli/dist/commands/issue-create.d.ts +3 -0
  40. package/cli/dist/commands/issue-create.js +329 -0
  41. package/cli/dist/commands/issue-project.d.ts +3 -0
  42. package/cli/dist/commands/issue-project.js +176 -0
  43. package/cli/dist/commands/issue-quick.d.ts +3 -0
  44. package/cli/dist/commands/issue-quick.js +126 -0
  45. package/cli/dist/commands/issue-read.d.ts +3 -0
  46. package/cli/dist/commands/issue-read.js +80 -0
  47. package/cli/dist/commands/issue-shared.d.ts +159 -0
  48. package/cli/dist/commands/issue-shared.js +419 -0
  49. package/cli/dist/commands/issue.d.ts +4 -0
  50. package/cli/dist/commands/issue.js +15 -0
  51. package/cli/dist/commands/names.d.ts +9 -0
  52. package/cli/dist/commands/names.js +16 -0
  53. package/cli/dist/commands/output.d.ts +78 -0
  54. package/cli/dist/commands/output.js +187 -0
  55. package/cli/dist/commands/pr.d.ts +3 -0
  56. package/cli/dist/commands/pr.js +933 -0
  57. package/cli/dist/commands/project-info.d.ts +48 -0
  58. package/cli/dist/commands/project-info.js +181 -0
  59. package/cli/dist/commands/project-sync.d.ts +91 -0
  60. package/cli/dist/commands/project-sync.js +273 -0
  61. package/cli/dist/commands/project.d.ts +5 -0
  62. package/cli/dist/commands/project.js +677 -0
  63. package/cli/dist/commands/registry.d.ts +6 -0
  64. package/cli/dist/commands/registry.js +27 -0
  65. package/cli/dist/commands/release.d.ts +10 -0
  66. package/cli/dist/commands/release.js +414 -0
  67. package/cli/dist/commands/repository-display.d.ts +3 -0
  68. package/cli/dist/commands/repository-display.js +7 -0
  69. package/cli/dist/commands/repository-match.d.ts +14 -0
  70. package/cli/dist/commands/repository-match.js +33 -0
  71. package/cli/dist/commands/repository.d.ts +5 -0
  72. package/cli/dist/commands/repository.js +265 -0
  73. package/cli/dist/commands/rich-panel.d.ts +22 -0
  74. package/cli/dist/commands/rich-panel.js +295 -0
  75. package/cli/dist/commands/security.d.ts +3 -0
  76. package/cli/dist/commands/security.js +164 -0
  77. package/cli/dist/commands/session.d.ts +3 -0
  78. package/cli/dist/commands/session.js +161 -0
  79. package/cli/dist/commands/tracker-sync.d.ts +58 -0
  80. package/cli/dist/commands/tracker-sync.js +53 -0
  81. package/cli/dist/commands/usage-error.d.ts +47 -0
  82. package/cli/dist/commands/usage-error.js +72 -0
  83. package/cli/dist/commands/value-types.d.ts +46 -0
  84. package/cli/dist/commands/value-types.js +94 -0
  85. package/cli/dist/commands/work-target.d.ts +13 -0
  86. package/cli/dist/commands/work-target.js +41 -0
  87. package/cli/dist/commands/worktree.d.ts +3 -0
  88. package/cli/dist/commands/worktree.js +335 -0
  89. package/cli/dist/domain/api-error-detail.d.ts +10 -0
  90. package/cli/dist/domain/api-error-detail.js +25 -0
  91. package/cli/dist/domain/branch-name.d.ts +5 -0
  92. package/cli/dist/domain/branch-name.js +10 -0
  93. package/cli/dist/domain/changelog.d.ts +43 -0
  94. package/cli/dist/domain/changelog.js +107 -0
  95. package/cli/dist/domain/config.d.ts +215 -0
  96. package/cli/dist/domain/config.js +300 -0
  97. package/cli/dist/domain/dependabot-pull-requests.d.ts +82 -0
  98. package/cli/dist/domain/dependabot-pull-requests.js +338 -0
  99. package/cli/dist/domain/dependabot-source.d.ts +51 -0
  100. package/cli/dist/domain/dependabot-source.js +4 -0
  101. package/cli/dist/domain/dependabot.d.ts +47 -0
  102. package/cli/dist/domain/dependabot.js +88 -0
  103. package/cli/dist/domain/errors.d.ts +19 -0
  104. package/cli/dist/domain/errors.js +31 -0
  105. package/cli/dist/domain/field-options.d.ts +31 -0
  106. package/cli/dist/domain/field-options.js +44 -0
  107. package/cli/dist/domain/format.d.ts +47 -0
  108. package/cli/dist/domain/format.js +168 -0
  109. package/cli/dist/domain/frontmatter.d.ts +122 -0
  110. package/cli/dist/domain/frontmatter.js +264 -0
  111. package/cli/dist/domain/git-runner.d.ts +10 -0
  112. package/cli/dist/domain/git-runner.js +4 -0
  113. package/cli/dist/domain/git-url.d.ts +17 -0
  114. package/cli/dist/domain/git-url.js +30 -0
  115. package/cli/dist/domain/issue-kind.d.ts +16 -0
  116. package/cli/dist/domain/issue-kind.js +16 -0
  117. package/cli/dist/domain/issue-readout.d.ts +107 -0
  118. package/cli/dist/domain/issue-readout.js +4 -0
  119. package/cli/dist/domain/json-null.d.ts +2 -0
  120. package/cli/dist/domain/json-null.js +5 -0
  121. package/cli/dist/domain/json.d.ts +21 -0
  122. package/cli/dist/domain/json.js +47 -0
  123. package/cli/dist/domain/prerequisites.d.ts +32 -0
  124. package/cli/dist/domain/prerequisites.js +192 -0
  125. package/cli/dist/domain/pull-request.d.ts +134 -0
  126. package/cli/dist/domain/pull-request.js +232 -0
  127. package/cli/dist/domain/python-format.d.ts +105 -0
  128. package/cli/dist/domain/python-format.js +304 -0
  129. package/cli/dist/domain/readiness.d.ts +172 -0
  130. package/cli/dist/domain/readiness.js +280 -0
  131. package/cli/dist/domain/reference-parser.d.ts +49 -0
  132. package/cli/dist/domain/reference-parser.js +119 -0
  133. package/cli/dist/domain/release.d.ts +49 -0
  134. package/cli/dist/domain/release.js +72 -0
  135. package/cli/dist/domain/repository-policy.d.ts +32 -0
  136. package/cli/dist/domain/repository-policy.js +65 -0
  137. package/cli/dist/domain/repository-resolver.d.ts +57 -0
  138. package/cli/dist/domain/repository-resolver.js +119 -0
  139. package/cli/dist/domain/sections.d.ts +69 -0
  140. package/cli/dist/domain/sections.js +514 -0
  141. package/cli/dist/domain/security-alerts-source.d.ts +29 -0
  142. package/cli/dist/domain/security-alerts-source.js +4 -0
  143. package/cli/dist/domain/security-alerts.d.ts +52 -0
  144. package/cli/dist/domain/security-alerts.js +107 -0
  145. package/cli/dist/domain/security-tracker.d.ts +115 -0
  146. package/cli/dist/domain/security-tracker.js +353 -0
  147. package/cli/dist/domain/state.d.ts +29 -0
  148. package/cli/dist/domain/state.js +56 -0
  149. package/cli/dist/domain/tracker-body.d.ts +70 -0
  150. package/cli/dist/domain/tracker-body.js +65 -0
  151. package/cli/dist/domain/yaml-document.d.ts +5 -0
  152. package/cli/dist/domain/yaml-document.js +206 -0
  153. package/cli/dist/infra/authored-notes.d.ts +37 -0
  154. package/cli/dist/infra/authored-notes.js +66 -0
  155. package/cli/dist/infra/changelog-file.d.ts +19 -0
  156. package/cli/dist/infra/changelog-file.js +43 -0
  157. package/cli/dist/infra/child-stdin.d.ts +4 -0
  158. package/cli/dist/infra/child-stdin.js +8 -0
  159. package/cli/dist/infra/config-store.d.ts +49 -0
  160. package/cli/dist/infra/config-store.js +121 -0
  161. package/cli/dist/infra/diagnostics.d.ts +9 -0
  162. package/cli/dist/infra/diagnostics.js +2 -0
  163. package/cli/dist/infra/file-kind.d.ts +9 -0
  164. package/cli/dist/infra/file-kind.js +26 -0
  165. package/cli/dist/infra/frontmatter-file.d.ts +14 -0
  166. package/cli/dist/infra/frontmatter-file.js +30 -0
  167. package/cli/dist/infra/git.d.ts +74 -0
  168. package/cli/dist/infra/git.js +133 -0
  169. package/cli/dist/infra/github/auth.d.ts +60 -0
  170. package/cli/dist/infra/github/auth.js +158 -0
  171. package/cli/dist/infra/github/client.d.ts +173 -0
  172. package/cli/dist/infra/github/client.js +576 -0
  173. package/cli/dist/infra/github/dependabot-source.d.ts +13 -0
  174. package/cli/dist/infra/github/dependabot-source.js +33 -0
  175. package/cli/dist/infra/github/errors.d.ts +47 -0
  176. package/cli/dist/infra/github/errors.js +42 -0
  177. package/cli/dist/infra/github/index.d.ts +13 -0
  178. package/cli/dist/infra/github/index.js +19 -0
  179. package/cli/dist/infra/github/issues.d.ts +129 -0
  180. package/cli/dist/infra/github/issues.js +273 -0
  181. package/cli/dist/infra/github/payload.d.ts +44 -0
  182. package/cli/dist/infra/github/payload.js +80 -0
  183. package/cli/dist/infra/github/projects.d.ts +108 -0
  184. package/cli/dist/infra/github/projects.js +303 -0
  185. package/cli/dist/infra/github/pulls.d.ts +80 -0
  186. package/cli/dist/infra/github/pulls.js +161 -0
  187. package/cli/dist/infra/github/queries.d.ts +36 -0
  188. package/cli/dist/infra/github/queries.js +883 -0
  189. package/cli/dist/infra/github/readout.d.ts +18 -0
  190. package/cli/dist/infra/github/readout.js +156 -0
  191. package/cli/dist/infra/github/repos.d.ts +68 -0
  192. package/cli/dist/infra/github/repos.js +137 -0
  193. package/cli/dist/infra/github/rest-path.d.ts +14 -0
  194. package/cli/dist/infra/github/rest-path.js +31 -0
  195. package/cli/dist/infra/github/security-alerts-source.d.ts +15 -0
  196. package/cli/dist/infra/github/security-alerts-source.js +73 -0
  197. package/cli/dist/infra/gitignore-spec.d.ts +36 -0
  198. package/cli/dist/infra/gitignore-spec.js +235 -0
  199. package/cli/dist/infra/nx-scope.d.ts +51 -0
  200. package/cli/dist/infra/nx-scope.js +182 -0
  201. package/cli/dist/infra/os-error.d.ts +24 -0
  202. package/cli/dist/infra/os-error.js +38 -0
  203. package/cli/dist/infra/paths.d.ts +88 -0
  204. package/cli/dist/infra/paths.js +234 -0
  205. package/cli/dist/infra/state-store.d.ts +45 -0
  206. package/cli/dist/infra/state-store.js +118 -0
  207. package/cli/dist/infra/template-store.d.ts +15 -0
  208. package/cli/dist/infra/template-store.js +34 -0
  209. package/cli/dist/infra/text-file.d.ts +12 -0
  210. package/cli/dist/infra/text-file.js +33 -0
  211. package/cli/dist/infra/worktree-copy.d.ts +24 -0
  212. package/cli/dist/infra/worktree-copy.js +95 -0
  213. package/cli/dist/infra/worktree-links.d.ts +139 -0
  214. package/cli/dist/infra/worktree-links.js +433 -0
  215. package/cli/dist/infra/worktree-setup.d.ts +21 -0
  216. package/cli/dist/infra/worktree-setup.js +41 -0
  217. package/cli/dist/infra/worktree.d.ts +174 -0
  218. package/cli/dist/infra/worktree.js +567 -0
  219. package/cli/dist/mcp/result.d.ts +33 -0
  220. package/cli/dist/mcp/result.js +52 -0
  221. package/cli/dist/mcp/serve.d.ts +51 -0
  222. package/cli/dist/mcp/serve.js +183 -0
  223. package/cli/dist/mcp/server.d.ts +21 -0
  224. package/cli/dist/mcp/server.js +128 -0
  225. package/cli/dist/mcp/session-directory.d.ts +26 -0
  226. package/cli/dist/mcp/session-directory.js +84 -0
  227. package/cli/dist/package.json +1 -0
  228. package/cli/dist/version.d.ts +17 -0
  229. package/cli/dist/version.js +42 -0
  230. package/cli/templates/comment/merge-diff.md +34 -0
  231. package/cli/templates/comment/plan.md +181 -0
  232. package/cli/templates/comment/precede-notify.md +33 -0
  233. package/cli/templates/comment/review-findings.md +42 -0
  234. package/cli/templates/comment/review-resolution.md +48 -0
  235. package/cli/templates/issue/bug.md +63 -0
  236. package/cli/templates/issue/epic.md +54 -0
  237. package/cli/templates/issue/task-implement.md +67 -0
  238. package/cli/templates/issue/task.md +48 -0
  239. package/cli/templates/issue/tracker.md +48 -0
  240. package/cli/templates/pr/create.md +123 -0
  241. package/cli/templates/pr/release.md +56 -0
  242. package/hooks/lib/core/pecot-gitignore-ensure.sh +48 -0
  243. package/npm-shrinkwrap.json +83 -0
  244. package/package.json +22 -4
@@ -0,0 +1,164 @@
1
+ // `pecot security sync`。cli/commands/security.py の移植。open の Tracker の一覧を、open の security alert に書き直す。
2
+ // 途中で抜ける箇所は CommandExit を投げ、defineCommand が終了コードにする。
3
+ import { z } from 'zod';
4
+ import { ALERT_KINDS, kindLabel, listOpenAlerts, SecurityAlertError } from '../domain/security-alerts.js';
5
+ import { ALERT_LABEL, buildTrackerTitle, maxPriority, normalizeAlerts, parseTrackerEntries, planTracker, renderTrackerBody, TRACKER_PATTERN } from '../domain/security-tracker.js';
6
+ import { findTracker } from '../domain/tracker-body.js';
7
+ import { createDependabotSource } from '../infra/github/dependabot-source.js';
8
+ import { requireNumber } from '../infra/github/payload.js';
9
+ import { createScanAlertSource } from '../infra/github/security-alerts-source.js';
10
+ import { CommandExit, defineCommand } from './command-definition.js';
11
+ import { makeClient } from './github-client.js';
12
+ import { createIssue, putOnBoard } from './issue-board.js';
13
+ import { createOutput } from './output.js';
14
+ import { apiErrorDetail, isBodyUnchanged, loadTarget, settle, todayUtc, warnExtraTrackers } from './tracker-sync.js';
15
+ const FAILURE = 1;
16
+ // 一覧を取れなかった種類と無効の種類は行を触らず、足さない。終了コード 1 にするのは取れなかった種類だけ
17
+ async function fetchOpenAlerts(out, sources, host, owner, repository) {
18
+ const alerts = new Map();
19
+ const failed = new Set();
20
+ const disabled = new Set();
21
+ for (const kind of ALERT_KINDS) {
22
+ let raws;
23
+ try {
24
+ raws = await listOpenAlerts(sources, kind, owner, repository);
25
+ }
26
+ catch (error) {
27
+ if (!(error instanceof SecurityAlertError))
28
+ throw error;
29
+ failed.add(kind);
30
+ out.error(`could not list ${kindLabel(kind)} alerts (leaving its rows untouched): ${error.message}`);
31
+ continue;
32
+ }
33
+ if (raws === undefined) {
34
+ disabled.add(kind);
35
+ settle(out, { verb: 'skip', target: `${kindLabel(kind)} alerts (feature disabled)` });
36
+ continue;
37
+ }
38
+ alerts.set(kind, normalizeAlerts(kind, raws, host, owner, repository));
39
+ }
40
+ return { alerts, failed, disabled };
41
+ }
42
+ function operationsOf(plan, tracker, title, isUnchanged) {
43
+ if (tracker === undefined) {
44
+ if (!plan.create)
45
+ return { create: [{ verb: 'skip', target: 'Tracker creation' }], rewrite: [] };
46
+ const created = [{ verb: 'create', target: `Tracker "${title}" with priority ${String(maxPriority(plan.entries))}` }];
47
+ for (const entry of plan.entries)
48
+ created.push({ verb: 'add', target: `${entry.kind} #${String(entry.number)} to new Tracker` });
49
+ return { create: created, rewrite: [] };
50
+ }
51
+ const label = `#${String(tracker)}`;
52
+ if (isUnchanged)
53
+ return { create: [], rewrite: [{ verb: 'unchanged', target: `Tracker ${label}` }] };
54
+ const rewrite = [
55
+ ...plan.added.map((entry) => ({ verb: 'add', target: `${entry.kind} #${String(entry.number)} to Tracker ${label}` })),
56
+ ...plan.removed.map((entry) => ({ verb: 'remove', target: `${entry.kind} #${String(entry.number)} from Tracker ${label}` })),
57
+ ];
58
+ return { create: [], rewrite: rewrite.length === 0 ? [{ verb: 'update', target: `Tracker ${label} body` }] : rewrite };
59
+ }
60
+ // Tracker を作る・書き直す・ボードに載せる。失敗の数を返す。
61
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types -- GitHubClient は可変
62
+ async function applyChanges(out, input) {
63
+ const { client, config, owner, repository, plan, title, body, stages, number, isUnchanged } = input;
64
+ const priority = maxPriority(plan.entries);
65
+ try {
66
+ if (number === undefined) {
67
+ if (!plan.create) {
68
+ for (const operation of stages.create)
69
+ out.done(operation);
70
+ return 0;
71
+ }
72
+ const issue = await createIssue(client, owner, repository, title, body, [ALERT_LABEL]);
73
+ for (const operation of stages.create)
74
+ out.done(operation);
75
+ await putOnBoard(client, config, owner, repository, requireNumber(issue['number'], 'number'), priority, { onlyIfEmpty: false });
76
+ return 0;
77
+ }
78
+ let failures = 0;
79
+ let isUpdated = true;
80
+ if (!isUnchanged) {
81
+ try {
82
+ await client.updateIssueBody(owner, repository, number, body);
83
+ }
84
+ catch (error) {
85
+ const text = apiErrorDetail(error);
86
+ if (text === undefined)
87
+ throw error;
88
+ // 422 を含む。本文は書き直されていないので、次の実行でやり直す
89
+ failures += 1;
90
+ isUpdated = false;
91
+ out.error(`could not update Tracker #${String(number)} body: ${text}`);
92
+ }
93
+ }
94
+ if (isUpdated)
95
+ for (const operation of stages.rewrite)
96
+ out.done(operation);
97
+ // 作った後にボード登録が失敗した実行を直す。Status・Priority は空のときだけ入れる
98
+ await putOnBoard(client, config, owner, repository, number, priority, { onlyIfEmpty: true });
99
+ return failures;
100
+ }
101
+ catch (error) {
102
+ const text = apiErrorDetail(error);
103
+ if (text === undefined)
104
+ throw error;
105
+ out.error(`Tracker: ${text}`);
106
+ return 1;
107
+ }
108
+ }
109
+ const sync = defineCommand({
110
+ path: ['security', 'sync'],
111
+ description: 'Rewrite the open Tracker\'s list of open security alerts',
112
+ input: z.object({
113
+ dry_run: z.boolean().default(false).describe('Show the planned Tracker changes without changing anything'),
114
+ }),
115
+ positional: [],
116
+ interactive: false,
117
+ run: async (input, io) => {
118
+ const out = createOutput(input.dry_run, io);
119
+ const { config, owner, repository } = loadTarget(out, input.repo);
120
+ const client = makeClient(out, config);
121
+ if (client === undefined)
122
+ return FAILURE;
123
+ const sources = { scan: createScanAlertSource(client), dependabot: createDependabotSource(client) };
124
+ const { alerts, failed, disabled } = await fetchOpenAlerts(out, sources, config.host, owner, repository);
125
+ let issues;
126
+ try {
127
+ issues = await client.listIssuesByLabel(owner, repository, ALERT_LABEL);
128
+ }
129
+ catch (error) {
130
+ const text = apiErrorDetail(error);
131
+ if (text === undefined)
132
+ throw error;
133
+ out.error(`could not list issues: ${text}`);
134
+ return FAILURE;
135
+ }
136
+ const { tracker, others } = findTracker(issues, TRACKER_PATTERN);
137
+ warnExtraTrackers(out, tracker, others);
138
+ const existingBody = tracker?.body;
139
+ let entries;
140
+ try {
141
+ entries = tracker === undefined ? undefined : parseTrackerEntries(existingBody);
142
+ }
143
+ catch (error) {
144
+ if (!(error instanceof SecurityAlertError))
145
+ throw error;
146
+ out.error(`${tracker === undefined ? 'Tracker' : `Tracker #${String(tracker.number)}`}: ${error.message}`);
147
+ return FAILURE;
148
+ }
149
+ const plan = planTracker(alerts, entries, failed, disabled);
150
+ const title = buildTrackerTitle(todayUtc(new Date()));
151
+ const body = renderTrackerBody(plan.entries, config.host, owner, repository, { existingBody, disabledKinds: disabled, failedKinds: failed });
152
+ const number = tracker?.number;
153
+ const isUnchanged = isBodyUnchanged(body, existingBody);
154
+ const stages = operationsOf(plan, number, title, isUnchanged);
155
+ for (const operation of [...stages.create, ...stages.rewrite])
156
+ out.planned(operation);
157
+ const failures = input.dry_run ? 0 : await applyChanges(out, { client, config, owner, repository, plan, title, body, stages, number, isUnchanged });
158
+ if (failed.size > 0 || failures > 0)
159
+ throw new CommandExit(FAILURE);
160
+ return 0;
161
+ },
162
+ });
163
+ export const SECURITY_COMMANDS = [sync];
164
+ //# sourceMappingURL=security.js.map
@@ -0,0 +1,3 @@
1
+ import type { CommandDefinition } from './command-definition.js';
2
+ export declare const SESSION_COMMANDS: readonly CommandDefinition[];
3
+ //# sourceMappingURL=session.d.ts.map
@@ -0,0 +1,161 @@
1
+ // session status・session restore。cli/commands/session.py の移植。
2
+ import { z } from 'zod';
3
+ import { extractIssueNumberFromBranch } from '../domain/branch-name.js';
4
+ import { ConfigError } from '../domain/config.js';
5
+ import { PecotError } from '../domain/errors.js';
6
+ import { parseIssueReference, ReferenceParseError } from '../domain/reference-parser.js';
7
+ import { FLAG_PECOT_ACTIVE } from '../domain/state.js';
8
+ import { loadActiveConfig } from '../infra/config-store.js';
9
+ import { gitCurrentBranch, primaryRemoteRepository } from '../infra/git.js';
10
+ import { GitHubAPIError } from '../infra/github/errors.js';
11
+ import { hasFlag, readFlag } from '../infra/state-store.js';
12
+ import { defineCommand } from './command-definition.js';
13
+ import { FAILURE_EXIT_CODE } from './failure.js';
14
+ import { stopWith } from './command-stop.js';
15
+ import { directorySchema } from './directory-option.js';
16
+ import { createGitHubClient } from './github-client.js';
17
+ import { createOutput } from './output.js';
18
+ import { USAGE_EXIT_CODE } from './usage-error.js';
19
+ import { recordWorkTarget } from './work-target.js';
20
+ const ALL_FLAGS = [FLAG_PECOT_ACTIVE];
21
+ function collectFlagStatuses() {
22
+ return ALL_FLAGS.map((name) => {
23
+ const isSet = hasFlag(name);
24
+ return { name, isSet, content: isSet ? readFlag(name) ?? '' : '' };
25
+ });
26
+ }
27
+ // .pecot-active の Issue を GitHub から読んで、Markdown の行にする。読めなければ warning だけ出して空にする
28
+ async function activeIssueLines(statuses, output) {
29
+ const active = statuses.find(status => status.name === FLAG_PECOT_ACTIVE && status.isSet && status.content !== '');
30
+ if (active === undefined)
31
+ return [];
32
+ let reference;
33
+ try {
34
+ reference = parseIssueReference(active.content);
35
+ }
36
+ catch (error) {
37
+ if (error instanceof ReferenceParseError)
38
+ return [];
39
+ throw error;
40
+ }
41
+ let issue;
42
+ try {
43
+ const config = loadActiveConfig();
44
+ if (config === undefined) {
45
+ output.warning('cannot read the active Issue: no active project');
46
+ return [];
47
+ }
48
+ issue = await createGitHubClient(output, config.host).getIssueSlim(reference.repository.owner, reference.repository.repository, reference.number, 0);
49
+ }
50
+ catch (error) {
51
+ if (error instanceof ConfigError || error instanceof GitHubAPIError) {
52
+ output.warning(`cannot read the active Issue: ${error.message}`);
53
+ return [];
54
+ }
55
+ throw error;
56
+ }
57
+ const lines = ['', '## Active Issue', '', `#${String(issue.number)} ${issue.title} (${issue.state})`];
58
+ if (issue.body !== '')
59
+ lines.push('', issue.body);
60
+ return lines;
61
+ }
62
+ const STATUS = defineCommand({
63
+ path: ['session', 'status'],
64
+ description: 'Show the session flag state, branch information, and inferred Issue',
65
+ input: z.object({ directory: directorySchema }),
66
+ positional: [],
67
+ shortFlags: { directory: '-D' },
68
+ interactive: false,
69
+ run: async (input, io) => {
70
+ const output = createOutput(false, io);
71
+ const branch = gitCurrentBranch(input.directory);
72
+ const origin = primaryRemoteRepository(input.directory);
73
+ const issueNumber = branch === undefined ? undefined : extractIssueNumberFromBranch(branch);
74
+ const statuses = collectFlagStatuses();
75
+ const lines = [
76
+ '# Session status',
77
+ '',
78
+ `- Branch: ${branch ?? 'unknown'}`,
79
+ `- Repository: ${origin === undefined ? 'unknown' : `${origin.owner}/${origin.repository}`}`,
80
+ `- Inferred Issue: ${issueNumber === undefined ? 'none' : `#${String(issueNumber)}`}`,
81
+ '',
82
+ '| Flag | State | Content |',
83
+ '|------|-------|---------|',
84
+ ...statuses.map(status => `| ${status.name} | ${status.isSet ? 'set' : 'unset'} | ${status.content} |`),
85
+ ...await activeIssueLines(statuses, output),
86
+ ];
87
+ output.data(lines.join('\n'));
88
+ return 0;
89
+ },
90
+ });
91
+ // セッション名に添える題名。設定が無いか API が失敗したときだけ空にする。設定が読めないなら ConfigError を伝える
92
+ async function fetchIssueTitle(owner, repository, number, output) {
93
+ try {
94
+ const config = loadActiveConfig();
95
+ if (config === undefined)
96
+ return '';
97
+ const issue = await createGitHubClient(output, config.host).getIssueSlim(owner, repository, number, 0);
98
+ return issue.title;
99
+ }
100
+ catch (error) {
101
+ if (error instanceof ConfigError)
102
+ throw error;
103
+ if (error instanceof PecotError)
104
+ return '';
105
+ throw error;
106
+ }
107
+ }
108
+ // --issue が無いときの、ブランチ名と remote からの推定。決められなければ error を出して終了コード 1
109
+ function inferTarget(directory, output) {
110
+ const branch = gitCurrentBranch(directory);
111
+ if (branch === undefined) {
112
+ return stopWith(output, 'cannot read the branch name', FAILURE_EXIT_CODE);
113
+ }
114
+ const number = extractIssueNumberFromBranch(branch);
115
+ if (number === undefined) {
116
+ return stopWith(output, `cannot infer the Issue number from the branch name: ${branch}\nrun: pecot session restore --issue <owner/repo/N>`, FAILURE_EXIT_CODE);
117
+ }
118
+ const origin = primaryRemoteRepository(directory);
119
+ const message = 'cannot read the repository information';
120
+ return origin === undefined ? stopWith(output, message, FAILURE_EXIT_CODE) : { owner: origin.owner, repository: origin.repository, number };
121
+ }
122
+ // --issue の参照を分ける。形が誤っていれば error を出して終了コード 2
123
+ function parseTarget(issue, output) {
124
+ try {
125
+ const reference = parseIssueReference(issue);
126
+ return { owner: reference.repository.owner, repository: reference.repository.repository, number: reference.number };
127
+ }
128
+ catch (error) {
129
+ if (!(error instanceof ReferenceParseError))
130
+ throw error;
131
+ return stopWith(output, error.message, USAGE_EXIT_CODE);
132
+ }
133
+ }
134
+ const RESTORE = defineCommand({
135
+ path: ['session', 'restore'],
136
+ description: 'Restore lost session flags (such as .pecot-active) from the branch information',
137
+ input: z.object({
138
+ issue: z.string().optional().describe('Issue reference (owner/repo/number)'),
139
+ directory: directorySchema,
140
+ }),
141
+ positional: [],
142
+ shortFlags: { issue: '-i', directory: '-D' },
143
+ interactive: false,
144
+ run: async (input, io) => {
145
+ const output = createOutput(false, io);
146
+ const target = input.issue === undefined ? inferTarget(input.directory, output) : parseTarget(input.issue, output);
147
+ let title;
148
+ try {
149
+ title = await fetchIssueTitle(target.owner, target.repository, target.number, output);
150
+ }
151
+ catch (error) {
152
+ if (!(error instanceof ConfigError))
153
+ throw error;
154
+ return stopWith(output, error.message, FAILURE_EXIT_CODE);
155
+ }
156
+ recordWorkTarget(output, target.owner, target.repository, target.number, title, { failWithExit: true });
157
+ return 0;
158
+ },
159
+ });
160
+ export const SESSION_COMMANDS = [STATUS, RESTORE];
161
+ //# sourceMappingURL=session.js.map
@@ -0,0 +1,58 @@
1
+ import type { ProjectConfig } from '../domain/config.js';
2
+ import type { TrackerIssue } from '../domain/tracker-body.js';
3
+ import type { GitHubClient } from '../infra/github/index.js';
4
+ import type { Operation, Output } from './output.js';
5
+ /**
6
+ API を呼ばない操作の行を出す。dry-run では計画、実行では結果になる
7
+ */
8
+ export declare function settle(out: Output, operation: Operation): void;
9
+ export interface SyncTarget {
10
+ readonly config: ProjectConfig;
11
+ readonly owner: string;
12
+ readonly repository: string;
13
+ }
14
+ /**
15
+ 対象のリポ(--repo、主の remote の順)とアクティブな設定を決める。決められなければ error の行を出して終了コード 1 で止める。
16
+ 設定が無いときの文は Python の sync と同じ 1 行で、案内の行を付けない
17
+ */
18
+ export declare function loadTarget(out: Readonly<Output>, explicitRepository: string | undefined): SyncTarget;
19
+ /**
20
+ 複数の Tracker が open のときに警告する(番号が最小のものを使い、ほかは閉じない)
21
+ */
22
+ export declare function warnExtraTrackers(out: Readonly<Output>, tracker: Readonly<TrackerIssue> | undefined, others: readonly number[]): void;
23
+ /**
24
+ API エラーと通信エラーの文。ほかの例外なら undefined
25
+ */
26
+ export declare function apiErrorDetail(error: unknown): string | undefined;
27
+ /**
28
+ 書き直した本文が今の本文と同じか。比べるのは改行を `\n` に直したものどうし
29
+ */
30
+ export declare function isBodyUnchanged(body: string, existingBody: string | undefined): boolean;
31
+ /**
32
+ Tracker の題に入れる、立てた日(UTC)の YYYY-MM-DD
33
+ */
34
+ export declare function todayUtc(now: Readonly<Date>): string;
35
+ /**
36
+ 操作を段ごとに分けたもの。作る段と書き直す段は 2 つの sync に共通
37
+ */
38
+ export interface SyncStages {
39
+ readonly create: readonly Operation[];
40
+ readonly rewrite: readonly Operation[];
41
+ }
42
+ /**
43
+ Tracker を反映する処理に渡す共通の入力
44
+ */
45
+ export interface SyncApplyContext {
46
+ readonly client: GitHubClient;
47
+ readonly config: ProjectConfig;
48
+ readonly owner: string;
49
+ readonly repository: string;
50
+ readonly title: string;
51
+ readonly body: string;
52
+ /**
53
+ 使う Tracker の番号。open の Tracker が無ければ undefined
54
+ */
55
+ readonly number: number | undefined;
56
+ readonly isUnchanged: boolean;
57
+ }
58
+ //# sourceMappingURL=tracker-sync.d.ts.map
@@ -0,0 +1,53 @@
1
+ // Tracker に反映するコマンドと、PR の後片付けが共有する出力の補助。
2
+ import { TransportFailure } from '../infra/github/client.js';
3
+ import { GitHubAPIError } from '../infra/github/errors.js';
4
+ import { CommandExit } from './command-definition.js';
5
+ import { requireActiveConfigPlain } from './command-stop.js';
6
+ import { resolveReleaseTargetRepository } from './release.js';
7
+ /**
8
+ API を呼ばない操作の行を出す。dry-run では計画、実行では結果になる
9
+ */
10
+ export function settle(out, operation) {
11
+ out.planned(operation);
12
+ out.done(operation);
13
+ }
14
+ /**
15
+ 対象のリポ(--repo、主の remote の順)とアクティブな設定を決める。決められなければ error の行を出して終了コード 1 で止める。
16
+ 設定が無いときの文は Python の sync と同じ 1 行で、案内の行を付けない
17
+ */
18
+ export function loadTarget(out, explicitRepository) {
19
+ const target = resolveReleaseTargetRepository(undefined, explicitRepository, out);
20
+ if (target === undefined)
21
+ throw new CommandExit(1);
22
+ const config = requireActiveConfigPlain(out);
23
+ return { config, owner: target.owner, repository: target.repository };
24
+ }
25
+ /**
26
+ 複数の Tracker が open のときに警告する(番号が最小のものを使い、ほかは閉じない)
27
+ */
28
+ export function warnExtraTrackers(out, tracker, others) {
29
+ if (others.length === 0)
30
+ return;
31
+ const listed = others.map(number => `#${String(number)}`).join(', ');
32
+ const used = tracker === undefined ? 'none' : `#${String(tracker.number)}`;
33
+ out.warning(`multiple open Trackers; using ${used} (lowest number) and leaving ${listed} open`);
34
+ }
35
+ /**
36
+ API エラーと通信エラーの文。ほかの例外なら undefined
37
+ */
38
+ export function apiErrorDetail(error) {
39
+ return error instanceof GitHubAPIError || error instanceof TransportFailure ? error.message : undefined;
40
+ }
41
+ /**
42
+ 書き直した本文が今の本文と同じか。比べるのは改行を `\n` に直したものどうし
43
+ */
44
+ export function isBodyUnchanged(body, existingBody) {
45
+ return existingBody?.replaceAll('\r\n', '\n') === body;
46
+ }
47
+ /**
48
+ Tracker の題に入れる、立てた日(UTC)の YYYY-MM-DD
49
+ */
50
+ export function todayUtc(now) {
51
+ return now.toISOString().slice(0, 10);
52
+ }
53
+ //# sourceMappingURL=tracker-sync.js.map
@@ -0,0 +1,47 @@
1
+ import { BaseError } from '../domain/errors.js';
2
+ /**
3
+ 誤りが起きたコマンド。
4
+ synopsis は Usage 行の `pecot <path>` の後ろに続く書式。
5
+ 例は `[OPTIONS] {issue_ref}`。グループなら `[OPTIONS] COMMAND [ARGS]...` にする
6
+ */
7
+ export interface UsageContext {
8
+ readonly commandPath: readonly string[];
9
+ readonly synopsis: string;
10
+ }
11
+ /**
12
+ 使い方の誤り。終了コードは 2。context が無いときは枠だけを出す
13
+ */
14
+ export declare class UsageError extends BaseError {
15
+ readonly context: UsageContext | undefined;
16
+ constructor(message: string, context?: Readonly<UsageContext>);
17
+ }
18
+ /**
19
+ コマンドとグループの Usage 行に出す書式
20
+ */
21
+ export declare const COMMAND_SYNOPSIS = "[OPTIONS] COMMAND [ARGS]...";
22
+ export declare const USAGE_EXIT_CODE = 2;
23
+ export declare function missingArgumentMessage(name: string): string;
24
+ /**
25
+ names は長い形・短い形の順(例: `['--directory', '-D']`)
26
+ */
27
+ export declare function missingOptionMessage(names: readonly string[]): string;
28
+ /**
29
+ possibilities は近い長いオプション。あれば、辞書順に並べて案内を付ける(1 件でも同じ形)
30
+ */
31
+ export declare function noSuchOptionMessage(name: string, possibilities?: readonly string[]): string;
32
+ /**
33
+ possibilities は近いコマンド名。近い順のまま案内する
34
+ */
35
+ export declare function noSuchCommandMessage(name: string, possibilities?: readonly string[]): string;
36
+ export declare function optionRequiresArgumentMessage(name: string): string;
37
+ export declare function optionTakesNoValueMessage(name: string): string;
38
+ /**
39
+ names はオプションなら長い形・短い形の順、位置引数なら入力の名前
40
+ */
41
+ export declare function invalidValueMessage(names: string | readonly string[], detail: string): string;
42
+ export declare const MISSING_COMMAND_MESSAGE = "Missing command.";
43
+ /**
44
+ stderr に書く文字列を作る。Usage 行、Try 行、枠付きの Error の順。width は枠の幅(既定は 80)
45
+ */
46
+ export declare function formatUsageError(error: Readonly<UsageError>, width?: number): string;
47
+ //# sourceMappingURL=usage-error.d.ts.map
@@ -0,0 +1,72 @@
1
+ // 使い方の誤りの表示。Python の CLI が出していた click の Usage 行・Try 行と、枠付きの Error を写す。
2
+ // 全コマンドの共通部。コマンドの path と引数の書式は、呼び出す側が渡す。
3
+ import { BaseError } from '../domain/errors.js';
4
+ import { compareCodePoints } from './fuzzy.js';
5
+ import { renderPanel, wrapText } from './rich-panel.js';
6
+ const PROGRAM = 'pecot';
7
+ const DEFAULT_OUTPUT_WIDTH = 80;
8
+ /**
9
+ 使い方の誤り。終了コードは 2。context が無いときは枠だけを出す
10
+ */
11
+ export class UsageError extends BaseError {
12
+ context;
13
+ constructor(message, context) {
14
+ super(message);
15
+ this.context = context;
16
+ }
17
+ }
18
+ /**
19
+ コマンドとグループの Usage 行に出す書式
20
+ */
21
+ export const COMMAND_SYNOPSIS = '[OPTIONS] COMMAND [ARGS]...';
22
+ export const USAGE_EXIT_CODE = 2;
23
+ export function missingArgumentMessage(name) {
24
+ return `Missing argument '${name}'.`;
25
+ }
26
+ /**
27
+ names は長い形・短い形の順(例: `['--directory', '-D']`)
28
+ */
29
+ export function missingOptionMessage(names) {
30
+ return `Missing option ${names.map(name => `'${name}'`).join(' / ')}.`;
31
+ }
32
+ /**
33
+ possibilities は近い長いオプション。あれば、辞書順に並べて案内を付ける(1 件でも同じ形)
34
+ */
35
+ export function noSuchOptionMessage(name, possibilities = []) {
36
+ const hint = possibilities.length === 0 ? '' : ` (Possible options: ${possibilities.toSorted(compareCodePoints).join(', ')})`;
37
+ return `No such option: ${name}${hint}`;
38
+ }
39
+ /**
40
+ possibilities は近いコマンド名。近い順のまま案内する
41
+ */
42
+ export function noSuchCommandMessage(name, possibilities = []) {
43
+ const hint = possibilities.length === 0 ? '' : ` Did you mean ${possibilities.map(candidate => `'${candidate}'`).join(', ')}?`;
44
+ return `No such command '${name}'.${hint}`;
45
+ }
46
+ export function optionRequiresArgumentMessage(name) {
47
+ return `Option '${name}' requires an argument.`;
48
+ }
49
+ export function optionTakesNoValueMessage(name) {
50
+ return `Option '${name}' does not take a value.`;
51
+ }
52
+ /**
53
+ names はオプションなら長い形・短い形の順、位置引数なら入力の名前
54
+ */
55
+ export function invalidValueMessage(names, detail) {
56
+ const quoted = (typeof names === 'string' ? [names] : names).map(name => `'${name}'`).join(' / ');
57
+ return `Invalid value for ${quoted}: ${detail}`;
58
+ }
59
+ export const MISSING_COMMAND_MESSAGE = 'Missing command.';
60
+ function headerLines(context, width) {
61
+ const program = [PROGRAM, ...context.commandPath].join(' ');
62
+ const usage = wrapText(`Usage: ${program} ${context.synopsis}`, width).map(line => line.trimEnd());
63
+ return `${[...usage, `Try '${program} --help' for help.`].join('\n')}\n`;
64
+ }
65
+ /**
66
+ stderr に書く文字列を作る。Usage 行、Try 行、枠付きの Error の順。width は枠の幅(既定は 80)
67
+ */
68
+ export function formatUsageError(error, width = DEFAULT_OUTPUT_WIDTH) {
69
+ const header = error.context === undefined ? '' : headerLines(error.context, width);
70
+ return `${header}${renderPanel('Error', error.message, width)}`;
71
+ }
72
+ //# sourceMappingURL=usage-error.js.map
@@ -0,0 +1,46 @@
1
+ import { z } from 'zod';
2
+ /**
3
+ CLI の値の形。真偽値は値を取らない旗、list は繰り返して渡す
4
+ */
5
+ export type FlagKind = 'boolean' | 'list' | 'value';
6
+ export interface IntegerType {
7
+ readonly type: 'integer';
8
+ readonly min: number | undefined;
9
+ readonly max: number | undefined;
10
+ }
11
+ export interface FloatType {
12
+ readonly type: 'float';
13
+ readonly min: number | undefined;
14
+ readonly max: number | undefined;
15
+ }
16
+ /**
17
+ 選択肢の型(z.enum)。値は選択肢のどれかに一致しなければならない
18
+ */
19
+ export interface ChoiceType {
20
+ readonly type: 'choice';
21
+ readonly choices: readonly string[];
22
+ }
23
+ export type ValueType = ChoiceType | FloatType | IntegerType | {
24
+ readonly type: 'string';
25
+ };
26
+ export declare function flagKindOf(schema: z.core.$ZodType): FlagKind;
27
+ /**
28
+ 値 1 つの型。list は要素の型
29
+ */
30
+ export declare function valueTypeOf(schema: z.core.$ZodType): ValueType;
31
+ /**
32
+ 範囲の文。両側なら `0<=x<=20`、片側なら `x>=1` か `x<=9`
33
+ */
34
+ export declare function describeRange(min: number | undefined, max: number | undefined): string;
35
+ export type Conversion = {
36
+ readonly isValid: true;
37
+ readonly value: unknown;
38
+ } | {
39
+ readonly isValid: false;
40
+ readonly message: string;
41
+ };
42
+ /**
43
+ コマンドラインの文字列を、型に応じて変換する。誤りの文は typer 同梱の click と同じ
44
+ */
45
+ export declare function convertValue(text: string, valueType: ValueType): Conversion;
46
+ //# sourceMappingURL=value-types.d.ts.map