forked from electric-cloud-community/DSL-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
findObjectsSample.dsl
170 lines (143 loc) · 4.99 KB
/
findObjectsSample.dsl
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
// Imports needed for invoking findObjects in the DSL script
import com.electriccloud.query.Filter
import com.electriccloud.query.CompositeFilter
import com.electriccloud.query.PropertyFilter
import com.electriccloud.query.Operator
import com.electriccloud.query.SelectSpec
import com.electriccloud.util.SortOrder
import com.electriccloud.util.SortSpec
import com.fasterxml.jackson.databind.ObjectMapper
import org.dom4j.DocumentHelper
import groovy.json.JsonSlurper
// Make sure that the pipelines that
// findObjects is looking for do exist
project 'US Test', {
pipeline 'pipeline-0', {
testprop1 = 'val1'
}
pipeline 'pipeline-1', {
testprop2 = 'val2'
}
pipeline 'pipeline-3', {
testprop2 = 'val3'
}
}
/**
* Sample filter definition for:
* projectName equals "US Test"
* AND
* (
* pipelineName equals "pipeline-0"
* or
* pipelineName equals "pipeline-1"
* )
*/
def filters = [[
propertyName: "projectName",
operator: "equals",
operand1: "US Test"
],
[ filters: [[
propertyName: "pipelineName",
operator: "equals",
operand1: "pipeline-0"
],[
propertyName: "pipelineName",
operator: "equals",
operand1: "pipeline-1"
]],
operator: "or"
]]
/**
* Simple selects can be specified simply as strings
* Use the object structure if recurse option needs to be controlled.
* The above 2 forms can be completed in the same list of selects.
*/
def selects = ["testprop1", [propertyName: "testprop2", recurse: true]]
/**
* Sort columns
*/
def sorts = [[propertyName: "pipelineName", order: "descending"], [propertyName: "projectName"]]
// make the call
def result = findObjectsSimplified([objectType: 'pipeline', filters: filters, selects: selects, sorts: sorts])
def response = processFindObjectsResponse(result)
// traverse the response using XPath like syntax
assert response.objectId.size() == 2
assert response.object.size() == 2
assert response.object[0].pipeline.pipelineName == 'pipeline-1'
response.object.each {
assert it.pipeline.pipelineName in ['pipeline-0','pipeline-1']
}
// optionally return the infoset-aware findObjects response as the DSL outcome
result
/**
* Helper function that takes care of converting findObject input arguments to the
* structure required by the API.
*/
def findObjectsSimplified(Map args) {
findObjectsSimplified(args.objectType, args.filters, args.selects, args.sorts)
}
/**
* Helper function that takes care of converting findObject input arguments to the
* structure required by the API.
* @param objectType
* @param filters
* @param selects
* @param sorts
*/
def findObjectsSimplified(String objectType, def filters = null, def selects = null, def sorts = null) {
def result = findObjects(objectType: objectType, filter: constructFilters(filters), select: constructSelects(selects), sort: constructSorts(sorts))
}
/**
* Helper function to convert the list of filters to a filter structure
* recognized by findObjects for DSL evaluation.
*/
def constructFilters(def filters) {
filters?.collect { f ->
def op = Operator.valueOf(f.operator)
if (op.isBoolean()) {
new CompositeFilter(op, constructFilters(f.filters) as Filter[])
} else {
new PropertyFilter(f.propertyName, op, f.operand1, f.operand2)
}
}
}
/**
* Helper function to convert the list of select strings to a list of SelectSpec
* recognized by findObjects for DSL evaluation.
*/
def constructSelects(def selects) {
selects?.collect { s ->
s instanceof String ? new SelectSpec(s, false) : new SelectSpec(s.propertyName, s.recurse)
}
}
/**
* Helper function to convert the list of sort instances to a list of SortSpec
* recognized by findObjects for DSL evaluation.
*/
def constructSorts(def sorts) {
sorts?.collect { s ->
new SortSpec(s.propertyName, SortOrder.valueOf(s.order?:"ascending"))
}
}
/**
* Helper function that conversts the findObject response into a
* document tree that can be traversed similar to XPath expressions.
*/
def processFindObjectsResponse(def infoset) {
if (infoset.class.simpleName == 'JsonInfosetResultImpl') { // when called REST
def root = infoset.asNode()
def mapper = new ObjectMapper();
String jsonData = mapper.writeValueAsString(root)
def jsonSlurper = new JsonSlurper()
jsonSlurper.parseText(jsonData)
} else { // when called via ectool
def document = DocumentHelper.createDocument();
def root = document.addElement('result');
infoset.elements?.each { element ->
def elementDoc = DocumentHelper.parseText(element.asXML())
root.add(elementDoc.getRootElement())
}
new XmlSlurper().parseText(root.asXML())
}
}