From 175642f82af3a451b69e06504f45f8461e26aa18 Mon Sep 17 00:00:00 2001 From: PendaGTP <38917281+PendaGTP@users.noreply.github.com> Date: Thu, 2 Jan 2025 20:37:45 +0100 Subject: [PATCH] fix: create new repositories during full-sync operation The full-sync script now properly creates new repositories defined in the configuration instead of only updating existing ones. This fixes the different behavior between webhook-based sync and full-sync runs. --- lib/settings.js | 57 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/settings.js b/lib/settings.js index 05ddcbe4..addd4570 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -357,7 +357,7 @@ ${this.results.reduce((x, y) => { async updateAll () { // this.subOrgConfigs = this.subOrgConfigs || await this.getSubOrgConfigs(this.github, this.repo, this.log) // this.repoConfigs = this.repoConfigs || await this.getRepoConfigs(this.github, this.repo, this.log) - return this.eachRepositoryRepos(this.github, this.config.restrictedRepos, this.log).then(res => { + return this.eachRepositoryRepos(this.github, this.log).then(res => { this.appendToResults(res) }) } @@ -468,19 +468,49 @@ ${this.results.reduce((x, y) => { return restrictedRepos.filter((restrictedRepo) => { return RegExp(restrictedRepo).test(repoName) }).length > 0 } - async eachRepositoryRepos (github, restrictedRepos, log) { + async eachRepositoryRepos (github, log) { log.debug('Fetching repositories') - return github.paginate('GET /installation/repositories').then(repositories => { - return Promise.all(repositories.map(repository => { - if (this.isRestricted(repository.name)) { - return null - } - const { owner, name } = repository - return this.updateRepos({ owner: owner.login, repo: name }) + const processedRepos = new Set() + const results = [] + + // Process existing repositories + const existingRepoResults = await github.paginate('GET /installation/repositories') + .then(repositories => { + return Promise.all(repositories.map(repository => { + if (this.isRestricted(repository.name)) { + return null + } + const { owner, name } = repository + processedRepos.add(`${owner.login}/${name}`) + return this.updateRepos({ owner: owner.login, repo: name }) + })) }) - ) - }) + + // Process missing repositories + const repoInConfigs = Object.values(this.repoConfigs) + .filter(config => config.repository?.name) + .map(config => { + return { + name: config.repository.name, + owner: config.repository.organization || this.repo.owner + } + }) + const missingRepoResults = await Promise.all( + repoInConfigs + .filter(repo => !this.isRestricted(repo.name)) + .filter(repo => !processedRepos.has(`${repo.owner}/${repo.name}`)) + .map(repo => { + processedRepos.add(`${repo.owner}/${repo.name}`) + return this.updateRepos({ owner: repo.owner, repo: repo.name }) + }) + ) + + results + .concat(existingRepoResults || [], missingRepoResults || []) + .filter(result => result !== null) + + return results } /** @@ -799,14 +829,13 @@ ${this.results.reduce((x, y) => { } async getReposForCustomProperty (customPropertyTuple) { - const name=Object.keys(customPropertyTuple)[0] + const name = Object.keys(customPropertyTuple)[0] let q = `props.${name}:${customPropertyTuple[name]}` q = encodeURIComponent(q) const options = this.github.request.endpoint((`/orgs/${this.repo.owner}/properties/values?repository_query=${q}`)) return this.github.paginate(options) } - isObject (item) { return (item && typeof item === 'object' && !Array.isArray(item)) } @@ -820,7 +849,7 @@ ${this.results.reduce((x, y) => { } } -function prettify(obj) { +function prettify (obj) { if (obj === null || obj === undefined) { return '' }