Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -58,8 +59,29 @@ export function computeTotalCount<T>(
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';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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';
}),
);

Expand Down Expand Up @@ -245,6 +246,7 @@ export async function findAllImports(

async function resolveReposDefaultBranches(
logger: LoggerService,
config: Config,
gitlabApiService: GitlabApiService,
githubApiService: GithubApiService,
allLocations: Iterable<string>,
Expand All @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ export async function createRouter(
logger,
config,
gitApiService:
parseGitURLForApprovalTool(q.repo) === 'GITLAB'
parseGitURLForApprovalTool(q.repo, config) === 'GITLAB'
? gitlabApiService
: githubApiService,
catalogHttpClient,
Expand Down