Skip to content

Create a fresh copy of the base repository config #795

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()}\``
Expand Down Expand Up @@ -144,7 +144,7 @@ class Settings {
})
}

logError(msg) {
logError (msg) {
this.log.error(msg)
this.errors.push({
owner: this.repo.owner,
Expand All @@ -154,7 +154,7 @@ class Settings {
})
}

async handleResults() {
async handleResults () {
const { payload } = this.context

// Create a checkrun if not in nop mode
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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)
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -366,15 +367,15 @@ ${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 => {
this.appendToResults(res)
})
}

getSubOrgConfig(repoName) {
getSubOrgConfig (repoName) {
if (this.subOrgConfigs) {
for (const k of Object.keys(this.subOrgConfigs)) {
const repoPattern = new Glob(k)
Expand All @@ -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)}`)
Expand Down Expand Up @@ -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} `)
Expand All @@ -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)) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`)
Expand All @@ -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 })
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down