mr_item_model.ts 3.1 KB
Newer Older
1 2 3 4
import * as vscode from 'vscode';
import { PROGRAMMATIC_COMMANDS } from '../../command_names';
import { ChangedFileItem } from './changed_file_item';
import { ItemModel } from './item_model';
5
import { GqlDiscussion, GqlTextDiffDiscussion } from '../../gitlab/graphql/get_discussions';
6
import { handleError } from '../../log';
7
import { UserFriendlyError } from '../../errors/user_friendly_error';
8
import { GitLabCommentThread } from '../../review/gitlab_comment_thread';
9
import { CommentingRangeProvider } from '../../review/commenting_range_provider';
10
import { WrappedRepository } from '../../git/wrapped_repository';
11

12
const isTextDiffDiscussion = (discussion: GqlDiscussion): discussion is GqlTextDiffDiscussion => {
13 14 15 16
  const firstNote = discussion.notes.nodes[0];
  return firstNote?.position?.positionType === 'text';
};

17
export class MrItemModel extends ItemModel {
18
  constructor(readonly mr: RestIssuable, readonly repository: WrappedRepository) {
19 20 21 22 23 24 25 26 27
    super();
  }

  getTreeItem(): vscode.TreeItem {
    const { iid, title, author } = this.mr;
    const item = new vscode.TreeItem(
      `!${iid} · ${title}`,
      vscode.TreeItemCollapsibleState.Collapsed,
    );
28 29 30
    if (author.avatar_url) {
      item.iconPath = vscode.Uri.parse(author.avatar_url);
    }
31 32 33 34
    return item;
  }

  async getChildren(): Promise<vscode.TreeItem[]> {
35 36 37
    const overview = new vscode.TreeItem('Overview');
    overview.iconPath = new vscode.ThemeIcon('note');
    overview.command = {
38
      command: PROGRAMMATIC_COMMANDS.SHOW_RICH_CONTENT,
39
      arguments: [this.mr, this.repository.rootFsPath],
40
      title: 'Show MR Overview',
41
    };
T
Tomas Vik 已提交
42
    const { mrVersion } = await this.repository.reloadMr(this.mr);
43
    try {
44
      await this.initializeMrDiscussions(mrVersion);
45
    } catch (e) {
46 47
      handleError(
        new UserFriendlyError(
48 49 50
          `The extension failed to preload discussions on the MR diff.
            It's possible that you've encountered
            https://gitlab.com/gitlab-org/gitlab/-/issues/298827.`,
51 52 53
          e,
        ),
      );
54
    }
55 56

    const changedFiles = mrVersion.diffs.map(
57
      d => new ChangedFileItem(this.mr, mrVersion, d, this.repository.rootFsPath),
58
    );
59
    return [overview, ...changedFiles];
60 61
  }

62
  private async initializeMrDiscussions(mrVersion: RestMrVersion): Promise<void> {
63 64 65 66
    const commentController = vscode.comments.createCommentController(
      this.mr.references.full,
      this.mr.title,
    );
T
Tomas Vik 已提交
67
    const gitlabService = this.repository.getGitLabService();
68

69 70 71 72
    if (await gitlabService.canUserCommentOnMr(this.mr)) {
      commentController.commentingRangeProvider = new CommentingRangeProvider(this.mr, mrVersion);
    }

73 74 75
    const discussions = await gitlabService.getDiscussions({
      issuable: this.mr,
    });
76
    const discussionsOnDiff = discussions.filter(isTextDiffDiscussion);
77 78
    const threads = discussionsOnDiff.map(discussion => {
      return GitLabCommentThread.createThread({
79
        commentController,
80
        repositoryRoot: this.repository.rootFsPath,
81
        mr: this.mr,
82
        discussion,
83
        gitlabService,
84
      });
85 86 87
    });
    this.setDisposableChildren([...threads, commentController]);
  }
88
}