Skip to content

Commit d303fbf

Browse files
committed
Common code in filters extracted to base class
1 parent c23abe5 commit d303fbf

1 file changed

Lines changed: 46 additions & 61 deletions

File tree

src/main/java/io/jenkins/plugins/gitlabbranchsource/BranchDiscoveryTrait.java

Lines changed: 46 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ public String getDisplayName() {
253253
}
254254

255255
/**
256-
* Filter that excludes branches that are also filed as a merge request.
256+
* Base class for branch filters that support an always-included regex.
257+
* Handles the common filter then delegates to {@link #isExcludedBranch} for strategy-specific logic.
257258
*/
258-
public static class ExcludeOriginMRBranchesSCMHeadFilter extends SCMHeadFilter {
259+
public abstract static class BranchDiscoverySCMHeadFilter extends SCMHeadFilter {
259260

260261
/**
261262
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
@@ -267,23 +268,49 @@ public static class ExcludeOriginMRBranchesSCMHeadFilter extends SCMHeadFilter {
267268
*
268269
* @param branchesAlwaysIncludedRegexPattern the branchesAlwaysIncludedRegexPattern.
269270
*/
270-
public ExcludeOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
271+
protected BranchDiscoverySCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
271272
this.branchesAlwaysIncludedRegexPattern = branchesAlwaysIncludedRegexPattern;
272273
}
273274

274275
/**
275276
* {@inheritDoc}
276277
*/
277278
@Override
278-
public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
279-
if (head instanceof BranchSCMHead && request instanceof GitLabSCMSourceRequest) {
280-
if (branchesAlwaysIncludedRegexPattern != null
281-
&& branchesAlwaysIncludedRegexPattern
282-
.matcher(head.getName())
283-
.matches()) {
284-
return false;
285-
}
279+
public final boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
280+
if (!(head instanceof BranchSCMHead)) {
281+
return false;
282+
}
283+
if (branchesAlwaysIncludedRegexPattern != null
284+
&& branchesAlwaysIncludedRegexPattern.matcher(head.getName()).matches()) {
285+
return false;
286+
}
287+
return isExcludedBranch(request, (BranchSCMHead) head);
288+
}
289+
290+
/**
291+
* Strategy-specific exclusion logic, called only for branch heads not covered by
292+
* the always-included regex.
293+
*
294+
* @param request the current SCM source request.
295+
* @param head the branch head being evaluated.
296+
* @return {@code true} if the branch should be excluded from discovery.
297+
*/
298+
protected abstract boolean isExcludedBranch(
299+
@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head);
300+
}
286301

302+
/**
303+
* Filter that excludes branches that are also filed as a merge request.
304+
*/
305+
public static class ExcludeOriginMRBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
306+
307+
public ExcludeOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
308+
super(branchesAlwaysIncludedRegexPattern);
309+
}
310+
311+
@Override
312+
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
313+
if (request instanceof GitLabSCMSourceRequest) {
287314
for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
288315
// only match if the merge request is an origin merge request
289316
if (m.getSourceProjectId().equals(m.getTargetProjectId())
@@ -299,35 +326,15 @@ public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead he
299326
/**
300327
* Filter that excludes branches that are not also filed as a merge request.
301328
*/
302-
public static class OnlyOriginMRBranchesSCMHeadFilter extends SCMHeadFilter {
329+
public static class OnlyOriginMRBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
303330

304-
/**
305-
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
306-
*/
307-
private final Pattern branchesAlwaysIncludedRegexPattern;
308-
309-
/**
310-
* Constructor
311-
*
312-
* @param branchesAlwaysIncludedRegexPattern the branchesAlwaysIncludedRegexPattern.
313-
*/
314331
public OnlyOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
315-
this.branchesAlwaysIncludedRegexPattern = branchesAlwaysIncludedRegexPattern;
332+
super(branchesAlwaysIncludedRegexPattern);
316333
}
317334

318-
/**
319-
* {@inheritDoc}
320-
*/
321335
@Override
322-
public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
323-
if (head instanceof BranchSCMHead && request instanceof GitLabSCMSourceRequest) {
324-
if (branchesAlwaysIncludedRegexPattern != null
325-
&& branchesAlwaysIncludedRegexPattern
326-
.matcher(head.getName())
327-
.matches()) {
328-
return false;
329-
}
330-
336+
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
337+
if (request instanceof GitLabSCMSourceRequest) {
331338
for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
332339
if (m.getSourceProjectId().equals(m.getTargetProjectId())
333340
&& !m.getSourceBranch().equalsIgnoreCase(head.getName())) {
@@ -342,37 +349,15 @@ public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead he
342349
/**
343350
* Filter that excludes all branches except those matching the always-included regex.
344351
*/
345-
public static class OnlyExplicitlyListedBranchesSCMHeadFilter extends SCMHeadFilter {
346-
347-
/**
348-
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
349-
*/
350-
private final Pattern branchesAlwaysIncludedRegexPattern;
352+
public static class OnlyExplicitlyListedBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
351353

352-
/**
353-
* Constructor
354-
*
355-
* @param branchesAlwaysIncludedRegexPattern the branchesAlwaysIncludedRegexPattern.
356-
*/
357354
public OnlyExplicitlyListedBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
358-
this.branchesAlwaysIncludedRegexPattern = branchesAlwaysIncludedRegexPattern;
355+
super(branchesAlwaysIncludedRegexPattern);
359356
}
360357

361-
/**
362-
* {@inheritDoc}
363-
*/
364358
@Override
365-
public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead head) {
366-
if (head instanceof BranchSCMHead) {
367-
if (branchesAlwaysIncludedRegexPattern != null
368-
&& branchesAlwaysIncludedRegexPattern
369-
.matcher(head.getName())
370-
.matches()) {
371-
return false;
372-
}
373-
return true;
374-
}
375-
return false;
359+
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
360+
return true;
376361
}
377362
}
378363
}

0 commit comments

Comments
 (0)