/
BuildHistoryTab.java
174 lines (156 loc) · 6.79 KB
/
BuildHistoryTab.java
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
/*
* The MIT License (MIT)
*
* Copyright (c) 2014, Groupon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.groupon.jenkins.branchhistory;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.groupon.jenkins.SetupConfig;
import com.groupon.jenkins.dynamic.build.DbBackedProject;
import com.groupon.jenkins.dynamic.build.DynamicBuild;
import com.groupon.jenkins.dynamic.build.DynamicProject;
import com.groupon.jenkins.dynamic.build.DynamicProjectBranchTabsProperty;
import com.groupon.jenkins.dynamic.build.repository.DynamicBuildRepository;
import hudson.model.Queue;
import jenkins.model.Jenkins;
import java.util.*;
public class BuildHistoryTab {
private boolean active;
private String url;
private String font;
private String state;
private String name;
private boolean removable;
private DynamicProject project;
private static BuildHistoryTab getAll(DynamicProject project){
return new BuildHistoryTab("all","fa fa-users","grey","All", false,project);
}
private static BuildHistoryTab getMine(DynamicProject project){
return new BuildHistoryTab("mine","octicon octicon-person","grey","mine", false,project);
}
private static BuildHistoryTab getBranch(String branch, DynamicProject project){
return new BuildHistoryTab(branch,"octicon octicon-git-branch","grey",branch, true,project);
}
public BuildHistoryTab(String url, String font, String state, String name, boolean removable, DynamicProject project) {
this.url = url;
this.font = font;
this.state = state;
this.name = name;
this.removable = removable;
this.project = project;
}
private DynamicBuildRepository getDynamicBuildRepository(){
return SetupConfig.get().getDynamicBuildRepository();
}
public Iterable<BuildHistoryRow> getBuilds() {
String branch = getUrl().equals("all")?null:getUrl();
Iterable<BuildHistoryRow> processedBuilds = Iterables.transform(filterSkipped(isMyBuilds() ? getDynamicBuildRepository().<DynamicBuild>getCurrentUserBuilds(project, BranchHistoryWidget.BUILD_COUNT) : getDynamicBuildRepository().<DynamicBuild>getLast(project, BranchHistoryWidget.BUILD_COUNT, branch)), getBuildTransformer());
return Iterables.concat(getQueuedBuilds(),processedBuilds);
}
private Iterable<BuildHistoryRow> getQueuedBuilds() {
ArrayList<BuildHistoryRow> queuedBuilds = new ArrayList<BuildHistoryRow>();
final List<Queue.Item> queuedItems = getQueuedItems();
for(int i=0 ; i < queuedItems.size(); i++){
int number = queuedItems.size() == 1 ? project.getNextBuildNumber() : project.getNextBuildNumber() + queuedItems.size() - i - 1;
queuedBuilds.add(new QueuedBuildHistoryRow(queuedItems.get(i),number));
}
return queuedBuilds;
}
private Function<DynamicBuild, BuildHistoryRow> getBuildTransformer() {
return new Function<DynamicBuild, BuildHistoryRow>() {
@Override
public BuildHistoryRow apply(DynamicBuild dynamicBuild) {
return new ProcessedBuildHistoryRow(dynamicBuild);
}
};
}
private Iterable<DynamicBuild> filterSkipped(Iterable<DynamicBuild> builds) {
return Iterables.filter(builds, new Predicate<DynamicBuild>() {
@Override
public boolean apply(DynamicBuild build) {
return !build.isSkipped();
}
});
}
public boolean isActive(){
return active;
}
public String getUrl(){
return url;
}
public String getFont(){
return font;
}
public String getState(){
return state;
}
public String getName(){
return name;
}
public boolean isRemovable(){
return removable;
}
private static Iterable<BuildHistoryTab> getTabs(List<String> branches,DynamicProject project) {
ArrayList<BuildHistoryTab> tabs = new ArrayList<BuildHistoryTab>();
tabs.add(getAll(project));
tabs.add(getMine(project));
for(String branch:branches){
tabs.add(getBranch(branch,project));
}
return tabs;
}
public void setActive() {
this.active =true;
}
public static Iterable<BuildHistoryTab> getTabs(DynamicProject project) {
DynamicProjectBranchTabsProperty tabsProperty = getTabsProperty(project);
List<String> branches = tabsProperty == null ? Collections.<String>emptyList() : tabsProperty.getBranches();
return getTabs(branches,project);
}
private static DynamicProjectBranchTabsProperty getTabsProperty(DynamicProject project) {
return project.getProperty(DynamicProjectBranchTabsProperty.class);
}
public boolean isMyBuilds() {
return getName().equals("mine");
}
public List<Queue.Item> getQueuedItems() {
LinkedList<Queue.Item> list = new LinkedList<Queue.Item>();
for (Queue.Item item : Jenkins.getInstance().getQueue().getApproximateItemsQuickly()) {
if (item.task == project) {
list.addFirst(item);
}
}
return list;
}
// public void doAjax(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
// req.getView(this, "ajax_build_history.jelly").forward(req, rsp);
// }
//
// public Iterable<DynamicBuild> getAjaxList() {
// StaplerRequest req = Stapler.getCurrentRequest();
// int firstBuildNumber = Integer.parseInt(req.getParameter("firstBuildNumber"));
// int lastBuildNumber = Integer.parseInt(req.getParameter("lastBuildNumber"));
// return model.getBuildsInProgress(firstBuildNumber, lastBuildNumber);
// }
}