@@ -20,6 +20,7 @@ import type { Config } from '@backstage/config';
2020import gitUrlParse from 'git-url-parse' ;
2121
2222import { CatalogHttpClient } from '../../../catalog/catalogHttpClient' ;
23+ import { getCatalogUrl } from '../../../catalog/catalogUtils' ;
2324import type { Components } from '../../../generated/openapi' ;
2425import type {
2526 GithubApiService ,
@@ -64,19 +65,35 @@ export async function findAllRepositories(
6465 deps . gitApiService . getRepositoriesFromIntegrations ( search , pageSize ) ,
6566 ] ) ;
6667
67- const alreadyImportedRepositoriesLocationTargets = Array . from (
68- new Set ( alreadyImportedRepositories . uniqueCatalogUrlLocations . keys ( ) ) ,
68+ const alreadyImportedRepositoriesLocationTargets = new Set (
69+ alreadyImportedRepositories . uniqueCatalogUrlLocations . keys ( ) ,
6970 ) ;
7071
7172 const { repositories : allRepositories , errors } = allRepositoriesResponse ;
7273
7374 const notImportedYetRepositories = allRepositories . filter ( repo => {
74- const html_urlWithSlash = repo . html_url . concat ( '/' ) ;
75-
76- const alreadyImported = alreadyImportedRepositoriesLocationTargets . some (
77- target => target . startsWith ( html_urlWithSlash ) ,
75+ const catalogUrl = getCatalogUrl (
76+ deps . config ,
77+ repo . html_url . replace ( / \/ $ / , '' ) ,
78+ repo . default_branch ,
7879 ) ;
7980
81+ let alreadyImported =
82+ alreadyImportedRepositoriesLocationTargets . has ( catalogUrl ) ;
83+
84+ if ( ! alreadyImported ) {
85+ // Workaround: when a GitHub repository is imported via Backstage, the
86+ // resulting registered catalog location may use a '/tree/' URL for the
87+ // target instead of the '/blob/' URL format returned by getCatalogUrl.
88+ // To correctly detect already-imported repositories regardless of which
89+ // format was persisted, the '/tree/' variant is also checked here.
90+ // This branch can be removed once catalog locations are consistently
91+ // stored using the same '/blob/' format as getCatalogUrl returns.
92+ alreadyImported = alreadyImportedRepositoriesLocationTargets . has (
93+ catalogUrl . replace ( '/blob/' , '/tree/' ) ,
94+ ) ;
95+ }
96+
8097 return ! alreadyImported ;
8198 } ) ;
8299
0 commit comments