@@ -336,152 +336,166 @@ export default class IntegrationService {
336336 const transaction = await SequelizeRepository . createTransaction ( this . options )
337337
338338 try {
339- if ( EDITION === Edition . LFX ) {
340- for ( const id of ids ) {
341- let integration
342- try {
343- integration = await this . findById ( id )
339+ for ( const id of ids ) {
340+ let integration
341+ try {
342+ integration = await this . findById ( id )
344343
345- if ( integration . segmentId ) {
346- segmentId = integration . segmentId
347- }
348- } catch ( err ) {
349- throw new Error404 ( )
344+ if ( integration . segmentId ) {
345+ segmentId = integration . segmentId
350346 }
351- // remove github remotes from git integration
352- if (
347+ } catch ( err ) {
348+ throw new Error404 ( )
349+ }
350+ // remove github remotes from git integration
351+ if (
352+ integration . platform === PlatformType . GITHUB ||
353+ integration . platform === PlatformType . GITLAB ||
354+ integration . platform === PlatformType . GITHUB_NANGO
355+ ) {
356+ let shouldUpdateGit : boolean
357+ const mapping =
353358 integration . platform === PlatformType . GITHUB ||
354- integration . platform === PlatformType . GITLAB ||
355359 integration . platform === PlatformType . GITHUB_NANGO
356- ) {
357- let shouldUpdateGit : boolean
358- const mapping =
359- integration . platform === PlatformType . GITHUB ||
360- integration . platform === PlatformType . GITHUB_NANGO
361- ? await this . getGithubRepos ( id )
362- : await this . getGitlabRepos ( id )
363-
364- const repos : Record < string , string [ ] > = mapping . reduce ( ( acc , { url, segment } ) => {
365- if ( ! acc [ segment . id ] ) {
366- acc [ segment . id ] = [ ]
367- }
368- acc [ segment . id ] . push ( url )
369- return acc
370- } , { } )
360+ ? await this . getGithubRepos ( id )
361+ : await this . getGitlabRepos ( id )
371362
372- for ( const [ segmentId , urls ] of Object . entries ( repos ) ) {
373- urls . forEach ( ( url ) => toRemoveRepo . add ( url ) )
363+ const repos : Record < string , string [ ] > = mapping . reduce ( ( acc , { url, segment } ) => {
364+ if ( ! acc [ segment . id ] ) {
365+ acc [ segment . id ] = [ ]
366+ }
367+ acc [ segment . id ] . push ( url )
368+ return acc
369+ } , { } )
374370
375- const segmentOptions : IRepositoryOptions = {
376- ...this . options ,
377- currentSegments : [
378- {
379- ...this . options . currentSegments [ 0 ] ,
380- id : segmentId as string ,
381- } ,
382- ] ,
383- }
371+ for ( const [ segmentId , urls ] of Object . entries ( repos ) ) {
372+ urls . forEach ( ( url ) => toRemoveRepo . add ( url ) )
384373
385- try {
386- await IntegrationRepository . findByPlatform ( PlatformType . GIT , segmentOptions )
387- shouldUpdateGit = true
388- } catch ( err ) {
389- shouldUpdateGit = false
390- }
374+ const segmentOptions : IRepositoryOptions = {
375+ ...this . options ,
376+ currentSegments : [
377+ {
378+ ...this . options . currentSegments [ 0 ] ,
379+ id : segmentId as string ,
380+ } ,
381+ ] ,
382+ }
391383
392- if ( shouldUpdateGit ) {
393- const gitInfo = await this . gitGetRemotes ( segmentOptions )
394- const gitRemotes = gitInfo [ segmentId ] . remotes
395- await this . gitConnectOrUpdate (
396- {
397- remotes : gitRemotes . filter ( ( remote ) => ! urls . includes ( remote ) ) ,
398- } ,
399- segmentOptions ,
400- )
401- }
384+ try {
385+ await IntegrationRepository . findByPlatform ( PlatformType . GIT , segmentOptions )
386+ shouldUpdateGit = true
387+ } catch ( err ) {
388+ shouldUpdateGit = false
402389 }
403390
404- if (
405- integration . platform === PlatformType . GITHUB ||
406- integration . platform === PlatformType . GITHUB_NANGO
407- ) {
408- // soft delete github repos
409- await GithubReposRepository . delete ( integration . id , {
410- ...this . options ,
411- transaction,
412- } )
391+ if ( shouldUpdateGit ) {
392+ const gitInfo = await this . gitGetRemotes ( segmentOptions )
393+ const gitRemotes = gitInfo [ segmentId ] . remotes
394+ const remainingRemotes = gitRemotes . filter ( ( remote ) => ! urls . includes ( remote ) )
413395
414- // Also soft delete from git.repositories for git-integration V2
415- try {
416- // Find the Git integration ID for this segment
396+ if ( remainingRemotes . length === 0 ) {
397+ // If no remotes left, delete the Git integration entirely
417398 const gitIntegration = await IntegrationRepository . findByPlatform (
418399 PlatformType . GIT ,
400+ segmentOptions ,
401+ )
402+
403+ // Soft delete git.repositories for git-integration V2
404+ await GitReposRepository . delete ( gitIntegration . id , {
405+ ...this . options ,
406+ transaction,
407+ } )
408+
409+ // Then delete the git integration
410+ await IntegrationRepository . destroy ( gitIntegration . id , {
411+ ...this . options ,
412+ transaction,
413+ } )
414+ } else {
415+ // Update with remaining remotes
416+ await this . gitConnectOrUpdate (
419417 {
420- ...this . options ,
421- currentSegments : [ { id : integration . segmentId } as any ] ,
422- transaction,
418+ remotes : remainingRemotes ,
423419 } ,
424- )
425- if ( gitIntegration ) {
426- await GitReposRepository . delete ( gitIntegration . id , {
427- ...this . options ,
428- transaction,
429- } )
430- }
431- } catch ( err ) {
432- this . options . log . info (
433- 'No Git integration found for segment, skipping git.repositories cleanup' ,
420+ segmentOptions ,
434421 )
435422 }
436423 }
437424 }
438425
439- if ( integration . platform === PlatformType . GITLAB ) {
440- if ( integration . settings . webhooks ) {
441- await removeGitlabWebhooks (
442- integration . token ,
443- integration . settings . webhooks . map ( ( hook ) => hook . projectId ) ,
444- integration . settings . webhooks . map ( ( hook ) => hook . hookId ) ,
445- )
446- }
447-
448- // soft delete gitlab repos
449- await GitlabReposRepository . delete ( integration . id , {
426+ if (
427+ integration . platform === PlatformType . GITHUB ||
428+ integration . platform === PlatformType . GITHUB_NANGO
429+ ) {
430+ // soft delete github repos
431+ await GithubReposRepository . delete ( integration . id , {
450432 ...this . options ,
451433 transaction,
452434 } )
453- }
454435
455- await IntegrationRepository . destroy ( id , {
456- ...this . options ,
457- transaction,
458- } )
436+ // Also soft delete from git.repositories for git-integration V2
437+ try {
438+ // Find the Git integration ID for this segment
439+ const gitIntegration = await IntegrationRepository . findByPlatform ( PlatformType . GIT , {
440+ ...this . options ,
441+ currentSegments : [ { id : integration . segmentId } as any ] ,
442+ transaction,
443+ } )
444+ if ( gitIntegration ) {
445+ await GitReposRepository . delete ( gitIntegration . id , {
446+ ...this . options ,
447+ transaction,
448+ } )
449+ }
450+ } catch ( err ) {
451+ this . options . log . info (
452+ 'No Git integration found for segment, skipping git.repositories cleanup' ,
453+ )
454+ }
455+ }
459456 }
460- } else {
461- for ( const id of ids ) {
462- await IntegrationRepository . destroy ( id , {
457+
458+ if ( integration . platform === PlatformType . GITLAB ) {
459+ if ( integration . settings . webhooks ) {
460+ await removeGitlabWebhooks (
461+ integration . token ,
462+ integration . settings . webhooks . map ( ( hook ) => hook . projectId ) ,
463+ integration . settings . webhooks . map ( ( hook ) => hook . hookId ) ,
464+ )
465+ }
466+
467+ // soft delete gitlab repos
468+ await GitlabReposRepository . delete ( integration . id , {
463469 ...this . options ,
464470 transaction,
465471 } )
466472 }
473+
474+ await IntegrationRepository . destroy ( id , {
475+ ...this . options ,
476+ transaction,
477+ } )
467478 }
468479
469480 const collectionService = new CollectionService ( { ...this . options , transaction } )
470481
471482 const qx = SequelizeRepository . getQueryExecutor ( this . options )
472483
473- const [ insightsProject ] = await collectionService . findInsightsProjectsBySegmentId ( segmentId )
484+ let insightsProject = null
485+ let widgets = [ ]
474486
475- const { widgets } = await collectionService . findSegmentsWidgetsById ( segmentId )
487+ if ( segmentId ) {
488+ const [ project ] = await collectionService . findInsightsProjectsBySegmentId ( segmentId )
489+ insightsProject = project
490+ const widgetsResult = await collectionService . findSegmentsWidgetsById ( segmentId )
491+ widgets = widgetsResult . widgets
492+ await deleteSegmentRepositories ( qx , {
493+ segmentId,
494+ } )
495+ }
476496
477497 const insightsRepo = insightsProject ?. repositories ?? [ ]
478-
479- await deleteSegmentRepositories ( qx , {
480- segmentId,
481- } )
482-
483498 const filteredRepos = insightsRepo . filter ( ( repo ) => ! toRemoveRepo . has ( repo ) )
484-
485499 // remove duplicates
486500 const repositories = [ ...new Set < string > ( filteredRepos ) ]
487501
0 commit comments