diff --git a/src/platforms/GitHub/API.ts b/src/platforms/GitHub/API.ts index e5e0272..0a15427 100644 --- a/src/platforms/GitHub/API.ts +++ b/src/platforms/GitHub/API.ts @@ -1,4 +1,5 @@ import { errors } from 'platforms' +import { isEnterprise } from '.' function apiRateLimitExceeded(content: any /* safe any */) { return content?.['documentation_url'] === 'https://developer.github.com/v3/#rate-limiting' @@ -55,12 +56,14 @@ async function request( } } +const API_ENDPOINT = isEnterprise() ? `${window.location.host}/api/v3` : 'api.github.com' + export async function getRepoMeta( userName: string, repoName: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}` return await request(url, { accessToken }) } @@ -70,7 +73,7 @@ export async function getTreeData( branchName: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/trees/${branchName}?recursive=1` return await request(url, { accessToken }) } @@ -80,7 +83,7 @@ export async function getBlobData( sha: string, accessToken?: string, ): Promise { - const url = `https://api.github.com/repos/${userName}/${repoName}/git/blobs/${sha}` + const url = `https://${API_ENDPOINT}/repos/${userName}/${repoName}/git/blobs/${sha}` return await request(url, { accessToken }) } diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 9f88a7a..08b00a7 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -76,10 +76,15 @@ function getUrlForRedirect( type = 'blob', path = '', ) { - return `https://github.com/${userName}/${repoName}/${type}/${branchName}/${path}` + return `https://${window.location.host}/${userName}/${repoName}/${type}/${branchName}/${path}` +} + +export function isEnterprise() { + return !window.location.host.endsWith('github.com') } export const GitHub: Platform = { + isEnterprise, resolveMeta() { if (!URLHelper.isInRepoPage()) { return null diff --git a/src/platforms/Gitee/index.ts b/src/platforms/Gitee/index.ts index cb2916c..68075ba 100644 --- a/src/platforms/Gitee/index.ts +++ b/src/platforms/Gitee/index.ts @@ -78,6 +78,9 @@ function getUrlForRedirect( } export const Gitee: Platform = { + isEnterprise() { + return !window.location.host.endsWith('gitee.com') + }, resolveMeta() { if (!DOMHelper.isInRepoPage()) { return null diff --git a/src/platforms/dummyPlatformForTypeSafety.ts b/src/platforms/dummyPlatformForTypeSafety.ts index 9852e99..8f40194 100644 --- a/src/platforms/dummyPlatformForTypeSafety.ts +++ b/src/platforms/dummyPlatformForTypeSafety.ts @@ -1,4 +1,7 @@ export const dummyPlatformForTypeSafety: Platform = { + isEnterprise() { + return false + }, resolveMeta() { return null }, diff --git a/src/platforms/platform.d.ts b/src/platforms/platform.d.ts index 0746e30..cbf60cc 100644 --- a/src/platforms/platform.d.ts +++ b/src/platforms/platform.d.ts @@ -1,4 +1,5 @@ type Platform = { + isEnterprise(): boolean resolveMeta(): MetaData | null getMetaData( metaData: Pick, diff --git a/src/utils/gitSubmodule.ts b/src/utils/gitSubmodule.ts index b1fa06d..be78173 100644 --- a/src/utils/gitSubmodule.ts +++ b/src/utils/gitSubmodule.ts @@ -11,7 +11,7 @@ function transformModuleGitURL(node: TreeNode, URL: string) { const matched = URL.match(subModuleURLRegex.git) if (!matched) return const [_, userName, repoName] = matched - return appendCommitPath(`https://github.com/${userName}/${repoName}`, node) + return appendCommitPath(`https://${window.location.host}/${userName}/${repoName}`, node) } function cutDotGit(URL: string) {