-
Notifications
You must be signed in to change notification settings - Fork 0
/
_Jenkins final report.groovy
119 lines (85 loc) · 2.83 KB
/
_Jenkins final report.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
import java.text.SimpleDateFormat
import com.cloudbees.hudson.plugins.folder.*
import groovyjarjarasm.asm.Item
String startDate = "11/01/2017 00:00";
String startDateFolders = "6/01/2017 00:00";
String endDate = "11/30/2017 23:59";
String dateFormatPattern = "MM/dd/yyyy HH:mm";
Calendar startDateCal = parseDate(startDate,dateFormatPattern);
Calendar startDateFodlersCal = parseDate(startDateFolders,dateFormatPattern);
Calendar endDateCal = parseDate(endDate,dateFormatPattern);
getTotalNumberOfFolders();
getNumberOfActiveFolders(startDateFodlersCal,endDateCal);
getTotalBuildsCount();
getActiveBuildsCount(startDateCal,endDateCal);
def getActiveBuildsCount(Calendar startRef,Calendar endRef) {
int count =0;
for(jobItm in Jenkins.instance.getAllItems()) {
if(jobItm instanceof Job && jobItm.getLastBuild() != null) {
for(build in jobItm.builds) {
Date buildDate = build.getTime();
Calendar buildCal = Calendar.getInstance();
buildCal.setTime(buildDate);
if(buildCal.before(endRef) && buildCal.after(startRef)) {
count++;
}
}
}
}
println("Number of active builds: " + count);
}
def getTotalBuildsCount(){
int count = 0;
for(jobRoot in Jenkins.instance.getAllItems()) {
if(jobRoot instanceof Job && jobRoot.getLastBuild() != null) {
count += jobRoot.getLastBuild().number;
}
}
println("Total builds: " + count);
}
def getTotalNumberOfFolders(){
int count = 0;
Jenkins.instance.getAllItems().each{
if(it instanceof Folder){
count++;
}
}
println("Total folders: " + count);
}
def getNumberOfActiveFolders(Calendar startRef,Calendar endRef){
int count = 0;
//root items
for(jobRoot in Jenkins.instance.getAllItems()) {
if(jobRoot instanceof Folder) {
boolean buildInFolderFound= false;
//jobs in folder
for(subjob in jobRoot.getItems()) {
if(subjob instanceof Job && subjob.getLastBuild() != null) {
//found subjob with builds
for(build in subjob.builds) {
//get build date and compare, 1 is enough
Date buildDate = build.getTime();
Calendar buildCal = Calendar.getInstance();
buildCal.setTime(buildDate);
if(buildCal.before(endRef) && buildCal.after(startRef)) {
count++;
buildInFolderFound = true;
break;
}
}
}
if(buildInFolderFound) {
break;
}
}
}
}
println("Active Folders: " + count);
}
def parseDate(String dateIn,String pattern){
SimpleDateFormat sf = new SimpleDateFormat(pattern);
Calendar referenceDate = Calendar.getInstance();
Date parsed = sf.parse(dateIn);
referenceDate.setTime(parsed);
return referenceDate;
}