Skip to content

Commit dd0f4d2

Browse files
committed
feat(mr diff): post reply.
1 parent 8c16aa6 commit dd0f4d2

File tree

4 files changed

+120
-42
lines changed

4 files changed

+120
-42
lines changed

src/codingServer.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import got from 'got';
55
import {
66
AuthFailResult,
77
AuthSuccessResult,
8-
CodingResponse,
8+
ICodingResponse,
99
IRepoListResponse,
1010
IMRDiffResponse,
1111
IMRDetailResponse,
@@ -21,6 +21,8 @@ import {
2121
IMRCommentResp,
2222
IFileDiffParam,
2323
IFileDiffResp,
24+
ILineNoteResp,
25+
ILineNoteForm,
2426
} from 'src/typings/respResult';
2527

2628
import { PromiseAdapter, promiseFromEvent, parseQuery, parseCloneUrl } from 'src/common/utils';
@@ -223,7 +225,7 @@ export class CodingServer {
223225

224226
public async getUserInfo(team: string, token: string = this._session?.accessToken || ``) {
225227
try {
226-
const result: CodingResponse = await got
228+
const result: ICodingResponse = await got
227229
.get(`https://${team || `codingcorp`}.coding.net/api/current_user`, {
228230
searchParams: {
229231
access_token: token,
@@ -270,10 +272,10 @@ export class CodingServer {
270272
};
271273
}
272274

273-
public async getMRList(repo?: string, status?: string): Promise<CodingResponse> {
275+
public async getMRList(repo?: string, status?: string): Promise<ICodingResponse> {
274276
try {
275277
const { repoApiPrefix } = await this.getApiPrefix();
276-
const result: CodingResponse = await got
278+
const result: ICodingResponse = await got
277279
.get(`${repoApiPrefix}/merges/query`, {
278280
searchParams: {
279281
status,
@@ -417,7 +419,7 @@ export class CodingServer {
417419
public async closeMR(iid: string) {
418420
try {
419421
const { repoApiPrefix } = await this.getApiPrefix();
420-
const result: CodingResponse = await got
422+
const result: ICodingResponse = await got
421423
.post(`${repoApiPrefix}/merge/${iid}/refuse`, {
422424
searchParams: {
423425
access_token: this._session?.accessToken,
@@ -437,7 +439,7 @@ export class CodingServer {
437439
public async approveMR(iid: string) {
438440
try {
439441
const { repoApiPrefix } = await this.getApiPrefix();
440-
const result: CodingResponse = await got
442+
const result: ICodingResponse = await got
441443
.post(`${repoApiPrefix}/merge/${iid}/good`, {
442444
searchParams: {
443445
access_token: this._session?.accessToken,
@@ -457,7 +459,7 @@ export class CodingServer {
457459
public async disapproveMR(iid: string) {
458460
try {
459461
const { repoApiPrefix } = await this.getApiPrefix();
460-
const result: CodingResponse = await got
462+
const result: ICodingResponse = await got
461463
.delete(`${repoApiPrefix}/merge/${iid}/good`, {
462464
searchParams: {
463465
access_token: this._session?.accessToken,
@@ -477,7 +479,7 @@ export class CodingServer {
477479
public async mergeMR(iid: string) {
478480
try {
479481
const { repoApiPrefix } = await this.getApiPrefix();
480-
const result: CodingResponse = await got
482+
const result: ICodingResponse = await got
481483
.post(`${repoApiPrefix}/merge/${iid}/merge`, {
482484
searchParams: {
483485
access_token: this._session?.accessToken,
@@ -500,7 +502,7 @@ export class CodingServer {
500502
public async updateMRTitle(iid: string, title: string) {
501503
try {
502504
const { repoApiPrefix } = await this.getApiPrefix();
503-
const result: CodingResponse = await got
505+
const result: ICodingResponse = await got
504506
.put(`${repoApiPrefix}/merge/${iid}/update-title`, {
505507
searchParams: {
506508
access_token: this._session?.accessToken,
@@ -630,7 +632,7 @@ export class CodingServer {
630632

631633
public async addMRReviewers(iid: string, ids: number[]): Promise<number[]> {
632634
const { repoApiPrefix } = await this.getApiPrefix();
633-
const tasks: Promise<CodingResponse>[] = ids.map((id) => {
635+
const tasks: Promise<ICodingResponse>[] = ids.map((id) => {
634636
return got
635637
.post(`${repoApiPrefix}/merge/${iid}/reviewers`, {
636638
searchParams: {
@@ -640,7 +642,7 @@ export class CodingServer {
640642
})
641643
.json();
642644
});
643-
const result: PromiseSettledResult<CodingResponse>[] = await Promise.allSettled(tasks);
645+
const result: PromiseSettledResult<ICodingResponse>[] = await Promise.allSettled(tasks);
644646
const fulfilled = ids.reduce((res, cur, idx) => {
645647
if (result[idx].status === `fulfilled`) {
646648
res = res.concat(cur);
@@ -653,7 +655,7 @@ export class CodingServer {
653655

654656
public async removeMRReviewers(iid: string, ids: number[]): Promise<number[]> {
655657
const { repoApiPrefix } = await this.getApiPrefix();
656-
const tasks: Promise<CodingResponse>[] = ids.map((id) => {
658+
const tasks: Promise<ICodingResponse>[] = ids.map((id) => {
657659
return got
658660
.delete(`${repoApiPrefix}/merge/${iid}/reviewers`, {
659661
searchParams: {
@@ -663,7 +665,7 @@ export class CodingServer {
663665
})
664666
.json();
665667
});
666-
const result: PromiseSettledResult<CodingResponse>[] = await Promise.allSettled(tasks);
668+
const result: PromiseSettledResult<ICodingResponse>[] = await Promise.allSettled(tasks);
667669
const fulfilled = ids.reduce((res, cur, idx) => {
668670
if (result[idx].status === `fulfilled`) {
669671
res = res.concat(cur);
@@ -744,6 +746,27 @@ export class CodingServer {
744746
}
745747
}
746748

749+
public async postLineNote(data: ILineNoteForm) {
750+
try {
751+
const { repoApiPrefix } = await this.getApiPrefix();
752+
const resp: ILineNoteResp = await got.post(`${repoApiPrefix}/line_notes`, {
753+
resolveBodyOnly: true,
754+
responseType: `json`,
755+
searchParams: {
756+
access_token: this._session?.accessToken,
757+
},
758+
form: data,
759+
});
760+
761+
if (resp.code) {
762+
return Promise.reject(resp);
763+
}
764+
return resp;
765+
} catch (e) {
766+
return Promise.reject(e);
767+
}
768+
}
769+
747770
get loggedIn() {
748771
return this._loggedIn;
749772
}

src/extension.ts

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@ import { ReleaseTreeDataProvider } from 'src/tree/releaseTree';
99
import { IRepoInfo, IMRWebViewDetail, ISessionData } from 'src/typings/commonTypes';
1010
import { GitService } from 'src/common/gitService';
1111
import { MRUriScheme } from 'src/common/contants';
12-
import { IDiffComment, IMRData, IFileDiffParam } from 'src/typings/respResult';
13-
import { replyNote } from './reviewCommentController';
12+
import {
13+
IDiffComment,
14+
IMRData,
15+
IFileDiffParam,
16+
IFileDiffResp,
17+
IDiffFile,
18+
} from 'src/typings/respResult';
19+
import { replyNote, ReviewComment } from './reviewCommentController';
1420
import { getDiffLineNumber, isHunkLine } from 'src/common/utils';
1521

1622
export async function activate(context: vscode.ExtensionContext) {
@@ -53,7 +59,9 @@ export async function activate(context: vscode.ExtensionContext) {
5359
'Merge request diff comments',
5460
);
5561
context.subscriptions.push(commentController);
62+
5663
const commentResolveData: { [key: string]: boolean } = {};
64+
const diffFileData: { [key: string]: IDiffFile } = {};
5765

5866
commentController.commentingRangeProvider = {
5967
provideCommentingRanges: async (
@@ -73,9 +81,12 @@ export async function activate(context: vscode.ExtensionContext) {
7381
compare: params.get('rightSha') ?? ``,
7482
mergeRequestId: mrId ?? ``,
7583
};
76-
const {
77-
data: { diffLines },
78-
} = await codingSrv.fetchFileDiffs(param);
84+
const fileIdent = `${params.get(`mr`)}/${params.get(`path`)}`;
85+
86+
const { data } = await codingSrv.fetchFileDiffs(param);
87+
diffFileData[fileIdent] = data;
88+
const { diffLines } = data;
89+
7990
const ret = diffLines.reduce((result, i) => {
8091
const isHunk = isHunkLine(i.text);
8192
if (!isHunk) {
@@ -277,19 +288,23 @@ export async function activate(context: vscode.ExtensionContext) {
277288

278289
const rootLine = root.diffFile.diffLines[root.diffFile.diffLines.length - 1];
279290
const lineNum = isLeft ? rootLine.leftNo - 1 : rootLine.rightNo - 1;
280-
const range = new vscode.Range(lineNum - 1, 0, lineNum - 1, 0);
291+
const range = new vscode.Range(lineNum, 0, lineNum, 0);
281292

282293
const commentList: vscode.Comment[] = i.map((c) => {
283294
const body = new vscode.MarkdownString(tdService.turndown(c.content));
284295
body.isTrusted = true;
285-
const comment: vscode.Comment = {
286-
mode: vscode.CommentMode.Preview,
296+
const comment = new ReviewComment(
287297
body,
288-
author: {
298+
vscode.CommentMode.Preview,
299+
{
289300
name: `${c.author.name}(${c.author.global_key})`,
290301
iconPath: vscode.Uri.parse(c.author.avatar, false),
291302
},
292-
};
303+
undefined,
304+
'canDelete',
305+
c.id,
306+
);
307+
293308
return comment;
294309
});
295310
const commentThread = commentController.createCommentThread(
@@ -318,14 +333,37 @@ export async function activate(context: vscode.ExtensionContext) {
318333
vscode.commands.registerCommand(
319334
`codingPlugin.diff.replyComment`,
320335
async (reply: vscode.CommentReply) => {
321-
replyNote(reply, context);
322336
const params = new URLSearchParams(decodeURIComponent(reply.thread.uri.query));
323337
const isRight = params.get('right') === `true`;
324-
const noteable_id = params.get('id'); // mr index id
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
325342
const commitId = isRight ? params.get('rightSha') : params.get('leftSha');
326-
const content = encodeURIComponent(reply.text);
343+
const content = reply.text;
327344
const noteable_type = `MergeRequestBean`;
328345
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) {}
329367
},
330368
),
331369
);

src/reviewCommentController.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22
import { ISessionData } from 'src/typings/commonTypes';
33
import { EmptyUserAvatar } from 'src/common/contants';
44

5-
let commentId = 1;
5+
let commentIdx = 1;
66

77
export class ReviewComment implements vscode.Comment {
88
id: number;
@@ -13,8 +13,9 @@ export class ReviewComment implements vscode.Comment {
1313
public author: vscode.CommentAuthorInformation,
1414
public parent?: vscode.CommentThread,
1515
public contextValue?: string,
16+
public commentId?: number,
1617
) {
17-
this.id = ++commentId;
18+
this.id = commentId ?? ++commentIdx;
1819
}
1920
}
2021

0 commit comments

Comments
 (0)