@@ -4,8 +4,7 @@ import { request } from '@octokit/request'
44import axios , { AxiosRequestConfig , AxiosResponse } from 'axios'
55import lodash from 'lodash'
66import moment from 'moment'
7- import { QueryTypes , Transaction } from 'sequelize'
8- import { v4 as uuidv4 } from 'uuid'
7+ import { Transaction } from 'sequelize'
98
109import { EDITION , Error400 , Error404 , Error542 } from '@crowd/common'
1110import { getGithubInstallationToken } from '@crowd/common_services'
@@ -15,6 +14,7 @@ import {
1514 deleteSegmentRepositories ,
1615 upsertSegmentRepositories ,
1716} from '@crowd/data-access-layer/src/collections'
17+ import { syncRepositoriesToGitV2 } from '@crowd/data-access-layer/src/integrations'
1818import {
1919 NangoIntegration ,
2020 connectNangoIntegration ,
@@ -908,7 +908,7 @@ export default class IntegrationService {
908908 args : [ { integrationIds : [ integration . id ] } ] ,
909909 } )
910910
911- return await txService . findById ( integrationId )
911+ return await this . findById ( integration . id )
912912 } catch ( err ) {
913913 this . options . log . error ( err , 'Error while creating or updating GitHub integration!' )
914914 if ( ! existingTransaction ) {
@@ -1340,11 +1340,15 @@ export default class IntegrationService {
13401340 )
13411341
13421342 // upsert repositories to git.repositories in order to be processed by git-integration V2
1343- await this . syncRepositoriesToGitV2 (
1344- remotes ,
1345- options || this . options ,
1343+ const qx = SequelizeRepository . getQueryExecutor ( {
1344+ ...( options || this . options ) ,
13461345 transaction,
1346+ } )
1347+ await syncRepositoriesToGitV2 (
1348+ qx ,
1349+ remotes ,
13471350 integration . id ,
1351+ ( options || this . options ) . currentSegments [ 0 ] . id ,
13481352 )
13491353
13501354 // Only commit if we created the transaction ourselves
@@ -1362,95 +1366,6 @@ export default class IntegrationService {
13621366 return integration
13631367 }
13641368
1365- /**
1366- * Syncs repositories to git.repositories table (git-integration V2)
1367- * @param remotes Array of repository objects with url and optional forkedFrom
1368- * @param options Repository options
1369- * @param transaction Database transaction
1370- * @param integrationId The integration ID from the git integration
1371- * @param inheritFromExistingRepos If true, queries githubRepos and gitlabRepos for IDs; if false, generates new UUIDs
1372- *
1373- * TODO: @Mouad After migration is complete, simplify this function by:
1374- * 1. Using an object parameter instead of multiple parameters for better maintainability
1375- * 2. Removing the inheritFromExistingRepos parameter since git.repositories will be the source of truth
1376- * 3. Simplifying the logic to only handle git.repositories operations
1377- */
1378- private async syncRepositoriesToGitV2 (
1379- remotes : Array < { url : string ; forkedFrom ?: string | null } > ,
1380- options : IRepositoryOptions ,
1381- transaction : Transaction ,
1382- integrationId : string ,
1383- ) {
1384- const seq = SequelizeRepository . getSequelize ( options )
1385-
1386- let repositoriesToSync : Array < {
1387- id : string
1388- url : string
1389- integrationId : string
1390- segmentId : string
1391- forkedFrom ?: string | null
1392- } > = [ ]
1393- // check GitHub repos first, fallback to GitLab repos if none found
1394- const existingRepos : Array < {
1395- id : string
1396- url : string
1397- } > = await seq . query (
1398- `
1399- WITH github_repos AS (
1400- SELECT id, url FROM "githubRepos"
1401- WHERE url IN (:urls) AND "deletedAt" IS NULL
1402- ),
1403- gitlab_repos AS (
1404- SELECT id, url FROM "gitlabRepos"
1405- WHERE url IN (:urls) AND "deletedAt" IS NULL
1406- )
1407- SELECT id, url FROM github_repos
1408- UNION ALL
1409- SELECT id, url FROM gitlab_repos
1410- WHERE NOT EXISTS (SELECT 1 FROM github_repos)
1411- ` ,
1412- {
1413- replacements : {
1414- urls : remotes . map ( ( r ) => r . url ) ,
1415- } ,
1416- type : QueryTypes . SELECT ,
1417- transaction,
1418- } ,
1419- )
1420-
1421- // Create a map of url to forkedFrom for quick lookup
1422- const forkedFromMap = new Map ( remotes . map ( ( r ) => [ r . url , r . forkedFrom ] ) )
1423-
1424- repositoriesToSync = existingRepos . map ( ( repo ) => ( {
1425- id : repo . id ,
1426- url : repo . url ,
1427- integrationId,
1428- segmentId : options . currentSegments [ 0 ] . id ,
1429- forkedFrom : forkedFromMap . get ( repo . url ) || null ,
1430- } ) )
1431-
1432- if ( repositoriesToSync . length === 0 ) {
1433- this . options . log . warn (
1434- 'No existing repos found in githubRepos or gitlabRepos - inserting new to git.repositories with new uuid' ,
1435- )
1436- repositoriesToSync = remotes . map ( ( remote ) => ( {
1437- id : uuidv4 ( ) , // Generate new UUID
1438- url : remote . url ,
1439- integrationId,
1440- segmentId : options . currentSegments [ 0 ] . id ,
1441- forkedFrom : remote . forkedFrom || null ,
1442- } ) )
1443- }
1444-
1445- // Sync to git.repositories v2
1446- await GitReposRepository . upsert ( repositoriesToSync , {
1447- ...options ,
1448- transaction,
1449- } )
1450-
1451- this . options . log . info ( `Synced ${ repositoriesToSync . length } repos to git v2` )
1452- }
1453-
14541369 async atlassianAdminConnect ( adminApi : string , organizationId : string ) {
14551370 const nangoPayload = {
14561371 params : {
0 commit comments