Skip to content

Commit ba0e1f3

Browse files
authored
fix: add support for gitlabRepos in git v2 migration [CM-696]
1 parent a79957d commit ba0e1f3

1 file changed

Lines changed: 29 additions & 16 deletions

File tree

backend/src/services/integrationService.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ export default class IntegrationService {
12881288
options || this.options,
12891289
transaction,
12901290
integration.id,
1291-
true, // inheritFromGithubRepos = true during migration until all repos are migrated then git.repositories can be used as source of truth instead of githubRepos
1291+
// inheritFromExistingRepos defaults to true during migration until all repos are migrated then git.repositories can be used as source of truth instead of existing repo tables
12921292
)
12931293

12941294
await SequelizeRepository.commitTransaction(transaction)
@@ -1306,19 +1306,19 @@ export default class IntegrationService {
13061306
* @param options Repository options
13071307
* @param transaction Database transaction
13081308
* @param integrationId The integration ID from the git integration
1309-
* @param inheritFromGithubRepos If true, queries githubRepos for IDs; if false, generates new UUIDs
1309+
* @param inheritFromExistingRepos If true, queries githubRepos and gitlabRepos for IDs; if false, generates new UUIDs
13101310
*
13111311
* TODO: @Mouad After migration is complete, simplify this function by:
13121312
* 1. Using an object parameter instead of multiple parameters for better maintainability
1313-
* 2. Removing the inheritFromGithubRepos parameter since git.repositories will be the source of truth
1313+
* 2. Removing the inheritFromExistingRepos parameter since git.repositories will be the source of truth
13141314
* 3. Simplifying the logic to only handle git.repositories operations
13151315
*/
13161316
private async syncRepositoriesToGitV2(
13171317
remotes: string[],
13181318
options: IRepositoryOptions,
13191319
transaction: Transaction,
13201320
integrationId: string,
1321-
inheritFromGithubRepos: boolean,
1321+
inheritFromExistingRepos: boolean = true,
13221322
) {
13231323
const seq = SequelizeRepository.getSequelize(options)
13241324

@@ -1329,14 +1329,25 @@ export default class IntegrationService {
13291329
segmentId: string
13301330
}> = []
13311331

1332-
if (inheritFromGithubRepos) {
1333-
// Query from githubRepos records to inherit their IDs for smoother migration into git-integration V2
1334-
const githubRepos = await seq.query(
1332+
if (inheritFromExistingRepos) {
1333+
// check GitHub repos first, fallback to GitLab repos if none found
1334+
const existingRepos: Array<{
1335+
id: string
1336+
url: string
1337+
}> = await seq.query(
13351338
`
1336-
SELECT id, url
1337-
FROM "githubRepos"
1338-
WHERE url IN (:urls)
1339-
AND "deletedAt" IS NULL
1339+
WITH github_repos AS (
1340+
SELECT id, url FROM "githubRepos"
1341+
WHERE url IN (:urls) AND "deletedAt" IS NULL
1342+
),
1343+
gitlab_repos AS (
1344+
SELECT id, url FROM "gitlabRepos"
1345+
WHERE url IN (:urls) AND "deletedAt" IS NULL
1346+
)
1347+
SELECT id, url FROM github_repos
1348+
UNION ALL
1349+
SELECT id, url FROM gitlab_repos
1350+
WHERE NOT EXISTS (SELECT 1 FROM github_repos)
13401351
`,
13411352
{
13421353
replacements: {
@@ -1347,15 +1358,17 @@ export default class IntegrationService {
13471358
},
13481359
)
13491360

1350-
repositoriesToSync = (githubRepos as any[]).map((repo) => ({
1361+
repositoriesToSync = existingRepos.map((repo) => ({
13511362
id: repo.id,
13521363
url: repo.url,
13531364
integrationId,
13541365
segmentId: options.currentSegments[0].id,
13551366
}))
13561367

13571368
if (repositoriesToSync.length === 0) {
1358-
this.options.log.warn('No githubRepos found - skipping repository sync to git v2')
1369+
this.options.log.warn(
1370+
'No existing repos found in githubRepos or gitlabRepos - skipping repository sync to git v2',
1371+
)
13591372
return
13601373
}
13611374
} else {
@@ -2467,7 +2480,7 @@ export default class IntegrationService {
24672480
for (const [segmentId, urls] of Object.entries(repos)) {
24682481
let isGitintegrationConfigured
24692482
const segmentOptions: IRepositoryOptions = {
2470-
...this.options,
2483+
...txOptions,
24712484
currentSegments: [
24722485
{
24732486
...this.options.currentSegments[0],
@@ -2490,14 +2503,14 @@ export default class IntegrationService {
24902503
{
24912504
remotes: Array.from(new Set([...gitRemotes, ...urls])),
24922505
},
2493-
segmentOptions,
2506+
{ ...segmentOptions, transaction },
24942507
)
24952508
} else {
24962509
await this.gitConnectOrUpdate(
24972510
{
24982511
remotes: urls,
24992512
},
2500-
segmentOptions,
2513+
{ ...segmentOptions, transaction },
25012514
)
25022515
}
25032516
}

0 commit comments

Comments
 (0)