Skip to content

Commit 5639d1a

Browse files
committed
Refactor comment reply list to query all replies by default
- Change "comment reply list" from requiring commentName to optional - Add --comment flag to filter replies by comment name - Use Core API (ReplyV1alpha1Api.listReply) to query all replies - Use Console API (ReplyV1alpha1ConsoleApi.listReplies) when --comment is provided - Update printReplyList to handle both ReplyList and ListedReplyList types
1 parent bc86ebe commit 5639d1a

File tree

4 files changed

+93
-27
lines changed

4 files changed

+93
-27
lines changed

src/commands/comment/__test__/comment-entry.spec.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,18 @@ test("tryRunCommentCommand dispatches reply list subcommands", async () => {
9999

100100
await expect(
101101
tryRunCommentCommand(
102-
["comment", "reply", "list", "comment-1", "--page", "2", "--size", "10", "--json"],
102+
[
103+
"comment",
104+
"reply",
105+
"list",
106+
"--comment",
107+
"comment-1",
108+
"--page",
109+
"2",
110+
"--size",
111+
"10",
112+
"--json",
113+
],
103114
runtimeMock as never,
104115
),
105116
).resolves.toBe(true);

src/commands/comment/format.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
ListedReply,
55
ListedReplyList,
66
Reply,
7+
ReplyList,
78
} from "@halo-dev/api-client";
89
import Table from "cli-table3";
910

@@ -166,22 +167,60 @@ export function printComment(comment: Comment, json = false): void {
166167
} as Record<string, unknown>);
167168
}
168169

169-
export function printReplyList(list: ListedReplyList | ListedReply[], json = false): void {
170+
function formatReplyRow(
171+
reply: Reply,
172+
widths: number[],
173+
ownerDisplayName?: string,
174+
): [string, string, string, string, string] {
175+
return [
176+
reply.metadata.name,
177+
truncateDisplayText(ownerDisplayName ?? reply.spec.owner?.displayName ?? "", widths[1]!),
178+
truncateDisplayText(stripHtmlTags(reply.spec.content), widths[2]!),
179+
reply.spec.approved ? "yes" : "no",
180+
formatTimestamp(reply.metadata.creationTimestamp ?? undefined),
181+
];
182+
}
183+
184+
export function printReplyList(
185+
list: ListedReplyList | ListedReply[] | ReplyList,
186+
json = false,
187+
): void {
170188
if (json) {
171189
printJson(list);
172190
return;
173191
}
174192

193+
const widths = getReplyListWidths();
194+
195+
// Handle ReplyList from Core API (listReply)
196+
if (
197+
!Array.isArray(list) &&
198+
"items" in list &&
199+
list.items.length > 0 &&
200+
!("reply" in list.items[0]!)
201+
) {
202+
const replyList = list as ReplyList;
203+
const rows = replyList.items.map((reply) => formatReplyRow(reply, widths));
204+
205+
printTable(["NAME", "OWNER", "CONTENT", "APPROVED", "CREATED AT"], rows, widths);
206+
printPaginationFooter({
207+
page: replyList.page,
208+
size: replyList.size,
209+
total: replyList.total,
210+
totalPages: replyList.totalPages,
211+
hasNext: replyList.hasNext,
212+
hasPrevious: replyList.hasPrevious,
213+
itemLabel: "reply",
214+
});
215+
return;
216+
}
217+
218+
// Handle ListedReplyList from Console API (listReplies)
175219
const items = Array.isArray(list) ? list : list.items;
176220
const total = Array.isArray(list) ? list.length : list.total;
177-
const widths = getReplyListWidths();
178-
const rows = items.map((item) => [
179-
item.reply.metadata.name,
180-
truncateDisplayText(resolveCommentOwnerName(item), widths[1]!),
181-
truncateDisplayText(stripHtmlTags(item.reply.spec.content), widths[2]!),
182-
item.reply.spec.approved ? "yes" : "no",
183-
formatTimestamp(item.reply.metadata.creationTimestamp ?? undefined),
184-
]);
221+
const rows = (items as ListedReply[]).map((item) =>
222+
formatReplyRow(item.reply, widths, item.owner?.displayName),
223+
);
185224

186225
printTable(["NAME", "OWNER", "CONTENT", "APPROVED", "CREATED AT"], rows, widths);
187226

src/commands/comment/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,8 @@ function buildCommentCli(runtime: RuntimeContext): CAC {
218218
(bin) =>
219219
`${bin} create-reply comment-abc123 --content "Thanks for your feedback" --quote-reply reply-abc123`,
220220
);
221-
commentCli.example((bin) => `${bin} reply list comment-abc123`);
221+
commentCli.example((bin) => `${bin} reply list`);
222+
commentCli.example((bin) => `${bin} reply list --comment comment-abc123`);
222223
commentCli.help();
223224

224225
return commentCli;

src/commands/comment/reply.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface CommentDeleteOptions extends CommentCommandOptions {
2323
interface ReplyListOptions extends CommentCommandOptions {
2424
page?: string;
2525
size?: string;
26+
comment?: string;
2627
}
2728

2829
export function buildApprovePatch(): JsonPatchInner[] {
@@ -44,24 +45,37 @@ export function buildReplyCli(runtime: RuntimeContext): CAC {
4445
const replyCli = cac("halo comment reply");
4546

4647
replyCli
47-
.command("list <commentName>", "List replies for a comment")
48+
.command("list", "List replies")
4849
.option("--profile <name>", "Halo profile name")
4950
.option("--json", "Output JSON")
50-
.option("--page <number>", "Page number")
51-
.option("--size <number>", "Page size")
52-
.action(async (commentName: string, options: ReplyListOptions) => {
51+
.option("--page <number>", "Page number", { default: 1 })
52+
.option("--size <number>", "Page size", { default: 20 })
53+
.option("--comment <name>", "Filter by comment name")
54+
.action(async (options: ReplyListOptions) => {
5355
const { profile, clients } = await runtime.getClientsForOptions(options);
54-
const replyConsoleApi = new ReplyV1alpha1ConsoleApi(
55-
undefined,
56-
profile.baseUrl,
57-
clients.axios,
58-
);
59-
const response = await replyConsoleApi.listReplies({
60-
commentName,
61-
page: parseNumberOption(options.page),
62-
size: parseNumberOption(options.size),
63-
});
64-
printReplyList(response.data, options.json);
56+
57+
if (options.comment) {
58+
// Use Console API to list replies for a specific comment
59+
const replyConsoleApi = new ReplyV1alpha1ConsoleApi(
60+
undefined,
61+
profile.baseUrl,
62+
clients.axios,
63+
);
64+
const response = await replyConsoleApi.listReplies({
65+
commentName: options.comment,
66+
page: parseNumberOption(options.page),
67+
size: parseNumberOption(options.size),
68+
});
69+
printReplyList(response.data, options.json);
70+
} else {
71+
// Use Core API to list all replies
72+
const replyApi = new ReplyV1alpha1Api(undefined, profile.baseUrl, clients.axios);
73+
const response = await replyApi.listReply({
74+
page: parseNumberOption(options.page),
75+
size: parseNumberOption(options.size),
76+
});
77+
printReplyList(response.data, options.json);
78+
}
6579
});
6680

6781
replyCli
@@ -124,7 +138,8 @@ export function buildReplyCli(runtime: RuntimeContext): CAC {
124138
});
125139

126140
replyCli.usage("<command> [flags]");
127-
replyCli.example((bin) => `${bin} list comment-abc123`);
141+
replyCli.example((bin) => `${bin} list`);
142+
replyCli.example((bin) => `${bin} list --comment comment-abc123`);
128143
replyCli.example((bin) => `${bin} get reply-abc123`);
129144
replyCli.example((bin) => `${bin} approve reply-abc123`);
130145
replyCli.example((bin) => `${bin} delete reply-abc123 --force`);

0 commit comments

Comments
 (0)