@pecot/workflow-mcp 5.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 +27 -22
- package/cli/dist/cli.js +6 -1
- package/cli/dist/commands/board-add.d.ts +3 -0
- package/cli/dist/commands/board-add.js +69 -0
- package/cli/dist/commands/board-judgment.d.ts +69 -0
- package/cli/dist/commands/board-judgment.js +150 -0
- package/cli/dist/commands/board-move.d.ts +3 -0
- package/cli/dist/commands/board-move.js +65 -0
- package/cli/dist/commands/board-readiness.d.ts +34 -0
- package/cli/dist/commands/board-readiness.js +108 -0
- package/cli/dist/commands/board-show.d.ts +3 -0
- package/cli/dist/commands/board-show.js +75 -0
- package/cli/dist/commands/board-sync.d.ts +3 -0
- package/cli/dist/commands/board-sync.js +120 -0
- package/cli/dist/commands/board-view.d.ts +151 -0
- package/cli/dist/commands/board-view.js +293 -0
- package/cli/dist/commands/board.d.ts +3 -0
- package/cli/dist/commands/board.js +12 -0
- package/cli/dist/commands/branch.d.ts +3 -0
- package/cli/dist/commands/branch.js +111 -0
- package/cli/dist/commands/command-binding.js +4 -0
- package/cli/dist/commands/command-definition.d.ts +39 -8
- package/cli/dist/commands/command-definition.js +20 -7
- package/cli/dist/commands/command-help.js +2 -2
- package/cli/dist/commands/command-hint.d.ts +5 -0
- package/cli/dist/commands/command-hint.js +13 -0
- package/cli/dist/commands/command-stop.d.ts +5 -3
- package/cli/dist/commands/command-stop.js +7 -6
- package/cli/dist/commands/failure-kind.d.ts +3 -0
- package/cli/dist/commands/failure-kind.js +35 -0
- package/cli/dist/commands/failure.d.ts +6 -0
- package/cli/dist/commands/failure.js +29 -3
- package/cli/dist/commands/github-client.d.ts +6 -2
- package/cli/dist/commands/github-client.js +13 -3
- package/cli/dist/commands/help.js +8 -13
- package/cli/dist/commands/issue-board.d.ts +61 -0
- package/cli/dist/commands/issue-board.js +85 -1
- package/cli/dist/commands/issue-comment.js +66 -30
- package/cli/dist/commands/issue-create.js +632 -92
- package/cli/dist/commands/issue-dependency.d.ts +3 -0
- package/cli/dist/commands/issue-dependency.js +310 -0
- package/cli/dist/commands/issue-readiness.d.ts +122 -0
- package/cli/dist/commands/issue-readiness.js +253 -0
- package/cli/dist/commands/issue-reopen.d.ts +3 -0
- package/cli/dist/commands/issue-reopen.js +130 -0
- package/cli/dist/commands/issue-shared.d.ts +33 -20
- package/cli/dist/commands/issue-shared.js +84 -76
- package/cli/dist/commands/issue-show.d.ts +3 -0
- package/cli/dist/commands/issue-show.js +239 -0
- package/cli/dist/commands/issue.d.ts +0 -1
- package/cli/dist/commands/issue.js +6 -8
- package/cli/dist/commands/names.d.ts +33 -0
- package/cli/dist/commands/names.js +63 -0
- package/cli/dist/commands/output.d.ts +32 -4
- package/cli/dist/commands/output.js +28 -8
- 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 +59 -0
- package/cli/dist/commands/pr-waiting.js +143 -0
- package/cli/dist/commands/pr.d.ts +23 -0
- package/cli/dist/commands/pr.js +378 -366
- package/cli/dist/commands/project-sync.d.ts +12 -3
- package/cli/dist/commands/project-sync.js +23 -13
- package/cli/dist/commands/project.d.ts +3 -2
- package/cli/dist/commands/project.js +371 -177
- package/cli/dist/commands/registry.js +15 -10
- package/cli/dist/commands/release.d.ts +2 -2
- package/cli/dist/commands/release.js +118 -130
- package/cli/dist/commands/renamed-commands.d.ts +26 -0
- package/cli/dist/commands/renamed-commands.js +53 -0
- package/cli/dist/commands/repository-match.js +9 -3
- package/cli/dist/commands/session-finish.d.ts +3 -0
- package/cli/dist/commands/session-finish.js +383 -0
- package/cli/dist/commands/session-start.d.ts +3 -0
- package/cli/dist/commands/session-start.js +252 -0
- package/cli/dist/commands/session.d.ts +3 -0
- package/cli/dist/commands/session.js +186 -32
- package/cli/dist/commands/tracker-sync-dependabot.d.ts +7 -0
- package/cli/dist/commands/{dependabot.js → tracker-sync-dependabot.js} +76 -68
- package/cli/dist/commands/tracker-sync-security.d.ts +7 -0
- package/cli/dist/commands/{security.js → tracker-sync-security.js} +71 -64
- package/cli/dist/commands/tracker-sync.d.ts +37 -0
- package/cli/dist/commands/tracker-sync.js +18 -6
- package/cli/dist/commands/tracker.d.ts +3 -0
- package/cli/dist/commands/tracker.js +20 -0
- package/cli/dist/commands/usage-error.d.ts +4 -0
- package/cli/dist/commands/usage-error.js +6 -0
- package/cli/dist/commands/work-target.d.ts +20 -3
- package/cli/dist/commands/work-target.js +49 -6
- package/cli/dist/commands/worktree.js +103 -33
- package/cli/dist/domain/batch-plan.d.ts +73 -0
- package/cli/dist/domain/batch-plan.js +114 -0
- package/cli/dist/domain/blocked-by-change.d.ts +99 -0
- package/cli/dist/domain/blocked-by-change.js +101 -0
- package/cli/dist/domain/board-move.d.ts +90 -0
- package/cli/dist/domain/board-move.js +92 -0
- package/cli/dist/domain/branch-name.js +1 -1
- package/cli/dist/domain/command-hint.d.ts +85 -0
- package/cli/dist/domain/command-hint.js +148 -0
- package/cli/dist/domain/config.d.ts +4 -0
- package/cli/dist/domain/config.js +11 -1
- package/cli/dist/domain/dependabot.js +1 -1
- package/cli/dist/domain/errors.d.ts +5 -0
- package/cli/dist/domain/errors.js +5 -0
- package/cli/dist/domain/format.d.ts +51 -4
- package/cli/dist/domain/format.js +72 -6
- package/cli/dist/domain/frontmatter.d.ts +18 -3
- package/cli/dist/domain/frontmatter.js +38 -12
- package/cli/dist/domain/issue-order.d.ts +55 -0
- package/cli/dist/domain/issue-order.js +65 -0
- package/cli/dist/domain/issue-readout.d.ts +36 -4
- package/cli/dist/domain/pull-request.d.ts +71 -4
- package/cli/dist/domain/pull-request.js +59 -10
- package/cli/dist/domain/readiness.d.ts +44 -0
- package/cli/dist/domain/readiness.js +153 -5
- package/cli/dist/domain/reference-parser.d.ts +3 -1
- package/cli/dist/domain/reference-parser.js +6 -2
- package/cli/dist/domain/release.d.ts +7 -3
- package/cli/dist/domain/release.js +5 -12
- package/cli/dist/domain/repository-policy.d.ts +9 -3
- package/cli/dist/domain/repository-policy.js +8 -7
- package/cli/dist/domain/repository-resolver.js +4 -1
- package/cli/dist/domain/restack.d.ts +62 -0
- package/cli/dist/domain/restack.js +53 -0
- package/cli/dist/domain/security-alerts.js +1 -1
- package/cli/dist/domain/state.d.ts +69 -0
- package/cli/dist/domain/state.js +67 -1
- package/cli/dist/domain/work-start.d.ts +30 -0
- package/cli/dist/domain/work-start.js +42 -0
- package/cli/dist/infra/authored-notes.d.ts +1 -1
- package/cli/dist/infra/authored-notes.js +10 -4
- package/cli/dist/infra/config-store.d.ts +4 -0
- package/cli/dist/infra/config-store.js +13 -0
- package/cli/dist/infra/git.d.ts +76 -0
- package/cli/dist/infra/git.js +123 -1
- package/cli/dist/infra/github/client.d.ts +5 -0
- package/cli/dist/infra/github/client.js +10 -7
- package/cli/dist/infra/github/errors.d.ts +18 -0
- package/cli/dist/infra/github/errors.js +22 -0
- package/cli/dist/infra/github/issues.d.ts +39 -3
- package/cli/dist/infra/github/issues.js +178 -23
- package/cli/dist/infra/github/projects.d.ts +35 -0
- package/cli/dist/infra/github/projects.js +89 -8
- package/cli/dist/infra/github/pulls.d.ts +105 -2
- package/cli/dist/infra/github/pulls.js +172 -5
- package/cli/dist/infra/github/queries.d.ts +15 -4
- package/cli/dist/infra/github/queries.js +203 -40
- package/cli/dist/infra/github/readout.d.ts +5 -1
- package/cli/dist/infra/github/readout.js +11 -33
- package/cli/dist/infra/github/repos.d.ts +18 -0
- package/cli/dist/infra/github/repos.js +31 -0
- package/cli/dist/infra/nx-scope.d.ts +1 -1
- package/cli/dist/infra/nx-scope.js +1 -1
- package/cli/dist/infra/state-store.d.ts +10 -1
- package/cli/dist/infra/state-store.js +14 -2
- package/cli/dist/infra/worktree-links.d.ts +3 -0
- package/cli/dist/infra/worktree-links.js +8 -1
- package/cli/dist/infra/worktree.d.ts +12 -1
- package/cli/dist/infra/worktree.js +40 -23
- package/cli/dist/mcp/result.d.ts +82 -7
- package/cli/dist/mcp/result.js +139 -19
- package/cli/dist/mcp/server.d.ts +2 -2
- package/cli/dist/mcp/server.js +57 -10
- package/cli/templates/comment/plan.md +14 -0
- package/cli/templates/comment/review-findings.md +3 -2
- package/cli/templates/issue/bug.md +13 -4
- package/cli/templates/issue/task-implement.md +13 -6
- package/cli/templates/issue/task.md +13 -4
- package/cli/templates/workflow/pecot-board-sync.yml +84 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/cli/dist/commands/dependabot.d.ts +0 -3
- package/cli/dist/commands/issue-project.d.ts +0 -3
- package/cli/dist/commands/issue-project.js +0 -176
- package/cli/dist/commands/issue-quick.d.ts +0 -3
- package/cli/dist/commands/issue-quick.js +0 -126
- package/cli/dist/commands/issue-read.d.ts +0 -3
- package/cli/dist/commands/issue-read.js +0 -80
- package/cli/dist/commands/project-info.d.ts +0 -48
- package/cli/dist/commands/project-info.js +0 -181
- package/cli/dist/commands/repository.d.ts +0 -5
- package/cli/dist/commands/repository.js +0 -265
- package/cli/dist/commands/security.d.ts +0 -3
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// グローバルの `--repo` と、今の git remote のリポジトリの食い違いを扱う(Python の `require_repo_match`)。
|
|
2
2
|
// 判定は domain/repository-policy.ts の applyRepositoryPolicy に任せ、ここは git の解決と行の出力、止めるときの終了コードだけを行う。
|
|
3
|
+
import { formatHinted } from '../domain/command-hint.js';
|
|
3
4
|
import { applyRepositoryPolicy } from '../domain/repository-policy.js';
|
|
4
5
|
import { AmbiguousRepositoryError, resolveRepository } from '../domain/repository-resolver.js';
|
|
5
6
|
import { gitRunnerFor } from '../infra/git.js';
|
|
6
7
|
import { CommandExit } from './command-definition.js';
|
|
8
|
+
import { WARNING_PREFIX } from './output.js';
|
|
7
9
|
/**
|
|
8
10
|
今の git remote からリポジトリを解決する。git の外・remote 無しは undefined
|
|
9
11
|
*/
|
|
@@ -24,10 +26,14 @@ export function resolveCurrentRepository(cwd) {
|
|
|
24
26
|
*/
|
|
25
27
|
export function requireRepositoryMatch(mode, explicit, io, cwd) {
|
|
26
28
|
const outcome = applyRepositoryPolicy({ explicit, resolved: resolveCurrentRepository(cwd), mode });
|
|
27
|
-
for (const line of outcome.stderr)
|
|
28
|
-
io.
|
|
29
|
+
for (const line of outcome.stderr) {
|
|
30
|
+
const text = formatHinted(line, io.surface ?? 'cli');
|
|
31
|
+
io.stderr(`${text}\n`);
|
|
32
|
+
if (text.startsWith(WARNING_PREFIX))
|
|
33
|
+
io.warning?.(text.slice(WARNING_PREFIX.length));
|
|
34
|
+
}
|
|
29
35
|
if (outcome.kind === 'exit')
|
|
30
|
-
throw new CommandExit(outcome.exitCode);
|
|
36
|
+
throw new CommandExit(outcome.exitCode, outcome.failure);
|
|
31
37
|
return outcome.adopted;
|
|
32
38
|
}
|
|
33
39
|
//# sourceMappingURL=repository-match.js.map
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types -- GitHubClient は内部の状態を持つ */
|
|
2
|
+
// session finish。cli/commands/pr.py の pr_cleanup の移植。
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { describeApiError } from '../domain/api-error-detail.js';
|
|
5
|
+
import { extractIssueNumberFromBranch } from '../domain/branch-name.js';
|
|
6
|
+
import { getDevelopBranch, homeRepositoryOf } from '../domain/config.js';
|
|
7
|
+
import { isEpicIssue } from '../domain/issue-kind.js';
|
|
8
|
+
import { extractClosingIssueNumbers } from '../domain/pull-request.js';
|
|
9
|
+
import { assumePullRequestState, describeReadinessChange, displayReference, hasReadinessChanged, isDone, isStackedOn } from '../domain/readiness.js';
|
|
10
|
+
import { createIssueReference } from '../domain/reference-parser.js';
|
|
11
|
+
import { FLAG_MERGE_SLASH_PENDING, FLAG_PECOT_ACTIVE, FLAG_PECOT_BASE, FLAG_PECOT_QUICK_MODE, FLAG_PECOT_SESSION_TITLE, FLAG_WIP_BRANCH_LINKED } from '../domain/state.js';
|
|
12
|
+
import { gitCurrentBranch, GitCommandError, runGit } from '../infra/git.js';
|
|
13
|
+
import { GitHubAPIError, GitHubNotFoundError, isClosedByAnotherRequest, isMissingReferenceError } from '../infra/github/errors.js';
|
|
14
|
+
import { isFileNotFound } from '../infra/os-error.js';
|
|
15
|
+
import { consumeFlag, removeFlag } from '../infra/state-store.js';
|
|
16
|
+
import { getCurrentWorktreeName, isWorktreeEnvironment } from '../infra/worktree.js';
|
|
17
|
+
import { commandHint, hinted } from './command-hint.js';
|
|
18
|
+
import { requireActiveConfigPlain, stopWith } from './command-stop.js';
|
|
19
|
+
import { createReadinessCache, judgeAssuming } from './issue-readiness.js';
|
|
20
|
+
import { moveIssuesWaitingFor, prerequisiteOf } from './pr-waiting.js';
|
|
21
|
+
import { kindOfFailure } from './failure.js';
|
|
22
|
+
import { createGitHubClient } from './github-client.js';
|
|
23
|
+
import { createOutput, DRY_RUN_FIELD } from './output.js';
|
|
24
|
+
import { defineCommand } from './command-definition.js';
|
|
25
|
+
import { settle } from './tracker-sync.js';
|
|
26
|
+
import { isPositive, parseReference, PR_REFERENCE_DESCRIPTION, primaryRemote, reasonOf } from './pr.js';
|
|
27
|
+
const SKIPPED_ISSUE_DATA = z.strictObject({
|
|
28
|
+
number: z.int().describe('Issue number'),
|
|
29
|
+
reason: z.enum(['epic', 'already-closed', 'not-found', 'failed']).describe('Why it was not closed: epic (protected), already-closed, not-found, or failed (see warnings)'),
|
|
30
|
+
});
|
|
31
|
+
const DELETED_BRANCH_DATA = z.strictObject({
|
|
32
|
+
branch: z.string().describe('Name of the branch'),
|
|
33
|
+
location: z.enum(['remote', 'local']).describe('Whether the remote or the local branch was deleted'),
|
|
34
|
+
});
|
|
35
|
+
const READINESS_CHANGE_DATA = z.strictObject({
|
|
36
|
+
issue: z.string().describe('Issue waiting for a closed Issue (#N, or owner/repo#N in another repository)'),
|
|
37
|
+
from: z.string().describe('Readiness before the merge (blocked, startable, or stackable on #N)'),
|
|
38
|
+
to: z.string().describe('Readiness after the merge (blocked, startable, or stackable on #N)'),
|
|
39
|
+
});
|
|
40
|
+
const RESTACK_DATA = z.strictObject({
|
|
41
|
+
issue: z.string().describe('Started Issue stacked on a closed Issue (#N, or owner/repo#N in another repository)'),
|
|
42
|
+
branch: z.string().optional().describe('Branch linked to that Issue, to restack in its working directory (omitted when none)'),
|
|
43
|
+
});
|
|
44
|
+
const SESSION_FINISH_OUTPUT = z.strictObject({
|
|
45
|
+
closedIssues: z.array(z.int()).describe('Issue numbers closed (to be closed on dry_run)'),
|
|
46
|
+
skippedIssues: z.array(SKIPPED_ISSUE_DATA).describe('Closing Issues of the PR that this command did not close'),
|
|
47
|
+
deletedBranches: z.array(DELETED_BRANCH_DATA).describe('Branches deleted (to be deleted on dry_run)'),
|
|
48
|
+
readinessChanges: z.array(READINESS_CHANGE_DATA).describe('Issues waiting for the closed Issues whose readiness changed by the merge'),
|
|
49
|
+
restack: z.array(RESTACK_DATA).describe('Upper stacked Issues to restack with pr restack in their working directories'),
|
|
50
|
+
currentBranch: z.string().optional().describe('Current branch after the local cleanup (omitted when the local cleanup was skipped)'),
|
|
51
|
+
dryRun: DRY_RUN_FIELD,
|
|
52
|
+
});
|
|
53
|
+
async function fetchPullRequestForCleanup(config, location, out) {
|
|
54
|
+
try {
|
|
55
|
+
const client = createGitHubClient(out, config.host);
|
|
56
|
+
return { client, pr: await client.getPullRequestSlim(location.owner, location.repository, location.number) };
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
if (error instanceof GitHubNotFoundError)
|
|
60
|
+
return stopWith(out, `PR not found: ${location.owner}/${location.repository}/${String(location.number)}`, 'github-not-found');
|
|
61
|
+
if (error instanceof GitHubAPIError)
|
|
62
|
+
return stopWith(out, error.message, kindOfFailure(error));
|
|
63
|
+
throw error;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function collectClosingIssueNumbers(pr) {
|
|
67
|
+
const numbers = new Set(pr.closing_issues.map(issue => issue.number));
|
|
68
|
+
for (const number of extractClosingIssueNumbers(pr.body))
|
|
69
|
+
numbers.add(number);
|
|
70
|
+
const branchNumber = extractIssueNumberFromBranch(pr.head_ref);
|
|
71
|
+
if (isPositive(branchNumber))
|
|
72
|
+
numbers.add(branchNumber);
|
|
73
|
+
return numbers;
|
|
74
|
+
}
|
|
75
|
+
// Epic 保護。Issue Type が Epic か、sub-issue を持つ Issue
|
|
76
|
+
async function findEpicProtectedNumbers(client, location, issueNumbers, out) {
|
|
77
|
+
const protectedNumbers = new Set();
|
|
78
|
+
const ordered = [...issueNumbers].toSorted((a, b) => a - b);
|
|
79
|
+
for (const number of ordered) {
|
|
80
|
+
try {
|
|
81
|
+
if (isEpicIssue(await client.getIssueKind(location.owner, location.repository, number)))
|
|
82
|
+
protectedNumbers.add(number);
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
if (error instanceof GitHubNotFoundError)
|
|
86
|
+
continue;
|
|
87
|
+
if (!(error instanceof GitHubAPIError))
|
|
88
|
+
throw error;
|
|
89
|
+
out.warning(`kind check of Issue #${String(number)} failed: ${error.message}; not protected`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return protectedNumbers;
|
|
93
|
+
}
|
|
94
|
+
async function closeRelatedIssues(context, client, location, closing, epicProtected) {
|
|
95
|
+
const { out, isDryRun, record } = context;
|
|
96
|
+
const protectedOrdered = [...epicProtected].toSorted((a, b) => a - b);
|
|
97
|
+
const closingOrdered = [...closing].toSorted((a, b) => a - b);
|
|
98
|
+
for (const number of protectedOrdered) {
|
|
99
|
+
settle(out, { verb: 'skip', target: `closing Issue #${String(number)} (Epic)` });
|
|
100
|
+
record.skippedIssues.push({ number, reason: 'epic' });
|
|
101
|
+
}
|
|
102
|
+
for (const number of closingOrdered) {
|
|
103
|
+
const operation = { verb: 'close', target: `Issue #${String(number)}` };
|
|
104
|
+
out.planned(operation);
|
|
105
|
+
if (isDryRun) {
|
|
106
|
+
record.closedIssues.push(number);
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
await client.closeIssue(location.owner, location.repository, number);
|
|
111
|
+
out.done(operation);
|
|
112
|
+
record.closedIssues.push(number);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error instanceof GitHubNotFoundError) {
|
|
116
|
+
out.warning(`Issue #${String(number)} not found; skipped`);
|
|
117
|
+
record.skippedIssues.push({ number, reason: 'not-found' });
|
|
118
|
+
}
|
|
119
|
+
else if (error instanceof GitHubAPIError) {
|
|
120
|
+
const isClosed = isClosedByAnotherRequest(error);
|
|
121
|
+
if (isClosed)
|
|
122
|
+
out.done({ verb: 'unchanged', target: `Issue #${String(number)} (already closed)` });
|
|
123
|
+
else
|
|
124
|
+
out.warning(`closing Issue #${String(number)} failed: ${describeApiError(error)}`);
|
|
125
|
+
record.skippedIssues.push({ number, reason: isClosed ? 'already-closed' : 'failed' });
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function mergedPullRequestOf(location, number, pr) {
|
|
134
|
+
return { repository: location, number, state: 'merged', headRefName: pr.head_ref, baseRefName: pr.base_ref, isDraft: pr.is_draft, isCrossRepository: false };
|
|
135
|
+
}
|
|
136
|
+
// 閉じた Issue のうち、判定に出ている前提がすべて済んでいるか
|
|
137
|
+
function isClosedReflected(judged, heads) {
|
|
138
|
+
const found = heads.flatMap(head => prerequisiteOf(judged, head) ?? []);
|
|
139
|
+
return found.length > 0 && found.every(({ issue }) => isDone(issue));
|
|
140
|
+
}
|
|
141
|
+
function waitingCheck(source, heads, isDryRun, cache, home) {
|
|
142
|
+
const { client, config, pullRequest } = source;
|
|
143
|
+
const names = heads.map(head => displayReference(head, home)).join(', ');
|
|
144
|
+
const unconfirmed = (name) => hinted `could not confirm that GitHub reflects the close of ${names} in the readiness of ${name}; its column was not moved; run ${commandHint(['board', 'sync'])} later to move it`;
|
|
145
|
+
if (!isDryRun)
|
|
146
|
+
return { isReflected: judged => isClosedReflected(judged, heads), unconfirmed };
|
|
147
|
+
// dry-run では Issue を閉じていないので、閉じた後を置き換えで作る
|
|
148
|
+
return {
|
|
149
|
+
isReflected: () => true,
|
|
150
|
+
unconfirmed,
|
|
151
|
+
assume: async ({ target, result }) => (result.kind === 'judged'
|
|
152
|
+
? { target, result: await judgeAssuming(client, config, result, prerequisites => assumePullRequestState(prerequisites, heads, pullRequest, 'merged'), cache) }
|
|
153
|
+
: { target, result }),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
// 閉じた Issue を待つ Issue の列と札を動かし、マージの前と比べた判定の変化と、載せ直す上の段を記録する
|
|
157
|
+
async function updateWaitingIssues(context, source) {
|
|
158
|
+
const { out, isDryRun, record } = context;
|
|
159
|
+
const { client, config, location, pullRequest } = source;
|
|
160
|
+
const alreadyClosed = record.skippedIssues.filter(({ reason }) => reason === 'already-closed').map(({ number }) => number);
|
|
161
|
+
const heads = [...record.closedIssues, ...alreadyClosed].toSorted((a, b) => a - b).map(number => createIssueReference(location, number));
|
|
162
|
+
if (heads.length === 0)
|
|
163
|
+
return;
|
|
164
|
+
const home = homeRepositoryOf(config);
|
|
165
|
+
const cache = createReadinessCache();
|
|
166
|
+
const board = { out, client, config, home };
|
|
167
|
+
const judged = await moveIssuesWaitingFor(board, { heads, check: waitingCheck(source, heads, isDryRun, cache, home), isDryRun, cache });
|
|
168
|
+
for (const { target, result } of judged) {
|
|
169
|
+
if (result.kind !== 'judged' || result.readiness === undefined)
|
|
170
|
+
continue;
|
|
171
|
+
const before = await judgeAssuming(client, config, result, prerequisites => assumePullRequestState(prerequisites, heads, pullRequest, 'open'), cache);
|
|
172
|
+
if (before.kind !== 'judged' || before.readiness === undefined || !hasReadinessChanged(before.readiness, result.readiness))
|
|
173
|
+
continue;
|
|
174
|
+
const { repository } = target.ref;
|
|
175
|
+
record.readinessChanges.push({ issue: displayReference(target.ref, home), from: describeReadinessChange(before.readiness, repository), to: describeReadinessChange(result.readiness, repository) });
|
|
176
|
+
}
|
|
177
|
+
if (record.readinessChanges.length > 0)
|
|
178
|
+
out.data(['Readiness changed:', ...record.readinessChanges.map(change => `- ${change.issue} ${change.from} -> ${change.to}`)].join('\n'));
|
|
179
|
+
for (const { target, result } of judged) {
|
|
180
|
+
if (result.kind !== 'judged' || !isStackedOn(result.target, result.prerequisites, heads))
|
|
181
|
+
continue;
|
|
182
|
+
const name = displayReference(target.ref, home);
|
|
183
|
+
const lower = heads.filter(head => prerequisiteOf(result, head)?.wait === 'stack').map(head => displayReference(head, home)).join(', ');
|
|
184
|
+
const [branch] = result.target.branches;
|
|
185
|
+
record.restack.push({ issue: name, ...branch !== undefined && { branch } });
|
|
186
|
+
out.warning(hinted `${name} is stacked on ${lower}; run ${commandHint(['pr', 'restack'])} in the working directory of ${branch ?? 'its branch'}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
// head ブランチを消し、続けて wip ブランチを消す。head が無くても wip は試す
|
|
190
|
+
async function deleteMergedBranch(context, client, location, headBranch) {
|
|
191
|
+
const { out, isDryRun, record } = context;
|
|
192
|
+
const operation = { verb: 'delete', target: `remote branch ${headBranch}` };
|
|
193
|
+
out.planned(operation);
|
|
194
|
+
if (isDryRun) {
|
|
195
|
+
record.deletedBranches.push({ branch: headBranch, location: 'remote' });
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
try {
|
|
199
|
+
await client.deleteBranch(location.owner, location.repository, headBranch);
|
|
200
|
+
out.done(operation);
|
|
201
|
+
record.deletedBranches.push({ branch: headBranch, location: 'remote' });
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
if (!(error instanceof GitHubAPIError))
|
|
205
|
+
throw error;
|
|
206
|
+
if (!isMissingReferenceError(error)) {
|
|
207
|
+
out.warning(`deleting remote branch ${headBranch} failed: ${describeApiError(error)}`);
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
out.done({ verb: 'unchanged', target: `remote branch ${headBranch} (already deleted)` });
|
|
211
|
+
}
|
|
212
|
+
const wipBranch = `wip/${headBranch}`;
|
|
213
|
+
try {
|
|
214
|
+
await client.deleteBranch(location.owner, location.repository, wipBranch);
|
|
215
|
+
out.done({ verb: 'delete', target: `remote branch ${wipBranch}` });
|
|
216
|
+
record.deletedBranches.push({ branch: wipBranch, location: 'remote' });
|
|
217
|
+
}
|
|
218
|
+
catch (error) {
|
|
219
|
+
if (!(error instanceof GitHubAPIError))
|
|
220
|
+
throw error;
|
|
221
|
+
if (!isMissingReferenceError(error))
|
|
222
|
+
out.warning(`deleting remote branch ${wipBranch} failed: ${describeApiError(error)}`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
// 手元と PR の repository が同じなら主の remote 名。違えば警告を出して undefined
|
|
226
|
+
function sameRepositoryRemote(cwd, location, out) {
|
|
227
|
+
const skipped = 'skipped local cleanup';
|
|
228
|
+
try {
|
|
229
|
+
const found = primaryRemote(cwd);
|
|
230
|
+
if (found === undefined) {
|
|
231
|
+
out.warning(`cannot read the remote URL; ${skipped}`);
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
if (found.location === undefined) {
|
|
235
|
+
out.warning(`cannot read the repository from the remote URL; ${skipped}`);
|
|
236
|
+
return undefined;
|
|
237
|
+
}
|
|
238
|
+
if (found.location.owner !== location.owner || found.location.repository !== location.repository) {
|
|
239
|
+
out.warning(`current repository (${found.location.owner}/${found.location.repository}) differs from the PR's (${location.owner}/${location.repository}); ${skipped}`);
|
|
240
|
+
return undefined;
|
|
241
|
+
}
|
|
242
|
+
return found.name;
|
|
243
|
+
}
|
|
244
|
+
catch {
|
|
245
|
+
out.warning(`cannot determine the repository; ${skipped}`);
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
// worktree では worktree 名、そうでなければ develop ブランチ
|
|
250
|
+
function resolveCheckoutTarget(cwd, config, location, out) {
|
|
251
|
+
try {
|
|
252
|
+
if (isWorktreeEnvironment(cwd))
|
|
253
|
+
return getCurrentWorktreeName(cwd) ?? 'main';
|
|
254
|
+
const developBranch = getDevelopBranch(config, location.owner, location.repository);
|
|
255
|
+
if (developBranch !== undefined && developBranch !== '')
|
|
256
|
+
return developBranch;
|
|
257
|
+
out.warning('develop_branch is not set; skipped local cleanup');
|
|
258
|
+
return undefined;
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
out.warning('cannot determine the worktree; skipped local cleanup');
|
|
262
|
+
return undefined;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
function didRunLocalStep(context, operation, cwd, arguments_, failure) {
|
|
266
|
+
const { out, isDryRun } = context;
|
|
267
|
+
out.planned(operation);
|
|
268
|
+
if (isDryRun)
|
|
269
|
+
return true;
|
|
270
|
+
try {
|
|
271
|
+
runGit(arguments_, { cwd, check: true });
|
|
272
|
+
}
|
|
273
|
+
catch (error) {
|
|
274
|
+
if (!(error instanceof GitCommandError) && !isFileNotFound(error))
|
|
275
|
+
throw error;
|
|
276
|
+
out.warning(`${failure} failed: ${reasonOf(error)}`);
|
|
277
|
+
return false;
|
|
278
|
+
}
|
|
279
|
+
out.done(operation);
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
function localCleanupAfterMerge(context, headBranch, config, location) {
|
|
283
|
+
const { out } = context;
|
|
284
|
+
const cwd = process.cwd();
|
|
285
|
+
const remote = sameRepositoryRemote(cwd, location, out);
|
|
286
|
+
if (remote === undefined)
|
|
287
|
+
return;
|
|
288
|
+
const currentBranch = gitCurrentBranch(cwd);
|
|
289
|
+
const checkoutTarget = resolveCheckoutTarget(cwd, config, location, out);
|
|
290
|
+
if (checkoutTarget === undefined)
|
|
291
|
+
return;
|
|
292
|
+
const developBranch = getDevelopBranch(config, location.owner, location.repository);
|
|
293
|
+
if (developBranch === undefined || developBranch === '') {
|
|
294
|
+
out.warning('develop_branch is not set; skipped local cleanup');
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
if (currentBranch === checkoutTarget) {
|
|
298
|
+
settle(out, { verb: 'unchanged', target: `current branch ${checkoutTarget}` });
|
|
299
|
+
}
|
|
300
|
+
else if (!didRunLocalStep(context, { verb: 'set', target: `current branch to ${checkoutTarget}` }, cwd, ['checkout', checkoutTarget], `checking out ${checkoutTarget}`)) {
|
|
301
|
+
// checkout が失敗したら、pull と branch -D は別のブランチに当たるので止める
|
|
302
|
+
if (currentBranch !== undefined)
|
|
303
|
+
context.record.currentBranch = currentBranch;
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
context.record.currentBranch = checkoutTarget;
|
|
307
|
+
didRunLocalStep(context, { verb: 'update', target: `${checkoutTarget} from ${remote}/${developBranch}` }, cwd, ['pull', remote, developBranch], `pulling ${developBranch}`);
|
|
308
|
+
if (headBranch === checkoutTarget) {
|
|
309
|
+
settle(out, { verb: 'skip', target: `deleting local branch ${headBranch} (it is the checkout target)` });
|
|
310
|
+
}
|
|
311
|
+
else if (didRunLocalStep(context, { verb: 'delete', target: `local branch ${headBranch}` }, cwd, ['branch', '-D', headBranch], `deleting local branch ${headBranch}`)) {
|
|
312
|
+
context.record.deletedBranches.push({ branch: headBranch, location: 'local' });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function removeActiveFlags(out) {
|
|
316
|
+
try {
|
|
317
|
+
for (const flag of [FLAG_PECOT_ACTIVE, FLAG_PECOT_BASE, FLAG_PECOT_SESSION_TITLE, FLAG_PECOT_QUICK_MODE, FLAG_WIP_BRANCH_LINKED])
|
|
318
|
+
removeFlag(flag);
|
|
319
|
+
}
|
|
320
|
+
catch (error) {
|
|
321
|
+
out.warning(`removing flag files failed: ${reasonOf(error)}`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
const FinishInputSchema = z.strictObject({
|
|
325
|
+
pr_ref: z.string().describe(PR_REFERENCE_DESCRIPTION),
|
|
326
|
+
dry_run: z.boolean().default(false).describe('Dry-run (do not actually clean up)'),
|
|
327
|
+
delete_branch: z.boolean().default(false).describe('Delete the branch'),
|
|
328
|
+
skip_local_cleanup: z.boolean().default(false).describe('Skip the local branch switch and deletion'),
|
|
329
|
+
});
|
|
330
|
+
async function runFinish(input, io) {
|
|
331
|
+
const out = createOutput(input.dry_run, io);
|
|
332
|
+
const record = { closedIssues: [], skippedIssues: [], deletedBranches: [], readinessChanges: [], restack: [], hasStarted: false };
|
|
333
|
+
try {
|
|
334
|
+
await finish(input, { out, isDryRun: input.dry_run, record });
|
|
335
|
+
}
|
|
336
|
+
finally {
|
|
337
|
+
const { closedIssues, skippedIssues, deletedBranches, readinessChanges, restack, currentBranch } = record;
|
|
338
|
+
if (record.hasStarted)
|
|
339
|
+
out.result({ closedIssues, skippedIssues, deletedBranches, readinessChanges, restack, ...currentBranch !== undefined && { currentBranch } });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
async function finish(input, context) {
|
|
343
|
+
const { out } = context;
|
|
344
|
+
const config = requireActiveConfigPlain(out);
|
|
345
|
+
const reference = parseReference(input.pr_ref, input.repo, out);
|
|
346
|
+
const location = { owner: reference.owner, repository: reference.repository };
|
|
347
|
+
const { client, pr } = await fetchPullRequestForCleanup(config, reference, out);
|
|
348
|
+
if (pr.state !== 'MERGED')
|
|
349
|
+
stopWith(out, hinted `PR #${String(reference.number)} is not merged yet (state: ${pr.state}); merge it with ${commandHint(['pr', 'merge'])} first`, 'pr-state');
|
|
350
|
+
context.record.hasStarted = true;
|
|
351
|
+
const closing = collectClosingIssueNumbers(pr);
|
|
352
|
+
const epicProtected = await findEpicProtectedNumbers(client, location, closing, out);
|
|
353
|
+
for (const number of epicProtected)
|
|
354
|
+
closing.delete(number);
|
|
355
|
+
out.count('Issues to close', closing.size);
|
|
356
|
+
await closeRelatedIssues(context, client, location, closing, epicProtected);
|
|
357
|
+
await updateWaitingIssues(context, { client, config, location, pullRequest: mergedPullRequestOf(location, reference.number, pr) });
|
|
358
|
+
if (input.delete_branch)
|
|
359
|
+
await deleteMergedBranch(context, client, location, pr.head_ref);
|
|
360
|
+
if (input.skip_local_cleanup)
|
|
361
|
+
return;
|
|
362
|
+
localCleanupAfterMerge(context, pr.head_ref, config, location);
|
|
363
|
+
if (input.dry_run) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
removeActiveFlags(out);
|
|
367
|
+
consumeFlag(FLAG_MERGE_SLASH_PENDING);
|
|
368
|
+
}
|
|
369
|
+
const sessionFinishCommand = defineCommand({
|
|
370
|
+
path: ['session', 'finish'],
|
|
371
|
+
description: 'Clean up after a merged PR (close related Issues, delete branches, local cleanup)',
|
|
372
|
+
input: FinishInputSchema,
|
|
373
|
+
output: SESSION_FINISH_OUTPUT,
|
|
374
|
+
positional: ['pr_ref'],
|
|
375
|
+
shortFlags: { dry_run: '-n', delete_branch: '-d' },
|
|
376
|
+
interactive: false,
|
|
377
|
+
run: async (input, io) => {
|
|
378
|
+
await runFinish(input, io);
|
|
379
|
+
return 0;
|
|
380
|
+
},
|
|
381
|
+
});
|
|
382
|
+
export const SESSION_FINISH_COMMANDS = [sessionFinishCommand];
|
|
383
|
+
//# sourceMappingURL=session-finish.js.map
|