diff --git a/lib/settings.js b/lib/settings.js index 56e20e88..2da38d71 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -26,7 +26,7 @@ class Settings { return settings } - static async syncSubOrgs(nop, context, suborg, repo, config, ref) { + static async syncSubOrgs (nop, context, suborg, repo, config, ref) { const settings = new Settings(nop, context, repo, config, ref, suborg) try { await settings.loadConfigs() @@ -38,7 +38,7 @@ class Settings { } } - static async sync(nop, context, repo, config, ref) { + static async sync (nop, context, repo, config, ref) { const settings = new Settings(nop, context, repo, config, ref) try { await settings.loadConfigs(repo) @@ -53,13 +53,13 @@ class Settings { } } - static async handleError(nop, context, repo, config, ref, nopcommand) { + static async handleError (nop, context, repo, config, ref, nopcommand) { const settings = new Settings(nop, context, repo, config, ref) settings.appendToResults([nopcommand]) await settings.handleResults() } - constructor(nop, context, repo, config, ref, suborg) { + constructor (nop, context, repo, config, ref, suborg) { this.ref = ref this.context = context this.installation_id = context.payload.installation.id @@ -98,7 +98,7 @@ class Settings { } // Create a check in the Admin repo for safe-settings. - async createCheckRun() { + async createCheckRun () { const startTime = new Date() let conclusion = 'success' let details = `Run on: \`${new Date().toISOString()}\`` @@ -144,7 +144,7 @@ class Settings { }) } - logError(msg) { + logError (msg) { this.log.error(msg) this.errors.push({ owner: this.repo.owner, @@ -154,7 +154,7 @@ class Settings { }) } - async handleResults() { + async handleResults () { const { payload } = this.context // Create a checkrun if not in nop mode @@ -164,12 +164,12 @@ class Settings { return } - //remove duplicate rows in this.results + // remove duplicate rows in this.results this.results = this.results.filter((thing, index, self) => { - return index === self.findIndex((t) => { - return t.type === thing.type && t.repo === thing.repo && t.plugin === thing.plugin - }) + return index === self.findIndex((t) => { + return t.type === thing.type && t.repo === thing.repo && t.plugin === thing.plugin }) + }) let error = false // Different logic @@ -281,12 +281,12 @@ ${this.results.reduce((x, y) => { await this.github.checks.update(params) } - async loadConfigs(repo) { + async loadConfigs (repo) { this.subOrgConfigs = await this.getSubOrgConfigs() this.repoConfigs = await this.getRepoConfigs(repo) } - async updateOrg() { + async updateOrg () { const rulesetsConfig = this.config.rulesets if (rulesetsConfig) { const RulesetsPlugin = Settings.PLUGINS.rulesets @@ -296,11 +296,12 @@ ${this.results.reduce((x, y) => { } } - async updateRepos(repo) { + async updateRepos (repo) { this.subOrgConfigs = this.subOrgConfigs || await this.getSubOrgConfigs() - let repoConfig = this.config.repository + // Create a fresh copy of the base repository config + let repoConfig = this.config.repository ? Object.assign({}, this.config.repository) : {} if (repoConfig) { - repoConfig = Object.assign(repoConfig, { name: repo.repo, org: repo.owner }) + repoConfig = Object.assign({}, repoConfig, { name: repo.repo, org: repo.owner }) } const subOrgConfig = this.getSubOrgConfig(repo.repo) @@ -314,9 +315,9 @@ ${this.results.reduce((x, y) => { 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)}`) if (subOrgConfig) { - let suborgRepoConfig = subOrgConfig.repository + let suborgRepoConfig = subOrgConfig.repository ? Object.assign({}, subOrgConfig.repository) : {} if (suborgRepoConfig) { - suborgRepoConfig = Object.assign(suborgRepoConfig, { name: repo.repo, org: repo.owner }) + suborgRepoConfig = Object.assign({}, suborgRepoConfig, { name: repo.repo, org: repo.owner }) repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, suborgRepoConfig) } } @@ -327,7 +328,7 @@ ${this.results.reduce((x, y) => { if (overrideRepoConfig) { repoConfig = this.mergeDeep.mergeDeep({}, repoConfig, overrideRepoConfig) } - const {shouldContinue, nopCommands} = await new Archive(this.nop, this.github, repo, repoConfig, this.log).sync() + const { shouldContinue, nopCommands } = await new Archive(this.nop, this.github, repo, repoConfig, this.log).sync() if (nopCommands) this.appendToResults(nopCommands) if (shouldContinue) { if (repoConfig) { @@ -366,7 +367,7 @@ ${this.results.reduce((x, y) => { } } - async updateAll() { + 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.log).then(res => { @@ -374,7 +375,7 @@ ${this.results.reduce((x, y) => { }) } - getSubOrgConfig(repoName) { + getSubOrgConfig (repoName) { if (this.subOrgConfigs) { for (const k of Object.keys(this.subOrgConfigs)) { const repoPattern = new Glob(k) @@ -387,13 +388,13 @@ ${this.results.reduce((x, y) => { } // Remove Org specific configs from the repo config - returnRepoSpecificConfigs(config) { + returnRepoSpecificConfigs (config) { const newConfig = Object.assign({}, config) // clone delete newConfig.rulesets return newConfig } - childPluginsList(repo) { + childPluginsList (repo) { const repoName = repo.repo const subOrgOverrideConfig = this.getSubOrgConfig(repoName) this.log.debug(`suborg config for ${repoName} is ${JSON.stringify(subOrgOverrideConfig)}`) @@ -425,11 +426,11 @@ ${this.results.reduce((x, y) => { return childPlugins } - getRepoOverrideConfig(repoName) { + getRepoOverrideConfig (repoName) { return this.repoConfigs[`${repoName}.yml`] || this.repoConfigs[`${repoName}.yaml`] || {} } - validate(section, baseConfig, overrideConfig) { + validate (section, baseConfig, overrideConfig) { const configValidator = this.configvalidators[section] if (configValidator) { this.log.debug(`Calling configvalidator for key ${section} `) @@ -448,7 +449,7 @@ ${this.results.reduce((x, y) => { } } - isRestricted(repoName) { + isRestricted (repoName) { const restrictedRepos = this.config.restrictedRepos // Skip configuring any restricted repos if (Array.isArray(restrictedRepos)) { @@ -480,7 +481,7 @@ ${this.results.reduce((x, y) => { return false } - includesRepo(repoName, restrictedRepos) { + includesRepo (repoName, restrictedRepos) { return restrictedRepos.filter((restrictedRepo) => { return RegExp(restrictedRepo).test(repoName) }).length > 0 } @@ -535,7 +536,7 @@ ${this.results.reduce((x, y) => { * @param params Params to fetch the file with * @return The parsed YAML file */ - async loadConfigMap(params) { + async loadConfigMap (params) { try { this.log.debug(` In loadConfigMap ${JSON.stringify(params)}`) const response = await this.github.repos.getContent(params).catch(e => { @@ -582,7 +583,7 @@ ${this.results.reduce((x, y) => { * @param params Params to fetch the file with * @return The parsed YAML file */ - async getRepoConfigMap() { + async getRepoConfigMap () { try { this.log.debug(` In getRepoConfigMap ${JSON.stringify(this.repo)}`) // GitHub getContent api has a hard limit of returning 1000 entries without @@ -649,7 +650,7 @@ ${this.results.reduce((x, y) => { * @param params Params to fetch the file with * @return The parsed YAML file */ - async getSubOrgConfigMap() { + async getSubOrgConfigMap () { try { this.log.debug(` In getSubOrgConfigMap ${JSON.stringify(this.repo)}`) const repo = { owner: this.repo.owner, repo: env.ADMIN_REPO } @@ -676,7 +677,7 @@ ${this.results.reduce((x, y) => { * @param {*} repo repo param * @returns repoConfigs object */ - async getRepoConfigs(repo) { + async getRepoConfigs (repo) { try { const overridePaths = await this.getRepoConfigMap() const repoConfigs = {} @@ -728,7 +729,7 @@ ${this.results.reduce((x, y) => { * @param params Params to fetch the file with * @return The parsed YAML file */ - async getSubOrgConfigs() { + async getSubOrgConfigs () { try { // Get all suborg configs even though we might be here becuase of a suborg config change // we will filter them out if request is due to a suborg config change @@ -805,7 +806,7 @@ ${this.results.reduce((x, y) => { } } - storeSubOrgConfigIfNoConflicts(subOrgConfigs, overridePath, repoName, data) { + storeSubOrgConfigIfNoConflicts (subOrgConfigs, overridePath, repoName, data) { const existingConfigForRepo = subOrgConfigs[repoName] if (existingConfigForRepo && existingConfigForRepo.source !== overridePath) { throw new Error(`Multiple suborg configs for ${repoName} in ${overridePath} and ${existingConfigForRepo?.source}`) @@ -819,7 +820,7 @@ ${this.results.reduce((x, y) => { * @param params Params to fetch the file with * @return The parsed YAML file */ - async loadYaml(filePath) { + async loadYaml (filePath) { try { const repo = { owner: this.repo.owner, repo: env.ADMIN_REPO } const params = Object.assign(repo, { path: filePath, ref: this.ref }) @@ -867,7 +868,7 @@ ${this.results.reduce((x, y) => { this.results = this.results.concat(results) } - async getReposForTeam(teamslug) { + async getReposForTeam (teamslug) { const options = this.github.rest.teams.listReposInOrg.endpoint.merge({ org: this.repo.owner, team_slug: teamslug, @@ -893,7 +894,6 @@ ${this.results.reduce((x, y) => { throw new Error(`Failed to filter repositories for property ${name}: ${error.message}`) } } - async getSubOrgRepositories (subOrgProperties) { const organizationName = this.repo.owner @@ -924,7 +924,7 @@ ${this.results.reduce((x, y) => { return (item && typeof item === 'object' && !Array.isArray(item)) } - isIterable(obj) { + isIterable (obj) { // checks for null and undefined if (obj == null) { return false