-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpipeline_config_validator.js
237 lines (224 loc) · 6.94 KB
/
pipeline_config_validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
require('dotenv').config()
const logger = require('./config/logger')
const fs = require('fs')
const Ajv = require('ajv')
const ajv = Ajv({allErrors: true})
const cardRules = require('./app/rules/card')
class PipelineConfigValidator {
constructor() {
this.defaultRules = Object.keys(cardRules)
}
validate() {
this.checkEnvVariables()
const commonUtilities = require('./app/utilities/common')
// get all the configuration irrespective of scope
let pipelineConfig = commonUtilities.getConfig()
// validate scope level schema
this.validateScopeLevelSchema(pipelineConfig)
// validate company configuration
let companyConfig = pipelineConfig.company
this.checkPublisherWebookURL(companyConfig.defaults.messagePublisher)
this.validateCommonSchema(companyConfig)
this.validatePublisher(companyConfig)
this.validateRules(companyConfig)
// validate board configuration
let boardsConfig = pipelineConfig.boards
if(boardsConfig) {
Object.keys(boardsConfig).map((boardId, index) => {
let boardConfig = commonUtilities.getScopeConfig(boardId)
this.checkPublisherWebookURL(boardConfig.defaults.messagePublisher)
this.validateCommonSchema(boardConfig)
this.validatePublisher(boardConfig)
this.validateRules(boardConfig)
})
}
process.exit(0)
}
checkPublisherWebookURL(publisher) {
publisher = publisher.toUpperCase()
let webhookURL = eval('process.env.' + publisher + '_WEBHOOK_URL')
if(!webhookURL)
this.showError('Set ' + publisher + '_WEBHOOK_URL env variable for ' + publisher + ' platform.')
}
checkEnvVariables() {
if(!process.env.TRELLO_TOKEN)
this.showError('Setup TRELLO_TOKEN env variable.')
if(!process.env.TRELLO_KEY)
this.showError('Setup TRELLO_KEY env variable.')
if(!process.env.TRELLO_CALLBACK_URL)
this.showError('Setup TRELLO_CALLBACK_URL env variable.')
if(!process.env.DB_URI)
this.showError('Setup DB_URI env variable.')
if(!process.env.APP_ENV)
this.showError('Setup APP_ENV env variable.')
}
validatePublisher(scopeConfiguration) {
let publisher = scopeConfiguration.defaults.messagePublisher
if(!['slack', 'teams', 'flock'].includes(publisher))
this.showError('Incorrect message publisher name: ' + publisher)
}
validateRules(scopeConfiguration) {
this.validateRulesName(scopeConfiguration.cardRules)
Object.keys(scopeConfiguration.listRules).map((listName, index) => {
let rules = scopeConfiguration.listRules[listName]
if(!Array.isArray(rules)) {
this.showError('Every list name should have array of rules')
}
this.validateRulesName(rules)
})
}
validateRulesName(specifiedRules) {
let invalidRules = []
specifiedRules.forEach((rule) => {
if(!this.defaultRules.includes(rule)) {
invalidRules.push(rule)
}
})
if(invalidRules.length > 0)
this.showError('Invalid Rules', invalidRules)
}
validateScopeLevelSchema(pipelineConfig) {
let schema = {
'type': 'object',
'properties': {
'company': {
'description': 'company level configuration',
'type': 'object'
},
'boards': {
'description': 'board level configuration',
'type': ['object', 'null']
}
},
'required': [
'company',
'boards'
]
}
let valid = ajv.validate(schema, pipelineConfig)
if(!valid)
this.showError(ajv.errors)
}
validateCommonSchema(scopeConfiguration) {
// rule check
let schema = {
'type': 'object',
'properties': {
'defaults': {
'description': 'default values',
'properties': {
'messagePublisher': {'type': 'string'},
'timeZone': {'type': 'string'},
'officeStartHour': {'type': 'string'},
'officeEndHour': {'type': 'string'},
'weekendDays': {
'type': 'array',
'maxItems': 6,
'minItems': 0
},
'checkerJobDelay': {
'type': 'number',
'maximum': 59
}
},
'required': [
'messagePublisher',
'officeStartHour',
'officeEndHour',
'weekendDays',
'checkerJobDelay'
]
},
'ruleConfig': {
'description': 'Rules configuration',
'properties': {
'titleWordCount': {
'description': 'titleWordCount rule configuration',
'properties': {
'min': {'type': 'number'}
},
'required': ['min']
},
'labelsRequired': {
'description': 'labelsRequired rule configuration',
'properties': {
'min': {'type': 'number'}
},
'required': ['min']
},
'membersRequired': {
'description': 'membersRequired rule configuration',
'properties': {
'min': {'type': 'number'}
},
'required': ['min']
},
'listOfNewCard': {
'description': 'listOfNewCard rule configuration',
'properties': {
'listName': {'type': 'string'}
},
'required': [
'listName'
]
},
'checkListItemWordCount': {
'description': 'checkListItemWordCount rule configuration',
'properties': {
'min': {'type': 'number'}
},
'required': ['min']
},
'pullRequestRequired': {
'description': 'pullRequestRequired rule configuration',
'properties': {
'vcHostingDomain': {
'type': 'string',
'format': 'url'
},
'ignoreLabel': {'type': 'string'}
},
'required': [
'vcHostingDomain',
'ignoreLabel'
]
}
},
'required': [
'titleWordCount',
'labelsRequired',
'membersRequired',
'listOfNewCard',
'checkListItemWordCount',
'pullRequestRequired'
]
},
'cardRules': {
'description': 'default rules for a card',
'type': 'array'
},
'listRules': {
'description': 'rules based on list name',
'type': 'object'
}
},
'required': [
'defaults',
'ruleConfig',
'cardRules',
'listRules'
]
}
let valid = ajv.validate(schema, scopeConfiguration)
if(!valid)
this.showError(ajv.errors)
}
showError(msg, options) {
logger.error(msg)
if(options)
logger.error(options)
process.exit(1)
}
}
let validator = new PipelineConfigValidator()
validator.validate()