Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icons for mapped segments #2556

Merged
merged 5 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions backend/src/database/repositories/githubReposRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,31 @@ export default class GithubReposRepository {

return results
}

static async hasMappedRepos(segmentId: string, options: IRepositoryOptions) {
const transaction = SequelizeRepository.getTransaction(options)
const tenantId = options.currentTenant.id

const result = await options.database.sequelize.query(
`
SELECT EXISTS (
SELECT 1
FROM "githubRepos" r
WHERE r."segmentId" = :segmentId
AND r."tenantId" = :tenantId
LIMIT 1
) as has_repos
`,
{
replacements: {
segmentId,
tenantId,
},
type: QueryTypes.SELECT,
transaction,
},
)

return result[0].has_repos
}
}
100 changes: 95 additions & 5 deletions backend/src/database/repositories/segmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,17 +706,48 @@ class SegmentRepository extends RepositoryBase<

const subprojects = projects.map((p) => p.subprojects).flat()
const integrationsBySegments = await this.queryIntegrationsForSubprojects(subprojects)
const mappedGithubReposBySegments = (
await Promise.all(
subprojects.map(async (s) => ({
segmentId: s.id,
hasMappedRepo: await this.hasMappedRepos(s.id),
})),
)
).reduce((acc, { segmentId, hasMappedRepo }) => {
if (hasMappedRepo) {
acc[segmentId] = true
}
return acc
}, {})

const count = projects.length > 0 ? Number.parseInt(projects[0].totalCount, 10) : 0

const rows = projects.map((i) => removeFieldsFromObject(i, 'totalCount'))

// assign integrations to subprojects
rows.forEach((row) => {
row.subprojects.forEach((subproject) => {
subproject.integrations = integrationsBySegments[subproject.id] || []
})
})
await Promise.all(
rows.map(async (row) => {
await Promise.all(
row.subprojects.map(async (subproject) => {
const integrations = integrationsBySegments[subproject.id] || []
const githubIntegration = integrations.find((i) => i.platform === 'github')

if (githubIntegration) {
githubIntegration.type = 'primary'
} else if (mappedGithubReposBySegments[subproject.id]) {
integrations.push({
platform: 'github',
segmentId: subproject.id,
type: 'mapped',
mappedWith: await this.mappedWith(subproject.id),
})
}

subproject.integrations = integrations
}),
)
}),
)

return { count, rows, limit: criteria.limit, offset: criteria.offset }
}
Expand Down Expand Up @@ -893,6 +924,65 @@ class SegmentRepository extends RepositoryBase<

return segments.map((i: any) => i.id)
}

async hasMappedRepos(segmentId: string) {
const transaction = SequelizeRepository.getTransaction(this.options)
const tenantId = this.options.currentTenant.id

const result = await this.options.database.sequelize.query(
`
SELECT EXISTS (
SELECT 1
FROM "githubRepos" r
LEFT JOIN "integrations" i ON r."integrationId" = i.id
WHERE r."segmentId" = :segmentId
AND r."tenantId" = :tenantId
AND (i.id IS NULL OR i."segmentId" != :segmentId)
LIMIT 1
) as has_repos
`,
{
replacements: {
segmentId,
tenantId,
},
type: QueryTypes.SELECT,
transaction,
},
)

return !!result[0].has_repos
}

async mappedWith(segmentId: string) {
const transaction = SequelizeRepository.getTransaction(this.options)
const tenantId = this.options.currentTenant.id

const result = await this.options.database.sequelize.query(
`
select
s.name as segment_name
from
"githubRepos" r
left join "integrations" i on r."integrationId" = i.id
left join "segments" s on i."segmentId" = s.id
where r."segmentId" = :segmentId
and r."tenantId" = :tenantId
and (i.id is null or i."segmentId" != :segmentId)
limit 1
`,
{
replacements: {
segmentId,
tenantId,
},
type: QueryTypes.SELECT,
transaction,
},
)

return result[0].segment_name as string
}
}

export default SegmentRepository
Loading
Loading