@@ -25,11 +25,7 @@ import {
2525 updateCollection ,
2626 updateInsightsProject ,
2727} from '@crowd/data-access-layer/src/collections'
28- import {
29- fetchIntegrationById ,
30- fetchIntegrationsForSegment ,
31- removePlainGitHubRepoMapping ,
32- } from '@crowd/data-access-layer/src/integrations'
28+ import { fetchIntegrationsForSegment } from '@crowd/data-access-layer/src/integrations'
3329import { QueryFilter } from '@crowd/data-access-layer/src/query'
3430import {
3531 ICreateRepositoryGroup ,
@@ -492,89 +488,6 @@ export class CollectionService extends LoggerBase {
492488 return listRepositoryGroups ( qx , { insightsProjectId } )
493489 }
494490
495- async findRepositoriesForSegment ( segmentId : string ) {
496- return SequelizeRepository . withTx ( this . options , async ( tx ) => {
497- const qx = SequelizeRepository . getQueryExecutor ( { ...this . options , transaction : tx } )
498- const integrations = await fetchIntegrationsForSegment ( qx , segmentId )
499-
500- // Initialize result with platform arrays
501- const result : Record < string , Array < { url : string ; label : string } > > = {
502- git : [ ] ,
503- github : [ ] ,
504- gitlab : [ ] ,
505- gerrit : [ ] ,
506- }
507-
508- const addToResult = ( platform : PlatformType , fullUrl : string , label : string ) => {
509- const platformKey = platform . toLowerCase ( )
510- if ( ! result [ platformKey ] . some ( ( item ) => item . url === fullUrl ) ) {
511- result [ platformKey ] . push ( { url : fullUrl , label } )
512- }
513- }
514-
515- // Add mapped repositories to GitHub platform
516- const segmentRepository = new SegmentRepository ( { ...this . options , transaction : tx } )
517- const githubMappedRepos = await segmentRepository . getGithubMappedRepos ( segmentId )
518- const gitlabMappedRepos = await segmentRepository . getGitlabMappedRepos ( segmentId )
519-
520- for ( const repo of [ ...githubMappedRepos , ...gitlabMappedRepos ] ) {
521- const url = repo . url
522- try {
523- const parsedUrl = new URL ( url )
524- if ( parsedUrl . hostname === 'github.com' ) {
525- const label = parsedUrl . pathname . slice ( 1 ) // removes leading '/'
526- addToResult ( PlatformType . GITHUB , url , label )
527- }
528- if ( parsedUrl . hostname === 'gitlab.com' ) {
529- const label = parsedUrl . pathname . slice ( 1 ) // removes leading '/'
530- addToResult ( PlatformType . GITLAB , url , label )
531- }
532- } catch ( err ) {
533- // Do nothing
534- }
535- }
536-
537- for ( const i of integrations ) {
538- if ( i . platform === PlatformType . GIT ) {
539- for ( const r of ( i . settings as any ) . remotes ) {
540- try {
541- const url = new URL ( r )
542- let label = r
543-
544- if ( url . hostname === 'gitlab.com' ) {
545- label = url . pathname . slice ( 1 )
546- } else if ( url . hostname === 'github.com' ) {
547- label = url . pathname . slice ( 1 )
548- }
549-
550- addToResult ( i . platform , r , label )
551- } catch {
552- this . options . log . warn ( `Invalid URL in remotes: ${ r } ` )
553- }
554- }
555- }
556-
557- if ( i . platform === PlatformType . GITLAB ) {
558- for ( const group of Object . values ( ( i . settings as any ) . groupProjects ) as any [ ] ) {
559- for ( const r of group ) {
560- const label = r . path_with_namespace
561- const fullUrl = `https://gitlab.com/${ label } `
562- addToResult ( i . platform , fullUrl , label )
563- }
564- }
565- }
566-
567- if ( i . platform === PlatformType . GERRIT ) {
568- for ( const r of ( i . settings as any ) . remote . repoNames ) {
569- addToResult ( i . platform , `${ ( i . settings as any ) . remote . orgURL } /q/project:${ r } ` , r )
570- }
571- }
572- }
573-
574- return result
575- } )
576- }
577-
578491 static isSingleRepoOrg ( orgs : GithubIntegrationSettings [ 'orgs' ] ) : boolean {
579492 return (
580493 Array . isArray ( orgs ) &&
@@ -721,74 +634,4 @@ export class CollectionService extends LoggerBase {
721634
722635 return result
723636 }
724-
725- static extractGithubRepoSlug ( url : string ) : any {
726- const parsedUrl = new URL ( url )
727- const pathname = parsedUrl . pathname
728- const parts = pathname . split ( '/' ) . filter ( Boolean )
729-
730- if ( parts . length >= 2 ) {
731- return `${ parts [ 0 ] } /${ parts [ 1 ] } `
732- }
733-
734- throw new Error ( 'Invalid GitHub URL format' )
735- }
736-
737- async findNangoRepositoriesToBeRemoved ( integrationId : string ) : Promise < string [ ] > {
738- return SequelizeRepository . withTx ( this . options , async ( tx ) => {
739- const qx = SequelizeRepository . getQueryExecutor ( { ...this . options , transaction : tx } )
740- const integration = await fetchIntegrationById ( qx , integrationId )
741-
742- if ( ! integration || integration . platform !== PlatformType . GITHUB_NANGO ) {
743- return [ ]
744- }
745-
746- const repoSlugs = new Set < string > ( )
747- const settings = integration . settings as any
748- const reposToBeRemoved = [ ]
749-
750- if ( ! settings . nangoMapping ) {
751- return [ ]
752- }
753-
754- if ( settings . orgs ) {
755- for ( const org of settings . orgs ) {
756- for ( const repo of org . repos ?? [ ] ) {
757- repoSlugs . add ( CollectionService . extractGithubRepoSlug ( repo . url ) )
758- }
759- }
760- }
761-
762- if ( settings . repos ) {
763- for ( const repo of settings . repos ) {
764- repoSlugs . add ( CollectionService . extractGithubRepoSlug ( repo . url ) )
765- }
766- }
767- // determine which connections to delete if needed
768- for ( const mappedRepo of Object . values ( settings . nangoMapping ) as {
769- owner : string
770- repoName : string
771- } [ ] ) {
772- if ( ! repoSlugs . has ( `${ mappedRepo . owner } /${ mappedRepo . repoName } ` ) ) {
773- reposToBeRemoved . push ( `https://github.com/${ mappedRepo . owner } /${ mappedRepo . repoName } ` )
774- }
775- }
776-
777- return reposToBeRemoved
778- } )
779- }
780-
781- async unmapGithubRepo ( integrationId : string , repo : string ) : Promise < void > {
782- return SequelizeRepository . withTx ( this . options , async ( tx ) => {
783- const qx = SequelizeRepository . getQueryExecutor ( { ...this . options , transaction : tx } )
784- await removePlainGitHubRepoMapping ( qx , this . options . redis , integrationId , repo )
785- } )
786- }
787-
788- async unmapGitlabRepo ( integrationId : string , repo : string ) : Promise < void > {
789- return SequelizeRepository . withTx ( this . options , async ( tx ) => {
790- const qx = SequelizeRepository . getQueryExecutor ( { ...this . options , transaction : tx } )
791- await removePlainGitHubRepoMapping ( qx , this . options . redis , integrationId , repo )
792- } )
793- }
794637}
0 commit comments