From 452586c420f59587f3f19137d01e9dfcee13d673 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Fri, 7 May 2021 13:57:37 +0200 Subject: [PATCH] refactor: group discussion and note types into separate files --- src/data_providers/items/mr_item_model.ts | 2 +- src/gitlab/gitlab_new_service.ts | 112 +++------------------- src/gitlab/graphql/get_discussions.ts | 36 ++++++- src/gitlab/graphql/shared.ts | 70 ++++++++++++++ src/review/gitlab_comment.test.ts | 2 +- src/review/gitlab_comment.ts | 2 +- src/review/gitlab_comment_thread.test.ts | 8 +- src/review/gitlab_comment_thread.ts | 10 +- 8 files changed, 127 insertions(+), 115 deletions(-) diff --git a/src/data_providers/items/mr_item_model.ts b/src/data_providers/items/mr_item_model.ts index 60e890a..cee0fa6 100644 --- a/src/data_providers/items/mr_item_model.ts +++ b/src/data_providers/items/mr_item_model.ts @@ -3,7 +3,7 @@ import { PROGRAMMATIC_COMMANDS } from '../../command_names'; import { createGitLabNewService } from '../../service_factory'; import { ChangedFileItem } from './changed_file_item'; import { ItemModel } from './item_model'; -import { GqlDiscussion, GqlTextDiffDiscussion } from '../../gitlab/gitlab_new_service'; +import { GqlDiscussion, GqlTextDiffDiscussion } from '../../gitlab/graphql/get_discussions'; import { handleError } from '../../log'; import { UserFriendlyError } from '../../errors/user_friendly_error'; import { GitLabCommentThread } from '../../review/gitlab_comment_thread'; diff --git a/src/gitlab/gitlab_new_service.ts b/src/gitlab/gitlab_new_service.ts index 3e788e5..b7e7fcd 100644 --- a/src/gitlab/gitlab_new_service.ts +++ b/src/gitlab/gitlab_new_service.ts @@ -13,26 +13,25 @@ import { GitLabProject } from './gitlab_project'; import { getRestIdFromGraphQLId } from '../utils/get_rest_id_from_graphql_id'; import { UserFriendlyError } from '../errors/user_friendly_error'; import { getMrPermissionsQuery, MrPermissionsQueryOptions } from './graphql/mr_permission'; -import { fragmentProjectDetails, GqlProject, noteDetailsFragment } from './graphql/shared'; +import { + fragmentProjectDetails, + GqlBasePosition, + GqlGenericNote, + GqlNote, + GqlProject, + GqlProjectResult, + Node, + noteDetailsFragment, +} from './graphql/shared'; import { GetProjectsOptions, GqlProjectsResult, queryGetProjects } from './graphql/get_projects'; import { getIssueDiscussionsQuery, getMrDiscussionsQuery, GetDiscussionsQueryOptions, + GqlDiscussion, + GetDiscussionsQueryResult, } from './graphql/get_discussions'; -interface Node { - pageInfo?: { - hasNextPage: boolean; - endCursor: string; - }; - nodes: T[]; -} - -interface GqlProjectResult { - project?: T; -} - interface GqlSnippetProject { id: string; snippets: Node; @@ -58,88 +57,6 @@ export interface GqlBlob { path: string; } -interface GqlUser { - avatarUrl: string | null; - name: string; - username: string; - webUrl: string; -} - -interface GqlBasePosition { - diffRefs: { - baseSha: string; - headSha: string; - }; - filePath: string; - newPath: string; - oldPath: string; -} - -interface GqlImagePosition extends GqlBasePosition { - positionType: 'image'; - newLine: null; - oldLine: null; -} - -interface GqlNewPosition extends GqlBasePosition { - positionType: 'text'; - newLine: number; - oldLine: null; -} -interface GqlOldPosition extends GqlBasePosition { - positionType: 'text'; - newLine: null; - oldLine: number; -} - -export type GqlTextPosition = GqlOldPosition | GqlNewPosition; - -interface GqlNotePermissions { - resolveNote: boolean; - adminNote: boolean; - createNote: boolean; -} - -interface GqlGenericNote { - id: string; - author: GqlUser; - createdAt: string; - system: boolean; - body: string; // TODO: remove this once the SystemNote.vue doesn't require plain text body - bodyHtml: string; - userPermissions: GqlNotePermissions; - position: T; -} - -interface GqlGenericDiscussion { - replyId: string; - createdAt: string; - resolved: boolean; - resolvable: boolean; - notes: Node; -} - -export type GqlTextDiffNote = GqlGenericNote; -type GqlImageNote = GqlGenericNote; -export type GqlOverviewNote = GqlGenericNote; -export type GqlNote = GqlTextDiffNote | GqlImageNote | GqlOverviewNote; - -export type GqlDiscussion = - | GqlGenericDiscussion - | GqlGenericDiscussion - | GqlGenericDiscussion; - -export type GqlTextDiffDiscussion = GqlGenericDiscussion; - -interface GqlDiscussionsProject { - mergeRequest?: { - discussions: Node; - }; - issue?: { - discussions: Node; - }; -} - interface RestLabelEvent { label: unknown; body: string; @@ -375,10 +292,7 @@ export class GitLabNewService { iid: String(issuable.iid), endCursor, }; - const result = await this.client.request>( - query, - options, - ); + const result = await this.client.request(query, options); assert(result.project, `Project ${projectPath} was not found.`); const discussions = result.project.issue?.discussions || result.project.mergeRequest?.discussions; diff --git a/src/gitlab/graphql/get_discussions.ts b/src/gitlab/graphql/get_discussions.ts index 257a594..3a2ac18 100644 --- a/src/gitlab/graphql/get_discussions.ts +++ b/src/gitlab/graphql/get_discussions.ts @@ -1,5 +1,13 @@ import { gql } from 'graphql-request'; -import { noteDetailsFragment } from './shared'; +import { + noteDetailsFragment, + Node, + GqlProjectResult, + GqlNote, + GqlTextDiffNote, + GqlOverviewNote, + GqlImageNote, +} from './shared'; const discussionsFragment = gql` ${noteDetailsFragment} @@ -59,3 +67,29 @@ export interface GetDiscussionsQueryOptions { iid: string; endCursor?: string; } + +interface GqlGenericDiscussion { + replyId: string; + createdAt: string; + resolved: boolean; + resolvable: boolean; + notes: Node; +} + +export type GqlTextDiffDiscussion = GqlGenericDiscussion; + +export type GqlDiscussion = + | GqlGenericDiscussion + | GqlGenericDiscussion + | GqlGenericDiscussion; + +interface GqlDiscussionsProject { + mergeRequest?: { + discussions: Node; + }; + issue?: { + discussions: Node; + }; +} + +export type GetDiscussionsQueryResult = GqlProjectResult; diff --git a/src/gitlab/graphql/shared.ts b/src/gitlab/graphql/shared.ts index f7e39ce..b0b0144 100644 --- a/src/gitlab/graphql/shared.ts +++ b/src/gitlab/graphql/shared.ts @@ -69,3 +69,73 @@ export interface GqlProject { webUrl: string; group?: GqlGroup; } + +export interface GqlProjectResult { + project?: T; +} + +export interface Node { + pageInfo?: { + hasNextPage: boolean; + endCursor: string; + }; + nodes: T[]; +} + +interface GqlUser { + avatarUrl: string | null; + name: string; + username: string; + webUrl: string; +} + +export interface GqlBasePosition { + diffRefs: { + baseSha: string; + headSha: string; + }; + filePath: string; + newPath: string; + oldPath: string; +} + +interface GqlImagePosition extends GqlBasePosition { + positionType: 'image'; + newLine: null; + oldLine: null; +} + +interface GqlNewPosition extends GqlBasePosition { + positionType: 'text'; + newLine: number; + oldLine: null; +} +interface GqlOldPosition extends GqlBasePosition { + positionType: 'text'; + newLine: null; + oldLine: number; +} + +export type GqlTextPosition = GqlOldPosition | GqlNewPosition; + +interface GqlNotePermissions { + resolveNote: boolean; + adminNote: boolean; + createNote: boolean; +} + +export interface GqlGenericNote { + id: string; + author: GqlUser; + createdAt: string; + system: boolean; + body: string; // TODO: remove this once the SystemNote.vue doesn't require plain text body + bodyHtml: string; + userPermissions: GqlNotePermissions; + position: T; +} + +export type GqlTextDiffNote = GqlGenericNote; +export type GqlImageNote = GqlGenericNote; +export type GqlOverviewNote = GqlGenericNote; +export type GqlNote = GqlTextDiffNote | GqlImageNote | GqlOverviewNote; diff --git a/src/review/gitlab_comment.test.ts b/src/review/gitlab_comment.test.ts index dfec84b..5a62722 100644 --- a/src/review/gitlab_comment.test.ts +++ b/src/review/gitlab_comment.test.ts @@ -1,7 +1,7 @@ import { GitLabComment } from './gitlab_comment'; import { GitLabCommentThread } from './gitlab_comment_thread'; import { noteOnDiff } from '../../test/integration/fixtures/graphql/discussions.js'; -import { GqlTextDiffNote } from '../gitlab/gitlab_new_service'; +import { GqlTextDiffNote } from '../gitlab/graphql/shared'; describe('GitLabComment', () => { let comment: GitLabComment; diff --git a/src/review/gitlab_comment.ts b/src/review/gitlab_comment.ts index 4a60413..2767a4f 100644 --- a/src/review/gitlab_comment.ts +++ b/src/review/gitlab_comment.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import { GqlTextDiffNote } from '../gitlab/gitlab_new_service'; +import { GqlTextDiffNote } from '../gitlab/graphql/shared'; import { GitLabCommentThread } from './gitlab_comment_thread'; interface CommentOptions { diff --git a/src/review/gitlab_comment_thread.test.ts b/src/review/gitlab_comment_thread.test.ts index 6ded44e..b67254b 100644 --- a/src/review/gitlab_comment_thread.test.ts +++ b/src/review/gitlab_comment_thread.test.ts @@ -5,12 +5,10 @@ import { noteOnDiff, } from '../../test/integration/fixtures/graphql/discussions.js'; import { GitLabComment } from './gitlab_comment'; -import { - GitLabNewService, - GqlTextDiffDiscussion, - GqlTextDiffNote, -} from '../gitlab/gitlab_new_service'; +import { GitLabNewService } from '../gitlab/gitlab_new_service'; import { mr } from '../test_utils/entities'; +import { GqlTextDiffNote } from '../gitlab/graphql/shared'; +import { GqlTextDiffDiscussion } from '../gitlab/graphql/get_discussions'; describe('GitLabCommentThread', () => { let gitlabCommentThread: GitLabCommentThread; diff --git a/src/review/gitlab_comment_thread.ts b/src/review/gitlab_comment_thread.ts index 6c0cb21..5d9da59 100644 --- a/src/review/gitlab_comment_thread.ts +++ b/src/review/gitlab_comment_thread.ts @@ -1,14 +1,10 @@ import * as vscode from 'vscode'; import * as assert from 'assert'; -import { - GitLabNewService, - GqlNote, - GqlTextDiffDiscussion, - GqlTextDiffNote, - GqlTextPosition, -} from '../gitlab/gitlab_new_service'; +import { GitLabNewService } from '../gitlab/gitlab_new_service'; import { GitLabComment } from './gitlab_comment'; import { toReviewUri } from './review_uri'; +import { GqlTextDiffDiscussion } from '../gitlab/graphql/get_discussions'; +import { GqlNote, GqlTextDiffNote, GqlTextPosition } from '../gitlab/graphql/shared'; const firstNoteFrom = (discussion: GqlTextDiffDiscussion): GqlTextDiffNote => { const note = discussion.notes.nodes[0]; -- GitLab