@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,265 @@
1
+ // リポジトリのブランチを扱うコマンド(create-branch・delete-branch)。Python の cli/commands/repo.py の移植。
2
+ import { z } from 'zod';
3
+ import { describeApiError } from '../domain/api-error-detail.js';
4
+ import { findRepository, getFieldId, getOptionId, isRepositoryInProject } from '../domain/config.js';
5
+ import { ReferenceParseError, parseIssueReference, parseRepositoryReference } from '../domain/reference-parser.js';
6
+ import { compareStrings, pythonStringRepr } from '../domain/python-format.js';
7
+ import { repositoryFullName } from '../domain/repository-resolver.js';
8
+ import { FLAG_PECOT_ACTIVE } from '../domain/state.js';
9
+ import { GitHubAPIError, isMissingReferenceError } from '../infra/github/errors.js';
10
+ import { readFlag } from '../infra/state-store.js';
11
+ import { createOutput } from './output.js';
12
+ import { defineCommand } from './command-definition.js';
13
+ import { requireActiveConfig, stopWith } from './command-stop.js';
14
+ import { createGitHubClient } from './github-client.js';
15
+ import { USAGE_EXIT_CODE } from './usage-error.js';
16
+ import { requireRepositoryMatch, resolveCurrentRepository } from './repository-match.js';
17
+ import { recordWorkTarget } from './work-target.js';
18
+ /**
19
+ A-1. 作業ディレクトリの git remote のリポジトリが Issue のリポジトリと違えば、終了コード 2 で止める
20
+ */
21
+ function rejectCwdRepositoryMismatch(out, owner, repository) {
22
+ const resolved = resolveCurrentRepository(process.cwd());
23
+ if (resolved?.source !== 'current-git')
24
+ return;
25
+ const current = resolved.repository;
26
+ if (current.owner === owner && current.repository === repository)
27
+ return;
28
+ stopWith(out, `the repository of the work Issue (${owner}/${repository}) differs from `
29
+ + `the CWD git remote (${repositoryFullName(current)})\n`
30
+ + 'move to the worktree of the target repository and run again:\n'
31
+ + `<project-root>/base/${repository}/ or <project-root>/<wtN>/${repository}/\n`
32
+ + 'create one with /pecot-w:w-worktree-add only if the repository has no worktree\n'
33
+ + 'to issue the command on purpose from outside the CWD (for example in a script), '
34
+ + 'give --allow-cross-repo', USAGE_EXIT_CODE);
35
+ }
36
+ /**
37
+ 3. ベースブランチを決める(--base を先に、無ければ設定の develop_branch)
38
+ */
39
+ function resolveBaseBranch(out, query) {
40
+ const { config, owner, repository, issueNumber, branchName, base } = query;
41
+ if (base !== undefined && base !== '')
42
+ return base;
43
+ const developBranch = findRepository(config, owner, repository)?.develop_branch;
44
+ if (developBranch === undefined || developBranch === '') {
45
+ stopWith(out, `develop_branch is not set for ${owner}/${repository}\n`
46
+ + `set it with: pecot project set-repo ${owner}/${repository} --develop-branch develop\n`
47
+ + `or give the base: pecot create-branch ${owner}/${repository}/${String(issueNumber)} ${branchName} --base main`);
48
+ }
49
+ return developBranch;
50
+ }
51
+ /**
52
+ A-2. .pecot-active が別の OPEN な Issue を指していれば、終了コード 2 で止める
53
+ */
54
+ async function guardActiveOverwrite(out, client, issueReference) {
55
+ const existing = readFlag(FLAG_PECOT_ACTIVE) ?? '';
56
+ if (existing === '' || existing === issueReference)
57
+ return;
58
+ let existingReference;
59
+ try {
60
+ existingReference = parseIssueReference(existing);
61
+ }
62
+ catch (error) {
63
+ if (!(error instanceof ReferenceParseError))
64
+ throw error;
65
+ out.warning(`the content of .pecot-active (${pythonStringRepr(existing)}) is malformed; overwriting it`);
66
+ return;
67
+ }
68
+ const { repository, number } = existingReference;
69
+ const { state } = await client.getIssueSlim(repository.owner, repository.repository, number);
70
+ if (state === 'OPEN') {
71
+ stopWith(out, `this worktree is already working on another Issue (${existing}, OPEN)\n`
72
+ + 'overwriting .pecot-active could mix up the tasks\n'
73
+ + 'finish the other Issue first, or create another worktree with '
74
+ + '/pecot-w:w-worktree-add and run /pecot-w:w-active again in its session\n'
75
+ + 'if it is stale and you want to overwrite it on purpose, give --force-overwrite-active', USAGE_EXIT_CODE);
76
+ }
77
+ out.warning(state === 'CLOSED'
78
+ ? `.pecot-active points to closed Issue ${existing}; overwriting it`
79
+ : `the state of ${existing} in .pecot-active is unexpected (${pythonStringRepr(state)}); overwriting it`);
80
+ }
81
+ /**
82
+ 7.5. プロジェクトアイテムのステータスを active にする(失敗は warning にとどめる)
83
+ */
84
+ async function setIssueStatusActive(out, client, config, issue) {
85
+ const { owner, repository, number } = issue;
86
+ const target = `Issue ${owner}/${repository}/${String(number)} status active`;
87
+ try {
88
+ const itemId = await client.getItemIdByIssue(config.project.id, owner, repository, number);
89
+ if (itemId === undefined || itemId === '') {
90
+ out.warning(`could not set ${target}: the Issue is not on the project board`);
91
+ return;
92
+ }
93
+ const fieldId = getFieldId(config, 'workflow');
94
+ const optionId = getOptionId(config, 'workflow', 'active');
95
+ if (fieldId === undefined || fieldId === '' || optionId === undefined || optionId === '') {
96
+ out.warning(`could not set ${target}: the workflow status field has no 'active' option`);
97
+ return;
98
+ }
99
+ await client.updateProjectItemField(config.project.id, itemId, fieldId, optionId);
100
+ out.done({ verb: 'set', target });
101
+ }
102
+ catch (error) {
103
+ // ブランチは作成済みなので、ステータスの失敗でコマンドは止めない
104
+ if (!(error instanceof GitHubAPIError))
105
+ throw error;
106
+ out.warning(`could not set ${target}: ${error.message}`);
107
+ }
108
+ }
109
+ const branchCreationCommand = defineCommand({
110
+ path: ['create-branch'],
111
+ description: 'Create a branch linked to an Issue (shown in the Development section of the Issue)',
112
+ input: z.strictObject({
113
+ issue_ref: z.string().describe('Issue reference (owner/repo/123)'),
114
+ branch_name: z.string().describe('Name of the branch to create'),
115
+ base: z.string().optional().describe('Base branch (default: develop_branch from the config)'),
116
+ checkout: z.boolean().default(true).describe('Kept for compatibility; has no effect'),
117
+ allow_cross_repo: z.boolean().default(false).describe('Allow the CWD repository to differ from the Issue ref (for scripts and runs outside the CWD)'),
118
+ force_overwrite_active: z.boolean().default(false).describe('Disable the .pecot-active overwrite guard (when sure it is stale)'),
119
+ }),
120
+ positional: ['issue_ref', 'branch_name'],
121
+ shortFlags: { base: '-b', allow_cross_repo: '-X', force_overwrite_active: '-F' },
122
+ negations: { checkout: '--no-checkout' },
123
+ interactive: false,
124
+ run: async (input, io) => {
125
+ requireRepositoryMatch('reject', input.repo, io, process.cwd());
126
+ const out = createOutput(false, io);
127
+ // 1. 設定取得
128
+ const config = requireActiveConfig(out);
129
+ // 2. issue_ref をパース(フル形式必須、基準リポは渡さない)
130
+ let issue;
131
+ try {
132
+ issue = parseIssueReference(input.issue_ref);
133
+ }
134
+ catch (error) {
135
+ if (!(error instanceof ReferenceParseError))
136
+ throw error;
137
+ return stopWith(out, error.message, USAGE_EXIT_CODE);
138
+ }
139
+ const { owner, repository } = issue.repository;
140
+ const issueNumber = issue.number;
141
+ // A-1. 作業ディレクトリと Issue ref のリポジトリの整合チェック
142
+ if (!input.allow_cross_repo)
143
+ rejectCwdRepositoryMismatch(out, owner, repository);
144
+ // 3. ベースブランチ決定(ローカル設定から)
145
+ const baseBranch = resolveBaseBranch(out, { config, owner, repository, issueNumber, branchName: input.branch_name, base: input.base });
146
+ const finalBranchName = `${input.branch_name}/${String(issueNumber)}`;
147
+ try {
148
+ const client = createGitHubClient(out, config.host);
149
+ // A-2. .pecot-active 上書きガード
150
+ if (!input.force_overwrite_active)
151
+ await guardActiveOverwrite(out, client, `${owner}/${repository}/${String(issueNumber)}`);
152
+ // 4. リポジトリがプロジェクトに含まれているか確認
153
+ if (!await isRepositoryInProject(config, owner, repository, client)) {
154
+ stopWith(out, `${owner}/${repository} is not in the project`);
155
+ }
156
+ // 5. Issue Node ID とタイトル取得(タイトルはセッション名用)
157
+ const { id: issueNodeId, title: issueTitle } = await client.getIssueNodeIdAndTitle(owner, repository, issueNumber);
158
+ // 6. ベースブランチの commit SHA 取得
159
+ const baseSha = await client.getBranchSha(owner, repository, baseBranch);
160
+ // 7. リンクブランチ作成(ブランチ名末尾に Issue 番号を自動付与)
161
+ const createdBranch = await client.createLinkedBranch(issueNodeId, baseSha, finalBranchName);
162
+ out.done({ verb: 'create', target: `branch ${createdBranch} in ${owner}/${repository} from ${baseBranch}` });
163
+ // 7.5. プロジェクトアイテムのステータスを active に更新
164
+ await setIssueStatusActive(out, client, config, { owner, repository, number: issueNumber });
165
+ // 9. 作業対象を .pecot-active と .pecot-session-title に保存(hook の自動投稿とセッション名用)
166
+ recordWorkTarget(out, owner, repository, issueNumber, issueTitle);
167
+ return 0;
168
+ }
169
+ catch (error) {
170
+ if (!(error instanceof GitHubAPIError))
171
+ throw error;
172
+ out.error(`could not create branch ${finalBranchName} in ${owner}/${repository}: ${error.message}`);
173
+ return 1;
174
+ }
175
+ },
176
+ });
177
+ const branchDeletionCommand = defineCommand({
178
+ path: ['delete-branch'],
179
+ description: 'Delete a remote branch (a protected branch is never deleted)',
180
+ input: z.strictObject({
181
+ repo_ref: z.string().describe('Repository reference (owner/repo)'),
182
+ branch: z.string().describe('Name of the branch to delete'),
183
+ dry_run: z.boolean().default(false).describe('Show what would be deleted without deleting anything'),
184
+ }),
185
+ positional: ['repo_ref', 'branch'],
186
+ shortFlags: { dry_run: '-n' },
187
+ interactive: false,
188
+ run: async (input, io) => {
189
+ requireRepositoryMatch('reject', input.repo, io, process.cwd());
190
+ const out = createOutput(input.dry_run, io);
191
+ // 1. repo_ref をパース
192
+ let target;
193
+ try {
194
+ target = parseRepositoryReference(input.repo_ref);
195
+ }
196
+ catch (error) {
197
+ if (!(error instanceof ReferenceParseError))
198
+ throw error;
199
+ return stopWith(out, error.message, USAGE_EXIT_CODE);
200
+ }
201
+ const { owner, repository } = target;
202
+ // 2. 設定取得
203
+ const config = requireActiveConfig(out);
204
+ // 3. 保護ブランチ収集(設定の develop_branch・release_branch と、API の既定ブランチ)
205
+ const protectedBranches = new Set();
206
+ const repositoryConfig = findRepository(config, owner, repository);
207
+ for (const branch of [repositoryConfig?.develop_branch, repositoryConfig?.release_branch]) {
208
+ if (branch !== undefined && branch !== '')
209
+ protectedBranches.add(branch);
210
+ }
211
+ let client;
212
+ try {
213
+ client = createGitHubClient(out, config.host);
214
+ protectedBranches.add(await client.getRepositoryDefaultBranch(owner, repository));
215
+ }
216
+ catch (error) {
217
+ if (!(error instanceof GitHubAPIError))
218
+ throw error;
219
+ out.error(`could not read the default branch of ${owner}/${repository}: ${error.message}`);
220
+ return 1;
221
+ }
222
+ // 4. 保護ブランチチェック
223
+ if (protectedBranches.has(input.branch)) {
224
+ out.error(`${input.branch} is a protected branch\nprotected: ${[...protectedBranches].toSorted(compareStrings).join(', ')}`);
225
+ return 1;
226
+ }
227
+ // 5. dry-run
228
+ const operation = { verb: 'delete', target: `remote branch ${input.branch} in ${owner}/${repository}` };
229
+ if (input.dry_run) {
230
+ out.planned(operation);
231
+ return 0;
232
+ }
233
+ // 6. 削除実行
234
+ try {
235
+ await client.deleteBranch(owner, repository, input.branch);
236
+ }
237
+ catch (error) {
238
+ if (!(error instanceof GitHubAPIError))
239
+ throw error;
240
+ out.error(`could not delete remote branch ${input.branch} in ${owner}/${repository}: ${error.message}`);
241
+ return 1;
242
+ }
243
+ out.done(operation);
244
+ // wip ブランチも連動削除
245
+ const wipBranch = `wip/${input.branch}`;
246
+ try {
247
+ await client.deleteBranch(owner, repository, wipBranch);
248
+ }
249
+ catch (error) {
250
+ if (!(error instanceof GitHubAPIError))
251
+ throw error;
252
+ // wip ブランチの削除の失敗だけは、API の応答の本文の詳細も見せる(Python の describe_api_error と同じ)。存在しない場合は何も出さない
253
+ if (!isMissingReferenceError(error))
254
+ out.warning(`could not delete remote branch ${wipBranch} in ${owner}/${repository}: ${describeApiError(error)}`);
255
+ return 0;
256
+ }
257
+ out.done({ verb: 'delete', target: `remote branch ${wipBranch} in ${owner}/${repository}` });
258
+ return 0;
259
+ },
260
+ });
261
+ /**
262
+ ブランチを扱うコマンド。並びは tests/parity/commands.ts と同じ
263
+ */
264
+ export const REPOSITORY_COMMANDS = [branchCreationCommand, branchDeletionCommand];
265
+ //# sourceMappingURL=repository.js.map
@@ -0,0 +1,22 @@
1
+ /**
2
+ 端末のセル数
3
+ */
4
+ export declare function cellLength(text: string): number;
5
+ /**
6
+ 幅に収まるように折り返した行を返す。短い行を空白で埋めはしない。
7
+ 入力の改行はそのまま行の区切りにする。長すぎる語は幅の単位で折る
8
+ */
9
+ export declare function wrapText(text: string, width: number): readonly string[];
10
+ /**
11
+ 行を幅のセル数まで空白で埋める
12
+ */
13
+ export declare function padCells(line: string, width: number): string;
14
+ /**
15
+ 枠の幅。typer と rich に合わせた順で決める。TERMINAL_WIDTH、COLUMNS、端末の幅、無ければ 80
16
+ */
17
+ export declare function outputWidth(environment: Readonly<Record<string, string | undefined>>, terminalColumns: number | undefined): number;
18
+ /**
19
+ 枠付きの表示。本文は幅に合わせて折り返す。末尾は改行で終わる
20
+ */
21
+ export declare function renderPanel(title: string, body: string, width: number): string;
22
+ //# sourceMappingURL=rich-panel.d.ts.map
@@ -0,0 +1,295 @@
1
+ // 端末向けの枠付き表示。Python の CLI が使っていた表示ライブラリ(rich 15)の折り返し規則を写す。
2
+ //
3
+ // 規則は次のとおり。
4
+ // - 単語は、前の空白・語・後ろの空白の並び。語が行に収まらなければ次の行へ送る。
5
+ // - 1 行より長い語は、幅の単位で折る。
6
+ // - 幅は端末のセル数で数える。幅の表は rich のもの(cell-table.ts)。
7
+ // - 異体字セレクタ U+FE0F は、直前の文字を 2 セルにする場合がある。ゼロ幅接合子 U+200D は、次の文字を数えない。
8
+ import { splitCodePoints } from '../domain/python-format.js';
9
+ import { CELL_WIDTHS, NARROW_TO_WIDE } from './cell-table.js';
10
+ const ZERO_WIDTH_JOINER = '‍';
11
+ const VARIATION_SELECTOR_16 = '️';
12
+ const WHITESPACE = /^\s$/u;
13
+ const NARROW_TO_WIDE_SET = new Set(NARROW_TO_WIDE);
14
+ const LAST_RANGE_END = Math.max(...CELL_WIDTHS.map(([, end]) => end));
15
+ /**
16
+ 先頭のコードポイント。空文字列は 0
17
+ */
18
+ function codePointOf(character) {
19
+ /* v8 ignore next -- 呼び出し側は 1 文字以上を渡す */
20
+ return character.codePointAt(0) ?? 0;
21
+ }
22
+ /**
23
+ 表の middle 番目の行。二分探索が範囲内の添字だけを渡す
24
+ */
25
+ function tableEntry(index) {
26
+ /* v8 ignore next -- 二分探索は範囲内の添字だけを渡す */
27
+ return CELL_WIDTHS.at(index) ?? [0, 0, 1];
28
+ }
29
+ /**
30
+ index 番目の文字。範囲外は空文字列
31
+ */
32
+ function characterAt(characters, index) {
33
+ /* v8 ignore next -- 呼び出し側は範囲内の添字を渡す */
34
+ return characters.at(index) ?? '';
35
+ }
36
+ function characterCells(character) {
37
+ const code = codePointOf(character);
38
+ if ((code > 0 && code < 32) || (code >= 0x7F && code < 0xA0))
39
+ return 0;
40
+ if (code > LAST_RANGE_END)
41
+ return 1;
42
+ let low = 0;
43
+ let high = CELL_WIDTHS.length - 1;
44
+ while (low <= high) {
45
+ const middle = (low + high) >> 1;
46
+ const [start, end, width] = tableEntry(middle);
47
+ if (code < start)
48
+ high = middle - 1;
49
+ else if (code > end)
50
+ low = middle + 1;
51
+ else
52
+ return width;
53
+ }
54
+ return 1;
55
+ }
56
+ function isSpecial(character) {
57
+ return character === ZERO_WIDTH_JOINER || character === VARIATION_SELECTOR_16;
58
+ }
59
+ function cellLengthOf(characters) {
60
+ if (characters.every(character => !isSpecial(character))) {
61
+ return characters.reduce((total, character) => total + characterCells(character), 0);
62
+ }
63
+ let total = 0;
64
+ let lastMeasured;
65
+ for (let index = 0; index < characters.length; index += 1) {
66
+ const character = characterAt(characters, index);
67
+ if (character === ZERO_WIDTH_JOINER) {
68
+ index += 1;
69
+ }
70
+ else if (character === VARIATION_SELECTOR_16) {
71
+ if (lastMeasured !== undefined) {
72
+ total += NARROW_TO_WIDE_SET.has(codePointOf(lastMeasured)) ? 1 : 0;
73
+ lastMeasured = undefined;
74
+ }
75
+ }
76
+ else {
77
+ const cells = characterCells(character);
78
+ if (cells > 0) {
79
+ lastMeasured = character;
80
+ total += cells;
81
+ }
82
+ }
83
+ }
84
+ return total;
85
+ }
86
+ /**
87
+ 端末のセル数
88
+ */
89
+ export function cellLength(text) {
90
+ return cellLengthOf(splitCodePoints(text));
91
+ }
92
+ /**
93
+ 文字を字素ごとの範囲に分ける。ゼロ幅の文字は直前の字素に付ける
94
+ */
95
+ function splitGraphemes(characters) {
96
+ const spans = [];
97
+ let lastMeasured;
98
+ let index = 0;
99
+ while (index < characters.length) {
100
+ const character = characterAt(characters, index);
101
+ const previous = spans.at(-1);
102
+ if (isSpecial(character)) {
103
+ if (previous === undefined) {
104
+ spans.push({ start: index, end: index + 1, cells: 0 });
105
+ index += 1;
106
+ }
107
+ else if (character === ZERO_WIDTH_JOINER) {
108
+ index += index < characters.length - 1 ? 2 : 1;
109
+ spans[spans.length - 1] = { ...previous, end: index };
110
+ }
111
+ else {
112
+ index += 1;
113
+ let cells = previous.cells;
114
+ if (lastMeasured !== undefined && NARROW_TO_WIDE_SET.has(codePointOf(lastMeasured))) {
115
+ lastMeasured = undefined;
116
+ cells += 1;
117
+ }
118
+ spans[spans.length - 1] = { ...previous, end: index, cells };
119
+ }
120
+ continue;
121
+ }
122
+ const cells = characterCells(character);
123
+ if (cells > 0) {
124
+ lastMeasured = character;
125
+ spans.push({ start: index, end: index + 1, cells });
126
+ }
127
+ else if (previous === undefined) {
128
+ spans.push({ start: index, end: index + 1, cells: 0 });
129
+ }
130
+ else {
131
+ spans[spans.length - 1] = { ...previous, end: index + 1 };
132
+ }
133
+ index += 1;
134
+ }
135
+ return spans;
136
+ }
137
+ /**
138
+ 行を幅の単位で折る。幅に収まらない字素は次の行へ送る
139
+ */
140
+ function chopCells(characters, width) {
141
+ const lines = [];
142
+ let lineSize = 0;
143
+ let lineOffset = 0;
144
+ for (const { start, cells } of splitGraphemes(characters)) {
145
+ if (lineSize + cells > width) {
146
+ lines.push(characters.slice(lineOffset, start));
147
+ lineOffset = start;
148
+ lineSize = 0;
149
+ }
150
+ lineSize += cells;
151
+ }
152
+ if (lineSize > 0)
153
+ lines.push(characters.slice(lineOffset));
154
+ return lines;
155
+ }
156
+ /**
157
+ 先頭から cells セル分を残す。字素の途中で切れるときは、半端な 1 セルを空白にする
158
+ */
159
+ function keepCells(characters, cells) {
160
+ if (cells <= 0)
161
+ return [];
162
+ const spans = splitGraphemes(characters);
163
+ let left = 0;
164
+ for (const span of spans) {
165
+ if (left + span.cells > cells) {
166
+ const rest = cells - left;
167
+ return [...characters.slice(0, span.start), ...Array.from({ length: rest }, () => ' ')];
168
+ }
169
+ left += span.cells;
170
+ if (left === cells)
171
+ return characters.slice(0, span.end);
172
+ }
173
+ return characters;
174
+ }
175
+ function truncateCells(characters, width) {
176
+ return cellLengthOf(characters) <= width ? characters : keepCells(characters, width);
177
+ }
178
+ function isSpaceAt(characters, index) {
179
+ return WHITESPACE.test(characterAt(characters, index));
180
+ }
181
+ function nextWord(characters, start) {
182
+ let index = start;
183
+ while (index < characters.length && isSpaceAt(characters, index))
184
+ index += 1;
185
+ if (index >= characters.length)
186
+ return undefined;
187
+ while (index < characters.length && !isSpaceAt(characters, index))
188
+ index += 1;
189
+ const bodyEnd = index;
190
+ while (index < characters.length && isSpaceAt(characters, index))
191
+ index += 1;
192
+ return { start, end: index, bodyEnd };
193
+ }
194
+ /**
195
+ 折り返す位置(文字の添字)を返す。長すぎる語は幅の単位で折る
196
+ */
197
+ function divideLine(characters, width) {
198
+ const breaks = [];
199
+ let cellOffset = 0;
200
+ let word = nextWord(characters, 0);
201
+ while (word !== undefined) {
202
+ const { start, end, bodyEnd } = word;
203
+ const wordLength = cellLengthOf(characters.slice(start, bodyEnd));
204
+ const wholeWord = characters.slice(start, end);
205
+ if (width - cellOffset >= wordLength) {
206
+ cellOffset += cellLengthOf(wholeWord);
207
+ }
208
+ else if (wordLength > width) {
209
+ const pieces = chopCells(wholeWord, width);
210
+ let position = start;
211
+ for (const [pieceIndex, piece] of pieces.entries()) {
212
+ if (position > 0)
213
+ breaks.push(position);
214
+ if (pieceIndex === pieces.length - 1)
215
+ cellOffset = cellLengthOf(piece);
216
+ else
217
+ position += piece.length;
218
+ }
219
+ }
220
+ else if (cellOffset > 0) {
221
+ breaks.push(start);
222
+ cellOffset = cellLengthOf(wholeWord);
223
+ }
224
+ word = nextWord(characters, end);
225
+ }
226
+ return breaks;
227
+ }
228
+ /**
229
+ 行がコードポイント数で幅を超えるとき、超えた分だけ末尾の空白を落とす
230
+ */
231
+ function stripExcessWhitespace(characters, width) {
232
+ if (characters.length <= width)
233
+ return characters;
234
+ let whitespace = 0;
235
+ while (whitespace < characters.length && isSpaceAt(characters, characters.length - 1 - whitespace))
236
+ whitespace += 1;
237
+ return characters.slice(0, characters.length - Math.min(whitespace, characters.length - width));
238
+ }
239
+ /**
240
+ 幅に収まるように折り返した行を返す。短い行を空白で埋めはしない。
241
+ 入力の改行はそのまま行の区切りにする。長すぎる語は幅の単位で折る
242
+ */
243
+ export function wrapText(text, width) {
244
+ return text.split('\n').flatMap((line) => {
245
+ const characters = splitCodePoints(line);
246
+ const offsets = [0, ...divideLine(characters, width), characters.length];
247
+ return offsets.slice(0, -1).map((from, index) => {
248
+ const piece = characters.slice(from, offsets.at(index + 1));
249
+ return truncateCells(stripExcessWhitespace(piece, width), width).join('');
250
+ });
251
+ });
252
+ }
253
+ /**
254
+ 行を幅のセル数まで空白で埋める
255
+ */
256
+ export function padCells(line, width) {
257
+ return `${line}${' '.repeat(Math.max(0, width - cellLength(line)))}`;
258
+ }
259
+ const MIN_WIDTH = 10;
260
+ const DEFAULT_WIDTH = 80;
261
+ function isDigits(value) {
262
+ return value !== undefined && /^\d+$/u.test(value);
263
+ }
264
+ /**
265
+ 枠の幅。typer と rich に合わせた順で決める。TERMINAL_WIDTH、COLUMNS、端末の幅、無ければ 80
266
+ */
267
+ export function outputWidth(environment, terminalColumns) {
268
+ const terminalWidth = environment['TERMINAL_WIDTH'];
269
+ const columns = environment['COLUMNS'];
270
+ let width = DEFAULT_WIDTH;
271
+ if (isDigits(terminalWidth) && Number(terminalWidth) > 0)
272
+ width = Number(terminalWidth);
273
+ else if (isDigits(columns) && Number(columns) > 0)
274
+ width = Number(columns);
275
+ else if (terminalColumns !== undefined && terminalColumns > 0)
276
+ width = terminalColumns;
277
+ return Math.max(MIN_WIDTH, width);
278
+ }
279
+ /**
280
+ 枠付きの表示。左上にタイトルを置く。lines は枠の内側の幅に収まっている行。末尾は改行で終わる
281
+ */
282
+ function framePanel(title, lines, width) {
283
+ const inner = width - 4;
284
+ const titleText = ` ${title} `;
285
+ const top = `╭─${titleText}${'─'.repeat(Math.max(0, width - 3 - cellLength(titleText)))}╮`;
286
+ const body = lines.map(line => `│ ${padCells(line, inner)} │`);
287
+ return `${[top, ...body, `╰${'─'.repeat(width - 2)}╯`].join('\n')}\n`;
288
+ }
289
+ /**
290
+ 枠付きの表示。本文は幅に合わせて折り返す。末尾は改行で終わる
291
+ */
292
+ export function renderPanel(title, body, width) {
293
+ return framePanel(title, wrapText(body, width - 4), width);
294
+ }
295
+ //# sourceMappingURL=rich-panel.js.map
@@ -0,0 +1,3 @@
1
+ import type { CommandDefinition } from './command-definition.js';
2
+ export declare const SECURITY_COMMANDS: readonly CommandDefinition[];
3
+ //# sourceMappingURL=security.d.ts.map