-
Notifications
You must be signed in to change notification settings - Fork 171
/
PromotedBuildAction.java
245 lines (208 loc) · 7.01 KB
/
PromotedBuildAction.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package hudson.plugins.promoted_builds;
import hudson.RestrictedSince;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildBadgeAction;
import hudson.model.Cause.UserCause;
import hudson.plugins.promoted_builds.conditions.ManualCondition;
import hudson.util.CopyOnWriteList;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;
/**
* {@link Action} for {@link AbstractBuild} indicating that it's promoted.
*
* @author Kohsuke Kawaguchi
*/
@ExportedBean
public final class PromotedBuildAction implements BuildBadgeAction {
//TODO: bug: serialization of builds into the badge
public final AbstractBuild<?,?> owner;
/**
* Per-process status.
*/
private final CopyOnWriteList<Status> statuses = new CopyOnWriteList<Status>();
public PromotedBuildAction(AbstractBuild<?,?> owner) {
assert owner!=null;
this.owner = owner;
}
public PromotedBuildAction(AbstractBuild<?,?> owner, Status firstStatus) {
this(owner);
statuses.add(firstStatus);
}
/**
* Gets the owning build.
*/
public AbstractBuild<?,?> getOwner() {
return owner;
}
/**
* Gets the owning project.
*/
public AbstractProject<?,?> getProject() {
return owner.getProject();
}
/**
* Checks if the given criterion is already promoted.
*/
public boolean contains(PromotionProcess process) {
for (Status s : statuses)
if(s.isFor(process))
return true;
return false;
}
/**
* Checks if the given criterion is already promoted.
*/
public boolean contains(String name) {
for (Status s : statuses)
if(s.name.equals(name))
return true;
return false;
}
/**
* Called when the build is qualified.
*/
public synchronized boolean add(Status status) throws IOException {
for (Status s : statuses)
if(s.name.equals(status.name))
return false; // already qualified. noop.
this.statuses.add(status);
status.parent = this;
owner.save();
return true;
}
/**
* Gets the read-only view of all the promotions that this build achieved.
*/
@Exported
public List<Status> getPromotions() {
return statuses.getView();
}
/**
* Gets the read-only view of all the promotion builds that this build achieved
* for a PromotionProcess.
*/
public List<Promotion> getPromotionBuilds(PromotionProcess promotionProcess) {
List<Promotion> filtered = new ArrayList<Promotion>();
for(Status s: getPromotions() ){
if( s.isFor(promotionProcess)){
filtered.addAll( s.getPromotionBuilds() );
}
}
return filtered;
}
/**
* Finds the {@link Status} that has matching {@link Status#name} value.
* Or {@code null} if not found.
*/
@CheckForNull
public Status getPromotion(String name) {
for (Status s : statuses)
if(s.name.equals(name))
return s;
return null;
}
public boolean hasPromotion() {
return !statuses.isEmpty();
}
/**
* @deprecated For internal code use {@link #canPromote(String)} with the name of the process that will be promoted instead.
*/
@Deprecated
@Restricted(NoExternalUse.class)
@RestrictedSince("3.0")
public boolean canPromote() {
return this.getProject().hasPermission(Promotion.PROMOTE);
}
@Restricted(NoExternalUse.class)
public boolean canPromote(String processName) {
PromotionProcess process = getPromotionProcess(processName);
ManualCondition manualCondition = null;
if (process != null) {
manualCondition = (ManualCondition) process.getPromotionCondition(ManualCondition.class.getName());
}
return PromotionPermissionHelper.hasPermission(getProject(), manualCondition);
}
/**
* Gets list of {@link PromotionProcess}s that are not yet attained.
* @return can be empty but never null.
*/
public List<PromotionProcess> getPendingPromotions() {
JobPropertyImpl pp = getProject().getProperty(JobPropertyImpl.class);
if(pp==null) return Collections.emptyList();
List<PromotionProcess> r = new ArrayList<PromotionProcess>();
for (PromotionProcess p : pp.getActiveItems()) {
if(!contains(p)) r.add(p);
}
return r;
}
/**
* Get the specified promotion process by name.
* @return The discovered process of {@code null} if the promotion cannot be found
*/
@CheckForNull
public PromotionProcess getPromotionProcess(String name) {
JobPropertyImpl pp = getProject().getProperty(JobPropertyImpl.class);
if (pp==null)
return null;
for (PromotionProcess p : pp.getItems()) {
if(p.getName().equals(name))
return p;
}
return null;
}
public String getIconFileName() {
return "star.png";
}
public String getDisplayName() {
return "Promotion Status";
}
public String getUrlName() {
return "promotion";
}
private Object readResolve() {
// resurrect the parent pointer when read from disk
for (Status s : statuses)
s.parent = this;
return this;
}
//
// web methods
//
/**
* Binds {@link Status} to URL hierarchy by its name.
*/
public Status getDynamic(String name, StaplerRequest req, StaplerResponse rsp) {
return getPromotion(name);
}
/**
* Force a promotion.
*/
@RequirePOST
public HttpResponse doForcePromotion(@QueryParameter("name") String name) throws IOException {
JobPropertyImpl pp = getProject().getProperty(JobPropertyImpl.class);
if(pp==null)
throw new IllegalStateException("This project doesn't have any promotion criteria set");
PromotionProcess p = pp.getItem(name);
if(p==null)
throw new IllegalStateException("This project doesn't have the promotion criterion called "+name);
ManualCondition manualCondition = (ManualCondition) p.getPromotionCondition(ManualCondition.class.getName());
PromotionPermissionHelper.checkPermission(getProject(), manualCondition);
p.promote(owner,new UserCause(),new ManualPromotionBadge());
return HttpResponses.redirectToDot();
}
}