diff --git a/plugins/techdocs-backend/src/default-branch.ts b/plugins/techdocs-backend/src/default-branch.ts index 2997a2acbf..21de004878 100644 --- a/plugins/techdocs-backend/src/default-branch.ts +++ b/plugins/techdocs-backend/src/default-branch.ts @@ -51,8 +51,14 @@ interface IGitlabBranch { function getGithubApiUrl(config: Config, url: string): URL { const { protocol, owner, name } = parseGitUrl(url); + const providerConfigs = + config.getOptionalConfigArray('integrations.github') ?? []; + + // TODO: Maybe we need to filter by host in the array, not sure about GHE + const targetProviderConfig = providerConfigs[0]; + const apiBaseUrl = - config.getOptionalString('integrations.github.apiBaseUrl') || + targetProviderConfig?.getOptionalString('integrations.github.apiBaseUrl') ?? 'api.github.com'; const apiRepos = 'repos'; diff --git a/plugins/techdocs-backend/src/helpers.ts b/plugins/techdocs-backend/src/helpers.ts index 4a21800b07..4cbf46516f 100644 --- a/plugins/techdocs-backend/src/helpers.ts +++ b/plugins/techdocs-backend/src/helpers.ts @@ -20,7 +20,7 @@ import parseGitUrl from 'git-url-parse'; import NodeGit, { Clone, Repository } from 'nodegit'; import fs from 'fs-extra'; import { getDefaultBranch } from './default-branch'; -import { getTokenForGitRepo } from './git-auth'; +import { getGitRepoType, getTokenForGitRepo } from './git-auth'; import { Entity } from '@backstage/catalog-model'; import { InputError } from '@backstage/backend-common'; import { RemoteProtocol } from './techdocs/stages/prepare/types'; @@ -121,14 +121,6 @@ export const checkoutGitRepository = async ( ): Promise => { const parsedGitLocation = parseGitUrl(repoUrl); const repositoryTmpPath = await getGitRepositoryTempFolder(repoUrl); - - // TODO: Should propably not be hardcoded names of env variables, but seems too hard to access config down here - const user = - process.env.GITHUB_PRIVATE_TOKEN_USER || - process.env.GITLAB_PRIVATE_TOKEN_USER || - process.env.AZURE_PRIVATE_TOKEN_USER || - ''; - const token = await getTokenForGitRepo(repoUrl); if (fs.existsSync(repositoryTmpPath)) { @@ -152,7 +144,9 @@ export const checkoutGitRepository = async ( } if (token) { - parsedGitLocation.token = `${user}:${token}`; + const type = getGitRepoType(repoUrl); + const auth = type === 'github' ? `${token}:x-oauth-basic` : `:${token}`; + parsedGitLocation.token = auth; } const repositoryCheckoutUrl = parsedGitLocation.toString('https');