Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 4x 4x 13x 6x 6x 6x 1x 1x 1x 5x 5x 6x 6x 6x 4x 4x 4x 1x 4x 4x 1x 1x 1x 1x 1x 1x 5x 5x 3x 3x 3x 2x 1x 2x 2x 1x 1x 2x 2x 1x 1x | import { AdoClient } from './client';
const STATUSES: Record<string, { code: number; api: string }> = {
Active: { code: 1, api: 'active' },
Fixed: { code: 2, api: 'fixed' },
WontFix: { code: 3, api: 'wontFix' },
Closed: { code: 4, api: 'closed' },
Pending: { code: 5, api: 'pending' },
};
export interface CreateCommentOptions {
comment: string;
status?: 'Active' | 'Fixed' | 'WontFix' | 'Closed' | 'Pending';
filePath?: string;
startLine?: number;
endLine?: number;
iterationId?: number;
threadId?: number; // If set, replies to existing thread instead of creating new one
}
export interface CreateCommentResult {
threadId: number;
commentId: number;
author: string;
publishedDate: string;
}
function normalizeFilePath(p: string): string {
const normalized = p.replace(/\\/g, '/');
return normalized.startsWith('/') ? normalized : `/${normalized}`;
}
function prBase(repo: string, prId: number): string {
return `git/repositories/${encodeURIComponent(repo)}/pullrequests/${prId}`;
}
/**
* Creates a new comment thread or replies to an existing thread.
* For inline comments, falls back to a generic comment if the inline post fails.
*/
export async function createComment(
client: AdoClient,
repo: string,
prId: number,
options: CreateCommentOptions
): Promise<CreateCommentResult> {
const { comment, status = 'Active', filePath, startLine, endLine, iterationId, threadId } = options;
const base = prBase(repo, prId);
// Reply to existing thread
if (threadId && threadId > 0) {
const url = `${base}/threads/${threadId}/comments`;
const result = await client.post<{ id: number; author: { displayName: string }; publishedDate: string }>(url, {
content: comment, parentCommentId: 0, commentType: 1,
});
return { threadId, commentId: result.id, author: result.author.displayName, publishedDate: result.publishedDate };
}
// Create new thread
const threadsUrl = `${base}/threads`;
const isInline = !!filePath && (startLine ?? 0) > 0;
const effectiveEndLine = endLine && endLine > 0 ? endLine : startLine!;
const body: Record<string, unknown> = {
comments: [{ content: comment, commentType: 1 }],
status: STATUSES[status]?.code ?? 1,
};
if (isInline) {
const normalizedPath = normalizeFilePath(filePath!);
body.threadContext = {
filePath: normalizedPath,
rightFileStart: { line: startLine, offset: 1 },
rightFileEnd: { line: effectiveEndLine, offset: 1 },
};
if ((iterationId ?? 0) > 0) {
body.pullRequestThreadContext = {
iterationContext: {
firstComparingIteration: iterationId,
secondComparingIteration: iterationId,
},
};
}
// Try inline first, fall back to generic comment on failure
try {
return extractResult(await client.post(threadsUrl, body));
} catch (err) {
console.warn(`Warning: Failed to post inline comment: ${(err as Error).message}`);
console.warn('Falling back to generic PR comment with file/line information appended.');
const lineInfo = startLine === effectiveEndLine ? `Line ${startLine}` : `Lines ${startLine}-${effectiveEndLine}`;
const fallbackBody: Record<string, unknown> = {
comments: [{ content: `${comment}\n\n**File:** \`${normalizedPath}\`\n**${lineInfo}**`, commentType: 1 }],
status: STATUSES[status]?.code ?? 1,
};
return extractResult(await client.post(threadsUrl, fallbackBody));
}
}
return extractResult(await client.post(threadsUrl, body));
}
function extractResult(result: { id: number; comments: Array<{ id: number; author: { displayName: string }; publishedDate: string }> }): CreateCommentResult {
const first = result.comments[0];
return { threadId: result.id, commentId: first.id, author: first.author.displayName, publishedDate: first.publishedDate };
}
/**
* Updates a thread's status. Fails silently — always returns without throwing.
*/
export async function updateThreadStatus(
client: AdoClient,
repo: string,
prId: number,
threadId: number,
status: string
): Promise<void> {
try {
const apiStatus = STATUSES[status]?.api ?? 'fixed';
await client.patch(`${prBase(repo, prId)}/threads/${threadId}`, { status: apiStatus });
console.log(`Thread #${threadId} status updated to '${status}'`);
} catch (err) {
console.warn(`Update-CopilotComment: Could not update thread #${threadId} — ${(err as Error).message}`);
}
}
/**
* Updates a comment's content. Fails silently.
*/
export async function updateCommentContent(
client: AdoClient,
repo: string,
prId: number,
threadId: number,
commentId: number,
content: string
): Promise<void> {
try {
await client.patch(`${prBase(repo, prId)}/threads/${threadId}/comments/${commentId}`, { content });
console.log(`Comment #${commentId} in thread #${threadId} content updated`);
} catch (err) {
console.warn(`Update-CopilotComment: Could not update comment #${commentId} in thread #${threadId} — ${(err as Error).message}`);
}
}
/**
* Deletes a comment. Fails silently.
*/
export async function deleteComment(
client: AdoClient,
repo: string,
prId: number,
threadId: number,
commentId: number
): Promise<void> {
try {
await client.delete(`${prBase(repo, prId)}/threads/${threadId}/comments/${commentId}`);
console.log(`Comment #${commentId} in thread #${threadId} deleted`);
} catch (err) {
console.warn(`Delete-CopilotComment: Could not delete comment #${commentId} in thread #${threadId} — ${(err as Error).message}`);
}
}
|