Skip to content

Commit 0ca0e05

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

1 file changed

Lines changed: 47 additions & 61 deletions

File tree

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

Lines changed: 47 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,50 @@ 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
285+
.matcher(head.getName())
286+
.matches()) {
287+
return false;
288+
}
289+
return isExcludedBranch(request, (BranchSCMHead) head);
290+
}
291+
292+
/**
293+
* Strategy-specific exclusion logic, called only for branch heads not covered by
294+
* the always-included regex.
295+
*
296+
* @param request the current SCM source request.
297+
* @param head the branch head being evaluated.
298+
* @return {@code true} if the branch should be excluded from discovery.
299+
*/
300+
protected abstract boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head);
301+
}
286302

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

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-
*/
314332
public OnlyOriginMRBranchesSCMHeadFilter(Pattern branchesAlwaysIncludedRegexPattern) {
315-
this.branchesAlwaysIncludedRegexPattern = branchesAlwaysIncludedRegexPattern;
333+
super(branchesAlwaysIncludedRegexPattern);
316334
}
317335

318-
/**
319-
* {@inheritDoc}
320-
*/
321336
@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-
337+
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
338+
if (request instanceof GitLabSCMSourceRequest) {
331339
for (MergeRequest m : ((GitLabSCMSourceRequest) request).getMergeRequests()) {
332340
if (m.getSourceProjectId().equals(m.getTargetProjectId())
333341
&& !m.getSourceBranch().equalsIgnoreCase(head.getName())) {
@@ -342,37 +350,15 @@ public boolean isExcluded(@NonNull SCMSourceRequest request, @NonNull SCMHead he
342350
/**
343351
* Filter that excludes all branches except those matching the always-included regex.
344352
*/
345-
public static class OnlyExplicitlyListedBranchesSCMHeadFilter extends SCMHeadFilter {
346-
347-
/**
348-
* The compiled {@link Pattern} of the branchesAlwaysIncludedRegex.
349-
*/
350-
private final Pattern branchesAlwaysIncludedRegexPattern;
353+
public static class OnlyExplicitlyListedBranchesSCMHeadFilter extends BranchDiscoverySCMHeadFilter {
351354

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

361-
/**
362-
* {@inheritDoc}
363-
*/
364359
@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;
360+
protected boolean isExcludedBranch(@NonNull SCMSourceRequest request, @NonNull BranchSCMHead head) {
361+
return true;
376362
}
377363
}
378364
}

0 commit comments

Comments
 (0)