Skip to content

Commit 5462d65

Browse files
PendaGTPdecyjphr
andauthored
refactor: improve fetching repositories with custom properties (#742)
Co-authored-by: Yadhav Jayaraman <[email protected]>
1 parent c674c20 commit 5462d65

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

lib/settings.js

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -772,16 +772,10 @@ ${this.results.reduce((x, y) => {
772772
})
773773
}
774774
if (data.suborgproperties) {
775-
const promises = data.suborgproperties.map((customProperty) => {
776-
return this.getReposForCustomProperty(customProperty)
777-
})
778-
await Promise.all(promises).then(res => {
779-
res.forEach(r => {
780-
r.forEach(e => {
781-
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, e.repository_name, data)
782-
})
783-
})
784-
})
775+
const subOrgRepositories = await this.getSubOrgRepositories(data.suborgproperties)
776+
subOrgRepositories.forEach(repo =>
777+
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, repo.repository_name, data)
778+
)
785779
}
786780
}
787781

@@ -880,12 +874,54 @@ ${this.results.reduce((x, y) => {
880874
return this.github.paginate(options)
881875
}
882876

883-
async getReposForCustomProperty (customPropertyTuple) {
884-
const name = Object.keys(customPropertyTuple)[0]
885-
let q = `props.${name}:${customPropertyTuple[name]}`
886-
q = encodeURIComponent(q)
887-
const options = this.github.request.endpoint((`/orgs/${this.repo.owner}/properties/values?repository_query=${q}`))
888-
return this.github.paginate(options)
877+
async getRepositoriesByProperty (organizationName, propertyFilter) {
878+
if (!organizationName || !propertyFilter) {
879+
throw new Error('Organization name and property filter are required')
880+
}
881+
882+
const [name] = Object.keys(propertyFilter)
883+
const value = propertyFilter[name]
884+
885+
try {
886+
const query = `props.${name}.${value}`
887+
const encodedQuery = encodeURIComponent(query)
888+
889+
return this.github.paginate(
890+
this.github.repos.getCustomPropertiesValues,
891+
{
892+
org: organizationName,
893+
repository_query: encodedQuery,
894+
per_page: 100
895+
}
896+
)
897+
} catch (error) {
898+
throw new Error(`Failed to filter repositories for property ${name}: ${error.message}`)
899+
}
900+
}
901+
902+
async getSubOrgRepositories (subOrgProperties) {
903+
const organizationName = this.repo.owner
904+
try {
905+
const repositories = await Promise.all(
906+
subOrgProperties.map(property =>
907+
this.getRepositoriesByProperty(organizationName, property)
908+
)
909+
)
910+
911+
// Deduplicate repositories based on repository_name
912+
const uniqueRepos = repositories
913+
.flat()
914+
.reduce((unique, repo) => {
915+
unique.set(repo.repository_name, repo)
916+
return unique
917+
}, new Map())
918+
919+
const result = Array.from(uniqueRepos.values())
920+
921+
return result
922+
} catch (error) {
923+
throw new Error(`Failed to fetch suborg repositories: ${error.message}`)
924+
}
889925
}
890926

891927
isObject (item) {

test/unit/lib/settings.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ repository:
274274
jest.spyOn(settings, 'loadConfigMap').mockImplementation(() => [{ name: "frontend", path: ".github/suborgs/frontend.yml" }])
275275
jest.spyOn(settings, 'loadYaml').mockImplementation(() => subOrgConfig)
276276
jest.spyOn(settings, 'getReposForTeam').mockImplementation(() => [{ name: 'repo-test' }])
277-
jest.spyOn(settings, 'getReposForCustomProperty').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
277+
jest.spyOn(settings, 'getSubOrgRepositories').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
278278

279279
const subOrgConfigs = await settings.getSubOrgConfigs()
280280
expect(settings.loadConfigMap).toHaveBeenCalledTimes(1)
@@ -291,7 +291,7 @@ repository:
291291
jest.spyOn(settings, 'loadConfigMap').mockImplementation(() => [{ name: "frontend", path: ".github/suborgs/frontend.yml" }, { name: "backend", path: ".github/suborgs/backend.yml" }])
292292
jest.spyOn(settings, 'loadYaml').mockImplementation(() => subOrgConfig)
293293
jest.spyOn(settings, 'getReposForTeam').mockImplementation(() => [{ name: 'repo-test' }])
294-
jest.spyOn(settings, 'getReposForCustomProperty').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
294+
jest.spyOn(settings, 'getSubOrgRepositories').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
295295

296296
expect(async () => await settings.getSubOrgConfigs()).rejects.toThrow('Multiple suborg configs for new-repo in .github/suborgs/backend.yml and .github/suborgs/frontend.yml')
297297
// try {

0 commit comments

Comments
 (0)