Skip to content

Commit d900367

Browse files
committed
chore(logs): improve log messages
1 parent 230b39d commit d900367

9 files changed

+139
-117
lines changed

index.js

+89-67
Large diffs are not rendered by default.

lib/configManager.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ module.exports = class ConfigManager {
66
constructor (context, ref) {
77
this.context = context
88
this.ref = ref
9-
this.log = context.log
9+
this.log = context.log.child({ context: 'ConfigManager' })
1010
}
1111

1212
/**
13-
* Loads a file from GitHub
14-
*
15-
* @param params Params to fetch the file with
16-
* @return The parsed YAML file
17-
*/
13+
* Loads a file from GitHub
14+
*
15+
* @param params Params to fetch the file with
16+
* @return The parsed YAML file
17+
*/
1818
async loadYaml (filePath) {
1919
try {
2020
const repo = { owner: this.context.repo().owner, repo: env.ADMIN_REPO }

lib/mergeDeep.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const mergeBy = require('./mergeArrayBy')
22
const NAME_FIELDS = ['name', 'username']
33
class MergeDeep {
44
constructor (log, ignorableFields, configvalidators = {}, overridevalidators = {}) {
5-
this.log = log
5+
this.log = log.child({ context: 'MergeDeep' })
66
this.ignorableFields = ignorableFields
77
this.configvalidators = configvalidators
88
this.overridevalidators = overridevalidators

lib/plugins/branches.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = class Branches {
88
this.github = github
99
this.repo = repo
1010
this.branches = settings
11-
this.log = log
11+
this.log = log.child({ context: 'Branches', repository: this.repo.repo })
1212
this.nop = nop
1313
}
1414

@@ -24,7 +24,7 @@ module.exports = class Branches {
2424
let p = Object.assign(this.repo, { branch: branch.name })
2525
if (branch.name === 'default') {
2626
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
27-
this.log(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
27+
this.log.debug(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
2828
}
2929
// Hack to handle closures and keep params from changing
3030
const params = Object.assign({}, p)
@@ -41,7 +41,7 @@ module.exports = class Branches {
4141
let p = Object.assign(this.repo, { branch: branch.name })
4242
if (branch.name === 'default') {
4343
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
44-
// this.log(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
44+
// this.log.debug(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
4545
}
4646
// Hack to handle closures and keep params from changing
4747
const params = Object.assign({}, p)
@@ -72,7 +72,7 @@ module.exports = class Branches {
7272
return Promise.resolve(resArray)
7373
}
7474
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
75-
return this.github.repos.updateBranchProtection(params).then(res => this.log(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.log.error(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
75+
return this.github.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.log.error(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
7676
}).catch((e) => {
7777
if (e.status === 404) {
7878
Object.assign(params, branch.protection, { headers: previewHeaders })
@@ -81,7 +81,7 @@ module.exports = class Branches {
8181
return Promise.resolve(resArray)
8282
}
8383
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
84-
return this.github.repos.updateBranchProtection(params).then(res => this.log(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.log.error(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
84+
return this.github.repos.updateBranchProtection(params).then(res => this.log.debug(`Branch protection applied successfully ${JSON.stringify(res.url)}`)).catch(e => { this.log.error(`Error applying branch protection ${JSON.stringify(e)}`); return [] })
8585
} else {
8686
this.log.error(e)
8787
// return

lib/plugins/diffable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module.exports = class Diffable {
2727
this.github = github
2828
this.repo = repo
2929
this.entries = entries
30-
this.log = log
30+
this.log = log.child({ context: this.constructor.name, repository: this.repo.repo })
3131
this.nop = nop
3232
}
3333

lib/plugins/repository.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ module.exports = class Repository {
5050
this.topics = this.settings.topics
5151
this.security = this.settings.security
5252
this.repo = repo
53-
this.log = log
53+
this.log = log.child({ context: 'Repository', repository: this.repo.repo })
5454
this.nop = nop
5555
this.force_create = this.settings.force_create
5656
this.template = this.settings.template
@@ -82,18 +82,18 @@ module.exports = class Repository {
8282
this.log.debug(`Result of comparing topics for changes source ${JSON.stringify(resp.data.topics)} target ${JSON.stringify(this.topics)} = ${topicResults}`)
8383

8484
if (this.nop && changes.hasChanges) {
85-
resArray.push(new NopCommand('Repository', this.repo, null, `Repo settings changes: ${results}`))
85+
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] Repo settings changes: ${results}`))
8686
}
8787
if (this.nop && topicChanges.hasChanges) {
88-
resArray.push(new NopCommand('Repository', this.repo, null, `Repo topics changes ${topicResults}`))
88+
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] Repo topics changes ${topicResults}`))
8989
}
9090
const promises = []
9191
if (changes.hasChanges) {
9292
this.log.debug('There are repo changes')
9393
if (this.settings.default_branch && (resp.data.default_branch !== this.settings.default_branch)) {
9494
this.log.debug('There is a rename of the default branch')
9595
if (this.nop) {
96-
resArray.push(new NopCommand('Repository', this.repo, this.github.repos.renameBranch.endpoint(resp.data.default_branch, this.settings.default_branch), `Repo rename default branch to ${this.settings.default_branch}`))
96+
resArray.push(new NopCommand('Repository', this.repo, this.github.repos.renameBranch.endpoint(resp.data.default_branch, this.settings.default_branch), `[${this.settings.name}] Repo rename default branch to ${this.settings.default_branch}`))
9797
} else {
9898
promises.push(this.renameBranch(resp.data.default_branch, this.settings.default_branch))
9999
}
@@ -111,7 +111,7 @@ module.exports = class Repository {
111111
} else {
112112
this.log.debug(`There are no changes for repo ${JSON.stringify(this.repo)}.`)
113113
if (this.nop) {
114-
resArray.push(new NopCommand('Repository', this.repo, null, `There are no changes for repo ${JSON.stringify(this.repo)}.`))
114+
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] There are no changes for repo ${JSON.stringify(this.repo)}.`))
115115
}
116116
}
117117
if (this.nop) {
@@ -123,7 +123,7 @@ module.exports = class Repository {
123123
if (e.status === 404) {
124124
if (this.force_create) {
125125
if (this.template) {
126-
this.log(`Creating repo using template ${this.template}`)
126+
this.log.debug(`Creating repo using template ${this.template}`)
127127
const options = { template_owner: this.repo.owner, template_repo: this.template, owner: this.repo.owner, name: this.repo.repo, private: (this.settings.private ? this.settings.private : true), description: this.settings.description ? this.settings.description : '' }
128128

129129
if (this.nop) {
@@ -136,7 +136,7 @@ module.exports = class Repository {
136136
// https://docs.github.com/en/rest/repos/repos#create-an-organization-repository uses org instead of owner like
137137
// the API to create a repo with a template
138138
this.settings.org = this.settings.owner
139-
this.log('Creating repo with settings ', this.settings)
139+
this.log.debug('Creating repo with settings ', this.settings)
140140
if (this.nop) {
141141
this.log.debug(`Creating Repo ${JSON.stringify(this.github.repos.createInOrg.endpoint(this.settings))} `)
142142
return Promise.resolve([
@@ -159,19 +159,19 @@ module.exports = class Repository {
159159
}
160160

161161
renameBranch (oldname, newname) {
162-
const parms = {
162+
const params = {
163163
owner: this.settings.owner,
164164
repo: this.settings.repo,
165165
branch: oldname,
166166
new_name: newname
167167
}
168-
this.log.debug(`Rename default branch repo with settings ${JSON.stringify(parms)}`)
168+
this.log.debug(`Rename default branch repo with settings ${JSON.stringify(params)}`)
169169
if (this.nop) {
170170
return Promise.resolve([
171-
new NopCommand(this.constructor.name, this.repo, this.github.repos.renameBranch.endpoint(parms), 'Rename Branch')
171+
new NopCommand(this.constructor.name, this.repo, this.github.repos.renameBranch.endpoint(params), 'Rename Branch')
172172
])
173173
}
174-
return this.github.repos.renameBranch(parms)
174+
return this.github.repos.renameBranch(params)
175175
}
176176

177177
updaterepo (resArray) {
@@ -196,14 +196,14 @@ module.exports = class Repository {
196196
if (this.topics) {
197197
if (repoData.data?.topics.length !== this.topics.length ||
198198
!repoData.data?.topics.every(t => this.topics.includes(t))) {
199-
this.log(`Updating repo with topics ${this.topics.join(',')}`)
199+
this.log.debug(`Updating repo with topics ${this.topics.join(',')}`)
200200
if (this.nop) {
201201
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.replaceAllTopics.endpoint(parms), 'Update Topics')))
202202
return Promise.resolve(resArray)
203203
}
204204
return this.github.repos.replaceAllTopics(parms)
205205
} else {
206-
this.log(`no need to update topics for ${repoData.data.name}`)
206+
this.log.debug(`no need to update topics for ${repoData.data.name}`)
207207
if (this.nop) {
208208
resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update topics for ${repoData.data.name}`)))
209209
return Promise.resolve(resArray)
@@ -215,15 +215,15 @@ module.exports = class Repository {
215215
// Added support for Code Security and Analysis
216216
updateSecurity (repoData, resArray) {
217217
if (this.security) {
218-
this.log(`Found repo with security settings ${JSON.stringify(this.security)}`)
218+
this.log.debug(`Found repo with security settings ${JSON.stringify(this.security)}`)
219219
if (this.security?.enableVulnerabilityAlerts === true || this.security?.enableVulnerabilityAlerts === false) {
220220
if (this.security.enableVulnerabilityAlerts === true) {
221-
this.log(`Enabling Dependabot alerts for owner: ${repoData.owner.login} and repo ${repoData.name}`)
221+
this.log.debug(`Enabling Dependabot alerts for owner: ${repoData.owner.login} and repo ${repoData.name}`)
222222
if (this.nop) {
223223
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.enableVulnerabilityAlerts.endpoint({
224224
owner: repoData.owner.login,
225225
repo: repoData.name
226-
}), 'Update Topics')))
226+
}), 'Update Security')))
227227
return Promise.resolve(resArray)
228228
}
229229
return this.github.repos.enableVulnerabilityAlerts({
@@ -232,12 +232,12 @@ module.exports = class Repository {
232232
})
233233
}
234234
if (this.security.enableVulnerabilityAlerts === false) {
235-
this.log(`Disabling Dependabot alerts for for owner: ${repoData.owner.login} and repo ${repoData.name}`)
235+
this.log.debug(`Disabling Dependabot alerts for for owner: ${repoData.owner.login} and repo ${repoData.name}`)
236236
if (this.nop) {
237237
resArray.push((new NopCommand(this.constructor.name, this.github.repos.disableVulnerabilityAlerts.endpoint({
238238
owner: repoData.owner.login,
239239
repo: repoData.name
240-
}), 'Update Topics')))
240+
}), 'Update Security')))
241241
return Promise.resolve(resArray)
242242
}
243243
return this.github.repos.disableVulnerabilityAlerts({
@@ -246,7 +246,7 @@ module.exports = class Repository {
246246
})
247247
}
248248
} else {
249-
this.log(`no need to update security for ${repoData.name}`)
249+
this.log.debug(`no need to update security for ${repoData.name}`)
250250
if (this.nop) {
251251
resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update security for ${repoData.name}`)))
252252
return Promise.resolve(resArray)

lib/plugins/validator.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = class Validator {
66
// this.regex = /[a-zA-Z0-9_-]+_\w[a-zA-Z0-9_-]+.*/gm
77
this.regex = new RegExp(this.pattern, 'gm')
88
this.repo = repo
9-
this.log = log
9+
this.log = log.child({ context: 'Validator', repository: this.repo.repo })
1010
this.nop = nop
1111
}
1212

@@ -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.warn(`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}`)
@@ -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.error(`Error in Validation checking ${error}`)
6363
}
6464
}
6565
}

lib/settings.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Settings {
5151
if (suborg) {
5252
this.subOrgConfigMap = [suborg]
5353
}
54-
this.log = context.log
54+
this.log = context.log.child({ context: 'Settings', repository: this.repo.repo })
5555
this.results = []
5656
this.configvalidators = {}
5757
this.overridevalidators = {}
@@ -198,7 +198,7 @@ ${this.results.reduce((x, y) => {
198198

199199
// If suborg config has been updated then only restrict to the repos for that suborg
200200
if (this.subOrgConfigMap && !subOrgConfig) {
201-
this.log.debug(`Skipping... SubOrg config changed but this repo is not part of it. ${JSON.stringify(repo)} suborg config ${JSON.stringify(this.subOrgConfigMap)}`)
201+
this.log.info(`Skipping... SubOrg config changed but this repo is not part of it. ${JSON.stringify(repo)} suborg config ${JSON.stringify(this.subOrgConfigMap)}`)
202202
return
203203
}
204204

@@ -286,10 +286,10 @@ ${this.results.reduce((x, y) => {
286286
if (Array.isArray(baseConfig) && Array.isArray(config)) {
287287
for (const baseEntry of baseConfig) {
288288
const newEntry = config.find(e => e.name === baseEntry.name)
289-
this.validate(section, baseEntry, newEntry)
289+
this.validate(repoName, section, baseEntry, newEntry)
290290
}
291291
} else {
292-
this.validate(section, baseConfig, config)
292+
this.validate(repoName, section, baseConfig, config)
293293
}
294294
if (section !== 'repositories' && section !== 'repository') {
295295
// Ignore any config that is not a plugin
@@ -303,7 +303,7 @@ ${this.results.reduce((x, y) => {
303303
return childPlugins
304304
}
305305

306-
validate (section, baseConfig, overrideConfig) {
306+
validate (repoName, section, baseConfig, overrideConfig) {
307307
const configValidator = this.configvalidators[section]
308308
if (configValidator) {
309309
this.log.debug(`Calling configvalidator for key ${section} `)
@@ -328,7 +328,7 @@ ${this.results.reduce((x, y) => {
328328
if (Array.isArray(restrictedRepos)) {
329329
// For backward compatibility support the old format
330330
if (restrictedRepos.includes(repoName)) {
331-
this.log.debug(`Skipping retricted repo ${repoName}`)
331+
this.log.info(`Skipping retricted repo ${repoName}`)
332332
return true
333333
} else {
334334
this.log.debug(`${repoName} not in restricted repos ${restrictedRepos}`)
@@ -339,12 +339,12 @@ ${this.results.reduce((x, y) => {
339339
this.log.debug(`Allowing ${repoName} in restrictedRepos.include [${restrictedRepos.include}]`)
340340
return false
341341
} else {
342-
this.log.debug(`Skipping repo ${repoName} not in restrictedRepos.include`)
342+
this.log.info(`Skipping repo ${repoName} not in restrictedRepos.include`)
343343
return true
344344
}
345345
} else if (Array.isArray(restrictedRepos.exclude)) {
346346
if (this.includesRepo(repoName, restrictedRepos.exclude)) {
347-
this.log.debug(`Skipping excluded repo ${repoName} in restrictedRepos.exclude`)
347+
this.log.info(`Skipping excluded repo ${repoName} in restrictedRepos.exclude`)
348348
return true
349349
} else {
350350
this.log.debug(`Allowing ${repoName} not in restrictedRepos.exclude [${restrictedRepos.exclude}]`)
@@ -453,8 +453,8 @@ ${this.results.reduce((x, y) => {
453453

454454
// read the repo contents using tree
455455
this.log.debug(`repos directory info ${JSON.stringify(repoDirInfo)}`)
456-
//const endpoint = `/repos/${this.repo.owner}/${repo.repo}/git/trees/${repoDirInfo.sha}`
457-
//this.log.debug(`endpoint: ${endpoint}`)
456+
// const endpoint = `/repos/${this.repo.owner}/${repo.repo}/git/trees/${repoDirInfo.sha}`
457+
// this.log.debug(`endpoint: ${endpoint}`)
458458
const treeParams = Object.assign(repo, { tree_sha: repoDirInfo.sha, recursive: 0 })
459459
const response = await this.github.git.getTree(treeParams).catch(e => {
460460
this.log.debug(`Error getting settings ${JSON.stringify(this.github.git.getTree.endpoint(treeParams))} ${e}`)

lib/validator.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = class Validator {
55
// this.regex = /[a-zA-Z0-9_-]+_\w[a-zA-Z0-9_-]+.*/gm
66
this.regex = new RegExp(this.pattern, 'gm')
77
this.repo = repo
8-
this.log = log
8+
this.log = log.child({ context: 'Validator', repository: this.repo.repo })
99
}
1010

1111
sync () {
@@ -18,7 +18,7 @@ module.exports = class Validator {
1818
}
1919
}).then(res => {
2020
if (this.repo.repo.search(this.regex) >= 0) {
21-
this.log(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
21+
this.log.debug(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
2222
if (res.data.names.find(x => x === 'validation-error')) {
2323
res.data.names = res.data.names.filter(x => x !== 'validation-error')
2424
return this.github.repos.replaceAllTopics({
@@ -31,7 +31,7 @@ module.exports = class Validator {
3131
})
3232
}
3333
} else {
34-
this.log(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
34+
this.log.error(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
3535
if (!res.data.names.find(x => x === 'validation-error')) {
3636
res.data.names.push('validation-error')
3737
return this.github.repos.replaceAllTopics({
@@ -46,7 +46,7 @@ module.exports = class Validator {
4646
}
4747
})
4848
} catch (error) {
49-
this.log(`Error in Validation checking ${error}`)
49+
this.log.error(`Error in Validation checking ${error}`)
5050
}
5151
}
5252
}

0 commit comments

Comments
 (0)