Skip to content

Commit 662e7e9

Browse files
committed
fix(mr comments): dispose threads before displaying comments.
1 parent dd0f4d2 commit 662e7e9

File tree

3 files changed

+77
-67
lines changed

3 files changed

+77
-67
lines changed

src/extension.ts

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export async function activate(context: vscode.ExtensionContext) {
6060
);
6161
context.subscriptions.push(commentController);
6262

63-
const commentResolveData: { [key: string]: boolean } = {};
63+
const commentThreads: { [key: string]: vscode.CommentThread[] } = {};
6464
const diffFileData: { [key: string]: IDiffFile } = {};
6565

6666
commentController.commentingRangeProvider = {
@@ -268,10 +268,9 @@ export async function activate(context: vscode.ExtensionContext) {
268268
{ preserveFocus: true },
269269
);
270270

271-
const identifier = `${mr.iid}/${file.path}`;
272-
if (commentResolveData[identifier]) {
273-
return;
274-
}
271+
commentThreads[mr.iid]?.forEach((c) => {
272+
c.dispose();
273+
});
275274

276275
try {
277276
const commentResp = await codingSrv.getMRComments(mr.iid);
@@ -307,14 +306,17 @@ export async function activate(context: vscode.ExtensionContext) {
307306

308307
return comment;
309308
});
309+
310310
const commentThread = commentController.createCommentThread(
311311
isRight ? headUri : parentUri,
312312
range,
313-
commentList,
313+
[],
314314
);
315+
commentThread.comments = commentList;
315316
commentThread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded;
317+
318+
commentThreads[mr.iid] = (commentThreads[mr.iid] ?? []).concat(commentThread);
316319
});
317-
commentResolveData[identifier] = true;
318320
} finally {
319321
}
320322
},
@@ -325,45 +327,15 @@ export async function activate(context: vscode.ExtensionContext) {
325327
vscode.commands.registerCommand(
326328
`codingPlugin.diff.createComment`,
327329
async (reply: vscode.CommentReply) => {
328-
replyNote(reply, context);
330+
replyNote(reply, context, codingSrv, diffFileData);
329331
},
330332
),
331333
);
332334
context.subscriptions.push(
333335
vscode.commands.registerCommand(
334336
`codingPlugin.diff.replyComment`,
335337
async (reply: vscode.CommentReply) => {
336-
const params = new URLSearchParams(decodeURIComponent(reply.thread.uri.query));
337-
const isRight = params.get('right') === `true`;
338-
const ident = `${params.get(`mr`)}/${params.get(`path`)}`;
339-
const diffFile = diffFileData[ident];
340-
341-
const noteable_id = params.get('id') ?? ``; // mr index id
342-
const commitId = isRight ? params.get('rightSha') : params.get('leftSha');
343-
const content = reply.text;
344-
const noteable_type = `MergeRequestBean`;
345-
const change_type = isRight ? 1 : 2;
346-
const line = reply.thread.range.start.line + 1;
347-
const path = encodeURIComponent(params.get(`path`) || ``);
348-
const targetPos = diffFile.diffLines.find((i) => {
349-
return i[isRight ? `rightNo` : `leftNo`] === line;
350-
});
351-
const position = targetPos?.index ?? 0;
352-
353-
try {
354-
const resp = await codingSrv.postLineNote({
355-
noteable_id,
356-
commitId: commitId ?? ``,
357-
content,
358-
noteable_type,
359-
change_type,
360-
line,
361-
path,
362-
position,
363-
});
364-
365-
replyNote(reply, context);
366-
} catch (e) {}
338+
replyNote(reply, context, codingSrv, diffFileData);
367339
},
368340
),
369341
);

src/reviewCommentController.ts

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
2-
import { ISessionData } from 'src/typings/commonTypes';
3-
import { EmptyUserAvatar } from 'src/common/contants';
42

5-
let commentIdx = 1;
3+
import { ISessionData, IDiffFileData } from 'src/typings/commonTypes';
4+
import { EmptyUserAvatar } from 'src/common/contants';
5+
import { CodingServer } from 'src/codingServer';
66

7+
let commentIdx = 0;
78
export class ReviewComment implements vscode.Comment {
89
id: number;
910
label: string | undefined;
@@ -19,29 +20,62 @@ export class ReviewComment implements vscode.Comment {
1920
}
2021
}
2122

22-
export function replyNote(reply: vscode.CommentReply, context: vscode.ExtensionContext) {
23-
const curUser = context.workspaceState.get<ISessionData>(`session`);
24-
const commentAuthor: vscode.CommentAuthorInformation = curUser?.user
25-
? {
26-
name: `${curUser.user.name} (${curUser.user.global_key})`,
27-
iconPath: vscode.Uri.parse(curUser.user.avatar, false),
28-
}
29-
: {
30-
name: `vscode user`,
31-
iconPath: vscode.Uri.parse(EmptyUserAvatar, false),
32-
};
33-
const thread = reply.thread;
34-
thread.contextValue = `editable`;
35-
const newComment = new ReviewComment(
36-
reply.text,
37-
vscode.CommentMode.Preview,
38-
commentAuthor,
39-
thread,
40-
thread.comments.length ? 'canDelete' : undefined,
41-
);
42-
// if (thread.contextValue === 'draft') {
43-
// newComment.label = 'pending';
44-
// }
23+
export async function replyNote(
24+
reply: vscode.CommentReply,
25+
context: vscode.ExtensionContext,
26+
codingSrv: CodingServer,
27+
diffFileData: IDiffFileData,
28+
) {
29+
const params = new URLSearchParams(decodeURIComponent(reply.thread.uri.query));
30+
const isRight = params.get('right') === `true`;
31+
const ident = `${params.get(`mr`)}/${params.get(`path`)}`;
32+
const diffFile = diffFileData[ident];
33+
34+
const noteable_id = params.get('id') ?? ``; // mr index id
35+
const commitId = isRight ? params.get('rightSha') : params.get('leftSha');
36+
const content = reply.text;
37+
const noteable_type = `MergeRequestBean`;
38+
const change_type = isRight ? 1 : 2;
39+
const line = reply.thread.range.start.line + 1;
40+
const path = encodeURIComponent(params.get(`path`) || ``);
41+
const targetPos = diffFile.diffLines.find((i) => {
42+
return i[isRight ? `rightNo` : `leftNo`] === line;
43+
});
44+
const position = targetPos?.index ?? 0;
45+
46+
try {
47+
const resp = await codingSrv.postLineNote({
48+
noteable_id,
49+
commitId: commitId ?? ``,
50+
content,
51+
noteable_type,
52+
change_type,
53+
line,
54+
path,
55+
position,
56+
});
57+
58+
const curUser = context.workspaceState.get<ISessionData>(`session`);
59+
const commentAuthor: vscode.CommentAuthorInformation = curUser?.user
60+
? {
61+
name: `${curUser.user.name} (${curUser.user.global_key})`,
62+
iconPath: vscode.Uri.parse(curUser.user.avatar, false),
63+
}
64+
: {
65+
name: `vscode user`,
66+
iconPath: vscode.Uri.parse(EmptyUserAvatar, false),
67+
};
68+
const thread = reply.thread;
69+
thread.contextValue = `editable`;
70+
const newComment = new ReviewComment(
71+
reply.text,
72+
vscode.CommentMode.Preview,
73+
commentAuthor,
74+
thread,
75+
thread.comments.length ? 'canDelete' : undefined,
76+
resp.data.id,
77+
);
4578

46-
thread.comments = [...thread.comments, newComment];
79+
thread.comments = [...thread.comments, newComment];
80+
} catch (e) {}
4781
}

src/typings/commonTypes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IMRDetail, IMRStatusItem, IUserItem } from './respResult';
1+
import { IDiffFile, IMRDetail, IMRStatusItem, IUserItem } from './respResult';
22

33
export interface IRepoInfo {
44
team: string;
@@ -41,3 +41,7 @@ export interface IMRWebViewDetail {
4141
};
4242
user: IUserItem;
4343
}
44+
45+
export interface IDiffFileData {
46+
[key: string]: IDiffFile;
47+
}

0 commit comments

Comments
 (0)