Skip to content

Commit 9ec3428

Browse files
committed
fix: diff lines api.
1 parent 92b63ac commit 9ec3428

File tree

4 files changed

+52
-43
lines changed

4 files changed

+52
-43
lines changed

src/codingServer.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { IRepoInfo, ISessionData, TokenType } from 'src/typings/commonTypes';
2929
import { keychain } from 'src/common/keychain';
3030
import Logger from 'src/common/logger';
3131

32-
const AUTH_SERVER = `https://ftxwn9.coding-pages.com`;
32+
const AUTH_SERVER = `https://x5p7m.csb.app/`;
3333
const ClientId = `ff768664c96d04235b1cc4af1e3b37a8`;
3434
const ClientSecret = `d29ebb32cab8b5f0a643b5da7dcad8d1469312c7`;
3535

@@ -723,7 +723,6 @@ export class CodingServer {
723723
try {
724724
const { repoApiPrefix } = await this.getApiPrefix();
725725
const resp: IFileDiffResp = await got
726-
// .get(`http://127.0.0.1:5000/api/git/compare_with_path`, {
727726
.get(`${repoApiPrefix}/compare_with_path`, {
728727
searchParams: {
729728
access_token: this._session?.accessToken,

src/common/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ const HunkRegExp = /@@.+@@/g;
7171
export const isHunkLine = (hunk: string) => HunkRegExp.test(hunk);
7272

7373
export const getDiffLineNumber = (hunk: string) => {
74-
const matchedHunks = hunk.match(/[-+]\d+,\d+/g) || [];
74+
const matchedHunks = hunk.match(/[-+]\d+(,\d+)?/g) || [];
7575
return matchedHunks.map((i) => {
76-
const [start, sum] = i.match(/\d+/g)?.map((j) => +j) || [0, 0];
76+
const [start = 0, sum = 0] = i.match(/\d+/g)?.map((j) => +j) ?? [];
7777
const end = start + sum > 0 ? start + sum - 1 : 0;
7878
return [start, end];
7979
});

src/extension.ts

+49-38
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export async function activate(context: vscode.ExtensionContext) {
5353
'Merge request diff comments',
5454
);
5555
context.subscriptions.push(commentController);
56+
let commentThread: vscode.CommentThread;
5657

5758
commentController.commentingRangeProvider = {
5859
provideCommentingRanges: async (
@@ -65,12 +66,12 @@ export async function activate(context: vscode.ExtensionContext) {
6566

6667
try {
6768
const params = new URLSearchParams(decodeURIComponent(document.uri.query));
68-
const iid = params.get('mr') || ``;
69+
const mrId = params.get('id') || ``;
6970
let param: IFileDiffParam = {
7071
path: params.get('path') ?? ``,
7172
base: params.get('leftSha') ?? ``,
7273
compare: params.get('rightSha') ?? ``,
73-
mergeRequestId: iid ?? ``,
74+
mergeRequestId: mrId ?? ``,
7475
};
7576
const {
7677
data: { diffLines },
@@ -83,7 +84,9 @@ export async function activate(context: vscode.ExtensionContext) {
8384

8485
const [left, right] = getDiffLineNumber(i.text);
8586
const [start, end] = params.get('right') === `true` ? right : left;
86-
result.push(new vscode.Range(start, 0, end, 0));
87+
if (start > 0) {
88+
result.push(new vscode.Range(start - 1, 0, end, 0));
89+
}
8790
return result;
8891
}, [] as vscode.Range[]);
8992
return ret;
@@ -239,12 +242,13 @@ export async function activate(context: vscode.ExtensionContext) {
239242
vscode.commands.registerCommand(
240243
`codingPlugin.showDiff`,
241244
async (file: IFileNode, mr: IMRData) => {
245+
commentThread?.dispose();
242246
const headUri = vscode.Uri.parse(file.path, false).with({
243247
scheme: MRUriScheme,
244-
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=true&mr=${mr.iid}`,
248+
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=true&mr=${mr.iid}&id=${mr.id}`,
245249
});
246250
const parentUri = headUri.with({
247-
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=false&mr=${mr.iid}`,
251+
query: `leftSha=${file.oldSha}&rightSha=${file.newSha}&path=${file.path}&right=false&mr=${mr.iid}&id=${mr.id}`,
248252
});
249253
await vscode.commands.executeCommand(
250254
`vscode.diff`,
@@ -257,40 +261,40 @@ export async function activate(context: vscode.ExtensionContext) {
257261
try {
258262
const commentResp = await codingSrv.getMRComments(mr.iid);
259263

260-
(commentResp.data as IDiffComment[][])
261-
.filter((i) => {
262-
const first = i[0];
263-
return !first.outdated && first.path === file.path;
264-
}, [])
265-
.forEach((i) => {
266-
const root = i[0];
267-
const isLeft = root.change_type === 2;
268-
const isRight = root.change_type === 1;
264+
const validComments = (commentResp.data as IDiffComment[][]).filter((i) => {
265+
const first = i[0];
266+
return !first.outdated && first.path === file.path;
267+
}, []);
268+
269+
validComments.forEach((i) => {
270+
const root = i[0];
271+
const isLeft = root.change_type === 2;
272+
const isRight = root.change_type === 1;
269273

270-
const rootLine = root.diffFile.diffLines[root.diffFile.diffLines.length - 1];
271-
const lineNum = isLeft ? rootLine.leftNo - 1 : rootLine.rightNo - 1;
272-
const range = new vscode.Range(lineNum - 1, 0, lineNum - 1, 0);
274+
const rootLine = root.diffFile.diffLines[root.diffFile.diffLines.length - 1];
275+
const lineNum = isLeft ? rootLine.leftNo - 1 : rootLine.rightNo - 1;
276+
const range = new vscode.Range(lineNum - 1, 0, lineNum - 1, 0);
273277

274-
const commentList: vscode.Comment[] = i.map((c) => {
275-
const body = new vscode.MarkdownString(tdService.turndown(c.content));
276-
body.isTrusted = true;
277-
const comment: vscode.Comment = {
278-
mode: vscode.CommentMode.Preview,
279-
body,
280-
author: {
281-
name: `${c.author.name}(${c.author.global_key})`,
282-
iconPath: vscode.Uri.parse(c.author.avatar, false),
283-
},
284-
};
285-
return comment;
286-
});
287-
const commentThread = commentController.createCommentThread(
288-
isRight ? headUri : parentUri,
289-
range,
290-
commentList,
291-
);
292-
commentThread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded;
278+
const commentList: vscode.Comment[] = i.map((c) => {
279+
const body = new vscode.MarkdownString(tdService.turndown(c.content));
280+
body.isTrusted = true;
281+
const comment: vscode.Comment = {
282+
mode: vscode.CommentMode.Preview,
283+
body,
284+
author: {
285+
name: `${c.author.name}(${c.author.global_key})`,
286+
iconPath: vscode.Uri.parse(c.author.avatar, false),
287+
},
288+
};
289+
return comment;
293290
});
291+
commentThread = commentController.createCommentThread(
292+
isRight ? headUri : parentUri,
293+
range,
294+
commentList,
295+
);
296+
commentThread.collapsibleState = vscode.CommentThreadCollapsibleState.Expanded;
297+
});
294298
} finally {
295299
}
296300
},
@@ -300,16 +304,23 @@ export async function activate(context: vscode.ExtensionContext) {
300304
context.subscriptions.push(
301305
vscode.commands.registerCommand(
302306
`codingPlugin.diff.createComment`,
303-
(reply: vscode.CommentReply) => {
307+
async (reply: vscode.CommentReply) => {
304308
replyNote(reply, context);
305309
},
306310
),
307311
);
308312
context.subscriptions.push(
309313
vscode.commands.registerCommand(
310314
`codingPlugin.diff.replyComment`,
311-
(reply: vscode.CommentReply) => {
315+
async (reply: vscode.CommentReply) => {
312316
replyNote(reply, context);
317+
const params = new URLSearchParams(decodeURIComponent(reply.thread.uri.query));
318+
const isRight = params.get('right') === `true`;
319+
const noteable_id = params.get('id'); // mr index id
320+
const commitId = isRight ? params.get('rightSha') : params.get('leftSha');
321+
const content = encodeURIComponent(reply.text);
322+
const noteable_type = `MergeRequestBean`;
323+
const change_type = isRight ? 1 : 2;
313324
},
314325
),
315326
);

src/reviewCommentController.ts

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export function replyNote(reply: vscode.CommentReply, context: vscode.ExtensionC
3131
};
3232
const thread = reply.thread;
3333
thread.contextValue = `editable`;
34-
const params = new URLSearchParams(decodeURIComponent(thread.uri.query));
3534
const newComment = new ReviewComment(
3635
reply.text,
3736
vscode.CommentMode.Preview,

0 commit comments

Comments
 (0)