@pecot/workflow-mcp 8.0.1-dev.60 → 8.0.1-dev.61
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.
|
@@ -3,18 +3,19 @@
|
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import { describeApiError } from '../domain/api-error-detail.js';
|
|
5
5
|
import { getDevelopBranch } from '../domain/config.js';
|
|
6
|
-
import { FORCE_PUSH_COMMAND } from '../domain/pull-request.js';
|
|
6
|
+
import { extractClosingIssueNumbers, FORCE_PUSH_COMMAND } from '../domain/pull-request.js';
|
|
7
7
|
import { describeGone, describeRestackCheck, RESTACK_PATH } from '../domain/restack.js';
|
|
8
8
|
import { FLAG_PECOT_RESTACK, formatRestackProgress, interpretRestackProgress } from '../domain/state.js';
|
|
9
9
|
import { abortRebase, conflictedFiles, continueRebase, fetchRemote, forkPoint, gitStdout, hasSameTree, isAncestor, isRebaseInProgress, mergeBase, PUSH_TIMEOUT_MILLISECONDS, pushWithLease, rebaseOnto, remoteOnlyChanges, resetHardTo, resolveCommit, } from '../infra/git.js';
|
|
10
|
+
import { findConfigFor } from '../infra/config-store.js';
|
|
10
11
|
import { readBaseRecord, readFlag, removeFlag, writeBaseRecord, writeFlag } from '../infra/state-store.js';
|
|
11
12
|
import { defineCommand } from './command-definition.js';
|
|
12
13
|
import { commandHint, hinted } from './command-hint.js';
|
|
13
14
|
import { PROJECT_INPUT, requireConfigFor, stopWith } from './command-stop.js';
|
|
14
15
|
import { kindOfFailure } from './failure.js';
|
|
15
|
-
import { createGitHubClient } from './github-client.js';
|
|
16
|
+
import { apiFailureMessage, createGitHubClient } from './github-client.js';
|
|
16
17
|
import { createOutput, DRY_RUN_FIELD } from './output.js';
|
|
17
|
-
import { stopOnMalformedRecord, validatePullRequestContext } from './pr.js';
|
|
18
|
+
import { primaryRemote, stopOnMalformedRecord, validatePullRequestContext } from './pr.js';
|
|
18
19
|
import { readRestackDecision } from './restack-reading.js';
|
|
19
20
|
import { readBaseOrInfer, warnUnresolved } from './base-reading.js';
|
|
20
21
|
import { USAGE_EXIT_CODE } from './usage-error.js';
|
|
@@ -31,6 +32,7 @@ const PR_RESTACK_OUTPUT = z.strictObject({
|
|
|
31
32
|
pullRequest: z.int().optional().describe('Merged PR of the lower branch (only when the cause is base-merged)'),
|
|
32
33
|
pushed: z.boolean().optional().describe('Whether the branch was pushed (only when restacked)'),
|
|
33
34
|
conflicts: z.array(z.string()).optional().describe('Files with conflicts (only on conflict)'),
|
|
35
|
+
relinked: z.int().optional().describe('Open PR whose body was saved again so that GitHub links its closing Issues (only after a restack onto the develop branch)'),
|
|
34
36
|
dryRun: DRY_RUN_FIELD,
|
|
35
37
|
});
|
|
36
38
|
const RestackInputSchema = z.strictObject({
|
|
@@ -117,6 +119,54 @@ async function finishRestack(out, cwd, progress, signal) {
|
|
|
117
119
|
out.result({ branch, base: ontoBranch, outcome: isSynced ? 'synced' : 'restacked', ...!isSynced && { pushed: leaseSha !== undefined } });
|
|
118
120
|
out.done({ verb: 'update', target: isSynced ? `${branch} synced with ${remoteBranch}` : `${branch} restacked on ${ontoBranch}` });
|
|
119
121
|
}
|
|
122
|
+
const RELINK_TARGET = 'to its closing Issues';
|
|
123
|
+
// 本文の Closes のうち、まだリンクになっていないものがあるか。GitHub がリンクにするのは、既定のブランチに向いた PR だけ
|
|
124
|
+
function hasUnlinkedClosingIssues(pullRequest, target) {
|
|
125
|
+
if (pullRequest.state !== 'OPEN' || pullRequest.base_ref !== pullRequest.default_branch)
|
|
126
|
+
return false;
|
|
127
|
+
const linked = new Set(pullRequest.closing_issues.filter(issue => issue.owner === target.owner && issue.repository === target.repository).map(issue => issue.number));
|
|
128
|
+
return extractClosingIssueNumbers(pullRequest.body, target).some(number => !linked.has(number));
|
|
129
|
+
}
|
|
130
|
+
// 開発ブランチへ載せ直した PR の本文を保存し直す。GitHub は base の付け替えでは Closes を読み直さず、Issue とリンクしない
|
|
131
|
+
async function relinkClosingIssues(out, target, progress) {
|
|
132
|
+
const { owner, repository, developBranch } = target;
|
|
133
|
+
if (progress.ontoBranch !== developBranch)
|
|
134
|
+
return;
|
|
135
|
+
let number;
|
|
136
|
+
try {
|
|
137
|
+
const client = createGitHubClient(out, target.host);
|
|
138
|
+
const { pullRequests } = await client.listPullRequestsByHead(owner, repository, progress.branch);
|
|
139
|
+
number = pullRequests.find(found => found.state === 'OPEN' && !found.isCrossRepository)?.number;
|
|
140
|
+
if (number === undefined)
|
|
141
|
+
return;
|
|
142
|
+
const pullRequest = await client.getPullRequestForFinish(owner, repository, number);
|
|
143
|
+
if (!hasUnlinkedClosingIssues(pullRequest, target))
|
|
144
|
+
return;
|
|
145
|
+
await client.updatePullRequestBody(owner, repository, number, pullRequest.body);
|
|
146
|
+
out.result({ relinked: number });
|
|
147
|
+
out.done({ verb: 'update', target: `PR #${String(number)} relinked ${RELINK_TARGET}` });
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
const label = number === undefined ? `the PR of ${progress.branch}` : `PR #${String(number)}`;
|
|
151
|
+
out.warning(`cannot relink ${label} ${RELINK_TARGET}: ${apiFailureMessage(error)}\nto link them, save the body of ${label} again on GitHub without changes`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function planRelink(out, branch, target, ontoBranch) {
|
|
155
|
+
if (ontoBranch === target.developBranch)
|
|
156
|
+
out.planned({ verb: 'update', target: `PR of ${branch} relinked ${RELINK_TARGET}` });
|
|
157
|
+
}
|
|
158
|
+
// --continue で終えた後に保存し直す先。rebase の間はブランチを読めないので、終えてから求める。求まらなければ保存し直さない
|
|
159
|
+
function relinkTargetOf(cwd, project) {
|
|
160
|
+
const location = primaryRemote(cwd)?.location;
|
|
161
|
+
if (location === undefined)
|
|
162
|
+
return undefined;
|
|
163
|
+
const lookup = findConfigFor(location, project);
|
|
164
|
+
if (lookup.kind !== 'found')
|
|
165
|
+
return undefined;
|
|
166
|
+
const { config } = lookup.found;
|
|
167
|
+
const developBranch = getDevelopBranch(config, location.owner, location.repository);
|
|
168
|
+
return developBranch === undefined || developBranch === '' ? undefined : { owner: location.owner, repository: location.repository, host: config.host, developBranch };
|
|
169
|
+
}
|
|
120
170
|
// --continue と --abort。途中の記録だけを読む。記録が無ければ、利用者が始めた rebase を動かさずに止まる
|
|
121
171
|
async function resumeRestack(out, cwd, mode, isDryRun, signal) {
|
|
122
172
|
const reading = interpretRestackProgress(readFlag(FLAG_PECOT_RESTACK));
|
|
@@ -135,18 +185,19 @@ async function resumeRestack(out, cwd, mode, isDryRun, signal) {
|
|
|
135
185
|
if (isDryRun) {
|
|
136
186
|
out.result({ branch: progress.branch, base: progress.ontoBranch, outcome: mode === 'abort' ? 'aborted' : 'restacked' });
|
|
137
187
|
out.planned(mode === 'abort' ? { verb: 'unchanged', target: `${progress.branch} restack aborted` } : { verb: 'update', target: `${progress.branch} restacked on ${progress.ontoBranch}` });
|
|
138
|
-
return;
|
|
188
|
+
return mode === 'abort' ? undefined : progress;
|
|
139
189
|
}
|
|
140
190
|
if (mode === 'abort') {
|
|
141
191
|
abortRebase(cwd);
|
|
142
192
|
removeFlag(FLAG_PECOT_RESTACK);
|
|
143
193
|
out.result({ branch: progress.branch, base: progress.ontoBranch, outcome: 'aborted' });
|
|
144
194
|
out.done({ verb: 'unchanged', target: `${progress.branch} restack aborted` });
|
|
145
|
-
return;
|
|
195
|
+
return undefined;
|
|
146
196
|
}
|
|
147
197
|
if (isRebasing && continueRebase(cwd) === 'conflict')
|
|
148
198
|
stopOnConflict(out, cwd, progress);
|
|
149
199
|
await finishRestack(out, cwd, progress, signal);
|
|
200
|
+
return progress;
|
|
150
201
|
}
|
|
151
202
|
function currentBranch(cwd) {
|
|
152
203
|
return gitStdout(['rev-parse', '--abbrev-ref', 'HEAD'], { cwd })?.trim();
|
|
@@ -199,7 +250,14 @@ async function runRestack(input, io) {
|
|
|
199
250
|
stopWith(out, 'give only one of check, continue and abort', 'invalid-input', USAGE_EXIT_CODE);
|
|
200
251
|
const cwd = '.';
|
|
201
252
|
if (input.continue || input.abort) {
|
|
202
|
-
await resumeRestack(out, cwd, input.abort ? 'abort' : 'continue', input.dry_run, io.signal);
|
|
253
|
+
const finished = await resumeRestack(out, cwd, input.abort ? 'abort' : 'continue', input.dry_run, io.signal);
|
|
254
|
+
const relinkTarget = finished === undefined ? undefined : relinkTargetOf(cwd, input.project);
|
|
255
|
+
if (finished === undefined || relinkTarget === undefined)
|
|
256
|
+
return;
|
|
257
|
+
if (input.dry_run)
|
|
258
|
+
planRelink(out, finished.branch, relinkTarget, finished.ontoBranch);
|
|
259
|
+
else
|
|
260
|
+
await relinkClosingIssues(out, relinkTarget, finished);
|
|
203
261
|
return;
|
|
204
262
|
}
|
|
205
263
|
const { currentBranch: branch, remoteName: remote, location } = validatePullRequestContext(cwd, out);
|
|
@@ -234,6 +292,7 @@ async function runRestack(input, io) {
|
|
|
234
292
|
if (!fetchResult.succeeded)
|
|
235
293
|
stopWith(out, `cannot fetch ${remote}: ${fetchResult.stderr.trim()}`, 'git-unresolved');
|
|
236
294
|
const target = { remote, owner: location.owner, repository: location.repository, reading, developBranch };
|
|
295
|
+
const relinkTarget = { owner: location.owner, repository: location.repository, host: config.host, developBranch };
|
|
237
296
|
const decision = await decideFor(out, cwd, config.host, target);
|
|
238
297
|
if (decision.kind === 'gone')
|
|
239
298
|
return stopWith(out, describeGone(decision), 'pr-state');
|
|
@@ -250,9 +309,11 @@ async function runRestack(input, io) {
|
|
|
250
309
|
const isSynced = isSyncedWithRemote(cwd, progress);
|
|
251
310
|
out.result({ branch, base: decision.base, outcome: isSynced ? 'synced' : 'restacked' });
|
|
252
311
|
out.planned({ verb: 'update', target: isSynced ? `${branch} synced with ${remote}/${branch}` : `${branch} restacked on ${decision.base}` });
|
|
312
|
+
planRelink(out, branch, relinkTarget, decision.base);
|
|
253
313
|
return;
|
|
254
314
|
}
|
|
255
315
|
await finishRestack(out, cwd, progress, io.signal);
|
|
316
|
+
await relinkClosingIssues(out, relinkTarget, progress);
|
|
256
317
|
return;
|
|
257
318
|
}
|
|
258
319
|
if (decision.kind !== 'moved' && decision.kind !== 'merged') {
|
|
@@ -264,6 +325,7 @@ async function runRestack(input, io) {
|
|
|
264
325
|
if (input.dry_run) {
|
|
265
326
|
out.result({ branch, base: decision.base, outcome: 'needs-restack', ...causeOf(decision) });
|
|
266
327
|
out.planned({ verb: 'update', target: `${branch} restacked on ${decision.base}` });
|
|
328
|
+
planRelink(out, branch, relinkTarget, decision.base);
|
|
267
329
|
return;
|
|
268
330
|
}
|
|
269
331
|
if (rebaseOnto(cwd, progress.ontoSha, rebaseStartOf(out, cwd, target, progress.ontoSha)) === 'conflict') {
|
|
@@ -271,6 +333,7 @@ async function runRestack(input, io) {
|
|
|
271
333
|
stopOnConflict(out, cwd, progress);
|
|
272
334
|
}
|
|
273
335
|
await finishRestack(out, cwd, progress, io.signal);
|
|
336
|
+
await relinkClosingIssues(out, relinkTarget, progress);
|
|
274
337
|
}
|
|
275
338
|
const prRestack = defineCommand({
|
|
276
339
|
path: RESTACK_PATH,
|
|
@@ -154,6 +154,10 @@ export declare class PullsApi extends IssuesApi {
|
|
|
154
154
|
*/
|
|
155
155
|
createPullRequest(owner: string, repository: string, title: string, body: string, head: string, base: string, isDraft?: boolean): Promise<JsonObject>;
|
|
156
156
|
/**
|
|
157
|
+
PR の本文を置き換える。同じ本文で保存すると、GitHub が Closes を読み直す
|
|
158
|
+
*/
|
|
159
|
+
updatePullRequestBody(owner: string, repository: string, number: number, body: string): Promise<JsonObject>;
|
|
160
|
+
/**
|
|
157
161
|
PR の詳細を取る(GraphQL)。関連 Issue とレビューを 1 回のクエリで取る
|
|
158
162
|
*/
|
|
159
163
|
getPullRequestSlim(owner: string, repository: string, number: number): Promise<PullRequestSlim>;
|
|
@@ -159,6 +159,12 @@ export class PullsApi extends IssuesApi {
|
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
/**
|
|
162
|
+
PR の本文を置き換える。同じ本文で保存すると、GitHub が Closes を読み直す
|
|
163
|
+
*/
|
|
164
|
+
async updatePullRequestBody(owner, repository, number, body) {
|
|
165
|
+
return this.restObject('PATCH', `${repositoryPath(owner, repository)}/pulls/${String(number)}`, { json: { body } });
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
162
168
|
PR の詳細を取る(GraphQL)。関連 Issue とレビューを 1 回のクエリで取る
|
|
163
169
|
*/
|
|
164
170
|
async getPullRequestSlim(owner, repository, number) {
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pecot/workflow-mcp",
|
|
3
|
-
"version": "8.0.1-dev.
|
|
3
|
+
"version": "8.0.1-dev.61",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@pecot/workflow-mcp",
|
|
9
|
-
"version": "8.0.1-dev.
|
|
9
|
+
"version": "8.0.1-dev.61",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@modelcontextprotocol/server": "^2.4.0",
|