diff --git a/src/gitlab/gitlab_new_service.ts b/src/gitlab/gitlab_new_service.ts index 87f44485db020f9c962e79ff8fce460c59a80eb5..596e139e93439e8a34ab3daa55d8d2256b415305 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 0f298b81532bdbb6de668c816beaf6704c04ce36..6f5848ea405c987be0dbe4a3d55ba8b217fe9cf8 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 0000000000000000000000000000000000000000..290e7a5191e5ea050761a81da0e0e32df25bb94f --- /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 ee11cb2a55516c49f40972dc08f6937db7eeb015..5478cc7f9107eb5ae662e6a64dd0ad6e898f3b08 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 }; }