Skip to content

Commit 6c297f2

Browse files
sshrestha02Sudhansu Shrestha
andauthored
Allow github context in configValidator and overrideValidator (#584)
* 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 <sudhansushrestha@sudhansus-mbp.lan>
1 parent b8abc1b commit 6c297f2

File tree

6 files changed

+180
-35
lines changed

6 files changed

+180
-35
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,6 @@ yarn.lock
136136
# aws sam stuff
137137
.aws-sam
138138
samconfig.toml
139+
140+
# test file to be ignored
141+
test.log

lib/deploymentConfig.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class DeploymentConfig {
2323
if (this.isIterable(overridevalidators)) {
2424
for (const validator of overridevalidators) {
2525
// eslint-disable-next-line no-new-func
26-
const f = new Function('baseconfig', 'overrideconfig', validator.script)
26+
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
2727
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
2828
}
2929
}
3030
const configvalidators = this.config.configvalidators
3131
if (this.isIterable(configvalidators)) {
3232
for (const validator of configvalidators) {
3333
// eslint-disable-next-line no-new-func
34-
const f = new Function('baseconfig', validator.script)
34+
const f = new Function('baseconfig', 'githubContext', validator.script)
3535
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
3636
}
3737
}

lib/mergeArrayBy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ function findMatchingIndex (properties, sourceItem, target) {
1111
}
1212
}
1313

14-
function mergeBy (key, configvalidator, overridevalidator, properties, target, source, options) {
14+
function mergeBy (key, configvalidator, overridevalidator, properties, target, source, options, githubContext) {
1515
const destination = target.slice()
1616
source.forEach(sourceItem => {
1717
const matchingIndex = findMatchingIndex(properties, sourceItem, target)
1818
if (matchingIndex > -1) {
1919
if (overridevalidator) {
20-
if (!overridevalidator.canOverride(target[matchingIndex], sourceItem)) {
20+
if (!overridevalidator.canOverride(target[matchingIndex], sourceItem, githubContext)) {
2121
throw new Error(overridevalidator.error)
2222
}
2323
}
2424
destination[matchingIndex] = merge(target[matchingIndex], sourceItem, options)
2525
if (configvalidator) {
26-
if (!configvalidator.isValid(destination[matchingIndex])) {
26+
if (!configvalidator.isValid(destination[matchingIndex], githubContext)) {
2727
throw new Error(configvalidator.error)
2828
}
2929
}
3030
} else {
3131
destination.push(sourceItem)
3232
if (configvalidator) {
33-
if (!configvalidator.isValid(sourceItem)) {
33+
if (!configvalidator.isValid(sourceItem, githubContext)) {
3434
throw new Error(configvalidator.error)
3535
}
3636
}

lib/mergeDeep.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ const NAME_USERNAME_PROPERTY = item => NAME_FIELDS.find(prop => Object.prototype
66
const GET_NAME_USERNAME_PROPERTY = item => { if (NAME_USERNAME_PROPERTY(item)) return item[NAME_USERNAME_PROPERTY(item)] }
77

88
class MergeDeep {
9-
constructor (log, ignorableFields, configvalidators = {}, overridevalidators = {}) {
9+
constructor (log, github, ignorableFields, configvalidators = {}, overridevalidators = {}) {
1010
this.log = log
11+
this.github = github
1112
this.ignorableFields = ignorableFields
1213
this.configvalidators = DeploymentConfig.configvalidators
1314
this.overridevalidators = DeploymentConfig.overridevalidators
@@ -269,7 +270,7 @@ class MergeDeep {
269270
}
270271
if (this.overridevalidators[key]) {
271272
// this.log.debug(`Calling overridevalidator for key ${key} `)
272-
if (!this.overridevalidators[key].canOverride(baseconfig, overrideconfig)) {
273+
if (!this.overridevalidators[key].canOverride(baseconfig, overrideconfig, this.github)) {
273274
this.log.error(`Error in calling overridevalidator for key ${key} ${this.overridevalidators[key].error}`)
274275
throw new Error(this.overridevalidators[key].error)
275276
}
@@ -279,7 +280,7 @@ class MergeDeep {
279280
validateConfig (key, baseconfig) {
280281
if (this.configvalidators[key]) {
281282
// this.log.debug(`Calling configvalidator for key ${key} `)
282-
if (!this.configvalidators[key].isValid(baseconfig)) {
283+
if (!this.configvalidators[key].isValid(baseconfig, this.github)) {
283284
this.log.error(`Error in calling configvalidator for key ${key} ${this.configvalidators[key].error}`)
284285
throw new Error(this.configvalidators[key].error)
285286
}
@@ -311,7 +312,7 @@ class MergeDeep {
311312
// Deep merge Array so that if the same element is there in source and target,
312313
// override the target with source otherwise include both source and target elements
313314
if (Array.isArray(source[key]) && Array.isArray(target[key])) {
314-
const combined = mergeBy(key, this.configvalidators[key], this.overridevalidators[key], NAME_FIELDS, target[key], source[key])
315+
const combined = mergeBy(key, this.configvalidators[key], this.overridevalidators[key], NAME_FIELDS, target[key], source[key], this.github)
315316
Object.assign(target, {
316317
[key]: combined
317318
})

lib/settings.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,20 @@ class Settings {
7777
if (this.isIterable(overridevalidators)) {
7878
for (const validator of overridevalidators) {
7979
// eslint-disable-next-line no-new-func
80-
const f = new Function('baseconfig', 'overrideconfig', validator.script)
80+
const f = new Function('baseconfig', 'overrideconfig', 'githubContext', validator.script)
8181
this.overridevalidators[validator.plugin] = { canOverride: f, error: validator.error }
8282
}
8383
}
8484
const configvalidators = config.configvalidators
8585
if (this.isIterable(configvalidators)) {
8686
for (const validator of configvalidators) {
87+
this.log.debug(`Logging each script: ${typeof validator.script}`)
8788
// eslint-disable-next-line no-new-func
88-
const f = new Function('baseconfig', validator.script)
89+
const f = new Function('baseconfig', 'githubContext', validator.script)
8990
this.configvalidators[validator.plugin] = { isValid: f, error: validator.error }
9091
}
9192
}
92-
this.mergeDeep = new MergeDeep(this.log, [], this.configvalidators, this.overridevalidators)
93+
this.mergeDeep = new MergeDeep(this.log, this.github, [], this.configvalidators, this.overridevalidators)
9394
}
9495

9596
// Create a check in the Admin repo for safe-settings.
@@ -411,15 +412,15 @@ ${this.results.reduce((x, y) => {
411412
const configValidator = this.configvalidators[section]
412413
if (configValidator) {
413414
this.log.debug(`Calling configvalidator for key ${section} `)
414-
if (!configValidator.isValid(overrideConfig)) {
415+
if (!configValidator.isValid(overrideConfig, this.github)) {
415416
this.log.error(`Error in calling configvalidator for key ${section} ${configValidator.error}`)
416417
throw new Error(configValidator.error)
417418
}
418419
}
419420
const overridevalidator = this.overridevalidators[section]
420421
if (overridevalidator) {
421422
this.log.debug(`Calling overridevalidator for key ${section} `)
422-
if (!overridevalidator.canOverride(baseConfig, overrideConfig)) {
423+
if (!overridevalidator.canOverride(baseConfig, overrideConfig, this.github)) {
423424
this.log.error(`Error in calling overridevalidator for key ${section} ${overridevalidator.error}`)
424425
throw new Error(overridevalidator.error)
425426
}

0 commit comments

Comments
 (0)