@danielstyle/featflow 0.0.0-stage → 0.6.4
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/LICENSE +21 -0
- package/README.md +249 -2
- package/README.zh-CN.md +250 -0
- package/bin/featflow.mjs +116 -0
- package/package.json +63 -4
- package/skills/featflow/SKILL.md +127 -0
- package/skills/featflow-issues/SKILL.md +104 -0
- package/skills/featflow-regression/SKILL.md +63 -0
- package/src/agent-docs.mjs +143 -0
- package/src/changed-files.mjs +204 -0
- package/src/commands/cleanup.mjs +221 -0
- package/src/commands/codegraph.mjs +108 -0
- package/src/commands/doctor.mjs +303 -0
- package/src/commands/finish.mjs +249 -0
- package/src/commands/hook-cmd.mjs +78 -0
- package/src/commands/hooks-cmd.mjs +247 -0
- package/src/commands/init.mjs +477 -0
- package/src/commands/issue.mjs +434 -0
- package/src/commands/new.mjs +311 -0
- package/src/commands/plan-check.mjs +75 -0
- package/src/commands/policy-cmd.mjs +55 -0
- package/src/commands/pr-cleanup.mjs +194 -0
- package/src/commands/regression.mjs +206 -0
- package/src/commands/setup.mjs +343 -0
- package/src/commands/size-check.mjs +137 -0
- package/src/commands/skills.mjs +181 -0
- package/src/commands/status.mjs +100 -0
- package/src/commands/sync.mjs +421 -0
- package/src/commands/verify.mjs +206 -0
- package/src/commands/worktree.mjs +205 -0
- package/src/config.mjs +295 -0
- package/src/detect.mjs +314 -0
- package/src/git.mjs +331 -0
- package/src/hook-adapters.mjs +218 -0
- package/src/hooks-core.mjs +420 -0
- package/src/index.mjs +14 -0
- package/src/init-args.mjs +294 -0
- package/src/init-hooks.mjs +312 -0
- package/src/init-prompts.mjs +191 -0
- package/src/issues-helpers.mjs +463 -0
- package/src/issues.mjs +521 -0
- package/src/plan.mjs +202 -0
- package/src/policy.mjs +143 -0
- package/src/regression.mjs +338 -0
- package/src/size-check.mjs +203 -0
- package/src/skills-manager.mjs +348 -0
- package/src/tui-select.mjs +378 -0
- package/templates/adapters/antigravity/hooks.json +42 -0
- package/templates/adapters/claude-code/settings.json +46 -0
- package/templates/adapters/codex/config.toml +19 -0
- package/templates/adapters/cursor/hooks.json +27 -0
- package/templates/agents/codex/codebase-locator.toml +28 -0
- package/templates/agents/codex/implementation-scout.toml +28 -0
- package/templates/agents/codex/regression-guard-planner.toml +20 -0
- package/templates/features/README.md +56 -0
- package/templates/features/_program_template.md +79 -0
- package/templates/features/_template.md +120 -0
- package/templates/features/current-feature.md +18 -0
- package/templates/githooks/post-merge +10 -0
- package/templates/githooks/pre-commit +14 -0
- package/templates/githooks/pre-push +13 -0
- package/templates/regression/regression-ledger.yml +23 -0
- package/templates/snippets/AGENTS-workflow.md +21 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { resolveAgentDocTargets } from "./agent-docs.mjs";
|
|
2
|
+
import { promptMultiSelect, promptSingleSelect } from "./tui-select.mjs";
|
|
3
|
+
|
|
4
|
+
export async function promptInteractiveOptions(prompter, options, detected, repoRoot, existingConfig = null) {
|
|
5
|
+
console.log("Detected repository environment:");
|
|
6
|
+
console.log(
|
|
7
|
+
` Base branch: ${detected.defaultBranch}${detected.hasDevBranch ? ` (integration: ${detected.integrationBranch})` : ""}`,
|
|
8
|
+
);
|
|
9
|
+
console.log(` Product paths: ${detected.productPaths.join(", ")} (${detected.productPathsSource})`);
|
|
10
|
+
console.log(` Verify command: ${detected.verifyCommand || "(none detected)"}`);
|
|
11
|
+
const docTargets = resolveAgentDocTargets(repoRoot, options.agentDocs);
|
|
12
|
+
console.log(` Agent docs: ${docTargets.join(", ")}`);
|
|
13
|
+
const detectedAgents = [
|
|
14
|
+
detected.hasAgentsDir || detected.hasAntigravityHooks ? "antigravity" : null,
|
|
15
|
+
detected.hasClaudeDir || detected.hasClaudeConfig ? "claude-code" : null,
|
|
16
|
+
detected.hasCodexDir || detected.hasCodexConfig ? "codex" : null,
|
|
17
|
+
detected.hasCursorDir || detected.hasCursorHooks ? "cursor" : null,
|
|
18
|
+
].filter(Boolean);
|
|
19
|
+
if (detectedAgents.length > 0) {
|
|
20
|
+
console.log(` Agent dirs: ${detectedAgents.join(", ")}`);
|
|
21
|
+
}
|
|
22
|
+
if (detected.existingHooksPath) {
|
|
23
|
+
console.log(` Git hooks path: ${detected.existingHooksPath} (will not overwrite)`);
|
|
24
|
+
}
|
|
25
|
+
console.log("");
|
|
26
|
+
|
|
27
|
+
if (!options.merge) {
|
|
28
|
+
if (!options.minimalExplicit && !options.localFeatures && !options.trackPointer) {
|
|
29
|
+
const profile = await promptSingleSelect(prompter, {
|
|
30
|
+
title: "Setup profile",
|
|
31
|
+
defaultId: "full",
|
|
32
|
+
choices: [
|
|
33
|
+
{ id: "full", label: "full", detail: "features/ templates, guardrails & hooks (uses global skills)", aliases: ["f"] },
|
|
34
|
+
{ id: "minimal", label: "minimal", detail: "lightweight (.featflow.json + AGENTS.md + local hooks)", aliases: ["m"] },
|
|
35
|
+
],
|
|
36
|
+
});
|
|
37
|
+
if (profile === "minimal") {
|
|
38
|
+
options.minimal = true;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!options.modeExplicit) {
|
|
43
|
+
const defaultMode = detected.hasDevBranch ? "developer-branch" : "trunk";
|
|
44
|
+
options.mode = await promptSingleSelect(prompter, {
|
|
45
|
+
title: "Branch mode",
|
|
46
|
+
defaultId: defaultMode,
|
|
47
|
+
choices: [
|
|
48
|
+
{ id: "trunk", label: "trunk", detail: `branch directly from ${detected.defaultBranch}`, aliases: ["t"] },
|
|
49
|
+
{
|
|
50
|
+
id: "developer-branch",
|
|
51
|
+
label: "developer-branch",
|
|
52
|
+
detail: `personal dev branch -> ${detected.integrationBranch} PR -> ${detected.defaultBranch}`,
|
|
53
|
+
aliases: ["dev", "d"],
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (options.mode === "trunk" && !options.finishTargetExplicit) {
|
|
60
|
+
const defaultFinish = detected.isHostedGit ? "pr" : "merge";
|
|
61
|
+
options.finishTarget = await promptSingleSelect(prompter, {
|
|
62
|
+
title: "Trunk finish target",
|
|
63
|
+
defaultId: defaultFinish,
|
|
64
|
+
choices: [
|
|
65
|
+
{ id: "merge", label: "merge", detail: `merge locally into ${detected.defaultBranch} & push`, aliases: ["m"] },
|
|
66
|
+
{ id: "pr", label: "pr", detail: "push task branch for Pull Request", aliases: ["p"] },
|
|
67
|
+
],
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!options.hooksExplicit) {
|
|
72
|
+
if (options.minimal) {
|
|
73
|
+
const hookChoice = await promptSingleSelect(prompter, {
|
|
74
|
+
title: "Git hooks",
|
|
75
|
+
defaultId: "local",
|
|
76
|
+
choices: [
|
|
77
|
+
{ id: "local", label: "local", detail: "install in .git/hooks/ (0 tracked files)", aliases: ["y", "yes", "l"] },
|
|
78
|
+
{ id: "none", label: "none", detail: "skip Git hooks", aliases: ["n", "no"] },
|
|
79
|
+
],
|
|
80
|
+
});
|
|
81
|
+
if (hookChoice === "local") {
|
|
82
|
+
options.localHooks = true;
|
|
83
|
+
} else {
|
|
84
|
+
options.noHooks = true;
|
|
85
|
+
}
|
|
86
|
+
} else {
|
|
87
|
+
const hookChoice = await promptSingleSelect(prompter, {
|
|
88
|
+
title: "Git hooks",
|
|
89
|
+
defaultId: "repo",
|
|
90
|
+
choices: [
|
|
91
|
+
{ id: "repo", label: "repo", detail: "tracked .githooks/ via core.hooksPath", aliases: ["r", "y", "yes"] },
|
|
92
|
+
{ id: "local", label: "local", detail: "local .git/hooks/ (0 tracked files)", aliases: ["l"] },
|
|
93
|
+
{ id: "none", label: "none", detail: "skip Git hooks", aliases: ["n", "no"] },
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
if (hookChoice === "none") {
|
|
97
|
+
options.noHooks = true;
|
|
98
|
+
} else if (hookChoice === "local") {
|
|
99
|
+
options.localHooks = true;
|
|
100
|
+
} else {
|
|
101
|
+
options.withGithooks = true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!options.minimal && !options.capabilitiesExplicit) {
|
|
108
|
+
const defaultCheckAll = !options.merge || options.preset === "full";
|
|
109
|
+
const selectedCaps = await promptMultiSelect(prompter, {
|
|
110
|
+
title: "Workflow guardrails & modules",
|
|
111
|
+
choices: [
|
|
112
|
+
{
|
|
113
|
+
id: "size-budget",
|
|
114
|
+
label: "File-size budget",
|
|
115
|
+
detail: "600 prod / 700 style / 900 test + pre-commit",
|
|
116
|
+
checked: Boolean(defaultCheckAll || options.withSizeBudget || existingConfig?.fileSizeBudget),
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
id: "regression",
|
|
120
|
+
label: "Regression Guardian",
|
|
121
|
+
detail: "regression-ledger.yml + impact check",
|
|
122
|
+
checked: Boolean(defaultCheckAll || options.withRegression || existingConfig?.regression),
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
id: "issues",
|
|
126
|
+
label: "GitHub Issues loop",
|
|
127
|
+
detail: "featflow issue CLI & GitHub workflow",
|
|
128
|
+
checked: Boolean(defaultCheckAll || options.withIssues || existingConfig?.issues),
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
id: "codex-agents",
|
|
132
|
+
label: "Codex subagents",
|
|
133
|
+
detail: ".codex/agents/*.toml read-only templates",
|
|
134
|
+
checked: Boolean(defaultCheckAll || options.withCodexAgents),
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
const capSet = new Set(selectedCaps);
|
|
139
|
+
options.withSizeBudget = capSet.has("size-budget");
|
|
140
|
+
options.withRegression = capSet.has("regression");
|
|
141
|
+
options.withIssues = capSet.has("issues");
|
|
142
|
+
options.withCodexAgents = capSet.has("codex-agents");
|
|
143
|
+
|
|
144
|
+
if (options.withIssues && !options.issueAssetsRepo && !existingConfig?.issues?.assetsRepo) {
|
|
145
|
+
const assetsRepo = (
|
|
146
|
+
await prompter.question("GitHub issue screenshot assets repo (<owner>/<repo>, Enter to skip): ")
|
|
147
|
+
).trim();
|
|
148
|
+
if (assetsRepo && assetsRepo.includes("/")) {
|
|
149
|
+
options.issueAssetsRepo = assetsRepo;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (!options.agentHooksExplicit) {
|
|
155
|
+
const precheckAll = options.preset === "full";
|
|
156
|
+
const selectedHooks = await promptMultiSelect(prompter, {
|
|
157
|
+
title: "AI Coding Agent Hooks",
|
|
158
|
+
choices: [
|
|
159
|
+
{
|
|
160
|
+
id: "antigravity",
|
|
161
|
+
label: "Antigravity",
|
|
162
|
+
detail: ".agents/hooks.json",
|
|
163
|
+
checked: precheckAll || options.withAntigravityHooks || detected.hasAgentsDir || detected.hasAntigravityHooks,
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
id: "claude-code",
|
|
167
|
+
label: "Claude Code",
|
|
168
|
+
detail: ".claude/settings.json",
|
|
169
|
+
checked: precheckAll || options.withClaudeHooks || detected.hasClaudeDir || detected.hasClaudeConfig,
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
id: "codex",
|
|
173
|
+
label: "OpenAI Codex",
|
|
174
|
+
detail: ".codex/config.toml",
|
|
175
|
+
checked: precheckAll || options.withCodexHooks || detected.hasCodexDir || detected.hasCodexConfig,
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: "cursor",
|
|
179
|
+
label: "Cursor",
|
|
180
|
+
detail: ".cursor/hooks.json",
|
|
181
|
+
checked: precheckAll || options.withCursorHooks || detected.hasCursorDir || detected.hasCursorHooks,
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
});
|
|
185
|
+
const hookSet = new Set(selectedHooks);
|
|
186
|
+
options.withAntigravityHooks = hookSet.has("antigravity");
|
|
187
|
+
options.withClaudeHooks = hookSet.has("claude-code");
|
|
188
|
+
options.withCodexHooks = hookSet.has("codex");
|
|
189
|
+
options.withCursorHooks = hookSet.has("cursor");
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { randomBytes } from "node:crypto";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import path from "node:path";
|
|
6
|
+
|
|
7
|
+
import { DEFAULT_ISSUES_CONFIG, DEFAULT_PREFIXES } from "./config.mjs";
|
|
8
|
+
import { commandExists, runReadSoft } from "./git.mjs";
|
|
9
|
+
|
|
10
|
+
export const VALID_ISSUE_TYPES = [...DEFAULT_PREFIXES];
|
|
11
|
+
|
|
12
|
+
export const TYPE_TO_DEFAULT_LABEL = {
|
|
13
|
+
fix: "bug",
|
|
14
|
+
feat: "enhancement",
|
|
15
|
+
docs: "documentation",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export const VALID_PRIORITIES = ["p0", "p1", "p2"];
|
|
19
|
+
|
|
20
|
+
export const STANDARD_WORKFLOW_LABELS = [
|
|
21
|
+
{
|
|
22
|
+
name: "status:in-progress",
|
|
23
|
+
color: "fbca04",
|
|
24
|
+
description: "Claimed and actively being worked on in an isolated worktree",
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
name: "status:in-review",
|
|
28
|
+
color: "5319e7",
|
|
29
|
+
description: "Fix pushed and verified; awaiting PR integration or manual review",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
name: "status:blocked",
|
|
33
|
+
color: "b60205",
|
|
34
|
+
description: "Work is blocked pending clarification or external dependency",
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "priority:p0",
|
|
38
|
+
color: "b60205",
|
|
39
|
+
description: "Critical urgency: production breakage, data loss, or security issue",
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
name: "priority:p1",
|
|
43
|
+
color: "d93f0b",
|
|
44
|
+
description: "High priority: significant user or workflow impact",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "priority:p2",
|
|
48
|
+
color: "0e8a16",
|
|
49
|
+
description: "Normal priority",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "agent-ready",
|
|
53
|
+
color: "0075ca",
|
|
54
|
+
description: "Scoped and ready for AI coding agent pickup",
|
|
55
|
+
},
|
|
56
|
+
];
|
|
57
|
+
|
|
58
|
+
export const ALLOWED_IMAGE_EXTS = new Set([".png", ".jpg", ".jpeg", ".webp", ".gif"]);
|
|
59
|
+
export const MAX_ATTACHMENT_BYTES = 10 * 1024 * 1024;
|
|
60
|
+
|
|
61
|
+
export function failIssue(message) {
|
|
62
|
+
throw new Error(message);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function formatLocalDateParts(now = new Date()) {
|
|
66
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
67
|
+
const yyyy = now.getFullYear();
|
|
68
|
+
const mm = pad(now.getMonth() + 1);
|
|
69
|
+
const dd = pad(now.getDate());
|
|
70
|
+
const hh = pad(now.getHours());
|
|
71
|
+
const mi = pad(now.getMinutes());
|
|
72
|
+
return {
|
|
73
|
+
datePart: `${yyyy}-${mm}-${dd}`,
|
|
74
|
+
featureStamp: `${yyyy}-${mm}-${dd}-${hh}${mi}`,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function resolveGhInvocation(args) {
|
|
79
|
+
const injectedScript = process.env.FEATFLOW_GH_SCRIPT;
|
|
80
|
+
if (injectedScript) {
|
|
81
|
+
return { command: process.execPath, argv: [injectedScript, ...args], isInjected: true };
|
|
82
|
+
}
|
|
83
|
+
return { command: "gh", argv: args, isInjected: false };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function ghRun(args, { input, cwd = process.cwd() } = {}) {
|
|
87
|
+
const { command, argv, isInjected } = resolveGhInvocation(args);
|
|
88
|
+
if (!isInjected && !commandExists("gh")) {
|
|
89
|
+
failIssue("GitHub CLI (`gh`) is required for `featflow issue`. Install `gh` and run `gh auth login`.");
|
|
90
|
+
}
|
|
91
|
+
const res = spawnSync(command, argv, {
|
|
92
|
+
cwd,
|
|
93
|
+
encoding: "utf8",
|
|
94
|
+
input,
|
|
95
|
+
stdio: [input !== undefined ? "pipe" : "ignore", "pipe", "pipe"],
|
|
96
|
+
});
|
|
97
|
+
if (res.error) {
|
|
98
|
+
failIssue(`gh ${args.join(" ")} failed: ${res.error.message}`);
|
|
99
|
+
}
|
|
100
|
+
if (res.status !== 0) {
|
|
101
|
+
const detail = (res.stderr || res.stdout || `exit code ${String(res.status)}`).trim();
|
|
102
|
+
failIssue(`gh ${args.join(" ")} failed:\n${detail}`);
|
|
103
|
+
}
|
|
104
|
+
return (res.stdout ?? "").trim();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function ghRunSoft(args, { input, cwd = process.cwd() } = {}) {
|
|
108
|
+
const { command, argv } = resolveGhInvocation(args);
|
|
109
|
+
const res = spawnSync(command, argv, {
|
|
110
|
+
cwd,
|
|
111
|
+
encoding: "utf8",
|
|
112
|
+
input,
|
|
113
|
+
stdio: [input !== undefined ? "pipe" : "ignore", "pipe", "pipe"],
|
|
114
|
+
});
|
|
115
|
+
return {
|
|
116
|
+
status: res.status ?? -1,
|
|
117
|
+
stdout: (res.stdout ?? "").trim(),
|
|
118
|
+
stderr: (res.stderr ?? "").trim(),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function getCurrentGitHubUser(cwd = process.cwd()) {
|
|
123
|
+
return ghRun(["api", "user", "--jq", ".login"], { cwd });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function parseGitHubRepoFromRemoteUrl(remoteUrl) {
|
|
127
|
+
const trimmed = String(remoteUrl ?? "").trim();
|
|
128
|
+
if (!trimmed) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const patterns = [
|
|
133
|
+
/^git@github\.com:([^/\s]+)\/([^/\s]+?)(?:\.git)?$/i,
|
|
134
|
+
/^ssh:\/\/git@github\.com\/([^/\s]+)\/([^/\s]+?)(?:\.git)?$/i,
|
|
135
|
+
/^https?:\/\/(?:[^@\s]+@)?github\.com\/([^/\s]+)\/([^/\s]+?)(?:\.git)?\/?$/i,
|
|
136
|
+
];
|
|
137
|
+
for (const regex of patterns) {
|
|
138
|
+
const match = trimmed.match(regex);
|
|
139
|
+
if (match) {
|
|
140
|
+
const owner = match[1];
|
|
141
|
+
const name = match[2];
|
|
142
|
+
return { owner, name, slug: `${owner}/${name}` };
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function resolveGitHubRepoSlug(repoRoot, config, explicitRepo = "") {
|
|
149
|
+
const cleanExplicit = String(explicitRepo ?? "").trim();
|
|
150
|
+
if (cleanExplicit) {
|
|
151
|
+
if (!cleanExplicit.includes("/")) {
|
|
152
|
+
failIssue(`Invalid --repo "${cleanExplicit}". Expected "<owner>/<repo>".`);
|
|
153
|
+
}
|
|
154
|
+
const [owner, name] = cleanExplicit.split("/");
|
|
155
|
+
return { owner, name, slug: `${owner}/${name}` };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (config?.issues?.repo && config.issues.repo.includes("/")) {
|
|
159
|
+
const [owner, name] = config.issues.repo.split("/");
|
|
160
|
+
return { owner, name, slug: `${owner}/${name}` };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const remoteName = config?.remote || "origin";
|
|
164
|
+
const remoteRes = runReadSoft("git", ["remote", "get-url", remoteName], repoRoot);
|
|
165
|
+
if (remoteRes.status === 0 && remoteRes.stdout) {
|
|
166
|
+
const parsed = parseGitHubRepoFromRemoteUrl(remoteRes.stdout);
|
|
167
|
+
if (parsed) {
|
|
168
|
+
return parsed;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (process.env.FEATFLOW_GH_SCRIPT || commandExists("gh")) {
|
|
173
|
+
const ghRes = ghRunSoft(["repo", "view", "--json", "nameWithOwner", "--jq", ".nameWithOwner"], { cwd: repoRoot });
|
|
174
|
+
if (ghRes.status === 0 && ghRes.stdout.includes("/")) {
|
|
175
|
+
const [owner, name] = ghRes.stdout.split("/");
|
|
176
|
+
return { owner, name, slug: `${owner}/${name}` };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
failIssue(
|
|
181
|
+
`Could not determine GitHub repository from remote "${remoteName}". Set \`issues.repo\` in .featflow.json or pass \`--repo <owner>/<repo>\`.`,
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function parseIssueRef(rawRef, defaultRepoSlug = "") {
|
|
186
|
+
const trimmed = String(rawRef ?? "").trim();
|
|
187
|
+
if (!trimmed) {
|
|
188
|
+
failIssue(
|
|
189
|
+
"Missing issue reference (expected `<number>`, `#<number>`, `<owner>/<repo>#<number>`, or a GitHub Issue URL).",
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const urlMatch = trimmed.match(/^https?:\/\/github\.com\/([^/]+)\/([^/]+)\/issues\/(\d+)(?:[/?#].*)?$/i);
|
|
194
|
+
if (urlMatch) {
|
|
195
|
+
const [, owner, name, numStr] = urlMatch;
|
|
196
|
+
return { owner, repoName: name, repoSlug: `${owner}/${name}`, number: Number(numStr), ref: `${name}#${numStr}` };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const slugHashMatch = trimmed.match(/^([^/\s]+)\/([^#\s]+)#(\d+)$/);
|
|
200
|
+
if (slugHashMatch) {
|
|
201
|
+
const [, owner, name, numStr] = slugHashMatch;
|
|
202
|
+
return { owner, repoName: name, repoSlug: `${owner}/${name}`, number: Number(numStr), ref: `${name}#${numStr}` };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const numMatch = trimmed.match(/^#?(\d+)$/);
|
|
206
|
+
if (numMatch && defaultRepoSlug && defaultRepoSlug.includes("/")) {
|
|
207
|
+
const [owner, name] = defaultRepoSlug.split("/");
|
|
208
|
+
const numStr = numMatch[1];
|
|
209
|
+
return { owner, repoName: name, repoSlug: `${owner}/${name}`, number: Number(numStr), ref: `${name}#${numStr}` };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
failIssue(
|
|
213
|
+
`Invalid issue reference "${rawRef}". Expected \`<number>\`, \`#<number>\`, \`<owner>/<repo>#<number>\`, or a GitHub Issue URL.`,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function parseIssueTitle(title) {
|
|
218
|
+
const match = String(title ?? "")
|
|
219
|
+
.trim()
|
|
220
|
+
.match(/^(fix|feat|docs|refactor|perf|security|chore|test)(?:\(([^)]+)\))?:\s*(.+)$/i);
|
|
221
|
+
if (!match) {
|
|
222
|
+
return null;
|
|
223
|
+
}
|
|
224
|
+
return {
|
|
225
|
+
type: match[1].toLowerCase(),
|
|
226
|
+
scope: (match[2] ?? "").trim(),
|
|
227
|
+
summary: match[3].trim(),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
export function formatIssueTitle(type, scope, rawTitle, validTypes = VALID_ISSUE_TYPES) {
|
|
232
|
+
const cleanType = String(type ?? "").trim().toLowerCase();
|
|
233
|
+
if (!validTypes.includes(cleanType)) {
|
|
234
|
+
failIssue(`Invalid --type "${type}". Expected one of: ${validTypes.join(", ")}`);
|
|
235
|
+
}
|
|
236
|
+
const cleanScope = String(scope ?? "").trim();
|
|
237
|
+
const cleanTitle = String(rawTitle ?? "").trim();
|
|
238
|
+
if (!cleanTitle) {
|
|
239
|
+
failIssue("Missing --title for issue creation.");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const parsed = parseIssueTitle(cleanTitle);
|
|
243
|
+
if (parsed) {
|
|
244
|
+
if (parsed.type !== cleanType) {
|
|
245
|
+
failIssue(
|
|
246
|
+
`Title prefix "${parsed.type}:" does not match --type "${cleanType}". Align them before creating the issue.`,
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
const effectiveScope = parsed.scope || cleanScope;
|
|
250
|
+
return `${cleanType}${effectiveScope ? `(${effectiveScope})` : ""}: ${parsed.summary}`;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return `${cleanType}${cleanScope ? `(${cleanScope})` : ""}: ${cleanTitle}`;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export function deriveBranchSpecFromIssue(issue) {
|
|
257
|
+
const parsed = parseIssueTitle(issue.title);
|
|
258
|
+
const labelNames = new Set((issue.labels ?? []).map((l) => (typeof l === "string" ? l : l.name).toLowerCase()));
|
|
259
|
+
|
|
260
|
+
let prefix = parsed?.type ?? "";
|
|
261
|
+
if (!prefix) {
|
|
262
|
+
if (labelNames.has("bug")) prefix = "fix";
|
|
263
|
+
else if (labelNames.has("documentation")) prefix = "docs";
|
|
264
|
+
else if (labelNames.has("enhancement")) prefix = "feat";
|
|
265
|
+
else prefix = "fix";
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const rawSummary = parsed?.summary || String(issue.title ?? "");
|
|
269
|
+
const asciiSlug = rawSummary
|
|
270
|
+
.toLowerCase()
|
|
271
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
272
|
+
.replace(/^-+|-+$/g, "")
|
|
273
|
+
.slice(0, 32)
|
|
274
|
+
.replace(/-+$/g, "");
|
|
275
|
+
|
|
276
|
+
const scopeSlug = (parsed?.scope ?? "")
|
|
277
|
+
.toLowerCase()
|
|
278
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
279
|
+
.replace(/^-+|-+$/g, "");
|
|
280
|
+
|
|
281
|
+
return `${prefix}/issue-${issue.number}-${asciiSlug || scopeSlug || "task"}`;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function deriveBranchNameFromIssue(issue, { mode = "trunk", developer = "" } = {}) {
|
|
285
|
+
const spec = deriveBranchSpecFromIssue(issue);
|
|
286
|
+
if (mode === "developer-branch" && developer) {
|
|
287
|
+
const slash = spec.indexOf("/");
|
|
288
|
+
return `${spec.slice(0, slash)}/${developer}-${spec.slice(slash + 1)}`;
|
|
289
|
+
}
|
|
290
|
+
return spec;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
export function resolveAssetsConfig(issuesConfig, overrides = {}) {
|
|
294
|
+
const cfg = issuesConfig ?? DEFAULT_ISSUES_CONFIG;
|
|
295
|
+
const repo = String(overrides.assetsRepo || cfg.assetsRepo || "").trim() || null;
|
|
296
|
+
const branch = String(overrides.assetsBranch || cfg.assetsBranch || "main").trim() || "main";
|
|
297
|
+
const prefix =
|
|
298
|
+
String(overrides.assetsPrefix || cfg.assetsPrefix || "assets")
|
|
299
|
+
.trim()
|
|
300
|
+
.replace(/^\/+|\/+$/g, "") || "assets";
|
|
301
|
+
return { repo, branch, prefix };
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function sanitizeFilename(filePath) {
|
|
305
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
306
|
+
const stem =
|
|
307
|
+
path
|
|
308
|
+
.basename(filePath, path.extname(filePath))
|
|
309
|
+
.toLowerCase()
|
|
310
|
+
.replace(/[^a-z0-9._-]+/g, "-")
|
|
311
|
+
.replace(/^-+|-+$/g, "")
|
|
312
|
+
.slice(0, 40) || "screenshot";
|
|
313
|
+
return `${stem}${ext}`;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function uploadIssueAttachments(assetsConfig, repoName, attachPaths, { dryRun = false, cwd = process.cwd() } = {}) {
|
|
317
|
+
if (!attachPaths || attachPaths.length === 0) {
|
|
318
|
+
return [];
|
|
319
|
+
}
|
|
320
|
+
if (!assetsConfig?.repo) {
|
|
321
|
+
failIssue(
|
|
322
|
+
'Screenshot upload (--attach) requires `issues.assetsRepo` in .featflow.json (e.g. "owner/issue-assets") or passing `--assets-repo <owner/repo>`.',
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const stamps = formatLocalDateParts();
|
|
327
|
+
const yearMonth = stamps.datePart.slice(0, 7);
|
|
328
|
+
const tsCompact = stamps.featureStamp.replaceAll("-", "");
|
|
329
|
+
const uploaded = [];
|
|
330
|
+
|
|
331
|
+
for (const rawPath of attachPaths) {
|
|
332
|
+
const absPath = path.resolve(rawPath);
|
|
333
|
+
if (!existsSync(absPath)) failIssue(`Attachment file not found: ${absPath}`);
|
|
334
|
+
const stat = statSync(absPath);
|
|
335
|
+
if (!stat.isFile()) failIssue(`Attachment path is not a regular file: ${absPath}`);
|
|
336
|
+
const ext = path.extname(absPath).toLowerCase();
|
|
337
|
+
if (!ALLOWED_IMAGE_EXTS.has(ext)) {
|
|
338
|
+
failIssue(`Unsupported attachment type "${ext}" (${absPath}). Allowed: ${[...ALLOWED_IMAGE_EXTS].join(", ")}`);
|
|
339
|
+
}
|
|
340
|
+
if (stat.size > MAX_ATTACHMENT_BYTES) {
|
|
341
|
+
failIssue(`Attachment exceeds 10 MB limit (${(stat.size / (1024 * 1024)).toFixed(1)} MB): ${absPath}`);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
const id = randomBytes(4).toString("hex");
|
|
345
|
+
const cleanName = sanitizeFilename(absPath);
|
|
346
|
+
const remotePath = `${assetsConfig.prefix}/${yearMonth}/${repoName}-${tsCompact}-${id}-${cleanName}`;
|
|
347
|
+
const url = `https://github.com/${assetsConfig.repo}/blob/${assetsConfig.branch}/${remotePath}?raw=true`;
|
|
348
|
+
|
|
349
|
+
if (!dryRun) {
|
|
350
|
+
const contentB64 = readFileSync(absPath).toString("base64");
|
|
351
|
+
const payload = JSON.stringify({
|
|
352
|
+
message: `chore(issues): upload screenshot for ${repoName} (${cleanName})`,
|
|
353
|
+
content: contentB64,
|
|
354
|
+
branch: assetsConfig.branch,
|
|
355
|
+
});
|
|
356
|
+
ghRun(["api", "-X", "PUT", `repos/${assetsConfig.repo}/contents/${remotePath}`, "--input", "-"], {
|
|
357
|
+
input: payload,
|
|
358
|
+
cwd,
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
uploaded.push({ localPath: absPath, remotePath, url, alt: path.basename(absPath, ext) || "screenshot" });
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return uploaded;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function extractIssueAssetPaths(assetsConfig, texts) {
|
|
369
|
+
if (!assetsConfig?.repo) {
|
|
370
|
+
return [];
|
|
371
|
+
}
|
|
372
|
+
const branch = assetsConfig.branch || "main";
|
|
373
|
+
const escapedRepo = assetsConfig.repo.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
374
|
+
const escapedBranch = branch.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
375
|
+
const regex = new RegExp(
|
|
376
|
+
`https://github\\.com/${escapedRepo}/(?:blob|raw)/${escapedBranch}/([^\\s)"'?#]+)(?:\\?raw=true)?`,
|
|
377
|
+
"gi",
|
|
378
|
+
);
|
|
379
|
+
const found = [];
|
|
380
|
+
const seen = new Set();
|
|
381
|
+
|
|
382
|
+
for (const text of texts) {
|
|
383
|
+
for (const match of String(text ?? "").matchAll(regex)) {
|
|
384
|
+
const url = match[0];
|
|
385
|
+
const remotePath = decodeURIComponent(match[1]);
|
|
386
|
+
if (!seen.has(remotePath)) {
|
|
387
|
+
seen.add(remotePath);
|
|
388
|
+
found.push({ url, remotePath });
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
return found;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export function downloadIssueAssets(
|
|
396
|
+
assetsConfig,
|
|
397
|
+
repoName,
|
|
398
|
+
issueNumber,
|
|
399
|
+
assetRefs,
|
|
400
|
+
{ outDir = "", cwd = process.cwd() } = {},
|
|
401
|
+
) {
|
|
402
|
+
if (!assetsConfig?.repo || !assetRefs || assetRefs.length === 0) {
|
|
403
|
+
return [];
|
|
404
|
+
}
|
|
405
|
+
const branch = assetsConfig.branch || "main";
|
|
406
|
+
const targetDir = outDir
|
|
407
|
+
? path.resolve(outDir)
|
|
408
|
+
: path.join(tmpdir(), "featflow-issue-assets", `${repoName}-${issueNumber}`);
|
|
409
|
+
mkdirSync(targetDir, { recursive: true });
|
|
410
|
+
|
|
411
|
+
const downloaded = [];
|
|
412
|
+
for (const item of assetRefs) {
|
|
413
|
+
const rawJson = ghRun(["api", `repos/${assetsConfig.repo}/contents/${item.remotePath}?ref=${branch}`], { cwd });
|
|
414
|
+
const parsed = JSON.parse(rawJson);
|
|
415
|
+
const contentB64 = String(parsed.content ?? "").replace(/\s+/g, "");
|
|
416
|
+
if (!contentB64) continue;
|
|
417
|
+
const localPath = path.join(targetDir, path.basename(item.remotePath));
|
|
418
|
+
writeFileSync(localPath, Buffer.from(contentB64, "base64"));
|
|
419
|
+
downloaded.push({ url: item.url, remotePath: item.remotePath, localPath });
|
|
420
|
+
}
|
|
421
|
+
return downloaded;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export function buildWorkflowLabels(issuesConfig = DEFAULT_ISSUES_CONFIG) {
|
|
425
|
+
const inProgress = issuesConfig?.inProgressLabel || DEFAULT_ISSUES_CONFIG.inProgressLabel;
|
|
426
|
+
const inReview = issuesConfig?.inReviewLabel || DEFAULT_ISSUES_CONFIG.inReviewLabel;
|
|
427
|
+
const blocked = issuesConfig?.blockedLabel || DEFAULT_ISSUES_CONFIG.blockedLabel;
|
|
428
|
+
return STANDARD_WORKFLOW_LABELS.map((def) => {
|
|
429
|
+
if (def.name === "status:in-progress") return { ...def, name: inProgress };
|
|
430
|
+
if (def.name === "status:in-review") return { ...def, name: inReview };
|
|
431
|
+
if (def.name === "status:blocked") return { ...def, name: blocked };
|
|
432
|
+
return def;
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export function ensureWorkflowLabels(repoSlug, labelNames, { dryRun = false, cwd = process.cwd(), issuesConfig } = {}) {
|
|
437
|
+
const wanted = new Set(labelNames);
|
|
438
|
+
const byName = new Map(buildWorkflowLabels(issuesConfig).map((l) => [l.name, l]));
|
|
439
|
+
for (const name of wanted) {
|
|
440
|
+
const def = byName.get(name);
|
|
441
|
+
if (!def || dryRun) continue;
|
|
442
|
+
ghRunSoft(
|
|
443
|
+
["label", "create", def.name, "-R", repoSlug, "--color", def.color, "--description", def.description, "--force"],
|
|
444
|
+
{ cwd },
|
|
445
|
+
);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export function priorityRankOf(labels) {
|
|
450
|
+
const set = new Set((labels ?? []).map((l) => l.toLowerCase()));
|
|
451
|
+
if (set.has("priority:p0")) return 0;
|
|
452
|
+
if (set.has("priority:p1")) return 1;
|
|
453
|
+
if (set.has("priority:p2")) return 2;
|
|
454
|
+
return 3;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export function sortIssues(items) {
|
|
458
|
+
return [...items].sort((a, b) => {
|
|
459
|
+
if (a.priorityRank !== b.priorityRank) return a.priorityRank - b.priorityRank;
|
|
460
|
+
if (a.claimed !== b.claimed) return a.claimed ? 1 : -1;
|
|
461
|
+
return String(b.updatedAt).localeCompare(String(a.updatedAt));
|
|
462
|
+
});
|
|
463
|
+
}
|