-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathBranchDiscoveryTrait.java
More file actions
364 lines (324 loc) · 11.9 KB
/
BranchDiscoveryTrait.java
File metadata and controls
364 lines (324 loc) · 11.9 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package io.jenkins.plugins.gitlabbranchsource;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.util.ListBoxModel;
import java.util.regex.Pattern;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMHeadOrigin;
import jenkins.scm.api.SCMRevision;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.trait.SCMHeadAuthority;
import jenkins.scm.api.trait.SCMHeadAuthorityDescriptor;
import jenkins.scm.api.trait.SCMHeadFilter;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceRequest;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.impl.trait.Discovery;
import org.gitlab4j.api.models.MergeRequest;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
/**
* A {@link Discovery} trait for GitLab that will discover branches on the repository.
*/
public class BranchDiscoveryTrait extends SCMSourceTrait {
/**
* The strategy encoded as a bit-field.
*/
private int strategyId;
/**
* Regex of branches that should always be included regardless of whether a merge request exists or not.
*/
private String branchesAlwaysIncludedRegex;
/**
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
*/
@CheckForNull
private transient Pattern branchesAlwaysIncludedRegexPattern;
/**
* Constructor for stapler.
*
* @param strategyId the strategy id.
*/
@DataBoundConstructor
public BranchDiscoveryTrait(int strategyId) {
this.strategyId = strategyId;
}
/**
* Constructor for legacy code.
*
* @param buildBranch build branches that are not filed as a MR.
* @param buildBranchWithMr build branches that are also MRs.
*/
public BranchDiscoveryTrait(boolean buildBranch, boolean buildBranchWithMr) {
this.strategyId = (buildBranch ? 1 : 0) + (buildBranchWithMr ? 2 : 0);
}
/**
* Returns the strategy id.
*
* @return the strategy id.
*/
public int getStrategyId() {
return strategyId;
}
/**
* Returns the branchesAlwaysIncludedRegex.
*
* @return the branchesAlwaysIncludedRegex.
*/
public String getBranchesAlwaysIncludedRegex() {
return branchesAlwaysIncludedRegex;
}
/**
* Sets the branchesAlwaysIncludedRegex.
*/
@DataBoundSetter
public void setBranchesAlwaysIncludedRegex(@CheckForNull String branchesAlwaysIncludedRegex) {
this.branchesAlwaysIncludedRegex = Util.fixEmptyAndTrim(branchesAlwaysIncludedRegex);
}
/**
* Returns the compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
*
* @return the branchesAlwaysIncludedRegexPattern.
*/
public Pattern getBranchesAlwaysIncludedRegexPattern() {
if (branchesAlwaysIncludedRegex != null && branchesAlwaysIncludedRegexPattern == null) {
branchesAlwaysIncludedRegexPattern = Pattern.compile(branchesAlwaysIncludedRegex);
}
return branchesAlwaysIncludedRegexPattern;
}
/**
* Returns {@code true} if building branches that are not filed as a MR.
*
* @return {@code true} if building branches that are not filed as a MR.
*/
@Restricted(NoExternalUse.class)
public boolean isBuildBranch() {
return (strategyId & 1) != 0;
}
/**
* Returns {@code true} if building branches that are filed as a MR.
*
* @return {@code true} if building branches that are filed as a MR.
*/
@Restricted(NoExternalUse.class)
public boolean isBuildBranchesWithMR() {
return (strategyId & 2) != 0;
}
/**
* {@inheritDoc}
*/
@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context;
ctx.wantBranches(true);
ctx.withAuthority(new BranchSCMHeadAuthority());
switch (strategyId) {
case 1:
ctx.wantOriginMRs(true);
ctx.withFilter(new ExcludeOriginMRBranchesSCMHeadFilter(getBranchesAlwaysIncludedRegexPattern()));
break;
case 2:
ctx.wantOriginMRs(true);
ctx.withFilter(new OnlyOriginMRBranchesSCMHeadFilter(getBranchesAlwaysIncludedRegexPattern()));
break;
case 4:
ctx.withFilter(new OnlyExplicitlyListedBranchesSCMHeadFilter(getBranchesAlwaysIncludedRegexPattern()));
break;
case 3:
default:
// we don't care if it is a MR or not, we're taking them all, no need to ask for MRs and no need
// to filter
break;
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean includeCategory(@NonNull SCMHeadCategory category) {
return category.isUncategorized();
}
/**
* Our descriptor.
*/
@Symbol("gitLabBranchDiscovery")
@Extension
@Discovery
public static class DescriptorImpl extends SCMSourceTraitDescriptor {
/**
* {@inheritDoc}
*/
@NonNull
@Override
public String getDisplayName() {
return Messages.BranchDiscoveryTrait_displayName();
}
/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSourceContext> getContextClass() {
return GitLabSCMSourceContext.class;
}
/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSource> getSourceClass() {
return GitLabSCMSource.class;
}
/**
* Populates the strategy options.
*
* @return the stategy options.
*/
@NonNull
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // stapler
public ListBoxModel doFillStrategyIdItems() {
ListBoxModel result = new ListBoxModel();
result.add(Messages.BranchDiscoveryTrait_excludeMRs(), "1");
result.add(Messages.BranchDiscoveryTrait_onlyMRs(), "2");
result.add(Messages.BranchDiscoveryTrait_onlyExplicitlyListed(), "4");
result.add(Messages.BranchDiscoveryTrait_allBranches(), "3");
return result;
}
}
/**
* Trusts branches from the origin repository.
*/
public static class BranchSCMHeadAuthority extends SCMHeadAuthority<SCMSourceRequest, BranchSCMHead, SCMRevision> {
/**
* {@inheritDoc}
*/
@Override
protected boolean checkTrusted(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
return true;
}
/**
* Out descriptor.
*/
@Extension
@Symbol("gitLabBranchHeadAuthority")
public static class DescriptorImpl extends SCMHeadAuthorityDescriptor {
/**
* {@inheritDoc}
*/
@Override
public boolean isApplicableToOrigin(@NonNull Class<? extends SCMHeadOrigin> originClass) {
return SCMHeadOrigin.Default.class.isAssignableFrom(originClass);
}
/**
* {@inheritDoc}
*/
@NonNull
@Override
public String getDisplayName() {
return Messages.BranchDiscoveryTrait_authorityDisplayName();
}
}
}
/**
* Base class for branch filters that support an always-included regex.
* Handles the common filter then delegates to {@link #isExcludedBranch} for strategy-specific logic.
*/
public abstract static class BranchDiscoverySCMHeadFilter extends SCMHeadFilter {
/**
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
*/
private final Pattern branchesAlwaysIncludedRegexPattern;
/**
* Constructor
*
* @param branchesAlwaysIncludedRegexPattern the branchesAlwaysIncludedRegexPattern.
*/
protected BranchDiscoverySCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
this.branchesAlwaysIncludedRegexPattern = branchesAlwaysIncludedRegexPattern;
}
/**
* {@inheritDoc}
*/
@Override
public final boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
if (!(head instanceof BranchSCMHead)) {
return false;
}
if (branchesAlwaysIncludedRegexPattern != null
&& branchesAlwaysIncludedRegexPattern
.matcher(head.getName())
.matches()) {
return false;
}
return isExcludedBranch(request, (BranchSCMHead) head);
}
/**
* Strategy-specific exclusion logic, called only for branch heads not covered by
* the always-included regex.
*
* @param request the current SCM source request.
* @param head the branch head being evaluated.
* @return {@code true} if the branch should be excluded from discovery.
*/
protected abstract boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head);
}
/**
* Filter that excludes branches that are also filed as a merge request.
*/
public static class ExcludeOriginMRBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
public ExcludeOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
super(branchesAlwaysIncludedRegexPattern);
}
@Override
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
if (request instanceof GitLabSCMSourceRequest) {
for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
// only match if the merge request is an origin merge request
if (m.getSourceProjectId().equals(m.getTargetProjectId())
&& m.getSourceBranch().equalsIgnoreCase(head.getName())) {
return true;
}
}
}
return false;
}
}
/**
* Filter that excludes branches that are not also filed as a merge request.
*/
public static class OnlyOriginMRBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
public OnlyOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
super(branchesAlwaysIncludedRegexPattern);
}
@Override
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
if (request instanceof GitLabSCMSourceRequest) {
for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
if (m.getSourceProjectId().equals(m.getTargetProjectId())
&& !m.getSourceBranch().equalsIgnoreCase(head.getName())) {
return true;
}
}
}
return false;
}
}
/**
* Filter that excludes all branches except those matching the always-included regex.
*/
public static class OnlyExplicitlyListedBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
public OnlyExplicitlyListedBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
super(branchesAlwaysIncludedRegexPattern);
}
@Override
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
return true;
}
}
}