Skip to content

Commit ff7a656

Browse files
decyjphrCopilot
andauthored
Fix 422 errors which might be do to Archive changes (#806)
* undo Archive changes * Update lib/settings.js Co-authored-by: Copilot <[email protected]> * Update lib/settings.js Co-authored-by: Copilot <[email protected]> * rollback the object assign fix suggested by Copilot --------- Co-authored-by: Copilot <[email protected]>
1 parent 6168667 commit ff7a656

File tree

1 file changed

+53
-83
lines changed

1 file changed

+53
-83
lines changed

lib/settings.js

+53-83
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ class Settings {
166166

167167
// remove duplicate rows in this.results
168168
this.results = this.results.filter((thing, index, self) => {
169-
return index === self.findIndex((t) => {
170-
return t.type === thing.type && t.repo === thing.repo && t.plugin === thing.plugin
169+
return index === self.findIndex((t) => {
170+
return t.type === thing.type && t.repo === thing.repo && t.plugin === thing.plugin
171+
})
171172
})
172-
})
173173

174174
let error = false
175175
// Different logic
@@ -296,12 +296,13 @@ ${this.results.reduce((x, y) => {
296296
}
297297
}
298298

299-
async updateRepos (repo) {
299+
async updateRepos(repo) {
300300
this.subOrgConfigs = this.subOrgConfigs || await this.getSubOrgConfigs()
301-
// Create a fresh copy of the base repository config
302-
let repoConfig = this.config.repository ? Object.assign({}, this.config.repository) : {}
301+
// Keeping this as is instead of doing an object assign as that would cause `Cannot read properties of undefined (reading 'startsWith')` error
302+
// Copilot code review would recoommend using object assign but that would cause the error
303+
let repoConfig = this.config.repository
303304
if (repoConfig) {
304-
repoConfig = Object.assign({}, repoConfig, { name: repo.repo, org: repo.owner })
305+
repoConfig = Object.assign(repoConfig, { name: repo.repo, org: repo.owner })
305306
}
306307

307308
const subOrgConfig = this.getSubOrgConfig(repo.repo)
@@ -315,9 +316,9 @@ ${this.results.reduce((x, y) => {
315316
this.log.debug(`Process normally... Not a SubOrg config change or SubOrg config was changed and this repo is part of it. ${JSON.stringify(repo)} suborg config ${JSON.stringify(this.subOrgConfigMap)}`)
316317

317318
if (subOrgConfig) {
318-
let suborgRepoConfig = subOrgConfig.repository ? Object.assign({}, subOrgConfig.repository) : {}
319+
let suborgRepoConfig = subOrgConfig.repository
319320
if (suborgRepoConfig) {
320-
suborgRepoConfig = Object.assign({}, suborgRepoConfig, { name: repo.repo, org: repo.owner })
321+
suborgRepoConfig = Object.assign(suborgRepoConfig, { name: repo.repo, org: repo.owner })
321322
repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, suborgRepoConfig)
322323
}
323324
}
@@ -328,45 +329,42 @@ ${this.results.reduce((x, y) => {
328329
if (overrideRepoConfig) {
329330
repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, overrideRepoConfig)
330331
}
331-
const { shouldContinue, nopCommands } = await new Archive(this.nop, this.github, repo, repoConfig, this.log).sync()
332-
if (nopCommands) this.appendToResults(nopCommands)
333-
if (shouldContinue) {
334-
if (repoConfig) {
335-
try {
336-
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)
337-
const childPlugins = this.childPluginsList(repo)
338-
const RepoPlugin = Settings.PLUGINS.repository
339-
return new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync().then(res => {
340-
this.appendToResults(res)
341-
return Promise.all(
342-
childPlugins.map(([Plugin, config]) => {
343-
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
344-
}))
345-
}).then(res => {
346-
this.appendToResults(res)
347-
})
348-
} catch (e) {
349-
if (this.nop) {
350-
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
351-
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
352-
this.appendToResults([nopcommand])
353-
// throw e
354-
} else {
355-
throw e
356-
}
357-
}
358-
} else {
359-
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
332+
if (repoConfig) {
333+
try {
334+
this.log.debug(`found a matching repoconfig for this repo ${JSON.stringify(repoConfig)}`)
360335
const childPlugins = this.childPluginsList(repo)
361-
return Promise.all(childPlugins.map(([Plugin, config]) => {
362-
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
363-
this.appendToResults(res)
364-
})
365-
}))
336+
const RepoPlugin = Settings.PLUGINS.repository
337+
return new RepoPlugin(this.nop, this.github, repo, repoConfig, this.installation_id, this.log, this.errors).sync().then(res => {
338+
this.appendToResults(res)
339+
return Promise.all(
340+
childPlugins.map(([Plugin, config]) => {
341+
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync()
342+
}))
343+
}).then(res => {
344+
this.appendToResults(res)
345+
})
346+
} catch (e) {
347+
if (this.nop) {
348+
const nopcommand = new NopCommand(this.constructor.name, this.repo, null, `${e}`, 'ERROR')
349+
this.log.error(`NOPCOMMAND ${JSON.stringify(nopcommand)}`)
350+
this.appendToResults([nopcommand])
351+
// throw e
352+
} else {
353+
throw e
354+
}
366355
}
356+
} else {
357+
this.log.debug(`Didnt find any a matching repoconfig for this repo ${JSON.stringify(repo)} in ${JSON.stringify(this.repoConfigs)}`)
358+
const childPlugins = this.childPluginsList(repo)
359+
return Promise.all(childPlugins.map(([Plugin, config]) => {
360+
return new Plugin(this.nop, this.github, repo, config, this.log, this.errors).sync().then(res => {
361+
this.appendToResults(res)
362+
})
363+
}))
367364
}
368365
}
369366

367+
370368
async updateAll () {
371369
// this.subOrgConfigs = this.subOrgConfigs || await this.getSubOrgConfigs(this.github, this.repo, this.log)
372370
// this.repoConfigs = this.repoConfigs || await this.getRepoConfigs(this.github, this.repo, this.log)
@@ -487,47 +485,17 @@ ${this.results.reduce((x, y) => {
487485

488486
async eachRepositoryRepos (github, log) {
489487
log.debug('Fetching repositories')
490-
491-
const processedRepos = new Set()
492-
const results = []
493-
494-
// Process existing repositories
495-
const existingRepoResults = await github.paginate('GET /installation/repositories')
496-
.then(repositories => {
497-
return Promise.all(repositories.map(repository => {
498-
if (this.isRestricted(repository.name)) {
499-
return null
500-
}
501-
const { owner, name } = repository
502-
processedRepos.add(`${owner.login}/${name}`)
503-
return this.updateRepos({ owner: owner.login, repo: name })
504-
}))
505-
})
506-
507-
// Process missing repositories
508-
const repoInConfigs = Object.values(this.repoConfigs)
509-
.filter(config => config.repository?.name)
510-
.map(config => {
511-
return {
512-
name: config.repository.name,
513-
owner: config.repository.organization || this.repo.owner
488+
return github.paginate('GET /installation/repositories').then(repositories => {
489+
return Promise.all(repositories.map(repository => {
490+
if (this.isRestricted(repository.name)) {
491+
return null
514492
}
515-
})
516-
const missingRepoResults = await Promise.all(
517-
repoInConfigs
518-
.filter(repo => !this.isRestricted(repo.name))
519-
.filter(repo => !processedRepos.has(`${repo.owner}/${repo.name}`))
520-
.map(repo => {
521-
processedRepos.add(`${repo.owner}/${repo.name}`)
522-
return this.updateRepos({ owner: repo.owner, repo: repo.name })
523-
})
524-
)
525-
526-
results
527-
.concat(existingRepoResults || [], missingRepoResults || [])
528-
.filter(result => result !== null)
529493

530-
return results
494+
const { owner, name } = repository
495+
return this.updateRepos({ owner: owner.login, repo: name })
496+
})
497+
)
498+
})
531499
}
532500

533501
/**
@@ -790,7 +758,7 @@ ${this.results.reduce((x, y) => {
790758
}
791759
)) {
792760
delete subOrgConfigs[key]
793-
}
761+
}
794762
}
795763
}
796764
return subOrgConfigs
@@ -894,6 +862,7 @@ ${this.results.reduce((x, y) => {
894862
throw new Error(`Failed to filter repositories for property ${name}: ${error.message}`)
895863
}
896864
}
865+
897866

898867
async getSubOrgRepositories (subOrgProperties) {
899868
const organizationName = this.repo.owner
@@ -940,6 +909,7 @@ function prettify (obj) {
940909
return JSON.stringify(obj, null, 2).replaceAll('\n', '<br>').replaceAll(' ', '&nbsp;')
941910
}
942911

912+
Settings.FILE_NAME = path.posix.join(CONFIG_PATH, env.SETTINGS_FILE_PATH)
943913
Settings.FILE_PATH = path.posix.join(CONFIG_PATH, env.SETTINGS_FILE_PATH)
944914
Settings.SUB_ORG_PATTERN = new Glob(`${CONFIG_PATH}/suborgs/*.yml`)
945915
Settings.REPO_PATTERN = new Glob(`${CONFIG_PATH}/repos/*.yml`)

0 commit comments

Comments
 (0)