Skip to content

Commit 9cc2f12

Browse files
decyjphrPendaGTP
andauthored
Merge 432 (#791)
* refactor: rename FILE_NAME to FILE_PATH for accuracy * refactor: rename DEPLOYMENT_CONFIG_FILE to DEPLOYMENT_CONFIG_FILE_PATH for consistency * refactor: move sub-org config pattern to Settings * refactor: move repo config pattern to Settings * merge pr 432 and fix log errors --------- Co-authored-by: PendaGTP <[email protected]>
1 parent 620cf14 commit 9cc2f12

File tree

16 files changed

+49
-47
lines changed

16 files changed

+49
-47
lines changed

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
2828
if (nop) {
2929
let filename = env.SETTINGS_FILE_PATH
3030
if (!deploymentConfig) {
31-
filename = env.DEPLOYMENT_CONFIG_FILE
31+
filename = env.DEPLOYMENT_CONFIG_FILE_PATH
3232
deploymentConfig = {}
3333
}
3434
const nopcommand = new NopCommand(filename, repo, null, e, 'ERROR')
@@ -53,7 +53,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
5353
if (nop) {
5454
let filename = env.SETTINGS_FILE_PATH
5555
if (!deploymentConfig) {
56-
filename = env.DEPLOYMENT_CONFIG_FILE
56+
filename = env.DEPLOYMENT_CONFIG_FILE_PATH
5757
deploymentConfig = {}
5858
}
5959
const nopcommand = new NopCommand(filename, repo, null, e, 'ERROR')
@@ -78,7 +78,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
7878
if (nop) {
7979
let filename = env.SETTINGS_FILE_PATH
8080
if (!deploymentConfig) {
81-
filename = env.DEPLOYMENT_CONFIG_FILE
81+
filename = env.DEPLOYMENT_CONFIG_FILE_PATH
8282
deploymentConfig = {}
8383
}
8484
const nopcommand = new NopCommand(filename, repo, null, e, 'ERROR')
@@ -104,7 +104,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
104104
if (nop) {
105105
let filename = env.SETTINGS_FILE_PATH
106106
if (!deploymentConfig) {
107-
filename = env.DEPLOYMENT_CONFIG_FILE
107+
filename = env.DEPLOYMENT_CONFIG_FILE_PATH
108108
deploymentConfig = {}
109109
}
110110
const nopcommand = new NopCommand(filename, repo, null, e, 'ERROR')
@@ -123,7 +123,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
123123
*/
124124
async function loadYamlFileSystem () {
125125
if (deploymentConfig === undefined) {
126-
const deploymentConfigPath = env.DEPLOYMENT_CONFIG_FILE
126+
const deploymentConfigPath = env.DEPLOYMENT_CONFIG_FILE_PATH
127127
if (fs.existsSync(deploymentConfigPath)) {
128128
deploymentConfig = yaml.load(fs.readFileSync(deploymentConfigPath))
129129
} else {
@@ -134,7 +134,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
134134
}
135135

136136
function getAllChangedSubOrgConfigs (payload) {
137-
const settingPattern = new Glob(`${env.CONFIG_PATH}/suborgs/*.yml`)
137+
const settingPattern = Settings.SUB_ORG_PATTERN
138138
// Changes will be an array of files that were added
139139
const added = payload.commits.map(c => {
140140
return (c.added.filter(s => {
@@ -158,7 +158,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
158158
}
159159

160160
function getAllChangedRepoConfigs (payload, owner) {
161-
const settingPattern = new Glob(`${env.CONFIG_PATH}/repos/*.yml`)
161+
const settingPattern = Settings.REPO_PATTERN
162162
// Changes will be an array of files that were added
163163
const added = payload.commits.map(c => {
164164
return (c.added.filter(s => {
@@ -270,11 +270,11 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
270270
}
271271

272272
const settingsModified = payload.commits.find(commit => {
273-
return commit.added.includes(Settings.FILE_NAME) ||
274-
commit.modified.includes(Settings.FILE_NAME)
273+
return commit.added.includes(Settings.FILE_PATH) ||
274+
commit.modified.includes(Settings.FILE_PATH)
275275
})
276276
if (settingsModified) {
277-
robot.log.debug(`Changes in '${Settings.FILE_NAME}' detected, doing a full synch...`)
277+
robot.log.debug(`Changes in '${Settings.FILE_PATH}' detected, doing a full synch...`)
278278
return syncAllSettings(false, context)
279279
}
280280

@@ -292,7 +292,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
292292
}))
293293
}
294294

295-
robot.log.debug(`No changes in '${Settings.FILE_NAME}' detected, returning...`)
295+
robot.log.debug(`No changes in '${Settings.FILE_PATH}' detected, returning...`)
296296
})
297297

298298
robot.on('create', async context => {
@@ -597,21 +597,21 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
597597
const changes = await context.octokit.repos.compareCommitsWithBasehead(params)
598598
const files = changes.data.files.map(f => { return f.filename })
599599

600-
const settingsModified = files.includes(Settings.FILE_NAME)
600+
const settingsModified = files.includes(Settings.FILE_PATH)
601601

602602
if (settingsModified) {
603-
robot.log.debug(`Changes in '${Settings.FILE_NAME}' detected, doing a full synch...`)
603+
robot.log.debug(`Changes in '${Settings.FILE_PATH}' detected, doing a full synch...`)
604604
return syncAllSettings(true, context, context.repo(), pull_request.head.ref)
605605
}
606606

607-
const repoChanges = getChangedRepoConfigName(new Glob(`${env.CONFIG_PATH}/repos/*.yml`), files, context.repo().owner)
607+
const repoChanges = getChangedRepoConfigName(Settings.REPO_PATTERN, files, context.repo().owner)
608608
if (repoChanges.length > 0) {
609609
return Promise.all(repoChanges.map(repo => {
610610
return syncSettings(true, context, repo, pull_request.head.ref)
611611
}))
612612
}
613613

614-
const subOrgChanges = getChangedSubOrgConfigName(new Glob(`${env.CONFIG_PATH}/suborgs/*.yml`), files, context.repo().owner)
614+
const subOrgChanges = getChangedSubOrgConfigName(Settings.SUB_ORG_PATTERN, files, context.repo().owner)
615615
if (subOrgChanges.length) {
616616
return Promise.all(subOrgChanges.map(suborg => {
617617
return syncSubOrgSettings(true, context, suborg, context.repo(), pull_request.head.ref)

lib/deploymentConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DeploymentConfig {
1313
static overridevalidators = {}
1414

1515
static {
16-
const deploymentConfigPath = process.env.DEPLOYMENT_CONFIG_FILE ? process.env.DEPLOYMENT_CONFIG_FILE : 'deployment-settings.yml'
16+
const deploymentConfigPath = env.DEPLOYMENT_CONFIG_FILE_PATH
1717
if (fs.existsSync(deploymentConfigPath)) {
1818
this.config = yaml.load(fs.readFileSync(deploymentConfigPath)) || {}
1919
} else {

lib/env.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = {
22
ADMIN_REPO: process.env.ADMIN_REPO || 'admin',
33
CONFIG_PATH: process.env.CONFIG_PATH || '.github',
44
SETTINGS_FILE_PATH: process.env.SETTINGS_FILE_PATH || 'settings.yml',
5-
DEPLOYMENT_CONFIG_FILE: process.env.DEPLOYMENT_CONFIG_FILE || 'deployment-settings.yml',
5+
DEPLOYMENT_CONFIG_FILE_PATH: process.env.DEPLOYMENT_CONFIG_FILE || 'deployment-settings.yml',
66
CREATE_PR_COMMENT: process.env.CREATE_PR_COMMENT || 'true',
77
CREATE_ERROR_ISSUE: process.env.CREATE_ERROR_ISSUE || 'true',
88
BLOCK_REPO_RENAME_BY_HUMAN: process.env.BLOCK_REPO_RENAME_BY_HUMAN || 'false'

lib/plugins/branches.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = class Branches extends ErrorStash {
3333
let p = Object.assign(this.repo, { branch: branch.name })
3434
if (branch.name === 'default') {
3535
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
36-
this.log(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
36+
this.log.debug(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
3737
}
3838
// Hack to handle closures and keep params from changing
3939
const params = Object.assign({}, p)
@@ -50,7 +50,7 @@ module.exports = class Branches extends ErrorStash {
5050
let p = Object.assign(this.repo, { branch: branch.name })
5151
if (branch.name === 'default') {
5252
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
53-
// this.log(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
53+
// this.log.debug(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
5454
}
5555
// Hack to handle closures and keep params from changing
5656
const params = Object.assign({}, p)
@@ -80,7 +80,7 @@ module.exports = class Branches extends ErrorStash {
8080
return Promise.resolve(resArray)
8181
}
8282
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
83-
return this.github.repos.updateBranchProtection(params).then(res => this.log(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
83+
return this.github.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
8484
}).catch((e) => {
8585
if (e.status === 404) {
8686
Object.assign(params, Overrides.removeOverrides(overrides, branch.protection, {}), { headers: previewHeaders })
@@ -89,7 +89,7 @@ module.exports = class Branches extends ErrorStash {
8989
return Promise.resolve(resArray)
9090
}
9191
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
92-
return this.github.repos.updateBranchProtection(params).then(res => this.log(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
92+
return this.github.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.logError(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
9393
} else {
9494
this.logError(e)
9595
if (this.nop) {

lib/plugins/rulesets.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = class Rulesets extends Diffable {
3434
org: this.repo.owner,
3535
headers: version
3636
})
37-
this.log(listOptions)
37+
this.log.debug(listOptions)
3838
return this.github.paginate(listOptions)
3939
.then(res => {
4040
const rulesets = res.map(ruleset => {
@@ -63,7 +63,7 @@ module.exports = class Rulesets extends Diffable {
6363
repo: this.repo.repo,
6464
headers: version
6565
})
66-
this.log(listOptions)
66+
this.log.debug(listOptions)
6767
return this.github.paginate(listOptions)
6868
.then(res => {
6969
const rulesets = res
@@ -111,7 +111,7 @@ module.exports = class Rulesets extends Diffable {
111111
Overrides.removeOverrides(overrides, parms, existing)
112112
this.log.debug(`Updating Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
113113
return this.github.request('PUT /orgs/{org}/rulesets/{id}', parms).then(res => {
114-
this.log(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
114+
this.log.debug(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
115115
return res
116116
}).catch(e => {
117117
return this.handleError(e)
@@ -125,7 +125,7 @@ module.exports = class Rulesets extends Diffable {
125125
Overrides.removeOverrides(overrides, parms, existing)
126126
this.log.debug(`Updating Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
127127
return this.github.request('PUT /repos/{owner}/{repo}/rulesets/{id}', parms).then(res => {
128-
this.log(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
128+
this.log.debug(`Ruleset updated successfully ${JSON.stringify(res.url)}`)
129129
return res
130130
}).catch(e => {
131131
return this.handleError(e)
@@ -143,7 +143,7 @@ module.exports = class Rulesets extends Diffable {
143143
Overrides.removeOverrides(overrides, attrs, {})
144144
this.log.debug(`Creating Rulesets with the following values ${JSON.stringify(attrs, null, 2)}`)
145145
return this.github.request('POST /orgs/{org}/rulesets', this.wrapAttrs(attrs)).then(res => {
146-
this.log(`Ruleset created successfully ${JSON.stringify(res.url)}`)
146+
this.log.debug(`Ruleset created successfully ${JSON.stringify(res.url)}`)
147147
return res
148148
}).catch(e => {
149149
return this.handleError(e)
@@ -157,7 +157,7 @@ module.exports = class Rulesets extends Diffable {
157157
Overrides.removeOverrides(overrides, attrs, {})
158158
this.log.debug(`Creating Rulesets with the following values ${JSON.stringify(attrs, null, 2)}`)
159159
return this.github.request('POST /repos/{owner}/{repo}/rulesets', this.wrapAttrs(attrs)).then(res => {
160-
this.log(`Ruleset created successfully ${JSON.stringify(res.url)}`)
160+
this.log.debug(`Ruleset created successfully ${JSON.stringify(res.url)}`)
161161
return res
162162
}).catch(e => {
163163
return this.handleError(e)
@@ -175,7 +175,7 @@ module.exports = class Rulesets extends Diffable {
175175
}
176176
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
177177
return this.github.request('DELETE /orgs/{org}/rulesets/{id}', parms).then(res => {
178-
this.log(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
178+
this.log.debug(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
179179
return res
180180
}).catch(e => {
181181
return this.handleError(e)
@@ -188,7 +188,7 @@ module.exports = class Rulesets extends Diffable {
188188
}
189189
this.log.debug(`Deleting Ruleset with the following values ${JSON.stringify(parms, null, 2)}`)
190190
return this.github.request('DELETE /repos/{owner}/{repo}/rulesets/{id}', parms).then(res => {
191-
this.log(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
191+
this.log.debug(`Ruleset deleted successfully ${JSON.stringify(res.url)}`)
192192
return res
193193
}).catch(e => {
194194
if (e.status === 404) {

lib/plugins/validator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = class Validator {
2020
}
2121
}).then(res => {
2222
if (this.repo.repo.search(this.regex) >= 0) {
23-
this.log(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
23+
this.log.debug(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
2424
if (this.nop) {
2525
return Promise.resolve([
2626
new NopCommand(this.constructor.name, this.repo, null, `Passed Validation for pattern ${this.pattern}`)
@@ -38,7 +38,7 @@ module.exports = class Validator {
3838
})
3939
}
4040
} else {
41-
this.log(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
41+
this.log.debug(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
4242
if (this.nop) {
4343
return Promise.resolve([
4444
new NopCommand(this.constructor.name, this.repo, null, `Failed Validation for pattern ${this.pattern}`, 'ERROR')
@@ -59,7 +59,7 @@ module.exports = class Validator {
5959
})
6060
.catch(() => {})
6161
} catch (error) {
62-
this.log(`Error in Validation checking ${error}`)
62+
this.log.debug(`Error in Validation checking ${error}`)
6363
}
6464
}
6565
}

lib/settings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,9 @@ function prettify (obj) {
908908
return JSON.stringify(obj, null, 2).replaceAll('\n', '<br>').replaceAll(' ', '&nbsp;')
909909
}
910910

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

913915
Settings.PLUGINS = {
914916
repository: require('./plugins/repository'),

test/integration/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function buildPushEvent () {
3838
payload: {
3939
ref: 'refs/heads/master',
4040
repository,
41-
commits: [{ modified: [settings.FILE_NAME], added: [] }]
41+
commits: [{ modified: [settings.FILE_PATH], added: [] }]
4242
}
4343
}
4444
}

test/integration/plugins/collaborators.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('collaborators plugin', function () {
2121
const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
2222
const encodedConfig = configFile.toString('base64')
2323
githubScope
24-
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_NAME}`)
24+
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_PATH}`)
2525
.reply(OK, { content: encodedConfig, name: 'settings.yml', type: 'file' })
2626
githubScope
2727
.get(`/repos/${repository.owner.name}/${repository.name}/collaborators?affiliation=direct`)

test/integration/plugins/milestones.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('milestones plugin', function () {
2121
const configFile = Buffer.from(fs.readFileSync(pathToConfig, 'utf8'))
2222
const encodedConfig = configFile.toString('base64')
2323
githubScope
24-
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_NAME}`)
24+
.get(`/repos/${repository.owner.name}/${repository.name}/contents/${settings.FILE_PATH}`)
2525
.reply(OK, { content: encodedConfig, name: 'settings.yml', type: 'file' })
2626
githubScope
2727
.patch(`/repos/${repository.owner.name}/${repository.name}`)

0 commit comments

Comments
 (0)