Skip to content

Commit b5718a0

Browse files
committed
fix(bulk-import): improve repository import logic to handle catalog URL instead of substrings
Signed-off-by: Dominik Augustín <daugusti@redhat.com>
1 parent f416bd1 commit b5718a0

1 file changed

Lines changed: 23 additions & 6 deletions

File tree

  • workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/repository

workspaces/bulk-import/plugins/bulk-import-backend/src/service/handlers/repository/repositories.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import type { Config } from '@backstage/config';
2020
import gitUrlParse from 'git-url-parse';
2121

2222
import { CatalogHttpClient } from '../../../catalog/catalogHttpClient';
23+
import { getCatalogUrl } from '../../../catalog/catalogUtils';
2324
import type { Components } from '../../../generated/openapi';
2425
import 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

Comments
 (0)