forked from jenkinsci/branch-api-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiBranchProjectDescriptor.java
More file actions
266 lines (245 loc) · 9.75 KB
/
MultiBranchProjectDescriptor.java
File metadata and controls
266 lines (245 loc) · 9.75 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* The MIT License
*
* Copyright (c) 2011-2013, CloudBees, Inc., Stephen Connolly.
*
* 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 jenkins.branch;
import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.cloudbees.hudson.plugins.folder.AbstractFolderDescriptor;
import com.cloudbees.hudson.plugins.folder.ChildNameGenerator;
import com.cloudbees.hudson.plugins.folder.FolderIconDescriptor;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.ExtensionList;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.model.TopLevelItemDescriptor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMSourceDescriptor;
import org.jvnet.tiger_types.Types;
/**
* <p>The {@link Descriptor} for {@link MultiBranchProject}s.</p>
*
* <p>Compatible {@link hudson.scm.SCM}s displayed by {@link jenkins.scm.impl.SingleSCMSource} (via their
* {@link hudson.scm.SCMDescriptor}) can be defined by overriding {@link #isApplicable(Descriptor)}:</p>
* <pre>
* @Override
* public boolean isApplicable(Descriptor descriptor) {
* if (descriptor instanceof SCMDescriptor) {
* SCMDescriptor d = (SCMDescriptor) descriptor;
* // Your logic
* }
* return super.isApplicable(descriptor);
* }
* </pre>
*
* @author Stephen Connolly
*/
public abstract class MultiBranchProjectDescriptor extends AbstractFolderDescriptor {
/**
* The base class of the projects that are produced by this factory.
*
* @since 2.0
*/
@NonNull
private final Class<? extends Job> projectClass;
/**
* Explicit constructor to use when type inference fails.
*
* @param clazz the {@link MultiBranchProject} that this descriptor is for.
* @param projectClass the {@link Job} type that the {@link MultiBranchProject} creates.
* @since 2.0
*/
protected MultiBranchProjectDescriptor(Class<? extends MultiBranchProject<?, ?>> clazz,
Class<? extends Job> projectClass) {
super(clazz);
this.projectClass = projectClass;
}
/**
* Semi explicit constructor to use when the descriptor is not an inner class of the {@link MultiBranchProject}.
*
* @param clazz the {@link MultiBranchProject} that this descriptor is for.
* @since 2.0
*/
protected MultiBranchProjectDescriptor(Class<? extends MultiBranchProject<?, ?>> clazz) {
super(clazz);
projectClass = inferProjectClass(clazz);
}
/**
* Fully inferring constructor to use when the descriptor is an inner class of the {@link MultiBranchProject}
* and type parameter inference works because it just should work.
*
* @since 2.0
*/
protected MultiBranchProjectDescriptor() {
super();
projectClass = inferProjectClass((Class) clazz);
}
/**
* Infers the base class of projects that the specified {@link MultiBranchProject} creates.
*
* @param clazz the specified {@link MultiBranchProject}.
* @return the base class of project that the specified {@link MultiBranchProject} creates.
*/
private static Class<? extends Job> inferProjectClass(Class<? extends MultiBranchProject> clazz) {
Type bt = Types.getBaseClass(clazz, MultiBranchProject.class);
if (bt instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) bt;
// this 'p' is the closest approximation of P of MultiBranchProject<P,R>.
Class p = Types.erasure(pt.getActualTypeArguments()[0]);
if (!Job.class.isAssignableFrom(p)) {
throw new AssertionError(
"Cannot infer job type produced by " + clazz + " perhaps use the explicit constructor");
}
return p;
} else {
throw new AssertionError(
"Cannot infer job type produced by " + clazz + " perhaps use the explicit constructor");
}
}
/**
* Returns the base class of the projects that are produced by this factory.
*
* @return the base class of the projects that are produced by this factory.
* @since 2.0
*/
@NonNull
public Class<? extends Job> getProjectClass() {
return projectClass;
}
/**
* We have to extend {@link TopLevelItemDescriptor} but we want to be able to access {@link #clazz} as a
* {@link MultiBranchProject} based type.
*
* @return the {@link #clazz}
*/
@NonNull
public Class<? extends MultiBranchProject> getClazz() {
return clazz.asSubclass(MultiBranchProject.class);
}
/**
* Gets the {@link SCMSourceDescriptor}s.
*
* @param onlyUserInstantiable {@code true} retains only those {@link jenkins.scm.api.SCMSource} types that
* are instantiable by the user.
* @return the list of {@link SCMSourceDescriptor}s.
*/
@SuppressWarnings("unused") // used by stapler
@NonNull
public List<SCMSourceDescriptor> getSCMSourceDescriptors(boolean onlyUserInstantiable) {
return SCMSourceDescriptor.forOwner(getClazz(), onlyUserInstantiable);
}
/**
* Returns the {@link BranchProjectFactoryDescriptor}s.
*
* @return the {@link BranchProjectFactoryDescriptor}s.
*/
@SuppressWarnings("unused") // used by stapler
@NonNull
public List<BranchProjectFactoryDescriptor> getProjectFactoryDescriptors() {
List<BranchProjectFactoryDescriptor> result = new ArrayList<>();
for (BranchProjectFactoryDescriptor descriptor : ExtensionList.lookup(BranchProjectFactoryDescriptor.class)) {
if (descriptor.isApplicable(getClazz()) && descriptor.getProjectClass().isAssignableFrom(getProjectClass())) {
result.add(descriptor);
}
}
return result;
}
/**
* Returns the {@link BranchSource.DescriptorImpl}.
*
* @return the {@link BranchSource.DescriptorImpl}.
*/
@SuppressWarnings({"unused", "unchecked"}) // used by stapler
@NonNull
public Descriptor<BranchSource> getBranchSourceDescriptor() {
return Jenkins.get().getDescriptorOrDie(BranchSource.class);
}
/**
* {@inheritDoc}
*/
@Override
public List<FolderIconDescriptor> getIconDescriptors() {
return Collections.singletonList(
Jenkins.get().getDescriptorByType(MetadataActionFolderIcon.DescriptorImpl.class)
);
}
/**
* {@inheritDoc}
*/
@Override
public boolean isIconConfigurable() {
return true;
}
@SuppressWarnings("unchecked")
@Override
@NonNull
public <I extends TopLevelItem> ChildNameGenerator<AbstractFolder<I>,I> childNameGenerator() {
return (ChildNameGenerator<AbstractFolder<I>, I>)ChildNameGeneratorImpl.INSTANCE;
}
public static class ChildNameGeneratorImpl<P extends Job<P, R> & TopLevelItem,
R extends Run<P, R>> extends ChildNameGenerator<MultiBranchProject<P,R>,P> {
/*package*/ static final ChildNameGeneratorImpl INSTANCE = new ChildNameGeneratorImpl();
@Override
@CheckForNull
public String itemNameFromItem(@NonNull MultiBranchProject<P,R> parent, @NonNull P item) {
BranchProjectFactory<P, R> factory = parent.getProjectFactory();
if (factory.isProject(item)) {
return NameEncoder.encode(factory.getBranch(item).getName());
}
String name = item.getName();
// if (name != null) {
return NameEncoder.encode(name);
// }
// return null;
}
@Override
@CheckForNull
public String dirNameFromItem(@NonNull MultiBranchProject<P,R> parent, @NonNull P item) {
BranchProjectFactory<P, R> factory = parent.getProjectFactory();
if (factory.isProject(item)) {
return NameMangler.apply(factory.getBranch(item).getName());
}
String name = item.getName();
// if (name != null) {
return NameMangler.apply(name);
// }
// return null;
}
@Override
@NonNull
public String itemNameFromLegacy(@NonNull MultiBranchProject<P, R> parent, @NonNull String legacyDirName) {
return NameEncoder.decode(legacyDirName);
}
@Override
@NonNull
public String dirNameFromLegacy(@NonNull MultiBranchProject<P, R> parent, @NonNull String legacyDirName) {
return NameMangler.apply(NameEncoder.decode(legacyDirName));
}
}
}