Skip to content

Commit

Permalink
Allow github context in configValidator and overrideValidator (#584)
Browse files Browse the repository at this point in the history
* init

* remove fdic refs in package log

* remove test.log

* update gitignore to include test.log

* fix all mergeDeep tests

* revert validator.test.js to main-enterprise

* revert public true flag to false

* fix formatting for pr

---------

Co-authored-by: Sudhansu Shrestha <[email protected]>
  • Loading branch information
sshrestha02 and Sudhansu Shrestha authored Feb 5, 2024
1 parent b8abc1b commit 6c297f2
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,6 @@ yarn.lock
# aws sam stuff
.aws-sam
samconfig.toml

# test file to be ignored
test.log
4 changes: 2 additions & 2 deletions lib/deploymentConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class DeploymentConfig {
if (this.isIterable(overridevalidators)) {
for (const validator of overridevalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'overrideconfig', validator.script)
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
}
}
const configvalidators = this.config.configvalidators
if (this.isIterable(configvalidators)) {
for (const validator of configvalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', validator.script)
const f = new Function('baseconfig', 'githubContext', validator.script)
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/mergeArrayBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ function findMatchingIndex (properties, sourceItem, target) {
}
}

function mergeBy (key, configvalidator, overridevalidator, properties, target, source, options) {
function mergeBy (key, configvalidator, overridevalidator, properties, target, source, options, githubContext) {
const destination = target.slice()
source.forEach(sourceItem => {
const matchingIndex = findMatchingIndex(properties, sourceItem, target)
if (matchingIndex > -1) {
if (overridevalidator) {
if (!overridevalidator.canOverride(target[matchingIndex], sourceItem)) {
if (!overridevalidator.canOverride(target[matchingIndex], sourceItem, githubContext)) {
throw new Error(overridevalidator.error)
}
}
destination[matchingIndex] = merge(target[matchingIndex], sourceItem, options)
if (configvalidator) {
if (!configvalidator.isValid(destination[matchingIndex])) {
if (!configvalidator.isValid(destination[matchingIndex], githubContext)) {
throw new Error(configvalidator.error)
}
}
} else {
destination.push(sourceItem)
if (configvalidator) {
if (!configvalidator.isValid(sourceItem)) {
if (!configvalidator.isValid(sourceItem, githubContext)) {
throw new Error(configvalidator.error)
}
}
Expand Down
9 changes: 5 additions & 4 deletions lib/mergeDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ const NAME_USERNAME_PROPERTY = item => NAME_FIELDS.find(prop => Object.prototype
const GET_NAME_USERNAME_PROPERTY = item => { if (NAME_USERNAME_PROPERTY(item)) return item[NAME_USERNAME_PROPERTY(item)] }

class MergeDeep {
constructor (log, ignorableFields, configvalidators = {}, overridevalidators = {}) {
constructor (log, github, ignorableFields, configvalidators = {}, overridevalidators = {}) {
this.log = log
this.github = github
this.ignorableFields = ignorableFields
this.configvalidators = DeploymentConfig.configvalidators
this.overridevalidators = DeploymentConfig.overridevalidators
Expand Down Expand Up @@ -269,7 +270,7 @@ class MergeDeep {
}
if (this.overridevalidators[key]) {
// this.log.debug(`Calling overridevalidator for key ${key} `)
if (!this.overridevalidators[key].canOverride(baseconfig, overrideconfig)) {
if (!this.overridevalidators[key].canOverride(baseconfig, overrideconfig, this.github)) {
this.log.error(`Error in calling overridevalidator for key ${key} ${this.overridevalidators[key].error}`)
throw new Error(this.overridevalidators[key].error)
}
Expand All @@ -279,7 +280,7 @@ class MergeDeep {
validateConfig (key, baseconfig) {
if (this.configvalidators[key]) {
// this.log.debug(`Calling configvalidator for key ${key} `)
if (!this.configvalidators[key].isValid(baseconfig)) {
if (!this.configvalidators[key].isValid(baseconfig, this.github)) {
this.log.error(`Error in calling configvalidator for key ${key} ${this.configvalidators[key].error}`)
throw new Error(this.configvalidators[key].error)
}
Expand Down Expand Up @@ -311,7 +312,7 @@ class MergeDeep {
// Deep merge Array so that if the same element is there in source and target,
// override the target with source otherwise include both source and target elements
if (Array.isArray(source[key]) && Array.isArray(target[key])) {
const combined = mergeBy(key, this.configvalidators[key], this.overridevalidators[key], NAME_FIELDS, target[key], source[key])
const combined = mergeBy(key, this.configvalidators[key], this.overridevalidators[key], NAME_FIELDS, target[key], source[key], this.github)
Object.assign(target, {
[key]: combined
})
Expand Down
11 changes: 6 additions & 5 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,20 @@ class Settings {
if (this.isIterable(overridevalidators)) {
for (const validator of overridevalidators) {
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', 'overrideconfig', validator.script)
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
}
}
const configvalidators = config.configvalidators
if (this.isIterable(configvalidators)) {
for (const validator of configvalidators) {
this.log.debug(`Logging each script: ${typeof validator.script}`)
// eslint-disable-next-line no-new-func
const f = new Function('baseconfig', validator.script)
const f = new Function('baseconfig', 'githubContext', validator.script)
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
}
}
this.mergeDeep = new MergeDeep(this.log, [], this.configvalidators, this.overridevalidators)
this.mergeDeep = new MergeDeep(this.log, this.github, [], this.configvalidators, this.overridevalidators)
}

// Create a check in the Admin repo for safe-settings.
Expand Down Expand Up @@ -411,15 +412,15 @@ ${this.results.reduce((x, y) => {
const configValidator = this.configvalidators[section]
if (configValidator) {
this.log.debug(`Calling configvalidator for key ${section} `)
if (!configValidator.isValid(overrideConfig)) {
if (!configValidator.isValid(overrideConfig, this.github)) {
this.log.error(`Error in calling configvalidator for key ${section} ${configValidator.error}`)
throw new Error(configValidator.error)
}
}
const overridevalidator = this.overridevalidators[section]
if (overridevalidator) {
this.log.debug(`Calling overridevalidator for key ${section} `)
if (!overridevalidator.canOverride(baseConfig, overrideConfig)) {
if (!overridevalidator.canOverride(baseConfig, overrideConfig, this.github)) {
this.log.error(`Error in calling overridevalidator for key ${section} ${overridevalidator.error}`)
throw new Error(overridevalidator.error)
}
Expand Down
Loading

0 comments on commit 6c297f2

Please sign in to comment.