@pecot/workflow-mcp 6.0.0 → 7.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.
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +4 -57
- package/cli/dist/commands/board-add.js +11 -1
- package/cli/dist/commands/board-judgment.d.ts +12 -3
- package/cli/dist/commands/board-judgment.js +12 -6
- package/cli/dist/commands/board-move.js +9 -1
- package/cli/dist/commands/board-readiness.d.ts +3 -3
- package/cli/dist/commands/board-show.js +5 -2
- package/cli/dist/commands/board-sync.js +14 -5
- package/cli/dist/commands/board-view.d.ts +94 -5
- package/cli/dist/commands/board-view.js +119 -17
- package/cli/dist/commands/branch.js +12 -1
- package/cli/dist/commands/command-definition.d.ts +19 -2
- package/cli/dist/commands/command-definition.js +9 -0
- package/cli/dist/commands/failure-kind.d.ts +1 -1
- package/cli/dist/commands/failure-kind.js +1 -0
- package/cli/dist/commands/issue-board.d.ts +31 -3
- package/cli/dist/commands/issue-board.js +28 -4
- package/cli/dist/commands/issue-comment.js +19 -2
- package/cli/dist/commands/issue-create.js +90 -7
- package/cli/dist/commands/issue-dependency.js +32 -4
- package/cli/dist/commands/issue-readiness.d.ts +30 -2
- package/cli/dist/commands/issue-readiness.js +35 -0
- package/cli/dist/commands/issue-reopen.js +18 -3
- package/cli/dist/commands/issue-shared.d.ts +3 -3
- package/cli/dist/commands/issue-shared.js +3 -2
- package/cli/dist/commands/issue-show.js +78 -8
- package/cli/dist/commands/names.js +1 -0
- package/cli/dist/commands/output.d.ts +18 -2
- package/cli/dist/commands/output.js +16 -3
- package/cli/dist/commands/pr-merge.d.ts +3 -0
- package/cli/dist/commands/pr-merge.js +230 -0
- package/cli/dist/commands/pr-reference.d.ts +8 -0
- package/cli/dist/commands/pr-reference.js +30 -0
- package/cli/dist/commands/pr-restack.d.ts +6 -0
- package/cli/dist/commands/pr-restack.js +233 -0
- package/cli/dist/commands/pr-waiting.d.ts +41 -2
- package/cli/dist/commands/pr-waiting.js +74 -37
- package/cli/dist/commands/pr.d.ts +10 -6
- package/cli/dist/commands/pr.js +160 -67
- package/cli/dist/commands/project-sync.d.ts +10 -2
- package/cli/dist/commands/project-sync.js +7 -6
- package/cli/dist/commands/project.js +173 -16
- package/cli/dist/commands/registry.js +2 -0
- package/cli/dist/commands/release.js +67 -18
- package/cli/dist/commands/repository-match.js +7 -2
- package/cli/dist/commands/session-finish.js +128 -12
- package/cli/dist/commands/session-start.js +22 -6
- package/cli/dist/commands/session.js +121 -20
- package/cli/dist/commands/tracker-sync-dependabot.js +21 -4
- package/cli/dist/commands/tracker-sync-security.js +22 -4
- package/cli/dist/commands/tracker-sync.d.ts +30 -0
- package/cli/dist/commands/tracker-sync.js +15 -0
- package/cli/dist/commands/tracker.js +2 -0
- package/cli/dist/commands/work-target.d.ts +10 -2
- package/cli/dist/commands/work-target.js +6 -4
- package/cli/dist/commands/worktree.js +71 -5
- package/cli/dist/domain/issue-readout.d.ts +0 -4
- package/cli/dist/domain/pull-request.d.ts +1 -1
- package/cli/dist/domain/readiness.d.ts +17 -0
- package/cli/dist/domain/readiness.js +39 -2
- package/cli/dist/domain/restack.d.ts +62 -0
- package/cli/dist/domain/restack.js +53 -0
- package/cli/dist/domain/state.d.ts +39 -0
- package/cli/dist/domain/state.js +28 -1
- package/cli/dist/domain/work-start.d.ts +0 -4
- package/cli/dist/domain/work-start.js +0 -7
- package/cli/dist/infra/git.d.ts +64 -0
- package/cli/dist/infra/git.js +110 -0
- package/cli/dist/infra/github/pulls.d.ts +81 -2
- package/cli/dist/infra/github/pulls.js +116 -5
- package/cli/dist/infra/github/queries.d.ts +4 -1
- package/cli/dist/infra/github/queries.js +47 -32
- package/cli/dist/infra/github/readout.js +0 -33
- package/cli/dist/mcp/result.d.ts +63 -16
- package/cli/dist/mcp/result.js +106 -30
- package/cli/dist/mcp/server.js +37 -4
- package/cli/templates/comment/plan.md +2 -2
- package/cli/templates/comment/review-findings.md +3 -2
- package/cli/templates/issue/bug.md +13 -4
- package/cli/templates/issue/task.md +13 -4
- package/cli/templates/workflow/pecot-board-sync.yml +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -10,7 +10,7 @@ import { stopWith, requireActiveConfigPlain } from './command-stop.js';
|
|
|
10
10
|
import { kindOfFailure } from './failure.js';
|
|
11
11
|
import { createGitHubClient } from './github-client.js';
|
|
12
12
|
import { ISSUE_REF_DESCRIPTION, issuePath, parseIssueReferenceOrExit, runWithExit } from './issue-shared.js';
|
|
13
|
-
import { blockedByReadCount, createReadinessCache, judgeSnapshot, readIssueReadiness, readinessJson, snapshotWithoutPrerequisites } from './issue-readiness.js';
|
|
13
|
+
import { blockedByReadCount, createReadinessCache, judgeSnapshot, READINESS_JSON, readIssueReadiness, readinessJson, snapshotWithoutPrerequisites } from './issue-readiness.js';
|
|
14
14
|
import { createOutput } from './output.js';
|
|
15
15
|
const MAX_COMMENT_LIMIT = 20;
|
|
16
16
|
const DEFAULT_COMMENT_LIMIT = 5;
|
|
@@ -25,14 +25,14 @@ function parentLine(issue, owner, repository) {
|
|
|
25
25
|
/**
|
|
26
26
|
Issue の情報を Markdown にする。owner・repo は、親が別のリポジトリかを調べる元
|
|
27
27
|
*/
|
|
28
|
-
function formatIssueMarkdown(issue, owner, repository,
|
|
28
|
+
function formatIssueMarkdown(issue, owner, repository, readinessLines) {
|
|
29
29
|
const lines = [`## #${String(issue.number)} ${issue.title}`, ''];
|
|
30
30
|
const meta = [`**State:** ${issue.state}`];
|
|
31
31
|
if (issue.comments.total_count > 0)
|
|
32
32
|
meta.push(`**Comments:** ${String(issue.comments.total_count)}`);
|
|
33
33
|
lines.push(meta.join(' | '), '');
|
|
34
34
|
const parent = parentLine(issue, owner, repository);
|
|
35
|
-
const header = [...parent === undefined ? [] : [parent], ...
|
|
35
|
+
const header = [...parent === undefined ? [] : [parent], ...readinessLines];
|
|
36
36
|
if (header.length > 0)
|
|
37
37
|
lines.push(...header, '');
|
|
38
38
|
if (issue.linked_branches.length > 0) {
|
|
@@ -54,6 +54,54 @@ function formatIssueMarkdown(issue, owner, repository, readiness) {
|
|
|
54
54
|
}
|
|
55
55
|
return lines.join('\n');
|
|
56
56
|
}
|
|
57
|
+
const PREREQUISITE_JSON = z.strictObject({
|
|
58
|
+
number: z.int().describe('Issue number of the prerequisite'),
|
|
59
|
+
repository: z.string().describe('Repository of the prerequisite (owner/repo)'),
|
|
60
|
+
state: z.enum(['OPEN', 'CLOSED']).describe('State of the prerequisite'),
|
|
61
|
+
wait: z.enum(['stack', 'merge']).describe('How to wait for the prerequisite (stack on its branch, or wait for its merge)'),
|
|
62
|
+
reason: z.string().nullable().describe('Reason written in the Waits for merge section (null when none)'),
|
|
63
|
+
});
|
|
64
|
+
const COMMENT_JSON = z.strictObject({
|
|
65
|
+
author: z.string().describe('Login of the author (ghost when deleted)'),
|
|
66
|
+
created_at: z.string().describe('Creation time (ISO 8601)'),
|
|
67
|
+
body: z.string().describe('Body of the comment'),
|
|
68
|
+
});
|
|
69
|
+
const COMMENTS_JSON = z.strictObject({
|
|
70
|
+
total_count: z.int().describe('Number of all comments'),
|
|
71
|
+
nodes: z.array(COMMENT_JSON).describe('Latest comments, up to comment_limit'),
|
|
72
|
+
});
|
|
73
|
+
const PARENT_JSON = z.strictObject({
|
|
74
|
+
number: z.int().describe('Issue number of the parent'),
|
|
75
|
+
title: z.string().describe('Title of the parent'),
|
|
76
|
+
state: z.string().describe('State of the parent (OPEN or CLOSED)'),
|
|
77
|
+
owner: z.string().nullable().describe('Owner of the parent repository'),
|
|
78
|
+
repo: z.string().nullable().describe('Name of the parent repository'),
|
|
79
|
+
});
|
|
80
|
+
const SUB_ISSUE_JSON = z.strictObject({
|
|
81
|
+
number: z.int().describe('Issue number of the sub-issue'),
|
|
82
|
+
state: z.string().describe('State of the sub-issue (OPEN or CLOSED)'),
|
|
83
|
+
title: z.string().describe('Title of the sub-issue'),
|
|
84
|
+
});
|
|
85
|
+
const LINKED_PR_JSON = z.strictObject({
|
|
86
|
+
number: z.int().describe('PR number'),
|
|
87
|
+
title: z.string().describe('Title of the PR'),
|
|
88
|
+
state: z.string().describe('State of the PR'),
|
|
89
|
+
});
|
|
90
|
+
const ISSUE_SHOW_OUTPUT = z.strictObject({
|
|
91
|
+
number: z.int().describe('Issue number'),
|
|
92
|
+
state: z.string().describe('State of the Issue (OPEN or CLOSED)'),
|
|
93
|
+
title: z.string().describe('Title of the Issue'),
|
|
94
|
+
body: z.string().describe('Body of the Issue'),
|
|
95
|
+
comments: COMMENTS_JSON.describe('Comments of the Issue'),
|
|
96
|
+
parent: PARENT_JSON.nullable().describe('Parent Issue (null when none)'),
|
|
97
|
+
sub_issues: z.array(SUB_ISSUE_JSON).describe('Sub-issues'),
|
|
98
|
+
linked_branches: z.array(z.string()).describe('Branches linked to the Issue'),
|
|
99
|
+
linked_prs: z.array(LINKED_PR_JSON).describe('PRs linked to the Issue'),
|
|
100
|
+
owner: z.string().describe('Owner of the Issue repository'),
|
|
101
|
+
repo: z.string().describe('Name of the Issue repository'),
|
|
102
|
+
blockedBy: z.array(PREREQUISITE_JSON).nullable().describe('Prerequisites (blocked by) of the Issue (null when they could not be read)'),
|
|
103
|
+
readiness: READINESS_JSON.nullable().describe('Readiness judgment (null when it could not be judged)'),
|
|
104
|
+
});
|
|
57
105
|
// 前提と判定を読めなかったとき
|
|
58
106
|
const UNREAD = { lines: [], blockedBy: JSON_NULL, readiness: JSON_NULL };
|
|
59
107
|
/**
|
|
@@ -63,10 +111,11 @@ function readinessView(source, readiness, text) {
|
|
|
63
111
|
const home = source.ref.repository;
|
|
64
112
|
const entries = source.prerequisites.map((prerequisite) => {
|
|
65
113
|
const { issue, wait, reason } = prerequisite;
|
|
114
|
+
const state = issue.state === 'open' ? 'OPEN' : 'CLOSED';
|
|
66
115
|
return {
|
|
67
116
|
label: displayReference(issue.ref, home),
|
|
68
117
|
title: source.titles.get(issueReferenceKey(issue.ref)) ?? '',
|
|
69
|
-
state
|
|
118
|
+
state,
|
|
70
119
|
status: describePrerequisiteState(prerequisite),
|
|
71
120
|
wait,
|
|
72
121
|
reason,
|
|
@@ -82,6 +131,27 @@ function readinessView(source, readiness, text) {
|
|
|
82
131
|
readiness: readiness === undefined || text === undefined ? JSON_NULL : readinessJson(readiness, text),
|
|
83
132
|
};
|
|
84
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
--json の標準出力と MCP の data の中身。キーを並べた順が --json の出力の順になる
|
|
136
|
+
*/
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types -- zod の入れ子の型
|
|
138
|
+
function issueShowJson(issue, owner, repository, view) {
|
|
139
|
+
return {
|
|
140
|
+
number: issue.number,
|
|
141
|
+
state: issue.state,
|
|
142
|
+
title: issue.title,
|
|
143
|
+
body: issue.body,
|
|
144
|
+
comments: { total_count: issue.comments.total_count, nodes: [...issue.comments.nodes] },
|
|
145
|
+
parent: issue.parent,
|
|
146
|
+
sub_issues: [...issue.sub_issues],
|
|
147
|
+
linked_branches: [...issue.linked_branches],
|
|
148
|
+
linked_prs: [...issue.linked_prs],
|
|
149
|
+
owner,
|
|
150
|
+
repo: repository,
|
|
151
|
+
blockedBy: view.blockedBy === JSON_NULL ? JSON_NULL : [...view.blockedBy],
|
|
152
|
+
readiness: view.readiness,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
85
155
|
function judgedView(judged) {
|
|
86
156
|
const { readiness } = judged;
|
|
87
157
|
return readinessView(judged, readiness, readiness === undefined ? undefined : describeReadiness(readiness, judged.prerequisites, judged.ref.repository));
|
|
@@ -139,6 +209,7 @@ const ISSUE_SHOW = defineCommand({
|
|
|
139
209
|
json: z.boolean().default(false).describe('Output as JSON'),
|
|
140
210
|
comment_limit: z.int().min(0).max(MAX_COMMENT_LIMIT).default(DEFAULT_COMMENT_LIMIT).describe('Number of comments to show (0-20; 0 hides them)'),
|
|
141
211
|
}),
|
|
212
|
+
output: ISSUE_SHOW_OUTPUT,
|
|
142
213
|
positional: ['issue_ref'],
|
|
143
214
|
shortFlags: { json: '-j', comment_limit: '-c' },
|
|
144
215
|
interactive: false,
|
|
@@ -157,11 +228,10 @@ const ISSUE_SHOW = defineCommand({
|
|
|
157
228
|
throw error;
|
|
158
229
|
return stopWith(output, `could not read Issue ${issuePath(owner, repository, number)}: ${error.message}`, kindOfFailure(error));
|
|
159
230
|
}
|
|
160
|
-
const jsonFields = { ...issue, blockedByCount: undefined };
|
|
161
231
|
const view = await readView(output, client, config, issue, createIssueReference(createRepositoryReference(owner, repository), number));
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
232
|
+
const json = issueShowJson(issue, owner, repository, view);
|
|
233
|
+
output.result(json);
|
|
234
|
+
output.data(input.json ? JSON.stringify(json, undefined, 2) : formatIssueMarkdown(issue, owner, repository, view.lines));
|
|
165
235
|
});
|
|
166
236
|
},
|
|
167
237
|
});
|
|
@@ -51,6 +51,7 @@ export const COMMAND_VERBS = [
|
|
|
51
51
|
{ name: 'sync', meaning: '判定や一覧に合わせて書き直す' },
|
|
52
52
|
{ name: 'move', meaning: 'ボードの列を移す' },
|
|
53
53
|
{ name: 'merge', meaning: 'PR をマージする' },
|
|
54
|
+
{ name: 'restack', meaning: '積んだブランチを、base の新しい位置へ載せ直す' },
|
|
54
55
|
{ name: 'comment', meaning: 'コメントを書く' },
|
|
55
56
|
{ name: 'reopen', meaning: '閉じたものを開き直す' },
|
|
56
57
|
{ name: 'block', meaning: '前提を付ける' },
|
|
@@ -34,7 +34,14 @@ export interface Operation {
|
|
|
34
34
|
*/
|
|
35
35
|
readonly aside?: boolean;
|
|
36
36
|
}
|
|
37
|
-
|
|
37
|
+
/**
|
|
38
|
+
dry_run を持つコマンドの output の dryRun。値は createOutput の result が入れる
|
|
39
|
+
*/
|
|
40
|
+
export declare const DRY_RUN_FIELD: z.ZodOptional<z.ZodLiteral<true>>;
|
|
41
|
+
/**
|
|
42
|
+
D は MCP の成功の data の型(コマンドの output の z.input)。CLI では result は何も出さない
|
|
43
|
+
*/
|
|
44
|
+
export interface Output<D extends object = Record<string, unknown>> {
|
|
38
45
|
/**
|
|
39
46
|
dry-run のときだけ、実行する操作を stderr に出す
|
|
40
47
|
*/
|
|
@@ -56,8 +63,17 @@ export interface Output {
|
|
|
56
63
|
結果の本体を stdout にそのまま出す。末尾に改行が無ければ 1 つ足す
|
|
57
64
|
*/
|
|
58
65
|
readonly data: (text: string) => void;
|
|
66
|
+
/**
|
|
67
|
+
MCP の成功の data に項目を渡す。何度も呼べば浅く重なる(後の値が勝つ)ので、配列は組み終えてから 1 回だけ渡す。
|
|
68
|
+
dry-run のときは dryRun: true を重ねるので、output に DRY_RUN_FIELD を置く
|
|
69
|
+
*/
|
|
70
|
+
result(fields: Readonly<Partial<D>>): void;
|
|
59
71
|
}
|
|
60
|
-
|
|
72
|
+
/**
|
|
73
|
+
warning の行の前置き。MCP の側はこれで stderr から warning の文を拾う
|
|
74
|
+
*/
|
|
75
|
+
export declare const WARNING_PREFIX = "warning: ";
|
|
76
|
+
export declare function createOutput<D extends object = Record<string, unknown>>(isDryRun: boolean, io: CommandIo): Output<D>;
|
|
61
77
|
/**
|
|
62
78
|
fd から buffer へ読んだバイト数。0 は終端
|
|
63
79
|
*/
|
|
@@ -55,11 +55,19 @@ export function pastOf(verb) {
|
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
/**
|
|
58
|
+
dry_run を持つコマンドの output の dryRun。値は createOutput の result が入れる
|
|
59
|
+
*/
|
|
60
|
+
export const DRY_RUN_FIELD = z.literal(true).optional().describe('true on dry_run (nothing was changed)');
|
|
61
|
+
/**
|
|
62
|
+
warning の行の前置き。MCP の側はこれで stderr から warning の文を拾う
|
|
63
|
+
*/
|
|
64
|
+
export const WARNING_PREFIX = 'warning: ';
|
|
65
|
+
/**
|
|
58
66
|
末尾の改行を取り除き、2 行目以降の先頭に空白 2 つを付け、末尾に改行を 1 つ付ける
|
|
59
67
|
*/
|
|
60
68
|
function formatMessage(prefix, message) {
|
|
61
69
|
const lines = message.replace(/\n+$/, '').split('\n');
|
|
62
|
-
return `${prefix}
|
|
70
|
+
return `${prefix}${lines.join('\n ')}\n`;
|
|
63
71
|
}
|
|
64
72
|
export function createOutput(isDryRun, io) {
|
|
65
73
|
const render = (message) => formatHinted(message, io.surface ?? 'cli');
|
|
@@ -81,10 +89,12 @@ export function createOutput(isDryRun, io) {
|
|
|
81
89
|
writeResult(`${pastOf(verb)}: ${target}`, aside);
|
|
82
90
|
},
|
|
83
91
|
warning: (message) => {
|
|
84
|
-
|
|
92
|
+
const text = render(message);
|
|
93
|
+
io.stderr(formatMessage(WARNING_PREFIX, text));
|
|
94
|
+
io.warning?.(text.replace(/\n+$/, ''));
|
|
85
95
|
},
|
|
86
96
|
error: (message) => {
|
|
87
|
-
io.stderr(formatMessage('error', render(message)));
|
|
97
|
+
io.stderr(formatMessage('error: ', render(message)));
|
|
88
98
|
},
|
|
89
99
|
count: (label, value) => {
|
|
90
100
|
io.stderr(`count: ${label}: ${value === undefined ? 'unknown' : String(value)}\n`);
|
|
@@ -92,6 +102,9 @@ export function createOutput(isDryRun, io) {
|
|
|
92
102
|
data: (text) => {
|
|
93
103
|
io.stdout(text.endsWith('\n') ? text : `${text}\n`);
|
|
94
104
|
},
|
|
105
|
+
result(fields) {
|
|
106
|
+
io.data?.(isDryRun ? { ...fields, dryRun: true } : fields);
|
|
107
|
+
},
|
|
95
108
|
};
|
|
96
109
|
}
|
|
97
110
|
const RETRY_WAIT_MILLISECONDS = 10;
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- GitHubClient は内部の状態を持つ */
|
|
2
|
+
// pr merge。単独の PR もスタックの PR も、非同期のマージ API(merge-async)でマージする。
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
import { z } from 'zod';
|
|
5
|
+
import { describeApiError } from '../domain/api-error-detail.js';
|
|
6
|
+
import { jsonField, tryParseJsonText } from '../domain/json.js';
|
|
7
|
+
import { FLAG_MERGE_SLASH_PENDING } from '../domain/state.js';
|
|
8
|
+
import { GitHubAPIError, GitHubNotFoundError } from '../infra/github/errors.js';
|
|
9
|
+
import { consumeFlag } from '../infra/state-store.js';
|
|
10
|
+
import { requireActiveConfigPlain, stopWith } from './command-stop.js';
|
|
11
|
+
import { defineCommand } from './command-definition.js';
|
|
12
|
+
import { kindOfFailure } from './failure.js';
|
|
13
|
+
import { createGitHubClient } from './github-client.js';
|
|
14
|
+
import { createOutput, DRY_RUN_FIELD } from './output.js';
|
|
15
|
+
import { parseReference, PR_REFERENCE_DESCRIPTION } from './pr-reference.js';
|
|
16
|
+
// 要求を送らずに止める(ドラフト、閉じている、スタックの最下段でない)
|
|
17
|
+
const NOT_MERGEABLE_EXIT_CODE = 2;
|
|
18
|
+
const POLL_INTERVAL_MILLISECONDS = 2000;
|
|
19
|
+
// R14 では 3〜8 秒で終わった。時刻でなく回数で数え、偽のタイマーでも同じに動かす
|
|
20
|
+
const POLL_LIMIT = 30;
|
|
21
|
+
const POLL_LIMIT_SECONDS = (POLL_INTERVAL_MILLISECONDS * POLL_LIMIT) / 1000;
|
|
22
|
+
// 同期のマージ API が、マージできない(405)・head が sha と違う(409)ときに返す状態コード
|
|
23
|
+
const SYNC_MERGE_REJECTED_STATUSES = new Set([405, 409]);
|
|
24
|
+
const MergeInputSchema = z.strictObject({
|
|
25
|
+
pr_ref: z.string().describe(PR_REFERENCE_DESCRIPTION),
|
|
26
|
+
dry_run: z.boolean().default(false).describe('Read the PR and its stack, and show what would happen without merging'),
|
|
27
|
+
});
|
|
28
|
+
const PR_MERGE_OUTPUT = z.strictObject({
|
|
29
|
+
pullRequest: z.string().describe('PR (owner/repo/N)'),
|
|
30
|
+
base: z.string().describe('Base branch the PR is merged into'),
|
|
31
|
+
status: z.enum(['merged', 'enqueued']).optional().describe('merged, or enqueued when the PR was added to the merge queue (absent on dry_run)'),
|
|
32
|
+
stack: z.strictObject({
|
|
33
|
+
number: z.int().describe('Stack number'),
|
|
34
|
+
size: z.int().describe('Number of PRs in the stack from this PR upward (K of "1 of K")'),
|
|
35
|
+
}).optional().describe('Stack the PR is the bottom of (absent for a PR outside a stack)'),
|
|
36
|
+
dryRun: DRY_RUN_FIELD,
|
|
37
|
+
});
|
|
38
|
+
function pathOf(target) {
|
|
39
|
+
return `${target.owner}/${target.repository}/${String(target.number)}`;
|
|
40
|
+
}
|
|
41
|
+
// マージまで進まずに止まるので、/pecot-w:w-merge が立てた確認の免除を残さない。dry-run は状態を変えない
|
|
42
|
+
function stopNotMergeable(out, message, isDryRun) {
|
|
43
|
+
if (!isDryRun)
|
|
44
|
+
consumeFlag(FLAG_MERGE_SLASH_PENDING);
|
|
45
|
+
return stopWith(out, message, 'pr-state', NOT_MERGEABLE_EXIT_CODE);
|
|
46
|
+
}
|
|
47
|
+
// 印は残す。w-merge は必須チェックを待って同じ PR に pr_merge を呼び直す
|
|
48
|
+
function stopRejected(out, reason) {
|
|
49
|
+
return stopWith(out, `merge was rejected: ${reason}`, 'pr-state');
|
|
50
|
+
}
|
|
51
|
+
async function readPullRequest(out, client, target) {
|
|
52
|
+
try {
|
|
53
|
+
return await client.getPullRequestForMerge(target.owner, target.repository, target.number);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof GitHubNotFoundError)
|
|
57
|
+
return stopWith(out, `PR not found: ${pathOf(target)}`, 'github-not-found');
|
|
58
|
+
if (!(error instanceof GitHubAPIError))
|
|
59
|
+
throw error;
|
|
60
|
+
return stopWith(out, `could not read PR ${pathOf(target)}: ${describeApiError(error)}`, kindOfFailure(error));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function checkState(out, name, pullRequest, isDryRun) {
|
|
64
|
+
if (pullRequest.state === 'MERGED')
|
|
65
|
+
stopNotMergeable(out, `${name} is already merged`, isDryRun);
|
|
66
|
+
if (pullRequest.state !== 'OPEN')
|
|
67
|
+
stopNotMergeable(out, `${name} is closed`, isDryRun);
|
|
68
|
+
if (pullRequest.isDraft)
|
|
69
|
+
stopNotMergeable(out, `${name} is a draft`, isDryRun);
|
|
70
|
+
}
|
|
71
|
+
// 下の段に open の PR があれば止める。マージ済みで並びに残る PR は飛ばす。スタックの機能が無いリポは単独の PR として扱う
|
|
72
|
+
async function bottomOfStack(out, client, target, isDryRun) {
|
|
73
|
+
let stack;
|
|
74
|
+
try {
|
|
75
|
+
stack = await client.getStackOfPullRequest(target.owner, target.repository, target.number);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error instanceof GitHubNotFoundError)
|
|
79
|
+
return undefined;
|
|
80
|
+
if (!(error instanceof GitHubAPIError))
|
|
81
|
+
throw error;
|
|
82
|
+
return stopWith(out, `could not read the stack of PR ${pathOf(target)}: ${describeApiError(error)}`, kindOfFailure(error));
|
|
83
|
+
}
|
|
84
|
+
if (stack === undefined)
|
|
85
|
+
return undefined;
|
|
86
|
+
const index = stack.pullRequests.indexOf(target.number);
|
|
87
|
+
for (const lower of stack.pullRequests.slice(0, index)) {
|
|
88
|
+
const pullRequest = await readPullRequest(out, client, { ...target, number: lower });
|
|
89
|
+
if (pullRequest.state === 'OPEN') {
|
|
90
|
+
stopNotMergeable(out, `PR #${String(target.number)} is not the bottom of stack #${String(stack.number)}. Merge PR #${String(lower)} first`, isDryRun);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return { number: stack.number, size: stack.pullRequests.length - index };
|
|
94
|
+
}
|
|
95
|
+
function finishMerged(context) {
|
|
96
|
+
context.out.done({ verb: 'merge', target: `${context.name} into ${context.base}${context.suffix}` });
|
|
97
|
+
context.out.result({ status: 'merged' });
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
function finishEnqueued(context) {
|
|
101
|
+
context.out.done({ verb: 'add', target: `${context.name} to merge queue of ${context.base}` });
|
|
102
|
+
context.out.result({ status: 'enqueued' });
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
function finishResult(context, result) {
|
|
106
|
+
if (result.status === 'merged')
|
|
107
|
+
return finishMerged(context);
|
|
108
|
+
if (result.status === 'enqueued')
|
|
109
|
+
return finishEnqueued(context);
|
|
110
|
+
return stopRejected(context.out, result.message ?? 'GitHub gave no reason');
|
|
111
|
+
}
|
|
112
|
+
// PR の状態から決める。マージ済みでもキューの中でもなければ undefined
|
|
113
|
+
async function finishIfSettled(context) {
|
|
114
|
+
const pullRequest = await readPullRequest(context.out, context.client, context.target);
|
|
115
|
+
if (pullRequest.state === 'MERGED')
|
|
116
|
+
return finishMerged(context);
|
|
117
|
+
if (pullRequest.isInMergeQueue)
|
|
118
|
+
return finishEnqueued(context);
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
function stopUnconfirmed(context) {
|
|
122
|
+
return stopWith(context.out, `could not confirm the merge of ${context.name}: it is neither merged nor in the merge queue`, 'pr-state');
|
|
123
|
+
}
|
|
124
|
+
async function finishFromState(context) {
|
|
125
|
+
return await finishIfSettled(context) ?? stopUnconfirmed(context);
|
|
126
|
+
}
|
|
127
|
+
// 結果の 404 は、消えた結果のほか、書いた直後の読み取りの遅れでも返る。そのときは期限まで読み直す
|
|
128
|
+
async function pollResult(context, uuid) {
|
|
129
|
+
const { owner, repository, number } = context.target;
|
|
130
|
+
let isResultMissing = false;
|
|
131
|
+
for (let attempt = 0; attempt < POLL_LIMIT; attempt += 1) {
|
|
132
|
+
await delay(POLL_INTERVAL_MILLISECONDS);
|
|
133
|
+
let result;
|
|
134
|
+
try {
|
|
135
|
+
result = await context.client.getAsyncMergeResult(owner, repository, number, uuid);
|
|
136
|
+
}
|
|
137
|
+
catch (error) {
|
|
138
|
+
if (!(error instanceof GitHubAPIError))
|
|
139
|
+
throw error;
|
|
140
|
+
return stopWith(context.out, `could not read the merge result of PR ${pathOf(context.target)}: ${describeApiError(error)}`, kindOfFailure(error));
|
|
141
|
+
}
|
|
142
|
+
if (result === undefined) {
|
|
143
|
+
isResultMissing = true;
|
|
144
|
+
const settled = await finishIfSettled(context);
|
|
145
|
+
if (settled !== undefined)
|
|
146
|
+
return settled;
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
isResultMissing = false;
|
|
150
|
+
if (result.status !== 'pending')
|
|
151
|
+
return finishResult(context, result);
|
|
152
|
+
}
|
|
153
|
+
return isResultMissing
|
|
154
|
+
? stopUnconfirmed(context)
|
|
155
|
+
: stopWith(context.out, `merge did not finish in ${String(POLL_LIMIT_SECONDS)}s. Check ${context.name}`, 'github-api');
|
|
156
|
+
}
|
|
157
|
+
function reasonOf(error) {
|
|
158
|
+
const message = jsonField(tryParseJsonText(error.responseBody ?? ''), 'message');
|
|
159
|
+
return typeof message === 'string' && message !== '' ? message : describeApiError(error);
|
|
160
|
+
}
|
|
161
|
+
// merge-async の無い GHES 向け。sha を渡し、読んだ後に push された PR をマージしない
|
|
162
|
+
async function mergeSynchronously(context, pullRequest) {
|
|
163
|
+
const { owner, repository, number } = context.target;
|
|
164
|
+
try {
|
|
165
|
+
await context.client.mergePullRequest(owner, repository, number, 'squash', pullRequest.headRefOid);
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
if (!(error instanceof GitHubAPIError))
|
|
169
|
+
throw error;
|
|
170
|
+
return error.statusCode !== undefined && SYNC_MERGE_REJECTED_STATUSES.has(error.statusCode)
|
|
171
|
+
? stopRejected(context.out, reasonOf(error))
|
|
172
|
+
: stopWith(context.out, `could not merge PR ${pathOf(context.target)}: ${describeApiError(error)}`, kindOfFailure(error));
|
|
173
|
+
}
|
|
174
|
+
return finishMerged(context);
|
|
175
|
+
}
|
|
176
|
+
async function requestMerge(context, pullRequest) {
|
|
177
|
+
const { owner, repository, number } = context.target;
|
|
178
|
+
let requested;
|
|
179
|
+
try {
|
|
180
|
+
requested = await context.client.requestAsyncMerge(owner, repository, number, { sha: pullRequest.headRefOid, mergeQueue: pullRequest.isMergeQueueEnabled });
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (!(error instanceof GitHubAPIError))
|
|
184
|
+
throw error;
|
|
185
|
+
// 同期の API はスタックの PR を 403 で拒むので、スタックの中では戻さない
|
|
186
|
+
return error instanceof GitHubNotFoundError && !context.isInStack
|
|
187
|
+
? mergeSynchronously(context, pullRequest)
|
|
188
|
+
: stopWith(context.out, `could not merge PR ${pathOf(context.target)}: ${describeApiError(error)}`, kindOfFailure(error));
|
|
189
|
+
}
|
|
190
|
+
switch (requested.outcome) {
|
|
191
|
+
case 'rejected': {
|
|
192
|
+
return stopRejected(context.out, requested.message);
|
|
193
|
+
}
|
|
194
|
+
// 文書は 409 の本文の形を示していない。uuid が無ければ PR の状態から決める
|
|
195
|
+
case 'conflict': {
|
|
196
|
+
return requested.uuid === undefined ? finishFromState(context) : pollResult(context, requested.uuid);
|
|
197
|
+
}
|
|
198
|
+
case 'accepted': {
|
|
199
|
+
const { result } = requested;
|
|
200
|
+
return result.status === 'pending' && result.uuid !== undefined ? pollResult(context, result.uuid) : finishResult(context, result);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
export const PR_MERGE_COMMAND = defineCommand({
|
|
205
|
+
path: ['pr', 'merge'],
|
|
206
|
+
description: 'Merge a PR with the asynchronous merge API (a PR in a stack must be the bottom one)',
|
|
207
|
+
input: MergeInputSchema,
|
|
208
|
+
output: PR_MERGE_OUTPUT,
|
|
209
|
+
positional: ['pr_ref'],
|
|
210
|
+
shortFlags: { dry_run: '-n' },
|
|
211
|
+
interactive: false,
|
|
212
|
+
run: async (input, io) => {
|
|
213
|
+
const out = createOutput(input.dry_run, io);
|
|
214
|
+
const config = requireActiveConfigPlain(out);
|
|
215
|
+
const target = parseReference(input.pr_ref, input.repo, out);
|
|
216
|
+
const client = createGitHubClient(out, config.host);
|
|
217
|
+
const name = `PR #${String(target.number)}`;
|
|
218
|
+
const pullRequest = await readPullRequest(out, client, target);
|
|
219
|
+
checkState(out, name, pullRequest, input.dry_run);
|
|
220
|
+
const stack = await bottomOfStack(out, client, target, input.dry_run);
|
|
221
|
+
out.result({ pullRequest: pathOf(target), base: pullRequest.baseRefName, ...stack !== undefined && { stack } });
|
|
222
|
+
const suffix = stack === undefined ? '' : ` (stack #${String(stack.number)}, 1 of ${String(stack.size)})`;
|
|
223
|
+
if (input.dry_run) {
|
|
224
|
+
out.planned({ verb: 'merge', target: `${name} into ${pullRequest.baseRefName}${suffix}` });
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
return requestMerge({ out, client, target, name, base: pullRequest.baseRefName, suffix, isInStack: stack !== undefined }, pullRequest);
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
//# sourceMappingURL=pr-merge.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Output } from './output.js';
|
|
2
|
+
export declare const PR_REFERENCE_DESCRIPTION = "PR reference (`owner/repo/N`, or the short form `N` in the repository of `--repo`, or else of the git remote of the current directory)";
|
|
3
|
+
export declare function parseReference(reference: string, globalRepository: string | undefined, out: Readonly<Output>): {
|
|
4
|
+
readonly owner: string;
|
|
5
|
+
readonly repository: string;
|
|
6
|
+
readonly number: number;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=pr-reference.d.ts.map
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// PR の参照(pr_ref)の読み方。pr show・pr merge・session finish が共有する。
|
|
2
|
+
import { ReferenceParseError, parsePullRequestReference } from '../domain/reference-parser.js';
|
|
3
|
+
import { AmbiguousRepositoryError, resolveRepository } from '../domain/repository-resolver.js';
|
|
4
|
+
import { gitRunnerFor } from '../infra/git.js';
|
|
5
|
+
import { stopWith } from './command-stop.js';
|
|
6
|
+
import { USAGE_EXIT_CODE } from './usage-error.js';
|
|
7
|
+
export const PR_REFERENCE_DESCRIPTION = 'PR reference (`owner/repo/N`, or the short form `N` in the repository of `--repo`, or else of the git remote of the current directory)';
|
|
8
|
+
// グローバルの --repo か、いまの git の repository。決められなければ undefined
|
|
9
|
+
function resolveBaseRepository(globalRepository) {
|
|
10
|
+
try {
|
|
11
|
+
return resolveRepository(globalRepository, gitRunnerFor(process.cwd())).repository;
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error instanceof AmbiguousRepositoryError)
|
|
15
|
+
return undefined;
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function parseReference(reference, globalRepository, out) {
|
|
20
|
+
try {
|
|
21
|
+
const parsed = parsePullRequestReference(reference, resolveBaseRepository(globalRepository), { repositoryFlag: true });
|
|
22
|
+
return { owner: parsed.repository.owner, repository: parsed.repository.repository, number: parsed.number };
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
if (error instanceof ReferenceParseError)
|
|
26
|
+
return stopWith(out, error.hintedText ?? error.message, 'invalid-input', USAGE_EXIT_CODE);
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=pr-reference.js.map
|