-
Notifications
You must be signed in to change notification settings - Fork 0
/
use-case-1.groovy
77 lines (67 loc) · 3.04 KB
/
use-case-1.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
import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
import groovy.util.XmlParser
def startingPoints=["DevServices/Captial Markets and Finance/CMFT01/REO-VS/REOVS_RELEASE"]
def excludedPath = ["ZRelease"]
def allProject = []
def excludedProjects = []
Jenkins.instance.getAllItems(AbstractProject.class).each{item ->
def startingPoint = startingPoints.each{ start ->
if(item.fullName.toLowerCase().startsWith(start.toLowerCase())){
allProject.add(item.fullName.toLowerCase())
}
}
}
Jenkins.instance.getAllItems(AbstractProject.class).each{item ->
def startingPoint = excludedPath.each{ start ->
if(item.fullName.toLowerCase().contains(start.toLowerCase())){
excludedProjects.add(item.fullName.toLowerCase())
}
}
}
allProject.intersect(excludedProjects).each{allProject.remove(it);excludedProjects.remove(it)}
def includedProject= allProject+excludedProjects
def nexusIqBuilder = '''
<org.sonatype.nexus.ci.iq.IqPolicyEvaluatorBuildStep plugin="nexus-jenkins-plugin@3.9.20200722-164144.e3a1be0">
<com__sonatype__nexus__ci__iq__IqPolicyEvaluator____iqStage>Build</com__sonatype__nexus__ci__iq__IqPolicyEvaluator____iqStage>
<com__sonatype__nexus__ci__iq__IqPolicyEvaluator____iqApplication class="org.sonatype.nexus.ci.iq.SelectedApplication">
<applicationId></applicationId>
</com__sonatype__nexus__ci__iq__IqPolicyEvaluator____iqApplication>
<com__sonatype__nexus__ci__iq__IqPolicyEvaluator____failBuildOnNetworkError>false</com__sonatype__nexus__ci__iq__IqPolicyEvaluator____failBuildOnNetworkError>
<com__sonatype__nexus__ci__iq__IqPolicyEvaluator____jobCredentialsId></com__sonatype__nexus__ci__iq__IqPolicyEvaluator____jobCredentialsId>
<advancedProperties></advancedProperties>
</org.sonatype.nexus.ci.iq.IqPolicyEvaluatorBuildStep>
'''
Jenkins.instance.getAllItems(AbstractProject.class).each {item ->
includedProject.each{include ->
if(item.fullName.toLowerCase() == include.toLowerCase()){
def config = item.getConfigFile()
File file = config.getFile()
String fileContents = file.getText('UTF-8')
xml=new XmlParser().parseText(fileContents)
def nodeToModify=xml.builders.findAll{n->
if(! n."org.sonatype.nexus.ci.iq.IqPolicyEvaluatorBuildStep"){
newBuilder = new XmlParser( false, true ).parseText( nexusIqBuilder )
xml.find { it.name() == 'builders' }.children().add( 0, newBuilder )
file.withWriter { w ->
w.write(XmlUtil.serialize(xml))
}
// reload jenkins job config file
InputStream is = new FileInputStream(file)
item.updateByXml(new StreamSource(is))
item.save()
}
}
print("NexusIQ plugin added to "+ item.fullName +" in the build step\n")
}
else{
print("NexusIQ plugin skipped in "+ item.fullName +"\n")
}
}
}