Skip to content

Commit a178f47

Browse files
committed
refactor: improve fetching repositories with custom properties
1 parent 774b2e5 commit a178f47

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

lib/settings.js

+52-16
Original file line numberDiff line numberDiff line change
@@ -768,16 +768,10 @@ ${this.results.reduce((x, y) => {
768768
})
769769
}
770770
if (data.suborgproperties) {
771-
const promises = data.suborgproperties.map((customProperty) => {
772-
return this.getReposForCustomProperty(customProperty)
773-
})
774-
await Promise.all(promises).then(res => {
775-
res.forEach(r => {
776-
r.forEach(e => {
777-
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, e.repository_name, data)
778-
})
779-
})
780-
})
771+
const subOrgRepositories = await this.getSubOrgRepositories(data.suborgproperties)
772+
subOrgRepositories.forEach(repo =>
773+
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, repo.repository_name, data)
774+
)
781775
}
782776
}
783777

@@ -876,12 +870,54 @@ ${this.results.reduce((x, y) => {
876870
return this.github.paginate(options)
877871
}
878872

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

887923
isObject (item) {

test/unit/lib/settings.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ repository:
234234
jest.spyOn(settings, 'loadConfigMap').mockImplementation(() => [{ name: "frontend", path: ".github/suborgs/frontend.yml" }])
235235
jest.spyOn(settings, 'loadYaml').mockImplementation(() => subOrgConfig)
236236
jest.spyOn(settings, 'getReposForTeam').mockImplementation(() => [{ name: 'repo-test' }])
237-
jest.spyOn(settings, 'getReposForCustomProperty').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
237+
jest.spyOn(settings, 'getSubOrgRepositories').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
238238

239239
const subOrgConfigs = await settings.getSubOrgConfigs()
240240
expect(settings.loadConfigMap).toHaveBeenCalledTimes(1)
@@ -251,7 +251,7 @@ repository:
251251
jest.spyOn(settings, 'loadConfigMap').mockImplementation(() => [{ name: "frontend", path: ".github/suborgs/frontend.yml" }, { name: "backend", path: ".github/suborgs/backend.yml" }])
252252
jest.spyOn(settings, 'loadYaml').mockImplementation(() => subOrgConfig)
253253
jest.spyOn(settings, 'getReposForTeam').mockImplementation(() => [{ name: 'repo-test' }])
254-
jest.spyOn(settings, 'getReposForCustomProperty').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
254+
jest.spyOn(settings, 'getSubOrgRepositories').mockImplementation(() => [{ repository_name: 'repo-for-property' }])
255255

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

0 commit comments

Comments
 (0)