Skip to content

Commit

Permalink
chore(logs): improve log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
martinm82 committed Apr 26, 2023
1 parent ef55548 commit 0462191
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 116 deletions.
156 changes: 89 additions & 67 deletions index.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/configManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module.exports = class ConfigManager {
constructor (context, ref) {
this.context = context
this.ref = ref
this.log = context.log
this.log = context.log.child({ context: 'ConfigManager' })
}

/**
* Loads a file from GitHub
*
* @param params Params to fetch the file with
* @return The parsed YAML file
*/
* Loads a file from GitHub
*
* @param params Params to fetch the file with
* @return The parsed YAML file
*/
async loadYaml (filePath) {
try {
const repo = { owner: this.context.repo().owner, repo: env.ADMIN_REPO }
Expand Down
2 changes: 1 addition & 1 deletion lib/mergeDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const mergeBy = require('./mergeArrayBy')
const NAME_FIELDS = ['name', 'username']
class MergeDeep {
constructor (log, ignorableFields, configvalidators = {}, overridevalidators = {}) {
this.log = log
this.log = log.child({ context: 'MergeDeep' })
this.ignorableFields = ignorableFields
this.configvalidators = configvalidators
this.overridevalidators = overridevalidators
Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = class Branches {
this.github = github
this.repo = repo
this.branches = settings
this.log = log
this.log = log.child({ context: 'Branches', repository: this.repo.repo })
this.nop = nop
}

Expand All @@ -24,7 +24,7 @@ module.exports = class Branches {
let p = Object.assign(this.repo, { branch: branch.name })
if (branch.name === 'default') {
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
this.log(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
this.log.debug(`Deleting default branch protection for branch ${currentRepo.data.default_branch}`)
}
// Hack to handle closures and keep params from changing
const params = Object.assign({}, p)
Expand All @@ -41,7 +41,7 @@ module.exports = class Branches {
let p = Object.assign(this.repo, { branch: branch.name })
if (branch.name === 'default') {
p = Object.assign(this.repo, { branch: currentRepo.data.default_branch })
// this.log(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
// this.log.debug(`Setting default branch protection for branch ${currentRepo.data.default_branch}`)
}
// Hack to handle closures and keep params from changing
const params = Object.assign({}, p)
Expand Down Expand Up @@ -72,7 +72,7 @@ module.exports = class Branches {
return Promise.resolve(resArray)
}
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
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 [] })
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 [] })
}).catch((e) => {
if (e.status === 404) {
Object.assign(params, branch.protection, { headers: previewHeaders })
Expand All @@ -81,7 +81,7 @@ module.exports = class Branches {
return Promise.resolve(resArray)
}
this.log.debug(`Adding branch protection ${JSON.stringify(params)}`)
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 [] })
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 [] })
} else {
this.log.error(e)
// return
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/diffable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module.exports = class Diffable {
this.github = github
this.repo = repo
this.entries = entries
this.log = log
this.log = log.child({ context: this.constructor.name, repository: this.repo.repo })
this.nop = nop
}

Expand Down
36 changes: 18 additions & 18 deletions lib/plugins/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = class Repository {
this.topics = this.settings.topics
this.security = this.settings.security
this.repo = repo
this.log = log
this.log = log.child({ context: 'Repository', repository: this.repo.repo })
this.nop = nop
this.force_create = this.settings.force_create
this.template = this.settings.template
Expand Down Expand Up @@ -82,18 +82,18 @@ module.exports = class Repository {
this.log.debug(`Result of comparing topics for changes source ${JSON.stringify(resp.data.topics)} target ${JSON.stringify(this.topics)} = ${topicResults}`)

if (this.nop && changes.hasChanges) {
resArray.push(new NopCommand('Repository', this.repo, null, `Repo settings changes: ${results}`))
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] Repo settings changes: ${results}`))
}
if (this.nop && topicChanges.hasChanges) {
resArray.push(new NopCommand('Repository', this.repo, null, `Repo topics changes ${topicResults}`))
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] Repo topics changes ${topicResults}`))
}
const promises = []
if (changes.hasChanges) {
this.log.debug('There are repo changes')
if (this.settings.default_branch && (resp.data.default_branch !== this.settings.default_branch)) {
this.log.debug('There is a rename of the default branch')
if (this.nop) {
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}`))
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}`))
} else {
promises.push(this.renameBranch(resp.data.default_branch, this.settings.default_branch))
}
Expand All @@ -114,7 +114,7 @@ module.exports = class Repository {
} else {
this.log.debug(`There are no changes for repo ${JSON.stringify(this.repo)}.`)
if (this.nop) {
resArray.push(new NopCommand('Repository', this.repo, null, `There are no changes for repo ${JSON.stringify(this.repo)}.`))
resArray.push(new NopCommand('Repository', this.repo, null, `[${this.settings.name}] There are no changes for repo ${JSON.stringify(this.repo)}.`))
}
}
if (this.nop) {
Expand All @@ -126,7 +126,7 @@ module.exports = class Repository {
if (e.status === 404) {
if (this.force_create) {
if (this.template) {
this.log(`Creating repo using template ${this.template}`)
this.log.debug(`Creating repo using template ${this.template}`)
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 : '' }

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

renameBranch (oldname, newname) {
const parms = {
const params = {
owner: this.settings.owner,
repo: this.settings.repo,
branch: oldname,
new_name: newname
}
this.log.debug(`Rename default branch repo with settings ${JSON.stringify(parms)}`)
this.log.debug(`Rename default branch repo with settings ${JSON.stringify(params)}`)
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, this.github.repos.renameBranch.endpoint(parms), 'Rename Branch')
new NopCommand(this.constructor.name, this.repo, this.github.repos.renameBranch.endpoint(params), 'Rename Branch')
])
}
return this.github.repos.renameBranch(parms)
return this.github.repos.renameBranch(params)
}

updaterepo (resArray) {
Expand All @@ -199,14 +199,14 @@ module.exports = class Repository {
if (this.topics) {
if (repoData.data?.topics.length !== this.topics.length ||
!repoData.data?.topics.every(t => this.topics.includes(t))) {
this.log(`Updating repo with topics ${this.topics.join(',')}`)
this.log.debug(`Updating repo with topics ${this.topics.join(',')}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.replaceAllTopics.endpoint(parms), 'Update Topics')))
return Promise.resolve(resArray)
}
return this.github.repos.replaceAllTopics(parms)
} else {
this.log(`no need to update topics for ${repoData.data.name}`)
this.log.debug(`no need to update topics for ${repoData.data.name}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update topics for ${repoData.data.name}`)))
return Promise.resolve(resArray)
Expand All @@ -218,9 +218,9 @@ module.exports = class Repository {
// Added support for Code Security and Analysis
updateSecurity (repoData, resArray) {
if (this.security?.enableVulnerabilityAlerts === true || this.security?.enableVulnerabilityAlerts === false) {
this.log(`Found repo with security settings ${JSON.stringify(this.security)}`)
this.log.debug(`Found repo with security settings ${JSON.stringify(this.security)}`)
if (this.security.enableVulnerabilityAlerts === true) {
this.log(`Enabling Dependabot alerts for owner: ${repoData.owner.login} and repo ${repoData.name}`)
this.log.debug(`Enabling Dependabot alerts for owner: ${repoData.owner.login} and repo ${repoData.name}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, this.github.repos.enableVulnerabilityAlerts.endpoint({
owner: repoData.owner.login,
Expand All @@ -234,7 +234,7 @@ module.exports = class Repository {
})
}
else {
this.log(`Disabling Dependabot alerts for for owner: ${repoData.owner.login} and repo ${repoData.name}`)
this.log.debug(`Disabling Dependabot alerts for for owner: ${repoData.owner.login} and repo ${repoData.name}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.github.repos.disableVulnerabilityAlerts.endpoint({
owner: repoData.owner.login,
Expand All @@ -249,7 +249,7 @@ module.exports = class Repository {
}
}
else {
this.log(`no need to update security for ${repoData.name}`)
this.log.debug(`no need to update security for ${repoData.name}`)
if (this.nop) {
resArray.push((new NopCommand(this.constructor.name, this.repo, null, `no need to update security for ${repoData.name}`)))
return Promise.resolve(resArray)
Expand Down Expand Up @@ -289,4 +289,4 @@ module.exports = class Repository {
}
}
}
}
}
8 changes: 4 additions & 4 deletions lib/plugins/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = class Validator {
// this.regex = /[a-zA-Z0-9_-]+_\w[a-zA-Z0-9_-]+.*/gm
this.regex = new RegExp(this.pattern, 'gm')
this.repo = repo
this.log = log
this.log = log.child({ context: 'Validator', repository: this.repo.repo })
this.nop = nop
}

Expand All @@ -20,7 +20,7 @@ module.exports = class Validator {
}
}).then(res => {
if (this.repo.repo.search(this.regex) >= 0) {
this.log(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
this.log.debug(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, null, `Passed Validation for pattern ${this.pattern}`)
Expand All @@ -38,7 +38,7 @@ module.exports = class Validator {
})
}
} else {
this.log(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
this.log.warn(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
if (this.nop) {
return Promise.resolve([
new NopCommand(this.constructor.name, this.repo, null, `Failed Validation for pattern ${this.pattern}`)
Expand All @@ -59,7 +59,7 @@ module.exports = class Validator {
})
.catch(() => {})
} catch (error) {
this.log(`Error in Validation checking ${error}`)
this.log.error(`Error in Validation checking ${error}`)
}
}
}
20 changes: 10 additions & 10 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Settings {
if (suborg) {
this.subOrgConfigMap = [suborg]
}
this.log = context.log
this.log = context.log.child({ context: 'Settings', repository: this.repo.repo })
this.results = []
this.configvalidators = {}
this.overridevalidators = {}
Expand Down Expand Up @@ -198,7 +198,7 @@ ${this.results.reduce((x, y) => {

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

Expand Down Expand Up @@ -286,10 +286,10 @@ ${this.results.reduce((x, y) => {
if (Array.isArray(baseConfig) && Array.isArray(config)) {
for (const baseEntry of baseConfig) {
const newEntry = config.find(e => e.name === baseEntry.name)
this.validate(section, baseEntry, newEntry)
this.validate(repoName, section, baseEntry, newEntry)
}
} else {
this.validate(section, baseConfig, config)
this.validate(repoName, section, baseConfig, config)
}
if (section !== 'repositories' && section !== 'repository') {
// Ignore any config that is not a plugin
Expand All @@ -303,7 +303,7 @@ ${this.results.reduce((x, y) => {
return childPlugins
}

validate (section, baseConfig, overrideConfig) {
validate (repoName, section, baseConfig, overrideConfig) {
const configValidator = this.configvalidators[section]
if (configValidator) {
this.log.debug(`Calling configvalidator for key ${section} `)
Expand All @@ -328,7 +328,7 @@ ${this.results.reduce((x, y) => {
if (Array.isArray(restrictedRepos)) {
// For backward compatibility support the old format
if (restrictedRepos.includes(repoName)) {
this.log.debug(`Skipping retricted repo ${repoName}`)
this.log.info(`Skipping retricted repo ${repoName}`)
return true
} else {
this.log.debug(`${repoName} not in restricted repos ${restrictedRepos}`)
Expand All @@ -339,12 +339,12 @@ ${this.results.reduce((x, y) => {
this.log.debug(`Allowing ${repoName} in restrictedRepos.include [${restrictedRepos.include}]`)
return false
} else {
this.log.debug(`Skipping repo ${repoName} not in restrictedRepos.include`)
this.log.info(`Skipping repo ${repoName} not in restrictedRepos.include`)
return true
}
} else if (Array.isArray(restrictedRepos.exclude)) {
if (this.includesRepo(repoName, restrictedRepos.exclude)) {
this.log.debug(`Skipping excluded repo ${repoName} in restrictedRepos.exclude`)
this.log.info(`Skipping excluded repo ${repoName} in restrictedRepos.exclude`)
return true
} else {
this.log.debug(`Allowing ${repoName} not in restrictedRepos.exclude [${restrictedRepos.exclude}]`)
Expand Down Expand Up @@ -453,8 +453,8 @@ ${this.results.reduce((x, y) => {

// read the repo contents using tree
this.log.debug(`repos directory info ${JSON.stringify(repoDirInfo)}`)
//const endpoint = `/repos/${this.repo.owner}/${repo.repo}/git/trees/${repoDirInfo.sha}`
//this.log.debug(`endpoint: ${endpoint}`)
// const endpoint = `/repos/${this.repo.owner}/${repo.repo}/git/trees/${repoDirInfo.sha}`
// this.log.debug(`endpoint: ${endpoint}`)
const treeParams = Object.assign(repo, { tree_sha: repoDirInfo.sha, recursive: 0 })
const response = await this.github.git.getTree(treeParams).catch(e => {
this.log.debug(`Error getting settings ${JSON.stringify(this.github.git.getTree.endpoint(treeParams))} ${e}`)
Expand Down
8 changes: 4 additions & 4 deletions lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = class Validator {
// this.regex = /[a-zA-Z0-9_-]+_\w[a-zA-Z0-9_-]+.*/gm
this.regex = new RegExp(this.pattern, 'gm')
this.repo = repo
this.log = log
this.log = log.child({ context: 'Validator', repository: this.repo.repo })
}

sync () {
Expand All @@ -18,7 +18,7 @@ module.exports = class Validator {
}
}).then(res => {
if (this.repo.repo.search(this.regex) >= 0) {
this.log(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
this.log.debug(`Repo ${this.repo.repo} Passed Validation for pattern ${this.pattern}`)
if (res.data.names.find(x => x === 'validation-error')) {
res.data.names = res.data.names.filter(x => x !== 'validation-error')
return this.github.repos.replaceAllTopics({
Expand All @@ -31,7 +31,7 @@ module.exports = class Validator {
})
}
} else {
this.log(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
this.log.error(`Repo ${this.repo.repo} Failed Validation for pattern ${this.pattern}`)
if (!res.data.names.find(x => x === 'validation-error')) {
res.data.names.push('validation-error')
return this.github.repos.replaceAllTopics({
Expand All @@ -46,7 +46,7 @@ module.exports = class Validator {
}
})
} catch (error) {
this.log(`Error in Validation checking ${error}`)
this.log.error(`Error in Validation checking ${error}`)
}
}
}

0 comments on commit 0462191

Please sign in to comment.