forked from electric-cloud-community/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prop2json.groovy
124 lines (111 loc) · 3.86 KB
/
prop2json.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
/*
ElectricFlow DSL Example: Print out JSON equivalent of property sheet
Instructions:
1. Run this DSL code from the command line, DSL Editor or DSLIDE
Command line: ectool evalDsl --dslFile prop2json.groovy
2. Run the procedure against the sample property sheet (default parameter value)
or other property sheet. Make sure to reference the property using XPATH, e.g.,
/projects/My Project/pipelines/My Pipeline/stages/First Stage/myPropertySheetName
*/
project "Properties2JSON",{
// Sample property sheet
property "root",{
property "rootProp", value: "xyz"
property "noval"
property "expansion", value: '$[/myProject]'
property "sub1",{
property "prop1", value: "123"
property "prop2", value: "456"
property "subSub",{
property "prop3", value: "890"
}
}
property "sub2",{
property "propA", value: "abc"
}
}
procedure "prop2json", description: "Print out JSON of property sheet",{
formalParameter "path", defaultValue: "/projects/Properties2JSON/root",
required: true, description: "Path to property sheet"
step "Generate JSON from properties", shell: "ec-groovy", description: "See log or Job property PropertyJSON for output",
command: '''\
import groovy.json.*
import com.electriccloud.client.groovy.ElectricFlow
ElectricFlow ef = new ElectricFlow()
def PropertyName = ef.getProperty(propertyName: '$[path]').property.propertyName
def PropertyStucture = ef.getProperties(path: '$[path]', recurse: true, expand: false).propertySheet
def pretty = { obj ->
return JsonOutput.prettyPrint(JsonOutput.toJson(obj))
}
//println JsonOutput.prettyPrint(JsonOutput.toJson(PropertyStucture))
def prop2json
prop2json = { struct ->
def outStruct = [:]
struct.each { prop ->
if (prop?.propertySheet) {
def propStruct = [:]
propStruct << prop2json(prop.propertySheet.property)
outStruct << [(prop.propertyName):propStruct]
} else {
outStruct << [(prop.propertyName):prop?.value]
}
}
return outStruct
}
// println PropertyStucture.property[0].propertySheet.property.getClass() // class java.util.ArrayList
def OutputDataStructure = [(PropertyName):prop2json(PropertyStucture.property)]
println pretty(OutputDataStructure)
ef.setProperty(propertyName: "/myJob/PropertyJSON", value: pretty(OutputDataStructure))
'''.stripIndent()
} // procedure
} // project
/*
getProperties on "root" returns the edited structure below
{
"propertySheet": {
"property": [
{
"propertyName": "sub1",
"propertySheet": {
"property": [
{
"propertyName": "subSub",
"propertySheet": {
"property": [
{
"propertyName": "prop3",
"value": "890"
}
]
}
},
{
"propertyName": "prop1",
"value": "123"
},
{
"propertyName": "prop2",
"value": "456"
}
]
}
},
{
"propertyName": "sub2",
"propertySheet": {
"property": [
{
"propertyName": "propA",
"value": "abc"
}
]
}
},
{
"propertyName": "rootProp",
"value": "xyz"
}
]
}
}
*/