-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inclusion-Exclusion-Array.groovy
134 lines (119 loc) · 5.31 KB
/
Inclusion-Exclusion-Array.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
import hudson.model.*
import jenkins.model.Jenkins
import java.io.File;
import groovy.xml.XmlUtil
import java.io.InputStream;
import java.io.FileInputStream
import javax.xml.transform.stream.StreamSource
import com.cloudbees.hudson.plugins.folder.*
/*
* User defined Variables:
* =======================
* STARTING_PATH: The starting folder path where processing begins
*
* *The following 2 FOLDER_ variables are mutually exclusive. If there are values in both (or if both are empty), the script will not process any folder*
* FOLDER_EXCLUSIONS: The list of folders to exclude from the recursive processing
* FOLDER_INCLUSIONS: The list of folders to be processed. Subfolders will also be processed
*
* JOB_INCLUSION_PATTERN: Process those jobs that match this pattern. If this is empty, process all jobs, respecting the above 2 variables
*
* STEPXML: XML obtained from a valid job that corresponds to the Build step
*/
STARTING_PATH = "A/Specific/Jenkins/Starting/Path"
FOLDER_EXCLUSIONS = ["Exclued_Folder_1","Exclued_Folder_2"]
FOLDER_INCLUSIONS = []
JOB_INCLUSION_PATTERN = "_build"
STEPXML='''
<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>
'''
NODE_TO_ADD = new XmlSlurper().parseText( STEPXML )
/*
* If both lists have values, exit.
* If not, fetch the children of the node 'STARTING_PATH' and begin recursive processing
*/
if ( FOLDER_EXCLUSIONS.size() > 0 && FOLDER_INCLUSIONS.size() > 0 ) {
println("Enter values for either of the lists and rerun. Exiting...")
} else if ( FOLDER_EXCLUSIONS.size() == 0 && FOLDER_INCLUSIONS.size() == 0 ){
println("Both the lists are empty, enter values for either of the lists and rerun. Exiting...")
} else {
Jenkins.getInstance().getItemByFullName(STARTING_PATH).each{
if(it instanceof Folder){
processFolder(it)
} else {
println("Skipping this object (" + it.name + ") as it is not a folder...")
}
}
}
/*
* This function iterates through Folder objects recursively to process children Jobs and folders, logic below
* If the variable FOLDER_EXCLUSIONS has values, it excludes those folders and processes all other folder (including children)
* in the path provided in STARTING_PATH
*
* If the variable FOLDER_INCLUSIONS has values, it processes the jobs in it and any subfolders contained in levels below the
* mentioned values recursively. Any folder or job that is not a descendent of the folders provided in this variable are excluded
*/
void processFolder(Item folder){
folder.getItems().each{
if (it instanceof Folder){
if(FOLDER_EXCLUSIONS.size() > 0){
if(it.name in (FOLDER_EXCLUSIONS)) {
println("Skipping this Folder (" + it.name + ") as it is an exclusion...")
} else {
println("Processing folder: "+ it.name)
processFolder(it)
}
} else if(FOLDER_INCLUSIONS.size() > 0){
if(it.name in (FOLDER_INCLUSIONS)) {
println("Processing folder: "+ it.name)
processFolder(it)
} else {
def pathArray = it.fullName.tokenize('/')
if(FOLDER_INCLUSIONS.intersect(pathArray).size() > 0){
println("Processing child folder: "+ it.name)
processFolder(it)
} else {
println ("Skipping this Folder (" + it.name + ") as it is not a child of included folders...")
}
}
}
} else {
processJob(it)
}
}
}
/*
* This function is to process job objects. It reads the config XML, checks if the 'Invoke Nexus Policy Evaluation'
* step is present in the build steps and adds the step if it is not present. It then saves the XML and saves the Job
* so that the changes to the XML file are visible without needing a service restart
*/
void processJob(Item job){
if( job.name.contains(JOB_INCLUSION_PATTERN)){
println("Processing job: " + job.name)
config = job.getConfigFile()
File file = config.getFile()
def root = new XmlSlurper().parse( file )
if (! root.builders.childNodes().find{ it.name() == 'org.sonatype.nexus.ci.iq.IqPolicyEvaluatorBuildStep'} ) {
root.builders.appendNode( NODE_TO_ADD )
def outxml = XmlUtil.serialize( root )
file.withWriter { w ->
w.write(XmlUtil.serialize(outxml))
}
InputStream is = new FileInputStream(file);
job.updateByXml(new StreamSource(is));
job.save();
println("Added the build step and saved the job " + job.name)
} else {
println("Skipping insertion of build step as the step is present...")
}
} else {
println ("Skipping the job '" + job.name + "' as it does not match the pattern")
}
}