Skip to content

Commit d15fe6d

Browse files
committed
refactor: improve fetching repositories with custom properties
1 parent 3b7eb05 commit d15fe6d

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
@@ -763,16 +763,10 @@ ${this.results.reduce((x, y) => {
763763
})
764764
}
765765
if (data.suborgproperties) {
766-
const promises = data.suborgproperties.map((customProperty) => {
767-
return this.getReposForCustomProperty(customProperty)
768-
})
769-
await Promise.all(promises).then(res => {
770-
res.forEach(r => {
771-
r.forEach(e => {
772-
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, e.repository_name, data)
773-
})
774-
})
775-
})
766+
const subOrgRepositories = await this.getSubOrgRepositories(data.suborgproperties)
767+
subOrgRepositories.forEach(repo =>
768+
this.storeSubOrgConfigIfNoConflicts(subOrgConfigs, override.path, repo.repository_name, data)
769+
)
776770
}
777771
}
778772

@@ -871,12 +865,54 @@ ${this.results.reduce((x, y) => {
871865
return this.github.paginate(options)
872866
}
873867

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

882918
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)