-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcomplete.ts
More file actions
210 lines (187 loc) · 7.22 KB
/
complete.ts
File metadata and controls
210 lines (187 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { CliOptions } from "./utils.js";
import { createService, formatCliError } from "./utils.js";
import { colors } from "./colors.js";
import {
getBooleanFlag,
getStringFlag,
parseArgs,
validateSinglePositional,
} from "./args.js";
import { formatTaskShow } from "./show.js";
import { getCommitInfo } from "./git.js";
export async function completeCommand(
args: string[],
options: CliOptions,
): Promise<void> {
const { positional, flags } = parseArgs(
args,
{
result: { short: "r", hasValue: true },
commit: { short: "c", hasValue: true },
"no-commit": { hasValue: false },
force: { short: "f", hasValue: false },
help: { short: "h", hasValue: false },
},
"complete",
);
if (getBooleanFlag(flags, "help")) {
console.log(`${colors.bold}dex complete${colors.reset} - Mark a task as completed
${colors.bold}USAGE:${colors.reset}
dex complete <task-id> --result "completion notes" [--commit <sha>|--no-commit]
${colors.bold}ARGUMENTS:${colors.reset}
<task-id> Task ID to complete (required)
${colors.bold}OPTIONS:${colors.reset}
-r, --result <text> Completion result/notes (required)
-c, --commit <sha> Git commit SHA that implements this task
--no-commit Complete without linking a commit (issue stays open)
-f, --force Bypass validation checks (e.g., incomplete subtasks)
-h, --help Show this help message
${colors.bold}NOTES:${colors.reset}
For tasks linked to GitHub issues or Shortcut stories, you must specify either
--commit or --no-commit. This ensures issues are only closed when code is merged.
A task with incomplete subtasks cannot be completed unless --force is used.
${colors.bold}EXAMPLE:${colors.reset}
dex complete abc123 --result "Fixed by updating auth token refresh logic" --commit a1b2c3d
dex complete abc123 -r "Implemented and tested" -c a1b2c3d
dex complete abc123 --result "Planning complete, no code changes" --no-commit
`);
return;
}
const id = positional[0];
const result = getStringFlag(flags, "result");
const commitSha = getStringFlag(flags, "commit");
const hasNoCommit = getBooleanFlag(flags, "no-commit");
const hasForce = getBooleanFlag(flags, "force");
if (!id) {
console.error(`${colors.red}Error:${colors.reset} Task ID is required`);
console.error(`Usage: dex complete <task-id> --result "completion notes"`);
process.exit(1);
}
validateSinglePositional(positional, "complete", {
hint: "Use --result to provide completion notes",
});
if (!result) {
console.error(
`${colors.red}Error:${colors.reset} --result (-r) is required`,
);
console.error(`Usage: dex complete <task-id> --result "completion notes"`);
process.exit(1);
}
if (commitSha && hasNoCommit) {
console.error(
`${colors.red}Error:${colors.reset} Cannot use both --commit and --no-commit`,
);
process.exit(1);
}
const service = createService(options);
try {
// Fetch task to check for remote links
const existingTask = await service.get(id);
if (!existingTask) {
console.error(`${colors.red}Error:${colors.reset} Task ${id} not found`);
process.exit(1);
}
// Check if this is a leaf task (no subtasks) with a remote link
const hasRemoteLink = !!(
existingTask.metadata?.github || existingTask.metadata?.shortcut
);
const subtasks = await service.getChildren(id);
const isLeafTask = subtasks.length === 0;
// Check for incomplete subtasks
const incompleteSubtasks = subtasks.filter((s) => !s.completed);
if (incompleteSubtasks.length > 0 && !hasForce) {
console.error(
`${colors.red}Error:${colors.reset} Cannot complete task with ${incompleteSubtasks.length} incomplete subtask(s):`,
);
for (const subtask of incompleteSubtasks.slice(0, 5)) {
console.error(
` ${colors.dim}•${colors.reset} ${colors.bold}${subtask.id}${colors.reset}: ${subtask.name}`,
);
}
if (incompleteSubtasks.length > 5) {
console.error(
` ${colors.dim}... and ${incompleteSubtasks.length - 5} more${colors.reset}`,
);
}
console.error("");
console.error(
`Use ${colors.cyan}--force${colors.reset} to complete anyway.`,
);
process.exit(1);
}
// Only require --commit/--no-commit for leaf tasks with remote links
if (hasRemoteLink && isLeafTask && !commitSha && !hasNoCommit) {
const issueRef = existingTask.metadata?.github
? `GitHub issue #${existingTask.metadata.github.issueNumber}`
: `Shortcut story`;
console.error(
`${colors.red}Error:${colors.reset} Task is linked to ${issueRef}.`,
);
console.error(
` Use ${colors.cyan}--commit <sha>${colors.reset} to link a commit (closes issue when merged)`,
);
console.error(
` Use ${colors.cyan}--no-commit${colors.reset} to complete without a commit (issue stays open)`,
);
process.exit(1);
}
// Check for incomplete blockers and warn
const incompleteBlockers = await service.getIncompleteBlockers(id);
if (incompleteBlockers.length > 0) {
console.log(
`${colors.yellow}Warning:${colors.reset} This task is blocked by ${incompleteBlockers.length} incomplete task(s):`,
);
for (const blocker of incompleteBlockers) {
console.log(
` ${colors.dim}•${colors.reset} ${colors.bold}${blocker.id}${colors.reset}: ${blocker.name}`,
);
}
console.log("");
}
const metadata = commitSha
? {
commit: {
...getCommitInfo(commitSha),
timestamp: new Date().toISOString(),
},
}
: undefined;
const task = await service.complete(id, result, metadata, {
force: hasForce,
});
console.log(
`${colors.green}Completed${colors.reset} task ${colors.bold}${id}${colors.reset}`,
);
console.log(formatTaskShow(task));
// Check if all sibling subtasks are now complete and hint about parent
if (task.parent_id) {
const siblings = await service.getChildren(task.parent_id);
const allSiblingsComplete = siblings.every((s) => s.completed);
if (allSiblingsComplete) {
const parent = await service.get(task.parent_id);
const parentHasRemoteLink = !!(
parent?.metadata?.github || parent?.metadata?.shortcut
);
console.log("");
console.log(
`${colors.cyan}Hint:${colors.reset} All subtasks of ${colors.bold}${parent?.name || task.parent_id}${colors.reset} are now complete.`,
);
if (parentHasRemoteLink) {
console.log(
` ${colors.dim}•${colors.reset} Complete parent: ${colors.cyan}dex complete ${task.parent_id} --result "..."${colors.reset}`,
);
console.log(
` ${colors.dim} ${colors.reset}(Parent task with subtasks doesn't require --commit/--no-commit)`,
);
} else {
console.log(
` ${colors.dim}•${colors.reset} Complete parent: ${colors.cyan}dex complete ${task.parent_id} --result "..."${colors.reset}`,
);
}
}
}
} catch (err) {
console.error(formatCliError(err));
process.exit(1);
}
}