From d41218c9718e30b86fbcd02286327694f6fadbc9 Mon Sep 17 00:00:00 2001 From: Tomas Vik Date: Tue, 22 Jun 2021 09:50:04 +0200 Subject: [PATCH] refactor: introduce RestMr interface Credit: [@Musisimaru](https://gitlab.com/Musisimaru) (originally introduced in [!229](https://gitlab.com/gitlab-org/gitlab-vscode-extension/-/merge_requests/229)) --- src/gitlab/gitlab_new_service.ts | 2 +- src/types.d.ts | 5 ++++- src/utils/is_mr.ts | 2 ++ src/webview_controller.ts | 4 ++-- 4 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 src/utils/is_mr.ts diff --git a/src/gitlab/gitlab_new_service.ts b/src/gitlab/gitlab_new_service.ts index 87f4448..596e139 100644 --- a/src/gitlab/gitlab_new_service.ts +++ b/src/gitlab/gitlab_new_service.ts @@ -38,6 +38,7 @@ import { import { createDiffNoteMutation, GqlDiffPositionInput } from './graphql/create_diff_comment'; import { removeLeadingSlash } from '../utils/remove_leading_slash'; import { log, logError } from '../log'; +import { isMr } from '../utils/is_mr'; interface CreateNoteResult { createNote: { @@ -109,7 +110,6 @@ const updateNoteBodyMutation = gql` `; const getProjectPath = (issuable: RestIssuable) => issuable.references.full.split(/[#!]/)[0]; -const isMr = (issuable: RestIssuable) => Boolean(issuable.sha); const getIssuableGqlId = (issuable: RestIssuable) => `gid://gitlab/${isMr(issuable) ? 'MergeRequest' : 'Issue'}/${issuable.id}`; const getMrGqlId = (id: number) => `gid://gitlab/MergeRequest/${id}`; diff --git a/src/types.d.ts b/src/types.d.ts index 0f298b8..6f5848e 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -14,7 +14,6 @@ interface RestIssuable { project_id: number; web_url: string; author: { name: string; avatar_url: string | null }; - sha?: string; // only present in MR, legacy logic uses the presence to decide issuable type references: { full: string; // e.g. "gitlab-org/gitlab#219925" }; @@ -22,6 +21,10 @@ interface RestIssuable { name: string; } +interface RestMr extends RestIssuable { + sha: string; +} + interface RestMrVersion { head_commit_sha: string; base_commit_sha: string; diff --git a/src/utils/is_mr.ts b/src/utils/is_mr.ts new file mode 100644 index 0000000..290e7a5 --- /dev/null +++ b/src/utils/is_mr.ts @@ -0,0 +1,2 @@ +export const isMr = (issuable: RestIssuable): issuable is RestMr => + Boolean((issuable as RestMr).sha); diff --git a/src/webview_controller.ts b/src/webview_controller.ts index ee11cb2..5478cc7 100644 --- a/src/webview_controller.ts +++ b/src/webview_controller.ts @@ -7,6 +7,7 @@ import * as gitLabService from './gitlab_service'; import { createGitLabNewService } from './service_factory'; import { logError } from './log'; import { getInstanceUrl } from './utils/get_instance_url'; +import { isMr } from './utils/is_mr'; const webviewResourcePaths = { appScriptUri: 'src/webview/dist/js/app.js', @@ -140,8 +141,7 @@ class WebviewController { const lightMrIcon = getIconUri('light', 'merge_requests.svg'); const darkIssueIcon = getIconUri('dark', 'issues.svg'); const darkMrIcon = getIconUri('dark', 'merge_requests.svg'); - const isMr = issuable.sha !== undefined; - return isMr + return isMr(issuable) ? { light: lightMrIcon, dark: darkMrIcon } : { light: lightIssueIcon, dark: darkIssueIcon }; } -- GitLab