-
Notifications
You must be signed in to change notification settings - Fork 11
/
writeXMLproperties_scriptlet.groovy
436 lines (392 loc) · 21.5 KB
/
writeXMLproperties_scriptlet.groovy
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*** BEGIN META {"name" : "writeXMLProperties_scriptlet",
"comment" : "Writes an XML Summary report from a report configuration file. The report configuration acts as a template for report generation Supports table filtering",
"parameters" : [ 'workspaceVar','configProps'],
"core": "1.596",
"authors" : [{ name : "Ioannis Moutsatsos" }]} END META**/
/**
* Writes a Summary Display Jenkins Plugin XML Template driven by a Report Configuration file.
* Summary content can be read from files formatted as Java properties or in delimited format
* Author: Ioannis K. Moutsatsos
* Last Update: 6/9/2016
* Required Script Parameters: workspaceVar, configProps
*/
import groovy.xml.*
println "\n---------Write XML Template for Summary Display Plugin (writeXMLProperties_scriptlet.groovy)---------"
def env = System.getenv() //also get the environment
def workspace = workspaceVar//scriptlet parameter:$WORKSPACE
def options = [:]
reportConfigProps = configProps //scriptlet parameter:http://yourJenkinsUrl/userContent/reportConfiguration.properties
def imageExtensions = ['tif', 'tiff', 'png', 'jpeg', 'jpg', 'gif', 'bmp']
operRelational=['>','>=','<','<=', 'in']
operMethod=['startsWith','contains','endsWith', 'matches']
/* Set default size for rendered images.
May be changed from report configuration on per table basis using the 'imgwidth' table property
*/
def imgWidth = 200
def separator = ','
def noPropUse = false // a flag whether properties file will be used
//Create the properties objects, from the file system:
Properties configProps = new Properties() // report configuration
Properties summaryProps = new Properties() // report content
File configFile = new File(reportConfigProps)
configProps.load(configFile.newDataInputStream())
propSource = configProps.getProperty('summary.properties')
if (propSource.startsWith('none')) {
noPropUse = true
println 'Report does not use properties file'
} else {
if (propSource.startsWith('http')) {
propSource.toURL().eachLine {
if (it.contains('=')) {
urlprop = it.split('=')
summaryProps.put(urlprop[0], urlprop[1])
}
}
} else {
summaryFile = new File("${workspace}/${configProps.getProperty('summary.properties')}")
summaryProps.load(summaryFile.newDataInputStream())
}
} //end none else
def columnSet = []
def rowheader = []
def headerIndex = []
/* Maps for column selection criteria */
exactSelect = [:]
relationalSelect = [:]
isFiltered = false //will be set appropriately if the table data is filtered with a table.select property
def reportStyle = configProps.getProperty('report.style')
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
/*
Reference: https://wiki.jenkins-ci.org/display/JENKINS/Summary+Display+Plugin
Report style options include
Section (name)
Field (name,value, href)
Table
Tabs (field, table)
Accordion (field, table)
*/
switch (reportStyle) {
case "tab":
thead = []; theadTemp = []
thead = configProps.getProperty('tab.header').split(',')
//confirm that content file (CSV) exists for each tab.header and remove those headers for which it does not
println "TABS-Original: $thead"
thead.each {
if (configProps.getProperty("content.${it}") == 'table') {
if (configProps.getProperty("table.data.${it}").startsWith('http')) {
if (getResponseCode(configProps.getProperty("table.data.${it}")) == 200) {
theadTemp.add(it)
} else {
println "\tCould not access content: ${configProps.getProperty("table.data.${it}")}"
}
} else {
tabContent = "${workspace}/${configProps.getProperty("table.data.${it}")}"
tabContentFile = new File(tabContent)
if (tabContentFile.exists()) {
theadTemp.add(it)
} else {
println "\tCould not find content: $tabContent"
}
}// end logic check
}
if (configProps.getProperty("content.${it}") == 'field' && !noPropUse) {
tabContent = configProps.getProperty("summary.properties")
theadTemp.add(it)
}
}
thead = theadTemp //replace header with subset where content exists
println "TABS-Adjusted: $thead (from available content)"
xml.tabs {
thead.each {
//println it
tabContent = configProps.getProperty("content.${it}")
if (configProps.getProperty("separator.${it}") != null) {
separator = configProps.getProperty("separator.${it}")
} else {
separator = ','
}
startOfKey = configProps.getProperty("field.key.${it}")
valColor = configProps.getProperty('field.value.color')
tabName = it
tab(name: "$it") {
switch (tabContent) {
case "field":
summaryProps.sort().each { k, v ->
if (k.toString().startsWith(startOfKey) && v != null) {
if (v.startsWith('http')) {
field(name: k, value: 'Link', detailcolor: valColor, href: '/' + v.split(':/')[1])
} else {
field(name: k, value: v, detailcolor: valColor)
}
}
}//end each
break
case "table":
//create a table from referenced file
// println "Working with $tabName"
def dataTableSource = "${configProps.getProperty("table.data.${tabName}")}" //"${workspace}/${configProps.getProperty("table.data.${tabName}")}"
println "\n$tabName : Table data from: $dataTableSource"
propKey = "table.header.${tabName}"
// if no table.length property is defined we write the entire table
def ignoreLineCount = false
def rownum = 0
if (configProps.getProperty("table.select.${tabName}") != null) {
exactSelect = [:]
relationalSelect = [:]
isFiltered = true
parseSelectCriteria(configProps.getProperty("table.select.${tabName}"))
} else {
isFiltered = false
}
if (configProps.getProperty("table.length.${tabName}") != null) {
rownum = configProps.getProperty("table.length.${tabName}").toInteger()
} else {
ignoreLineCount = true
}
if (configProps.getProperty("table.imgwidth.${tabName}") != null) {
imgWidth = configProps.getProperty("table.imgwidth.${tabName}").toInteger()
}
if (dataTableSource.startsWith('http')) {
dataTableSource.toURL().withReader { reader ->
columnSet = reader.readLine().split(separator)
if (configProps.containsKey("table.header.${tabName}".toString())) {
rowheader = configProps.getProperty("table.header.${tabName}").split(',')
} else {
rowheader = columnSet
}
headerIndex = createIndexIntoList(rowheader.toList(), columnSet.toList())
}
} else {
dataTableSource = "${workspace}/${configProps.getProperty("table.data.${tabName}")}"
def dataFile = new File(dataTableSource)
dataFile.withReader {
reader ->
columnSet = reader.readLine().split(separator)
if (configProps.containsKey("table.header.${tabName}".toString())) {
rowheader = configProps.getProperty("table.header.${tabName}").split(',')
} else {
rowheader = columnSet
}
headerIndex = createIndexIntoList(rowheader.toList(), columnSet.toList())
}
//columnSet //must read CSV file from path
}
// DMPQM-298 make table sort-able
table(sorttable: "yes") {
//create table header
//now add table data
if (dataTableSource.startsWith('http')) {
lineCount = 0
dataTableSource.toURL().eachLine { uline, rowIndex ->
if (isSelected(rowheader, uline, exactSelect, relationalSelect, rowIndex)) {
headerIndex.removeAll([-1])
lineValueSet = uline.split(separator)
columnValueMatch=lineValueSet.size()==columnSet.size()
if (lineCount < rownum + 1 && columnValueMatch) {
lineValueSet = uline.split(separator)[headerIndex]
tr() {
lineValueSet.each { h ->
//if column heading exists we create a table cell, else we skip
//we also check that the row can be split into enough values or we skip
isImage = false //default flag for image file-values
if (h != null) {
//check if cell value is an image
imageExtensions.each {
if (h.endsWith(it)) {
isImage = true
}
}
if (h.startsWith('http') && isImage) {
def imgUrl = (h.split(':/')[1]).replaceAll('\\\\', '')
xml.mkp.yieldUnescaped("<td><![CDATA[<a href=\"${'/' + imgUrl}\"/><img width=$imgWidth src=\"${'/' + imgUrl}\"/></a>]]></td>")
} else if (h.startsWith('http')) {
td(value: 'Link', bgcolor: 'white', fontcolor: 'black', align: 'left', href: '/' + h.split(':/')[1])
} else {
td(value: h, bgcolor: 'white', fontcolor: 'black', align: 'left')
}
}
}
}
// if we are counting lines we keep track
if (!ignoreLineCount) {
lineCount++
}
}
}//end if isSelected
}//end each line
} //end if startsWith http
else {
//reading from local file system
dataFile = new File(dataTableSource)
lineCount = 0
dataFile.eachLine { uline, rowIndex ->
if (isSelected(rowheader, uline, exactSelect, relationalSelect, rowIndex)) {
headerIndex.removeAll([-1])
lineValueSet = uline.split(separator)
columnValueMatch=lineValueSet.size()==columnSet.size()
// println "${lineValueSet.size()}==${columnSet.size()}"
if (lineCount < rownum + 1 && columnValueMatch) {
// columnSet = uline.split(separator)
// thisHeaderIndex=headerIndex.intersect((0..(columnSet.size()-1)))
lineValueSet = uline.split(separator)[headerIndex]
tr() {
lineValueSet.each { h ->
//if column heading exists we create a table cell, else we skip
//we also check that the row can be split into enough values or we skip
//set logic flag if cell value is an image
isImage = false //default flag for image file-values
if (h != null) {
imageExtensions.each {
if (h.endsWith(it)) {
isImage = true
}
}
if (h.startsWith('http') && isImage) {
def imgUrl = (h.split(':/')[1]).replaceAll('\\\\', '')
xml.mkp.yieldUnescaped("<td><![CDATA[<a href=\"${'/' + imgUrl}\"/><img width=$imgWidth src=\"${'/' + imgUrl}\"/></a>]]></td>")
} else if (h.startsWith('http')) {
td(value: 'Link', bgcolor: 'white', fontcolor: 'black', align: 'left', href: '/' + h.split(':/')[1])
} else {
td(value: h, bgcolor: 'white', fontcolor: 'black', align: 'left')
}
}
}
}
// if we are counting lines we keep track
if (!ignoreLineCount) {
lineCount++
}
}
}//end if isSelected
}//end each line
}
}
break
}//end content
} //end tab
}
}
break
case "table":
println 'Table reports are not currently supported'
break
}
def writer4file = new FileWriter("$workspace/writeXmlSummary.xml")
XmlUtil.serialize(writer.toString(), writer4file)
println "\nSummary Display XML Template: ${new File("$workspace/writeXmlSummary.xml").getCanonicalPath()}"
writer4file.close() //close the file
/*
Find index of a list members in another list
For example if source[A,D,F] and target[A,B,C,D,E,F,G] we want to return sourceIndex[0,3,5]
*/
def createIndexIntoList(List source, List target) {
// println 'Getting index'
def indexList = []
source.each { s ->
indexList.add(target.indexOf(s))
}
return indexList
}
/* Method checks if a URL is accessible
*/
def getResponseCode(String urlString) throws MalformedURLException, IOException {
URL u = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
huc.setRequestMethod("GET");
huc.connect();
return huc.getResponseCode();
}
/*Parses a groovy map string with column selection criteria
* modifies global vars exactSelect, relationalSelect
* */
def parseSelectCriteria(selectMapString) {
selectClauses = evaluate("$selectMapString")
/*parse and assign exact and relational selection criteria*/
selectClauses.each { k, v ->
if (v.getClass() == LinkedHashMap) {
println "\tSELECT '$k' WHERE: $v"
relationalSelect.put(k, v)
} else {
println "\tSELECT '$k' WHERE: $v"
exactSelect.put(k, v)
}
}
}
/*Method to determine if a row should be included
* We always include header row and do not filter when the table.select is not set
* */
def isSelected(headList, rowText, exactSelect, relationalSelect, rowCount) {
if (isFiltered && rowCount > 1) {
line = rowText.split(',')
criteria = []
if (exactSelect != [:]) {
// println "Exact select $exactSelect"
criteria.add(isExactRowMatch(headList, line, exactSelect))
}
if (relationalSelect != [:]) {
// println "Method select $relationalSelect"
criteria.add(isRelationalRowMatch(headList, line, relationalSelect))
}
return criteria.contains(false) == false
}else{
//unless isFiltered we select all rows
return true
}
}
/*method determines whether a row should be included by the matching criteria
Matches by exact values using AND between clauses
*/
def isExactRowMatch(headlist, line, selClauses) {
columnIndex = selClauses.keySet()
ci = headlist.findIndexValues { it in columnIndex }
testVals = line[ci]
selectCombinations = GroovyCollections.combinations(selClauses.values())
return selectCombinations.findResult { (it as String) == (testVals as String) ? it : null } != null
}
/*
Method creates appropriate assertions that check whether a row
contains data that match the relational operators and methods
Note that we rely on a special grammar for that.
*/
def isRelationalRowMatch(headlist,line,selClauses) {
testClause = [] //a list to keep result of tests
columnIndex = selClauses.keySet()
columnIndex.each {
ci = headlist.findIndexValues { ind -> ind in it }
testValue = line[ci]
testValue.each { tv ->
/* assert that only supported operators are used */
assert selClauses[it].operator in operRelational.plus(operMethod)
if (selClauses[it].operator in operRelational ){
assertion = "$tv ${selClauses[it].operator} ${selClauses[it].value}"
// println "$tv ${selClauses[it].operator} ${selClauses[it].value}"
if (selClauses[it].negate==true){
// println 'Negating assertion'
assertion= "$tv ${selClauses[it].operator} ${selClauses[it].value}==false"
}else{
assertion = "$tv ${selClauses[it].operator} ${selClauses[it].value}".replace('\\','/')
}
testResult = evaluate(assertion)
testClause.add(testResult)
}//end in operRelational operator
if (selClauses[it].operator in operMethod ){
methodTest=[] //a list to keep test from methods
selClauses[it].value.each{op->
// println "${selClauses[it]}: ${selClauses[it].negate}"
if (selClauses[it].negate==true){
// println 'Negating assertion'
assertion= "\'${tv}\'.${selClauses[it].operator}(\'$op\')==false".replace('\\','/')
}else{
assertion= "\'${tv}\'.${selClauses[it].operator}(\'$op\')".replace('\\','/')
}
// println 'Asserting:'+assertion
methodTest.add(evaluate(assertion))
}
testClause.add(methodTest.contains(true))
}//end in operMethod operator
}
// println testClause
}
return testClause.contains(false) == false
}