diff --git a/workspaces/bulk-import/plugins/bulk-import-backend/src/helpers/utils.ts b/workspaces/bulk-import/plugins/bulk-import-backend/src/helpers/utils.ts index 05990c37a6..a255b6a5ef 100644 --- a/workspaces/bulk-import/plugins/bulk-import-backend/src/helpers/utils.ts +++ b/workspaces/bulk-import/plugins/bulk-import-backend/src/helpers/utils.ts @@ -15,6 +15,7 @@ */ import { parseEntityRef, stringifyEntityRef } from '@backstage/catalog-model'; +import { Config } from '@backstage/config'; import { InputError } from '@backstage/errors'; import gitUrlParse from 'git-url-parse'; @@ -58,8 +59,29 @@ export function computeTotalCount( return totalCount; } -export function parseGitURLForApprovalTool(repoUrl: string) { +export function parseGitURLForApprovalTool( + repoUrl: string, + config: Config, +): 'GIT' | 'GITLAB' { const parsedRepoUrl = new URL(repoUrl); + + const gitlabConfigs = config.getOptionalConfigArray('integrations.gitlab'); + if (gitlabConfigs) { + const gitlabHosts = gitlabConfigs.map(c => c.getString('host')); + if (gitlabHosts.includes(parsedRepoUrl.hostname)) { + return 'GITLAB'; + } + } + + const githubConfigs = config.getOptionalConfigArray('integrations.github'); + if (githubConfigs) { + const githubHosts = githubConfigs.map(c => c.getString('host')); + if (githubHosts.includes(parsedRepoUrl.hostname)) { + return 'GIT'; + } + } + + // Fallback for backward compatibility if integrations are not configured for the host if (parsedRepoUrl.hostname.includes('gitlab')) { return 'GITLAB'; } diff --git a/workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/import/bulkImports.ts b/workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/import/bulkImports.ts index 5e7957512c..28ee7efa38 100644 --- a/workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/import/bulkImports.ts +++ b/workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/import/bulkImports.ts @@ -131,6 +131,7 @@ export async function findAllImports( // It can be 'main' or something more convoluted like 'our/awesome/main'. const defaultBranchByRepoUrl = await resolveReposDefaultBranches( deps.logger, + deps.config, deps.gitlabApiService, deps.githubApiService, allLocations.keys(), @@ -149,14 +150,14 @@ export async function findAllImports( const importsReachableFromGHIntegrations = await deps.githubApiService.filterLocationsAccessibleFromIntegrations( importCandidates.filter(val => { - return parseGitURLForApprovalTool(val) === 'GIT'; + return parseGitURLForApprovalTool(val, deps.config) === 'GIT'; }), ); const importsReachableFromGLIntegrations = await deps.gitlabApiService.filterLocationsAccessibleFromIntegrations( importCandidates.filter(val => { - return parseGitURLForApprovalTool(val) === 'GITLAB'; + return parseGitURLForApprovalTool(val, deps.config) === 'GITLAB'; }), ); @@ -245,6 +246,7 @@ export async function findAllImports( async function resolveReposDefaultBranches( logger: LoggerService, + config: Config, gitlabApiService: GitlabApiService, githubApiService: GithubApiService, allLocations: Iterable, @@ -271,7 +273,7 @@ async function resolveReposDefaultBranches( // Have to do this for Both the gitlab/github, possibly a better way? // Should probably parse the URL to figure out which to use? defaultBranchByRepoUrlPromises.push( - (parseGitURLForApprovalTool(repoUrl) === 'GITLAB' + (parseGitURLForApprovalTool(repoUrl, config) === 'GITLAB' ? gitlabApiService : githubApiService ) diff --git a/workspaces/bulk-import/plugins/bulk-import-backend/src/service/router.ts b/workspaces/bulk-import/plugins/bulk-import-backend/src/service/router.ts index 3d1edb5cdb..c9cc0ee020 100644 --- a/workspaces/bulk-import/plugins/bulk-import-backend/src/service/router.ts +++ b/workspaces/bulk-import/plugins/bulk-import-backend/src/service/router.ts @@ -518,7 +518,7 @@ export async function createRouter( logger, config, gitApiService: - parseGitURLForApprovalTool(q.repo) === 'GITLAB' + parseGitURLForApprovalTool(q.repo, config) === 'GITLAB' ? gitlabApiService : githubApiService, catalogHttpClient,