Skip to content
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

chore(logs): improve log messages #413

Open
wants to merge 7 commits into
base: main-enterprise
Choose a base branch
from
Open
Show file tree
Hide file tree
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
158 changes: 91 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
7 changes: 7 additions & 0 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Base class for all plugins

module.exports = class Plugin {
constructor (log, repoName) {
this.log = log.child({ plugin: this.constructor.name, repository: repoName })
}
}
13 changes: 7 additions & 6 deletions lib/plugins/branches.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const Plugin = require('../plugin')
const NopCommand = require('../nopcommand')
const MergeDeep = require('../mergeDeep')
const ignorableFields = []
const previewHeaders = { accept: 'application/vnd.github.hellcat-preview+json,application/vnd.github.luke-cage-preview+json,application/vnd.github.zzzax-preview+json' }

module.exports = class Branches {
module.exports = class Branches extends Plugin {
constructor (nop, github, repo, settings, log) {
super(log, repo.repo)
this.github = github
this.repo = repo
this.branches = settings
this.log = log
this.nop = nop
}

Expand All @@ -24,7 +25,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 +42,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 +73,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 +82,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
5 changes: 3 additions & 2 deletions lib/plugins/diffable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
// remove(existing) {
// }
// }
const Plugin = require('../plugin')
const MergeDeep = require('../mergeDeep')
const NopCommand = require('../nopcommand')
const ignorableFields = ['id', 'node_id', 'default', 'url']
module.exports = class Diffable {
module.exports = class Diffable extends Plugin {
constructor (nop, github, repo, entries, log) {
super(log, repo.repo)
this.github = github
this.repo = repo
this.entries = entries
this.log = log
this.nop = nop
}

Expand Down
41 changes: 21 additions & 20 deletions lib/plugins/repository.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// const { restEndpointMethods } = require('@octokit/plugin-rest-endpoint-methods')
// const EndPoints = require('@octokit/plugin-rest-endpoint-methods')
const Plugin = require('../plugin')
const NopCommand = require('../nopcommand')
const MergeDeep = require('../mergeDeep')
const ignorableFields = [
Expand Down Expand Up @@ -42,15 +43,15 @@ const ignorableFields = [
'repo'
]

module.exports = class Repository {
module.exports = class Repository extends Plugin {
constructor (nop, github, repo, settings, installationId, log) {
super(log, repo.repo)
this.installationId = installationId
this.github = github
this.settings = Object.assign({ mediaType: { previews: ['nebula-preview'] } }, settings, repo)
this.topics = this.settings.topics
this.security = this.settings.security
this.repo = repo
this.log = log
this.nop = nop
this.force_create = this.settings.force_create
this.template = this.settings.template
Expand Down Expand Up @@ -82,18 +83,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 +115,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 +127,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 +140,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 +163,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 +200,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 +219,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 +235,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,12 +250,12 @@ 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)
}
}
}
}

updateAutomatedSecurityFixes(repoData, resArray) {
Expand Down Expand Up @@ -289,4 +290,4 @@ module.exports = class Repository {
}
}
}
}
}
11 changes: 6 additions & 5 deletions lib/plugins/validator.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const Plugin = require('../plugin')
const NopCommand = require('../nopcommand')
module.exports = class Validator {
module.exports = class Validator extends Plugin {
constructor (nop, github, repo, settings, log) {
super(log, repo.repo)
this.github = github
this.pattern = settings.pattern
// 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.nop = nop
}

Expand All @@ -20,7 +21,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 +39,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 +60,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unused to me. Maybe I'm missing something here :)

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
Loading