@@ -9,10 +9,10 @@ import {
99 fetchGlobalIntegrationsStatusCount ,
1010 fetchGlobalNotConnectedIntegrations ,
1111 fetchGlobalNotConnectedIntegrationsCount ,
12- getNangoMappingsForIntegration ,
12+ getNangoMappingsForIntegrations ,
1313} from '@crowd/data-access-layer/src/integrations'
14- import { SequelizeQueryExecutor } from '@crowd/data-access-layer/src/queryExecutor'
15- import { getReposGroupedByOrg } from '@crowd/data-access-layer/src/repositories'
14+ import { QueryExecutor } from '@crowd/data-access-layer/src/queryExecutor'
15+ import { getReposGroupedByOrgForIntegrations } from '@crowd/data-access-layer/src/repositories'
1616import { IntegrationRunState , PlatformType } from '@crowd/types'
1717
1818import SequelizeFilterUtils from '../utils/sequelizeFilterUtils'
@@ -172,7 +172,7 @@ class IntegrationRepository {
172172 throw new Error404 ( )
173173 }
174174
175- return this . _populateRelations ( record )
175+ return this . _populateRelations ( record , SequelizeRepository . getQueryExecutor ( options ) )
176176 }
177177
178178 static async findActiveIntegrationByPlatform ( platform : PlatformType ) {
@@ -188,7 +188,7 @@ class IntegrationRepository {
188188 throw new Error404 ( )
189189 }
190190
191- return this . _populateRelations ( record )
191+ return this . _populateRelations ( record , SequelizeRepository . getQueryExecutor ( options ) )
192192 }
193193
194194 /**
@@ -213,7 +213,7 @@ class IntegrationRepository {
213213 throw new Error404 ( )
214214 }
215215
216- return Promise . all ( records . map ( ( record ) => this . _populateRelations ( record ) ) )
216+ return this . _populateRelationsForRows ( records , SequelizeRepository . getQueryExecutor ( options ) )
217217 }
218218
219219 static async findByStatus (
@@ -263,7 +263,7 @@ class IntegrationRepository {
263263 throw new Error404 ( )
264264 }
265265
266- return this . _populateRelations ( record )
266+ return this . _populateRelations ( record , SequelizeRepository . getQueryExecutor ( options ) )
267267 }
268268
269269 static async findById ( id , options : IRepositoryOptions ) {
@@ -283,7 +283,7 @@ class IntegrationRepository {
283283 throw new Error404 ( )
284284 }
285285
286- return this . _populateRelations ( record )
286+ return this . _populateRelations ( record , SequelizeRepository . getQueryExecutor ( options ) )
287287 }
288288
289289 static async count ( filter , options : IRepositoryOptions ) {
@@ -474,7 +474,7 @@ class IntegrationRepository {
474474 transaction : SequelizeRepository . getTransaction ( options ) ,
475475 } )
476476
477- rows = await this . _populateRelationsForRows ( rows )
477+ rows = await this . _populateRelationsForRows ( rows , SequelizeRepository . getQueryExecutor ( options ) )
478478
479479 // Some integrations (i.e GitHub, Discord, Discourse, Groupsio) receive new data via webhook post-onboarding.
480480 // We track their last processedAt separately, and not using updatedAt.
@@ -573,26 +573,80 @@ class IntegrationRepository {
573573 } ) )
574574 }
575575
576- static async _populateRelationsForRows ( rows ) {
576+ static async _populateRelationsForRows ( rows , qx : QueryExecutor ) {
577577 if ( ! rows ) {
578578 return rows
579579 }
580580
581- return Promise . all ( rows . map ( ( record ) => this . _populateRelations ( record ) ) )
581+ const records = rows . map ( ( record ) => record . get ( { plain : true } ) )
582+
583+ const nangoIntegrationIds = records
584+ . filter ( ( r ) => r . platform === PlatformType . GITHUB_NANGO )
585+ . map ( ( r ) => r . id )
586+
587+ const githubIntegrationIds = records
588+ . filter (
589+ ( r ) =>
590+ ( r . platform === PlatformType . GITHUB || r . platform === PlatformType . GITHUB_NANGO ) &&
591+ r . settings ?. orgs ?. length > 0 ,
592+ )
593+ . map ( ( r ) => r . id )
594+
595+ const [ allNangoMappings , allReposByOrg ] = await Promise . all ( [
596+ getNangoMappingsForIntegrations ( qx , nangoIntegrationIds ) ,
597+ getReposGroupedByOrgForIntegrations ( qx , githubIntegrationIds ) ,
598+ ] )
599+
600+ return records . map ( ( output ) => {
601+ if ( output . platform === PlatformType . GITHUB_NANGO ) {
602+ const nangoMapping = allNangoMappings [ output . id ]
603+ if ( nangoMapping && Object . keys ( nangoMapping ) . length > 0 ) {
604+ output . settings = { ...output . settings , nangoMapping }
605+ }
606+ }
607+
608+ if (
609+ ( output . platform === PlatformType . GITHUB ||
610+ output . platform === PlatformType . GITHUB_NANGO ) &&
611+ output . settings ?. orgs ?. length > 0
612+ ) {
613+ const reposByOrg = allReposByOrg [ output . id ]
614+
615+ if ( reposByOrg && Object . keys ( reposByOrg ) . length > 0 ) {
616+ output . settings = {
617+ ...output . settings ,
618+ orgs : output . settings . orgs . map ( ( org ) => ( {
619+ ...org ,
620+ repos : ( reposByOrg [ org . name ] || [ ] ) . map ( ( r ) => ( {
621+ url : r . url ,
622+ name : r . name ,
623+ owner : r . owner ,
624+ forkedFrom : r . forkedFrom ,
625+ updatedAt : r . updatedAt ,
626+ } ) ) ,
627+ } ) ) ,
628+ }
629+ }
630+
631+ delete output . settings . repos
632+ delete output . settings . unavailableRepos
633+ }
634+
635+ return output
636+ } )
582637 }
583638
584- static async _populateRelations ( record ) {
639+ static async _populateRelations ( record , qx : QueryExecutor ) {
585640 if ( ! record ) {
586641 return record
587642 }
588643
589644 const output = record . get ( { plain : true } )
590645
591- const qx = new SequelizeQueryExecutor ( record . sequelize )
592-
593646 // For github-nango integrations, populate settings.nangoMapping from dedicated table
594647 if ( output . platform === PlatformType . GITHUB_NANGO ) {
595- const nangoMapping = await getNangoMappingsForIntegration ( qx , output . id )
648+ const allNangoMappings = await getNangoMappingsForIntegrations ( qx , [ output . id ] )
649+ const nangoMapping = allNangoMappings [ output . id ] || { }
596650 if ( Object . keys ( nangoMapping ) . length > 0 ) {
597651 output . settings = { ...output . settings , nangoMapping }
598652 }
@@ -603,7 +657,8 @@ class IntegrationRepository {
603657 ( output . platform === PlatformType . GITHUB || output . platform === PlatformType . GITHUB_NANGO ) &&
604658 output . settings ?. orgs ?. length > 0
605659 ) {
606- const reposByOrg = await getReposGroupedByOrg ( qx , output . id )
660+ const allReposByOrg = await getReposGroupedByOrgForIntegrations ( qx , [ output . id ] )
661+ const reposByOrg = allReposByOrg [ output . id ] || { }
607662
608663 // Only overwrite orgs[].repos from the repositories table if there are rows.
609664 // During the 'mapping' phase (legacy github connect), repos live in settings
0 commit comments