Skip to content

Commit 5b759e7

Browse files
authored
chore: migrate-repo-reads-to-public-repositories [CM-900] (#3765)
1 parent dda1c33 commit 5b759e7

8 files changed

Lines changed: 108 additions & 290 deletions

File tree

backend/src/database/repositories/githubReposRepository.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -105,59 +105,6 @@ export default class GithubReposRepository {
105105
await this.getCache(options).deleteAll()
106106
}
107107

108-
static async getMapping(integrationId, options: IRepositoryOptions) {
109-
const transaction = SequelizeRepository.getTransaction(options)
110-
111-
const results = await options.database.sequelize.query(
112-
`
113-
SELECT
114-
r.url,
115-
JSONB_BUILD_OBJECT(
116-
'id', s.id,
117-
'name', s.name
118-
) as "segment"
119-
FROM "githubRepos" r
120-
JOIN segments s ON s.id = r."segmentId"
121-
WHERE r."integrationId" = :integrationId
122-
AND r."deletedAt" is null
123-
`,
124-
{
125-
replacements: {
126-
integrationId,
127-
},
128-
type: QueryTypes.SELECT,
129-
transaction,
130-
},
131-
)
132-
133-
return results
134-
}
135-
136-
static async hasMappedRepos(segmentId: string, options: IRepositoryOptions) {
137-
const transaction = SequelizeRepository.getTransaction(options)
138-
139-
const result = await options.database.sequelize.query(
140-
`
141-
SELECT EXISTS (
142-
SELECT 1
143-
FROM "githubRepos" r
144-
WHERE r."segmentId" = :segmentId
145-
AND r."deletedAt" is null
146-
LIMIT 1
147-
) as has_repos
148-
`,
149-
{
150-
replacements: {
151-
segmentId,
152-
},
153-
type: QueryTypes.SELECT,
154-
transaction,
155-
},
156-
)
157-
158-
return result[0].has_repos
159-
}
160-
161108
static async delete(integrationId, options: IRepositoryOptions) {
162109
const seq = SequelizeRepository.getSequelize(options)
163110
const transaction = SequelizeRepository.getTransaction(options)

backend/src/database/repositories/gitlabReposRepository.ts

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -96,59 +96,6 @@ export default class GitlabReposRepository {
9696
await this.getCache(options).deleteAll()
9797
}
9898

99-
static async getMapping(integrationId, options: IRepositoryOptions) {
100-
const transaction = SequelizeRepository.getTransaction(options)
101-
102-
const results = await options.database.sequelize.query(
103-
`
104-
SELECT
105-
r.url,
106-
JSONB_BUILD_OBJECT(
107-
'id', s.id,
108-
'name', s.name
109-
) as "segment"
110-
FROM "gitlabRepos" r
111-
JOIN segments s ON s.id = r."segmentId"
112-
WHERE r."integrationId" = :integrationId
113-
AND r."deletedAt" is null
114-
`,
115-
{
116-
replacements: {
117-
integrationId,
118-
},
119-
type: QueryTypes.SELECT,
120-
transaction,
121-
},
122-
)
123-
124-
return results
125-
}
126-
127-
static async hasMappedRepos(segmentId: string, options: IRepositoryOptions) {
128-
const transaction = SequelizeRepository.getTransaction(options)
129-
130-
const result = await options.database.sequelize.query(
131-
`
132-
SELECT EXISTS (
133-
SELECT 1
134-
FROM "gitlabRepos" r
135-
WHERE r."segmentId" = :segmentId
136-
AND r."deletedAt" is null
137-
LIMIT 1
138-
) as has_repos
139-
`,
140-
{
141-
replacements: {
142-
segmentId,
143-
},
144-
type: QueryTypes.SELECT,
145-
transaction,
146-
},
147-
)
148-
149-
return result[0].has_repos
150-
}
151-
15299
static async delete(integrationId, options: IRepositoryOptions) {
153100
const seq = SequelizeRepository.getSequelize(options)
154101
const transaction = SequelizeRepository.getTransaction(options)

backend/src/database/repositories/segmentRepository.ts

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ import { Error404 } from '@crowd/common'
66
import {
77
buildSegmentActivityTypes,
88
findSegmentById,
9+
getMappedWithSegmentName,
910
getSegmentActivityTypes,
11+
hasMappedRepos,
1012
isSegmentProject,
1113
isSegmentProjectGroup,
1214
populateSegmentRelations,
1315
} from '@crowd/data-access-layer/src/segments'
1416
import {
1517
ActivityTypeSettings,
1618
PageData,
19+
PlatformType,
1720
QueryData,
1821
SegmentCreateData,
1922
SegmentData,
@@ -633,11 +636,13 @@ class SegmentRepository extends RepositoryBase<
633636

634637
const subprojects = projects.map((p) => p.subprojects).flat()
635638
const integrationsBySegments = await this.queryIntegrationsForSubprojects(subprojects)
639+
const qx = SequelizeRepository.getQueryExecutor(this.options)
640+
const githubPlatforms = [PlatformType.GITHUB, PlatformType.GITHUB_NANGO]
636641
const mappedGithubReposBySegments = (
637642
await Promise.all(
638643
subprojects.map(async (s) => ({
639644
segmentId: s.id,
640-
hasMappedRepo: await this.hasMappedRepos(s.id),
645+
hasMappedRepo: await hasMappedRepos(qx, s.id, githubPlatforms),
641646
})),
642647
)
643648
).reduce((acc, { segmentId, hasMappedRepo }) => {
@@ -666,7 +671,7 @@ class SegmentRepository extends RepositoryBase<
666671
platform: 'github',
667672
segmentId: subproject.id,
668673
type: 'mapped',
669-
mappedWith: await this.mappedWith(subproject.id),
674+
mappedWith: await getMappedWithSegmentName(qx, subproject.id, githubPlatforms),
670675
})
671676
}
672677

@@ -890,66 +895,6 @@ class SegmentRepository extends RepositoryBase<
890895

891896
return segments.map((i: any) => i.id)
892897
}
893-
894-
async hasMappedRepos(segmentId: string) {
895-
const transaction = SequelizeRepository.getTransaction(this.options)
896-
const tenantId = this.options.currentTenant.id
897-
898-
const result = await this.options.database.sequelize.query(
899-
`
900-
SELECT EXISTS (
901-
SELECT 1
902-
FROM "githubRepos" r
903-
LEFT JOIN "integrations" i ON r."integrationId" = i.id
904-
WHERE r."segmentId" = :segmentId
905-
AND r."tenantId" = :tenantId
906-
AND r."deletedAt" IS NULL
907-
AND (i.id IS NULL OR i."segmentId" != :segmentId)
908-
LIMIT 1
909-
) as has_repos
910-
`,
911-
{
912-
replacements: {
913-
segmentId,
914-
tenantId,
915-
},
916-
type: QueryTypes.SELECT,
917-
transaction,
918-
},
919-
)
920-
921-
return !!result[0].has_repos
922-
}
923-
924-
async mappedWith(segmentId: string) {
925-
const transaction = SequelizeRepository.getTransaction(this.options)
926-
const tenantId = this.options.currentTenant.id
927-
928-
const result = await this.options.database.sequelize.query(
929-
`
930-
select
931-
s.name as segment_name
932-
from
933-
"githubRepos" r
934-
left join "integrations" i on r."integrationId" = i.id
935-
left join "segments" s on i."segmentId" = s.id
936-
where r."segmentId" = :segmentId
937-
and r."tenantId" = :tenantId
938-
and (i.id is null or i."segmentId" != :segmentId)
939-
limit 1
940-
`,
941-
{
942-
replacements: {
943-
segmentId,
944-
tenantId,
945-
},
946-
type: QueryTypes.SELECT,
947-
transaction,
948-
},
949-
)
950-
951-
return result[0].segment_name as string
952-
}
953898
}
954899

955900
export default SegmentRepository

backend/src/services/collectionService.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ import {
3535
listRepositoryGroups,
3636
updateRepositoryGroup,
3737
} from '@crowd/data-access-layer/src/repositoryGroups'
38-
import { findSegmentById } from '@crowd/data-access-layer/src/segments'
38+
import { findSegmentById, hasMappedRepos } from '@crowd/data-access-layer/src/segments'
3939
import { QueryResult } from '@crowd/data-access-layer/src/utils'
4040
import { GithubIntegrationSettings } from '@crowd/integrations'
4141
import { LoggerBase } from '@crowd/logging'
4242
import { DEFAULT_WIDGET_VALUES, PlatformType, Widgets } from '@crowd/types'
4343

44-
import SegmentRepository from '@/database/repositories/segmentRepository'
4544
import SequelizeRepository from '@/database/repositories/sequelizeRepository'
4645
import { IGithubInsights } from '@/types/githubTypes'
4746

@@ -582,9 +581,11 @@ export class CollectionService extends LoggerBase {
582581
]
583582

584583
// Check for mapped repositories and add GitHub if there are any
585-
const segmentRepository = new SegmentRepository(this.options)
586-
const hasMappedRepos = await segmentRepository.hasMappedRepos(segmentId)
587-
if (hasMappedRepos && !platforms.includes(PlatformType.GITHUB)) {
584+
const hasGithubMappedRepos = await hasMappedRepos(qx, segmentId, [
585+
PlatformType.GITHUB,
586+
PlatformType.GITHUB_NANGO,
587+
])
588+
if (hasGithubMappedRepos && !platforms.includes(PlatformType.GITHUB)) {
588589
platforms.push(PlatformType.GITHUB)
589590
}
590591

backend/src/services/integrationService.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ import {
2828
restoreRepositories,
2929
softDeleteRepositories,
3030
} from '@crowd/data-access-layer/src/repositories'
31-
import { getGithubMappedRepos, getGitlabMappedRepos } from '@crowd/data-access-layer/src/segments'
31+
import {
32+
getMappedRepos,
33+
getMappedWithSegmentName,
34+
hasMappedRepos,
35+
} from '@crowd/data-access-layer/src/segments'
3236
import {
3337
NangoIntegration,
3438
connectNangoIntegration,
@@ -1146,25 +1150,6 @@ export default class IntegrationService {
11461150
}
11471151
}
11481152

1149-
async getGithubRepos(integrationId): Promise<any[]> {
1150-
const transaction = await SequelizeRepository.createTransaction(this.options)
1151-
1152-
const txOptions = {
1153-
...this.options,
1154-
transaction,
1155-
}
1156-
1157-
try {
1158-
const mapping = await GithubReposRepository.getMapping(integrationId, txOptions)
1159-
1160-
await SequelizeRepository.commitTransaction(transaction)
1161-
return mapping
1162-
} catch (err) {
1163-
await SequelizeRepository.rollbackTransaction(transaction)
1164-
throw err
1165-
}
1166-
}
1167-
11681153
/**
11691154
* Get repository mappings for an integration
11701155
* Uses the unified public.repositories table instead of legacy githubRepos table
@@ -2638,8 +2623,9 @@ export default class IntegrationService {
26382623
updatedAt: string
26392624
}[]
26402625

2641-
const githubRepos = await this.getGithubRepos(integrationId)
2642-
const mappedSegments = githubRepos.map((repo) => repo.segment.id)
2626+
const qx = SequelizeRepository.getQueryExecutor(this.options)
2627+
const githubRepos = await getRepositoriesBySourceIntegrationId(qx, integrationId)
2628+
const mappedSegments = githubRepos.map((repo) => repo.segmentId)
26432629

26442630
const cacheRemote = new RedisCache(
26452631
'github-progress-remote',
@@ -2858,22 +2844,25 @@ export default class IntegrationService {
28582844
}
28592845

28602846
async getIntegrationMappedRepos(segmentId: string) {
2861-
const segmentRepository = new SegmentRepository(this.options)
2862-
const hasMappedRepos = await segmentRepository.hasMappedRepos(segmentId)
2847+
const qx = SequelizeRepository.getQueryExecutor(this.options)
2848+
const githubPlatforms = [PlatformType.GITHUB, PlatformType.GITHUB_NANGO]
2849+
2850+
const hasRepos = await hasMappedRepos(qx, segmentId, githubPlatforms)
28632851

2864-
if (!hasMappedRepos) {
2852+
if (!hasRepos) {
28652853
return null
28662854
}
28672855

2868-
const qx = SequelizeRepository.getQueryExecutor(this.options)
2869-
2870-
const gitlabMappedRepos = await getGitlabMappedRepos(qx, segmentId)
2871-
const githubMappedRepos = await getGithubMappedRepos(qx, segmentId)
2872-
const project = await segmentRepository.mappedWith(segmentId)
2856+
const [githubMappedRepos, githubNangoMappedRepos, gitlabMappedRepos] = await Promise.all([
2857+
getMappedRepos(qx, segmentId, PlatformType.GITHUB),
2858+
getMappedRepos(qx, segmentId, PlatformType.GITHUB_NANGO),
2859+
getMappedRepos(qx, segmentId, PlatformType.GITLAB),
2860+
])
2861+
const project = await getMappedWithSegmentName(qx, segmentId, githubPlatforms)
28732862

28742863
return {
28752864
project,
2876-
repositories: [...githubMappedRepos, ...gitlabMappedRepos],
2865+
repositories: [...githubMappedRepos, ...githubNangoMappedRepos, ...gitlabMappedRepos],
28772866
}
28782867
}
28792868

@@ -3077,25 +3066,6 @@ export default class IntegrationService {
30773066
}
30783067
}
30793068

3080-
async getGitlabRepos(integrationId): Promise<any[]> {
3081-
const transaction = await SequelizeRepository.createTransaction(this.options)
3082-
3083-
const txOptions = {
3084-
...this.options,
3085-
transaction,
3086-
}
3087-
3088-
try {
3089-
const mapping = await GitlabReposRepository.getMapping(integrationId, txOptions)
3090-
3091-
await SequelizeRepository.commitTransaction(transaction)
3092-
return mapping
3093-
} catch (err) {
3094-
await SequelizeRepository.rollbackTransaction(transaction)
3095-
throw err
3096-
}
3097-
}
3098-
30993069
async updateGithubIntegrationSettings(installId: string) {
31003070
this.options.log.info(`Updating GitHub integration settings for installId: ${installId}`)
31013071

0 commit comments

Comments
 (0)