提交 90ca16d5 编写于 作者: T Tomas Vik

Merge branch '408-prevent-duplicit-comment-controllers' into 'main'

fix: prevent duplicate comments and comment controllers

See merge request gitlab-org/gitlab-vscode-extension!284
......@@ -87,5 +87,16 @@ describe('MrItemModel', () => {
item.dispose();
expect(commentController.dispose).toHaveBeenCalled();
});
it('when we create comment controller for the same MR, we dispose the previously created controller', async () => {
await item.getChildren();
expect(commentController.dispose).not.toHaveBeenCalled();
// simulates another MR item opening the same MR
await item.getChildren();
expect(commentController.dispose).toHaveBeenCalled();
});
});
});
......@@ -8,6 +8,7 @@ import { UserFriendlyError } from '../../errors/user_friendly_error';
import { GitLabCommentThread } from '../../review/gitlab_comment_thread';
import { CommentingRangeProvider } from '../../review/commenting_range_provider';
import { WrappedRepository } from '../../git/wrapped_repository';
import { commentControllerProvider } from '../../review/comment_controller_provider';
const isTextDiffDiscussion = (discussion: GqlDiscussion): discussion is GqlTextDiffDiscussion => {
const firstNote = discussion.notes.nodes[0];
......@@ -60,18 +61,16 @@ export class MrItemModel extends ItemModel {
}
private async initializeMrDiscussions(mrVersion: RestMrVersion): Promise<void> {
const commentController = vscode.comments.createCommentController(
`gitlab-mr-${this.mr.references.full}`,
const gitlabService = this.repository.getGitLabService();
const userCanComment = await gitlabService.canUserCommentOnMr(this.mr);
const commentController = commentControllerProvider.borrowCommentController(
this.mr.references.full,
this.mr.title,
userCanComment ? new CommentingRangeProvider(this.mr, mrVersion) : undefined,
);
this.setDisposableChildren([commentController]);
const gitlabService = this.repository.getGitLabService();
if (await gitlabService.canUserCommentOnMr(this.mr)) {
commentController.commentingRangeProvider = new CommentingRangeProvider(this.mr, mrVersion);
}
const discussions = await gitlabService.getDiscussions({
issuable: this.mr,
});
......
import * as vscode from 'vscode';
export class CommentControllerProvider {
controllers: Record<string, vscode.CommentController | undefined> = {};
/**
* Creates comment controller and ensures it is the only existing controller for given MR
* This method exists for several reasons:
* - if we open MR multiple times, we want to prevent multiple comments being displayed
* - multiple comment controllers for the same MR make commenting on MR harder
* - we want to make sure that we add commentingRangeProvider correctly
*/
borrowCommentController(
mrFullReference: string,
title: string,
commentingRangeProvider?: vscode.CommentingRangeProvider,
): vscode.CommentController {
const existingController = this.controllers[mrFullReference];
if (existingController) {
existingController.dispose();
}
const controller = vscode.comments.createCommentController(
`gitlab-mr-${mrFullReference}`,
title,
);
// we must assign commentingRangeProvider right after we create the controller
// if there was an `async` call between, VS Code wouldn't show the commenting range
// this bug has been reported https://github.com/microsoft/vscode/issues/126475
controller.commentingRangeProvider = commentingRangeProvider;
this.controllers[mrFullReference] = controller;
return controller;
}
}
export const commentControllerProvider = new CommentControllerProvider();
......@@ -124,6 +124,7 @@ describe('MR Review', () => {
thread = { uri, range, comments };
return thread;
},
dispose: () => {},
};
sandbox.stub(vscode.comments, 'createCommentController').returns(commentController);
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册