@ulysses-ai/create-workspace 0.17.0-beta.0 → 0.19.0-beta.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/README.md +3 -3
- package/lib/init.mjs +4 -1
- package/lib/payload.mjs +18 -1
- package/lib/payload.test.mjs +55 -0
- package/lib/scaffold.mjs +23 -6
- package/lib/scaffold.test.mjs +59 -0
- package/package.json +1 -1
- package/template/CLAUDE.md.tmpl +19 -2
- package/template/{.claude → _claude}/hooks/_utils.mjs +1 -1
- package/template/_claude/hooks/repo-write-detection.mjs +204 -0
- package/template/{.claude → _claude}/hooks/session-start.mjs +35 -1
- package/template/_claude/hooks/subagent-start.mjs +111 -0
- package/template/{.claude → _claude}/lib/session-frontmatter.mjs +28 -0
- package/template/{.claude → _claude}/rules/coherent-revisions.md +1 -1
- package/template/_claude/rules/forge-operations.md +57 -0
- package/template/_claude/rules/git-conventions.md +39 -0
- package/template/_claude/rules/goal-driven-work.md +24 -0
- package/template/_claude/rules/honest-pushback.md +56 -0
- package/template/_claude/rules/memory-guidance.md +66 -0
- package/template/{.claude → _claude}/rules/superpowers-workflow.md.skip +1 -1
- package/template/{.claude → _claude}/rules/task-list-mirroring.md +6 -0
- package/template/_claude/rules/work-item-tracking.md +48 -0
- package/template/_claude/rules/workspace-structure.md +79 -0
- package/template/{.claude → _claude}/scripts/build-workspace-context.mjs +86 -30
- package/template/_claude/scripts/chat-record.mjs +315 -0
- package/template/_claude/scripts/cleanup-work-session.mjs +436 -0
- package/template/_claude/scripts/context-footprint.mjs +391 -0
- package/template/{.claude → _claude}/scripts/forges/github.mjs +46 -0
- package/template/{.claude → _claude}/scripts/forges/gitlab.mjs +3 -2
- package/template/{.claude → _claude}/scripts/forges/interface.mjs +13 -0
- package/template/{.claude → _claude}/scripts/generate-claude-local.mjs +21 -2
- package/template/_claude/scripts/migrate-sessions.mjs +1571 -0
- package/template/{.claude → _claude}/scripts/migrate-to-workspace-context.mjs +7 -2
- package/template/_claude/scripts/task-pr.mjs +447 -0
- package/template/_claude/scripts/task-worktree.mjs +525 -0
- package/template/{.claude → _claude}/scripts/trackers/github-issues.mjs +11 -0
- package/template/{.claude → _claude}/scripts/trackers/interface.mjs +8 -0
- package/template/_claude/scripts/workspace-diagnostics.mjs +654 -0
- package/template/{.claude → _claude}/skills/braindump/SKILL.md +12 -4
- package/template/{.claude → _claude}/skills/build-docs-site/SKILL.md +5 -5
- package/template/{.claude → _claude}/skills/build-docs-site/templates/spec.md.tmpl +1 -1
- package/template/_claude/skills/complete-work/SKILL.md +452 -0
- package/template/_claude/skills/context-placement/SKILL.md +202 -0
- package/template/{.claude/rules/goal-driven-work.md → _claude/skills/goal-driven-work/SKILL.md} +46 -19
- package/template/{.claude → _claude}/skills/handoff/SKILL.md +12 -4
- package/template/{.claude → _claude}/skills/maintenance/SKILL.md +56 -17
- package/template/_claude/skills/migrate-sessions/SKILL.md +70 -0
- package/template/{.claude → _claude}/skills/pause-work/SKILL.md +9 -1
- package/template/_claude/skills/release/SKILL.md +91 -0
- package/template/{.claude → _claude}/skills/start-work/SKILL.md +89 -7
- package/template/{.claude → _claude}/skills/workspace-init/SKILL.md +3 -1
- package/template/{.claude → _claude}/skills/workspace-update/SKILL.md +4 -0
- package/template/_gitignore +9 -0
- package/template/workspace.json.tmpl +4 -3
- package/template/.claude/hooks/repo-write-detection.mjs +0 -107
- package/template/.claude/hooks/subagent-start.mjs +0 -44
- package/template/.claude/rules/forge-operations.md +0 -107
- package/template/.claude/rules/git-conventions.md +0 -34
- package/template/.claude/rules/honest-pushback.md +0 -56
- package/template/.claude/rules/memory-guidance.md +0 -109
- package/template/.claude/rules/work-item-tracking.md +0 -90
- package/template/.claude/rules/workspace-structure.md +0 -137
- package/template/.claude/scripts/cleanup-work-session.mjs +0 -247
- package/template/.claude/skills/complete-work/SKILL.md +0 -498
- package/template/.claude/skills/release/SKILL.md +0 -151
- /package/template/{.claude → _claude}/agents/aside-researcher.md +0 -0
- /package/template/{.claude → _claude}/agents/implementer.md +0 -0
- /package/template/{.claude → _claude}/agents/researcher.md +0 -0
- /package/template/{.claude → _claude}/agents/reviewer.md +0 -0
- /package/template/{.claude → _claude}/hooks/bash-output-advisory.mjs +0 -0
- /package/template/{.claude → _claude}/hooks/post-compact.mjs +0 -0
- /package/template/{.claude → _claude}/hooks/pre-compact.mjs +0 -0
- /package/template/{.claude → _claude}/hooks/session-end.mjs +0 -0
- /package/template/{.claude → _claude}/hooks/version-freshness-check.mjs +0 -0
- /package/template/{.claude → _claude}/hooks/workspace-update-check.mjs +0 -0
- /package/template/{.claude → _claude}/lib/freshness.mjs +0 -0
- /package/template/{.claude → _claude}/lib/registry-check.mjs +0 -0
- /package/template/{.claude → _claude}/lib/require-node.mjs +0 -0
- /package/template/{.claude → _claude}/recipes/migrate-from-notion.md +0 -0
- /package/template/{.claude → _claude}/rules/agent-rules.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/cloud-infrastructure.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/config-review.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/documentation.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/local-dev-environment.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/product-integrity.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/scope-guard.md.skip +0 -0
- /package/template/{.claude → _claude}/rules/token-economics.md.skip +0 -0
- /package/template/{.claude → _claude}/scripts/add-repo-to-session.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/capture-context.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/create-work-session.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/migrate-canonical-priority.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/migrate-claude-md-freshness-include.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/migrate-open-work.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/migrate-session-layout.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/sweep-references.mjs +0 -0
- /package/template/{.claude → _claude}/scripts/sync-tasks.mjs +0 -0
- /package/template/{.claude → _claude}/settings.json +0 -0
- /package/template/{.claude → _claude}/skills/aside/SKILL.md +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/checklists/framing.md +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/checklists/pitfalls.md +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/checklists/review.md +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/scripts/bulk-fill-migration.py +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/scripts/forbidden-word-grep.mjs +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/scripts/leak-grep.mjs +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/custom.css.tmpl +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/docusaurus.config.ts.tmpl +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/Arrow.tsx +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/Box.tsx +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/DiagramContainer.tsx +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/Region.tsx +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/SectionTitle.tsx +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/primitives/tokens.ts +0 -0
- /package/template/{.claude → _claude}/skills/build-docs-site/templates/sidebars.ts.tmpl +0 -0
- /package/template/{.claude → _claude}/skills/promote/SKILL.md +0 -0
- /package/template/{.claude → _claude}/skills/setup-tracker/SKILL.md +0 -0
- /package/template/{.claude → _claude}/skills/sync-work/SKILL.md +0 -0
- /package/template/{.mcp.json → _mcp.json} +0 -0
|
@@ -91,14 +91,19 @@ function isGitRepo(root) {
|
|
|
91
91
|
|
|
92
92
|
function gitMv(root, src, dst, dryRun) {
|
|
93
93
|
if (dryRun) return { ok: true, dry: true };
|
|
94
|
+
// src and dst are relative to root. git resolves them against cwd: root, but the
|
|
95
|
+
// filesystem calls below do not — resolve explicitly or they act on process.cwd(),
|
|
96
|
+
// scattering directories wherever the caller happened to be standing.
|
|
97
|
+
const srcAbs = resolve(root, src);
|
|
98
|
+
const dstAbs = resolve(root, dst);
|
|
94
99
|
// git mv refuses if dst dir doesn't exist; ensure parent
|
|
95
|
-
mkdirSync(dirname(
|
|
100
|
+
mkdirSync(dirname(dstAbs), { recursive: true });
|
|
96
101
|
if (isGitRepo(root)) {
|
|
97
102
|
const r = spawnSync('git', ['mv', src, dst], { cwd: root, encoding: 'utf-8' });
|
|
98
103
|
if (r.status === 0) return { ok: true };
|
|
99
104
|
// fall through to plain rename — happens for untracked files
|
|
100
105
|
}
|
|
101
|
-
renameSync(
|
|
106
|
+
renameSync(srcAbs, dstAbs);
|
|
102
107
|
return { ok: true };
|
|
103
108
|
}
|
|
104
109
|
|
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// PR creation and merge for the task lifecycle (gh:163).
|
|
3
|
+
//
|
|
4
|
+
// The task path of /complete-work used to end with two hand-written
|
|
5
|
+
// JavaScript blocks — one to push and open a PR per repo, one to merge them
|
|
6
|
+
// in order and close the issue. Every run re-typed them, and every
|
|
7
|
+
// re-typing was a fresh chance to drop a guard: origins must be parsed
|
|
8
|
+
// before anything is pushed, body files must exist for every repo that
|
|
9
|
+
// gets a PR, and the workspace PR must never merge ahead of the project
|
|
10
|
+
// PRs it describes. This script is those blocks written once.
|
|
11
|
+
//
|
|
12
|
+
// It runs from the launcher and owns only this slice: the rebase and the
|
|
13
|
+
// drawer routing happen in the skill's earlier steps, and teardown stays
|
|
14
|
+
// in task-worktree.mjs --remove.
|
|
15
|
+
//
|
|
16
|
+
// Usage:
|
|
17
|
+
// node task-pr.mjs --create --root <launcher> --branch <branch>
|
|
18
|
+
// [--work-item gh:N] [--chat <name> | --repo <r> ...]
|
|
19
|
+
// --body-file <repo>=<path> ... [--out <file>]
|
|
20
|
+
// [--force-with-lease]
|
|
21
|
+
// node task-pr.mjs --merge --root <launcher> --prs <json-from-create>
|
|
22
|
+
// [--work-item gh:N]
|
|
23
|
+
//
|
|
24
|
+
// --create resolves the task's repos from the chat record's entries for
|
|
25
|
+
// the branch (--chat) or from repeated --repo flags, skips repos whose
|
|
26
|
+
// branch has no commits over origin/{default} (reported as empty), pushes
|
|
27
|
+
// each remaining branch, and opens one PR per repo through a per-repo
|
|
28
|
+
// forge. The PR title is the linked issue's title when --work-item is
|
|
29
|
+
// given, else the branch's first commit subject; the body is the repo's
|
|
30
|
+
// body file with `Closes <ref>` appended. Prints `{ prs, empty, pushed }`
|
|
31
|
+
// and, with --out, writes the same JSON to a file — a mid-run failure
|
|
32
|
+
// still writes what has landed so far, so the state survives the error.
|
|
33
|
+
// A repo that already has an open PR for the branch gets it reused, so a
|
|
34
|
+
// re-run never opens a duplicate.
|
|
35
|
+
//
|
|
36
|
+
// --merge merges the project PRs first (squash, delete branch), the
|
|
37
|
+
// workspace PR only when every project merge succeeded — a PR the forge
|
|
38
|
+
// reports as already MERGED counts as done and is not merged again, which
|
|
39
|
+
// is what makes re-running after a partial failure safe. An empty or
|
|
40
|
+
// malformed PRs file is refused outright: no PRs means nothing to merge
|
|
41
|
+
// and nothing to close. Once every PR is merged the launcher is pulled
|
|
42
|
+
// --ff-only — only when it sits on the workspace default branch (else
|
|
43
|
+
// pullSkipped), and a failed pull is reported as pullFailed in the JSON
|
|
44
|
+
// rather than an error, because the merges stand and the issue still
|
|
45
|
+
// closes — and then the linked issue closes with a Merged: comment. On a
|
|
46
|
+
// merge failure it stops and names the PRs still open.
|
|
47
|
+
|
|
48
|
+
import '../lib/require-node.mjs';
|
|
49
|
+
import { readFileSync, writeFileSync, mkdirSync, existsSync, realpathSync } from 'node:fs';
|
|
50
|
+
import { join, resolve, dirname } from 'node:path';
|
|
51
|
+
import { spawnSync } from 'node:child_process';
|
|
52
|
+
import { fileURLToPath } from 'node:url';
|
|
53
|
+
import { taskWorktreePath, defaultBranchFor, WORKSPACE_REPO } from './task-worktree.mjs';
|
|
54
|
+
import { readRecord } from './chat-record.mjs';
|
|
55
|
+
import { createForge } from './forges/interface.mjs';
|
|
56
|
+
import { createTracker } from './trackers/interface.mjs';
|
|
57
|
+
|
|
58
|
+
function isMainModule(metaUrl) {
|
|
59
|
+
if (!process.argv[1]) return false;
|
|
60
|
+
try {
|
|
61
|
+
return realpathSync(fileURLToPath(metaUrl)) === realpathSync(process.argv[1]);
|
|
62
|
+
} catch { return false; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Same remote shapes the forge adapters resolve a repo from. A URL that
|
|
66
|
+
// does not match is not forge-hosted, and this path supports forge-hosted
|
|
67
|
+
// repos only — a local/bare remote has no PR concept to aim at.
|
|
68
|
+
const FORGE_REMOTE_RE = /github\.com[:/]([^/]+)\/([^/.]+?)(?:\.git)?$/;
|
|
69
|
+
|
|
70
|
+
function parseForgeRemote(url) {
|
|
71
|
+
const m = String(url).trim().match(FORGE_REMOTE_RE);
|
|
72
|
+
return m ? { owner: m[1], name: m[2] } : null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function readWorkspace(rootDir) {
|
|
76
|
+
try {
|
|
77
|
+
return JSON.parse(readFileSync(join(rootDir, 'workspace.json'), 'utf-8'));
|
|
78
|
+
} catch {
|
|
79
|
+
throw new Error(`cannot read ${join(rootDir, 'workspace.json')} — is --root the launcher?`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// `workspace.forge: false` is an explicit opt-out; spreading it into a
|
|
84
|
+
// per-repo config would silently produce a default GitHub forge instead.
|
|
85
|
+
function assertForgeEnabled(ws) {
|
|
86
|
+
if (ws?.workspace?.forge === false) {
|
|
87
|
+
throw new Error('workspace.forge is false — forge operations are disabled here. Nothing was pushed; open the PR by hand.');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Branch names become refs, refspecs, and (via the slug) paths; git's own
|
|
92
|
+
// format check is the authority, and its --branch variant also rejects
|
|
93
|
+
// names a later git call could mistake for an option. It needs no
|
|
94
|
+
// repository, so it runs before any other git call this script makes.
|
|
95
|
+
function assertBranchName(gitFn, branch) {
|
|
96
|
+
const res = gitFn('git', ['check-ref-format', '--branch', branch], { encoding: 'utf8' });
|
|
97
|
+
if (res.error || res.status !== 0) throw new Error(`invalid branch name: ${branch}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// The --prs file may have been written by a BOM-emitting writer (PowerShell
|
|
101
|
+
// redirection among them); JSON.parse chokes on the marker, so strip it.
|
|
102
|
+
function stripBom(text) {
|
|
103
|
+
return text.charCodeAt(0) === 0xfeff ? text.slice(1) : text;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function writeOut(path, payload) {
|
|
107
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
108
|
+
writeFileSync(path, `${JSON.stringify(payload, null, 2)}\n`, 'utf8');
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function gitCheck(gitFn, cwd, args) {
|
|
112
|
+
const res = gitFn('git', ['-C', cwd, ...args], { encoding: 'utf8' });
|
|
113
|
+
if (res.error) throw new Error(`git: ${res.error.message}`);
|
|
114
|
+
return res;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function gitOut(gitFn, cwd, args) {
|
|
118
|
+
const res = gitCheck(gitFn, cwd, args);
|
|
119
|
+
if (res.status !== 0) {
|
|
120
|
+
throw new Error(`git -C ${cwd} ${args.join(' ')} failed: ${String(res.stderr || '').trim()}`);
|
|
121
|
+
}
|
|
122
|
+
return String(res.stdout || '').trim();
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// The merge base for "is this branch empty" and for the fallback PR title.
|
|
126
|
+
// origin/{default} is the truth the PR will merge against; the local branch
|
|
127
|
+
// covers a repo whose remote-tracking ref is missing.
|
|
128
|
+
function baseRefFor(gitFn, worktree, defaultBranch) {
|
|
129
|
+
for (const ref of [`origin/${defaultBranch}`, defaultBranch]) {
|
|
130
|
+
if (gitCheck(gitFn, worktree, ['rev-parse', '--verify', '--quiet', ref]).status === 0) return ref;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function commitsOverBase(gitFn, worktree, branch, defaultBranch) {
|
|
136
|
+
const base = baseRefFor(gitFn, worktree, defaultBranch);
|
|
137
|
+
// No base ref at all: the branch cannot be proven empty, so it proceeds.
|
|
138
|
+
if (!base) return null;
|
|
139
|
+
return parseInt(gitOut(gitFn, worktree, ['rev-list', '--count', `${base}..${branch}`]), 10) || 0;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function firstCommitSubject(gitFn, worktree, branch, defaultBranch) {
|
|
143
|
+
const base = baseRefFor(gitFn, worktree, defaultBranch);
|
|
144
|
+
const range = base ? `${base}..${branch}` : branch;
|
|
145
|
+
const subjects = gitOut(gitFn, worktree, ['log', '--reverse', '--format=%s', range])
|
|
146
|
+
.split(/\r?\n/).filter((l) => l.trim() !== '');
|
|
147
|
+
if (subjects.length > 0) return subjects[0].trim();
|
|
148
|
+
return gitOut(gitFn, worktree, ['log', '-1', '--format=%s', branch]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function pushBranch(gitFn, worktree, repo, branch, { forceWithLease = false } = {}) {
|
|
152
|
+
// `--` ends option parsing so the refspec can never read as a flag — git
|
|
153
|
+
// accepts it on push. It is deliberately NOT used on the rev-parse /
|
|
154
|
+
// rev-list / log calls, where git would flip the argument to a path.
|
|
155
|
+
const res = gitCheck(gitFn, worktree, [
|
|
156
|
+
'push', '-u', 'origin', ...(forceWithLease ? ['--force-with-lease'] : []), '--', branch,
|
|
157
|
+
]);
|
|
158
|
+
if (res.status === 0) return;
|
|
159
|
+
const stderr = String(res.stderr || '').trim();
|
|
160
|
+
if (!forceWithLease && /non-fast-forward|fetch first/i.test(stderr)) {
|
|
161
|
+
throw new Error(
|
|
162
|
+
`repo "${repo}": push rejected as non-fast-forward — the branch already exists on origin and the rebase rewrote it. Confirm and re-run with --force-with-lease; this script never forces on its own.`,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
throw new Error(`repo "${repo}": git push failed: ${stderr}`);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function resolveTaskRepos(rootDir, { chat, branch, repos }) {
|
|
169
|
+
if (repos.length > 0) return [...new Set(repos)];
|
|
170
|
+
if (!chat) {
|
|
171
|
+
throw new Error("--create needs the task's repos: pass --chat <name> (its record entries for the branch) or repeat --repo");
|
|
172
|
+
}
|
|
173
|
+
const rec = readRecord(rootDir, chat);
|
|
174
|
+
const entries = (rec?.tasks ?? []).filter((t) => t.branch === branch && t.repo);
|
|
175
|
+
if (entries.length === 0) {
|
|
176
|
+
throw new Error(`chat record "${chat}" has no task entries for branch ${branch} — pass --repo explicitly`);
|
|
177
|
+
}
|
|
178
|
+
return [...new Set(entries.map((t) => t.repo))];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async function createPrs(args, deps) {
|
|
182
|
+
assertBranchName(deps.gitFn, args.branch);
|
|
183
|
+
const rootDir = resolve(args.root);
|
|
184
|
+
const ws = readWorkspace(rootDir);
|
|
185
|
+
assertForgeEnabled(ws);
|
|
186
|
+
|
|
187
|
+
const targets = resolveTaskRepos(rootDir, args).map((repo) => {
|
|
188
|
+
const worktree = taskWorktreePath(rootDir, repo, args.branch);
|
|
189
|
+
if (!existsSync(worktree)) {
|
|
190
|
+
throw new Error(`no task worktree at ${worktree} for repo "${repo}" — create it with task-worktree.mjs --create`);
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
repo, worktree, isWorkspace: repo === WORKSPACE_REPO,
|
|
194
|
+
defaultBranch: defaultBranchFor(rootDir, repo, deps.gitFn),
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
for (const t of targets) t.commits = commitsOverBase(deps.gitFn, t.worktree, args.branch, t.defaultBranch);
|
|
199
|
+
const empty = targets.filter((t) => t.commits === 0).map((t) => t.repo);
|
|
200
|
+
const active = targets.filter((t) => t.commits !== 0);
|
|
201
|
+
|
|
202
|
+
// Every guard runs before the first push: a repo whose origin cannot
|
|
203
|
+
// host a PR, or whose body file is missing, must stop the whole task
|
|
204
|
+
// with nothing pushed and nothing half-opened.
|
|
205
|
+
for (const t of active) {
|
|
206
|
+
const url = gitOut(deps.gitFn, t.worktree, ['remote', 'get-url', 'origin']);
|
|
207
|
+
const parsed = parseForgeRemote(url);
|
|
208
|
+
if (!parsed) {
|
|
209
|
+
throw new Error(`repo "${t.repo}": origin (${url}) is not a forge-hosted repository — stopped before pushing anything. Complete this repo under the session model.`);
|
|
210
|
+
}
|
|
211
|
+
Object.assign(t, parsed);
|
|
212
|
+
}
|
|
213
|
+
for (const t of active) {
|
|
214
|
+
const bodyFile = args.bodyFiles.get(t.repo);
|
|
215
|
+
if (!bodyFile) {
|
|
216
|
+
throw new Error(`repo "${t.repo}" has commits to merge but no --body-file — one is required for every repo that gets a PR`);
|
|
217
|
+
}
|
|
218
|
+
if (!existsSync(bodyFile)) {
|
|
219
|
+
throw new Error(`repo "${t.repo}": body file not found: ${bodyFile}`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let issueTitle = null;
|
|
224
|
+
let issueRefs = null;
|
|
225
|
+
if (args.workItem) {
|
|
226
|
+
const tracker = deps.trackerFactory(ws.workspace?.tracker);
|
|
227
|
+
issueTitle = (await tracker.getIssue(args.workItem)).title;
|
|
228
|
+
issueRefs = new Map(active.map((t) => [t.repo, tracker.issueRef(args.workItem, { fromRepo: `${t.owner}/${t.name}` })]));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const prs = [];
|
|
232
|
+
const pushed = [];
|
|
233
|
+
try {
|
|
234
|
+
for (const t of active) {
|
|
235
|
+
pushBranch(deps.gitFn, t.worktree, t.repo, args.branch, { forceWithLease: args.forceWithLease });
|
|
236
|
+
pushed.push(t.repo);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
for (const t of active) {
|
|
240
|
+
const forge = deps.forgeFactory({ ...(ws.workspace?.forge ?? {}), repo: `${t.owner}/${t.name}` });
|
|
241
|
+
// Idempotency: a re-run must not open a second PR for a branch that
|
|
242
|
+
// already has one open against the same base. Reuse it as-is —
|
|
243
|
+
// re-titling or re-bodying an existing PR is a decision, not a
|
|
244
|
+
// default, and the one already open is the one reviewers watch.
|
|
245
|
+
const existing = (await forge.prList({ state: 'open', head: args.branch, base: t.defaultBranch }))
|
|
246
|
+
.find((p) => p.headRefName === args.branch && p.baseRefName === t.defaultBranch);
|
|
247
|
+
const title = issueTitle ?? firstCommitSubject(deps.gitFn, t.worktree, args.branch, t.defaultBranch);
|
|
248
|
+
let body = readFileSync(args.bodyFiles.get(t.repo), 'utf8').replace(/\s*$/, '');
|
|
249
|
+
if (issueRefs) body = `${body}\n\nCloses ${issueRefs.get(t.repo)}\n`;
|
|
250
|
+
const pr = existing ?? await forge.prCreate({ title, body, head: args.branch, base: t.defaultBranch });
|
|
251
|
+
prs.push({
|
|
252
|
+
repo: t.repo, owner: t.owner, name: t.name,
|
|
253
|
+
number: pr.number, id: pr.id, url: pr.url, isWorkspace: t.isWorkspace,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
} catch (err) {
|
|
257
|
+
// A failure after the first push must not lose the run's state: the
|
|
258
|
+
// pushes and PRs that landed go to --out so a re-run (or a --merge of
|
|
259
|
+
// what exists) starts from reality. Before the first push nothing has
|
|
260
|
+
// happened, and an earlier run's file stays untouched rather than
|
|
261
|
+
// being clobbered with a no-op result.
|
|
262
|
+
if (args.out && (pushed.length > 0 || prs.length > 0)) {
|
|
263
|
+
writeOut(args.out, { prs, empty, pushed });
|
|
264
|
+
}
|
|
265
|
+
throw err;
|
|
266
|
+
}
|
|
267
|
+
const result = { prs, empty, pushed };
|
|
268
|
+
if (args.out) writeOut(args.out, result);
|
|
269
|
+
return result;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
async function mergePrs(args, deps) {
|
|
273
|
+
const rootDir = resolve(args.root);
|
|
274
|
+
const ws = readWorkspace(rootDir);
|
|
275
|
+
assertForgeEnabled(ws);
|
|
276
|
+
|
|
277
|
+
let parsed;
|
|
278
|
+
try {
|
|
279
|
+
parsed = JSON.parse(stripBom(readFileSync(args.prs, 'utf-8')));
|
|
280
|
+
} catch (err) {
|
|
281
|
+
throw new Error(`cannot read PRs file ${args.prs}: ${err.message}`);
|
|
282
|
+
}
|
|
283
|
+
// An empty or malformed file is a refusal, not a no-op: with nothing to
|
|
284
|
+
// merge there is nothing that earns an issue close, and "Merged: " with
|
|
285
|
+
// no URLs would tell the tracker a lie.
|
|
286
|
+
if (!parsed || !Array.isArray(parsed.prs)) {
|
|
287
|
+
throw new Error(`PRs file ${args.prs} is malformed — expected the JSON from a --create run ({ prs: [...] })`);
|
|
288
|
+
}
|
|
289
|
+
const prs = parsed.prs;
|
|
290
|
+
if (prs.length === 0) {
|
|
291
|
+
throw new Error(`PRs file ${args.prs} lists no PRs — nothing to merge and nothing to close; --merge refuses to close an issue on an empty merge`);
|
|
292
|
+
}
|
|
293
|
+
for (const p of prs) {
|
|
294
|
+
if (!p || !p.owner || !p.name || !p.id) {
|
|
295
|
+
throw new Error(`PRs file entry is missing owner/name/id: ${JSON.stringify(p)}`);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
const forgeFor = (p) => deps.forgeFactory({ ...(ws.workspace?.forge ?? {}), repo: `${p.owner}/${p.name}` });
|
|
299
|
+
|
|
300
|
+
// State first, so a re-run knows what an earlier run already finished.
|
|
301
|
+
// A PR the forge reports MERGED is done; anything else is offered to
|
|
302
|
+
// prMerge, which reports its own failure if the PR cannot merge. If the
|
|
303
|
+
// view itself fails, fall through to the merge attempt — that path
|
|
304
|
+
// produces the honest error when something is genuinely wrong.
|
|
305
|
+
const alreadyMerged = new Set();
|
|
306
|
+
for (const p of prs) {
|
|
307
|
+
try {
|
|
308
|
+
const view = await forgeFor(p).prView({ id: p.id });
|
|
309
|
+
if (view?.state === 'MERGED') alreadyMerged.add(p.id);
|
|
310
|
+
} catch { /* state unknown — the merge attempt below decides */ }
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const merged = [];
|
|
314
|
+
const failures = [];
|
|
315
|
+
for (const p of prs.filter((x) => !x.isWorkspace)) {
|
|
316
|
+
if (alreadyMerged.has(p.id)) { merged.push(p); continue; }
|
|
317
|
+
try {
|
|
318
|
+
await forgeFor(p).prMerge({ id: p.id, strategy: 'squash', deleteBranch: true });
|
|
319
|
+
merged.push(p);
|
|
320
|
+
} catch (err) { failures.push(`${p.repo}: ${err.message}`); }
|
|
321
|
+
}
|
|
322
|
+
const workspacePr = prs.find((x) => x.isWorkspace) ?? null;
|
|
323
|
+
if (failures.length === 0 && workspacePr) {
|
|
324
|
+
if (alreadyMerged.has(workspacePr.id)) {
|
|
325
|
+
merged.push(workspacePr);
|
|
326
|
+
} else {
|
|
327
|
+
try {
|
|
328
|
+
await forgeFor(workspacePr).prMerge({ id: workspacePr.id, strategy: 'squash', deleteBranch: true });
|
|
329
|
+
merged.push(workspacePr);
|
|
330
|
+
} catch (err) { failures.push(`${workspacePr.repo}: ${err.message}`); }
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (failures.length > 0) {
|
|
334
|
+
const open = prs.filter((p) => !merged.includes(p)).map((p) => `${p.repo}: ${p.url}`);
|
|
335
|
+
throw new Error(
|
|
336
|
+
`merge stopped (${failures.join('; ')}). PRs still open: ${open.join(', ')}. The workspace PR is never merged ahead of the project PRs — fix the failure and re-run --merge.`,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Every PR in the file is merged from here on. The launcher pull is a
|
|
341
|
+
// convenience, not a gate: it happens only when the launcher sits on the
|
|
342
|
+
// workspace default branch, a failure is a reported flag rather than an
|
|
343
|
+
// error (the merges stand and the issue still closes), and the user is
|
|
344
|
+
// told to pull by hand before teardown.
|
|
345
|
+
const result = { merged: merged.map(({ repo, number, url }) => ({ repo, number, url })), closed: null };
|
|
346
|
+
const launcherBranch = gitOut(deps.gitFn, rootDir, ['branch', '--show-current']) || '(detached HEAD)';
|
|
347
|
+
if (launcherBranch !== defaultBranchFor(rootDir, WORKSPACE_REPO, deps.gitFn)) {
|
|
348
|
+
result.pullSkipped = `launcher on ${launcherBranch}`;
|
|
349
|
+
} else {
|
|
350
|
+
const pull = gitCheck(deps.gitFn, rootDir, ['pull', '--ff-only']);
|
|
351
|
+
if (pull.status !== 0) result.pullFailed = true;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (args.workItem) {
|
|
355
|
+
const tracker = deps.trackerFactory(ws.workspace?.tracker);
|
|
356
|
+
await tracker.closeIssue(args.workItem, { comment: `Merged: ${merged.map((p) => p.url).join(' ')}` });
|
|
357
|
+
result.closed = args.workItem;
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const MODE_FLAGS = new Set(['--create', '--merge']);
|
|
363
|
+
const VALUE_FLAGS = new Set(['--root', '--branch', '--work-item', '--chat', '--prs', '--out']);
|
|
364
|
+
|
|
365
|
+
function parseArgs(argv) {
|
|
366
|
+
const args = {
|
|
367
|
+
root: '.', mode: null, branch: null, workItem: null, chat: null,
|
|
368
|
+
repos: [], bodyFiles: new Map(), prs: null, out: null, forceWithLease: false,
|
|
369
|
+
};
|
|
370
|
+
const rest = argv.slice(2);
|
|
371
|
+
const value = (i, flag) => {
|
|
372
|
+
const v = rest[i + 1];
|
|
373
|
+
if (v === undefined || v.startsWith('--')) throw new Error(`${flag} requires a value`);
|
|
374
|
+
return v;
|
|
375
|
+
};
|
|
376
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
377
|
+
const a = rest[i];
|
|
378
|
+
if (MODE_FLAGS.has(a)) {
|
|
379
|
+
if (args.mode) throw new Error(`only one of --create, --merge may be given (already have --${args.mode})`);
|
|
380
|
+
args.mode = a.slice(2);
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
if (a === '--repo') { args.repos.push(value(i, a)); i += 1; continue; }
|
|
384
|
+
if (a === '--body-file') {
|
|
385
|
+
const v = value(i, a);
|
|
386
|
+
const eq = v.indexOf('=');
|
|
387
|
+
if (eq <= 0) throw new Error(`--body-file expects <repo>=<path>, got: ${v}`);
|
|
388
|
+
args.bodyFiles.set(v.slice(0, eq), v.slice(eq + 1));
|
|
389
|
+
i += 1;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (a === '--force-with-lease') { args.forceWithLease = true; continue; }
|
|
393
|
+
if (VALUE_FLAGS.has(a)) {
|
|
394
|
+
args[a.slice(2).replace(/-([a-z])/g, (_, c) => c.toUpperCase())] = value(i, a);
|
|
395
|
+
i += 1;
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
throw new Error(`unknown argument: ${a}`);
|
|
399
|
+
}
|
|
400
|
+
if (!args.mode) throw new Error('one of --create, --merge is required');
|
|
401
|
+
if (args.mode === 'create') {
|
|
402
|
+
if (!args.branch) throw new Error('--create requires --branch');
|
|
403
|
+
if (!args.chat && args.repos.length === 0) throw new Error('--create requires --chat or at least one --repo');
|
|
404
|
+
if (args.prs) throw new Error('--prs is only valid with --merge');
|
|
405
|
+
} else {
|
|
406
|
+
if (!args.prs) throw new Error('--merge requires --prs');
|
|
407
|
+
for (const flag of ['branch', 'chat']) {
|
|
408
|
+
if (args[flag]) throw new Error(`--${flag} is only valid with --create`);
|
|
409
|
+
}
|
|
410
|
+
if (args.repos.length > 0) throw new Error('--repo is only valid with --create');
|
|
411
|
+
if (args.bodyFiles.size > 0) throw new Error('--body-file is only valid with --create');
|
|
412
|
+
if (args.forceWithLease) throw new Error('--force-with-lease is only valid with --create');
|
|
413
|
+
if (args.out) throw new Error('--out is only valid with --create');
|
|
414
|
+
}
|
|
415
|
+
return args;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Run one task-pr command. `argv` is a process.argv-shaped array; `deps`
|
|
420
|
+
* is injectable so tests can fake git, the forge, and the tracker:
|
|
421
|
+
* { gitFn, forgeFactory, trackerFactory }
|
|
422
|
+
* Resolves with the command's JSON result; throws on any failure.
|
|
423
|
+
*/
|
|
424
|
+
async function run(argv, deps = {}) {
|
|
425
|
+
const resolved = {
|
|
426
|
+
gitFn: spawnSync,
|
|
427
|
+
forgeFactory: createForge,
|
|
428
|
+
trackerFactory: createTracker,
|
|
429
|
+
...deps,
|
|
430
|
+
};
|
|
431
|
+
const args = parseArgs(argv);
|
|
432
|
+
return args.mode === 'create' ? createPrs(args, resolved) : mergePrs(args, resolved);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
async function main() {
|
|
436
|
+
const out = await run(process.argv);
|
|
437
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}\n`);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (isMainModule(import.meta.url)) {
|
|
441
|
+
main().catch((err) => {
|
|
442
|
+
process.stderr.write(`task-pr: ${err.message}\n`);
|
|
443
|
+
process.exit(2);
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
export { run, parseArgs, parseForgeRemote };
|