forked from Dhanielk/sfdc_merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfdc_merge.groovy
executable file
·203 lines (179 loc) · 5.19 KB
/
sfdc_merge.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
import groovy.io.GroovyPrintStream
import groovy.json.JsonSlurperClassic
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
import java.util.logging.Logger
Logger logger = Logger.getLogger("")
def mergeType = args[3]
def scriptBase = args[4]
def xmlParser = new XmlParser(false, true, true)
def ancientNodes = xmlParser.parse(args[0])
def oursNodes = xmlParser.parse(args[1])
def theirsNodes = xmlParser.parse(args[2])
def conflictOurs = xmlParser.parse(scriptBase + '/nodes/ConflictOurs.xml')
def conflictTheirs = xmlParser.parse(scriptBase + '/nodes/ConflictTheirs.xml')
def conflictNoOther = xmlParser.parse(scriptBase + '/nodes/ConflictNoOther.xml')
def patchCore = scriptBase
def patchConfig = scriptBase
switch(mergeType) {
case 'profile':
patchCore += '/core/core.profile'
patchConfig += '/config/profile.json'
break
case 'permissionset':
patchCore += '/core/core.permissionset'
patchConfig += '/config/permissionset.json'
break
case 'labels':
patchCore += '/core/core.labels'
patchConfig += '/config/labels.json'
break
default:
System.exit(1)
break
}
def base = xmlParser.parse(patchCore)
def config = new JsonSlurperClassic().parse(new File(patchConfig))
// #### MAIN ####
ancient = [:]
ancientNodes."*".each { node ->
def uniqueNodeKey = buildUniqueKey(node, config."${getLocalPart(node)}")
if (uniqueNodeKey) {
ancient[uniqueNodeKey] = [
nodeType: getLocalPart(node),
node: node
]
}
}
ours = [:]
oursIds = []
unmatchedNodeCount = 0
oursNodes."*".each { node ->
uniqueNodeKey = buildUniqueKey(node, config."${getLocalPart(node)}", unmatchedNodeCount++)
oursIds << uniqueNodeKey
ours[uniqueNodeKey] = [
nodeType: getLocalPart(node),
node: node,
existsInAncient: (ancient[uniqueNodeKey] != null),
isEqualsToAncient: areNodesEqual(node, ancient[uniqueNodeKey], config."${getLocalPart(node)}")
]
}
def conflictCounter = 0
theirsNodes."*".each { node ->
def localPart = getLocalPart(node);
uniqueNodeKey = buildUniqueKey(node, config."${getLocalPart(node)}")
if (uniqueNodeKey) {
existsInAncient = ancient[uniqueNodeKey] != null
isEqualsToAncient = areNodesEqual(node, ancient[uniqueNodeKey], config."${getLocalPart(node)}")
existsInOurs = oursIds.remove(uniqueNodeKey)
isEqualsToOurs = areNodesEqual(node, ours[uniqueNodeKey], config."${getLocalPart(node)}")
if ((!existsInAncient && existsInOurs && isEqualsToOurs) ||
(existsInAncient && (
(existsInOurs && (isEqualsToOurs || isEqualsToAncient)) ||
(!existsInOurs && isEqualsToAncient)))) {
// Keep OURS
// do nothing
} else if (existsInAncient && existsInOurs && ours[uniqueNodeKey].isEqualsToAncient) {
// Use THEIRS
ours[uniqueNodeKey].node = node
} else if (!existsInAncient && !existsInOurs) {
// Use THEIRS
ours[uniqueNodeKey] = [
node: node
]
} else {
// CONFLICT detected
conflictCounter++
node.append conflictTheirs
if (existsInOurs) {
ours[uniqueNodeKey].node.append conflictOurs
} else {
node.append conflictNoOther
}
ours["${uniqueNodeKey}CONFLICT#"] = [
node: node
]
}
}
}
oursIds.each { id ->
if (ours[id]) {
if (ours[id].existsInAncient && !ours[id].isEqualsToAncient) {
conflictCounter++
ours[id].node.append conflictOurs
ours[id].node.append conflictNoOther
} else if (ours[id].isEqualsToAncient) {
ours.remove(id)
}
}
}
ours.sort { it.key }.each {
base.append it.value.node
}
def sw = new StringWriter()
def printer = new XmlNodePrinter(new PrintWriter(sw), ' ')
printer.with {
preserveWhitespace = true
expandEmptyElements = true
}
printer.print(base)
// writing the file in ours
new File(args[1]).withWriter('UTF-8') { it.write "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n$sw" }
println "Conflicts Found: $conflictCounter"
System.exit(conflictCounter)
// #### FUNCTIONS ####
def buildUniqueKey(def node, def nodeTypeConfig) {
def uniqueKey = null
if (nodeTypeConfig) {
uniqueKey = getLocalPart(node) + "#"
if (nodeTypeConfig.uniqueKeys) {
nodeTypeConfig.uniqueKeys.each { key ->
if (node."$key") {
uniqueKey += node."$key"[0].value()[0] + "#"
}
}
} else {
def exclusiveUniqueKey = ''
for (def eukList : nodeTypeConfig.exclusiveUniqueKeys) {
for (def euk : eukList) {
if (node."$euk"[0])
exclusiveUniqueKey += node."$euk"[0].value()[0] + "#"
}
if (exclusiveUniqueKey != '') {
break
}
}
uniqueKey += exclusiveUniqueKey
}
}
uniqueKey
}
def buildUniqueKey(def node, def nodeTypeConfig, def count) {
def uniqueKey = buildUniqueKey(node, nodeTypeConfig)
if (uniqueKey == null) {
uniqueKey += getLocalPart(node) + "#${count}#"
}
uniqueKey
}
def areNodesEqual(def node1, def node2, def nodeTypeConfig) {
if (node2 == null || node2.node == null) {
return false
}
if (nodeTypeConfig && nodeTypeConfig.equalKeys && !nodeTypeConfig.equalKeys.isEmpty()) {
for (def key : nodeTypeConfig.equalKeys) {
if (node1."$key"[0] && node2."$key"[0] && node1."$key"[0].value()[0] != node2.node."$key"[0].value()[0]) {
return false
}
}
return true
} else {
return node1.value()[0] == node2.node.value()[0]
}
}
def getLocalPart(def node) {
try {
return node.name().localPart
} catch (Exception ex) {
return node.name().split(':')[0]
}
}