diff options
Diffstat (limited to 'src')
5 files changed, 143 insertions, 5 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java new file mode 100644 index 0000000..eb80d86 --- /dev/null +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java @@ -0,0 +1,106 @@ +package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder; + +import java.util.logging.Logger; +import java.util.regex.Pattern; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.logging.Level; + +/** + * Mutable wrapper + */ +class Mutable<T> { + private T value; + public Mutable() { this.value = null; } + public Mutable(T value) { this.value = value; } + T get() { return this.value; } + void set(T value) { this.value = value; } +} + +abstract class Filter { + protected static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); + + public static final String RX_FILTER_FLAG = "r"; + public static final String RX_FILTER_FLAG_SINGLE = RX_FILTER_FLAG + ":"; + + abstract public boolean apply(String filter, BitbucketCause cause); + abstract public boolean check(String filter); +} + +class EmptyFilter extends Filter { + @Override + public boolean apply(String filter, BitbucketCause cause) { return true; } + @Override + public boolean check(String filter) { return true; } +} + +class AnyFlag extends Filter { + @Override + public boolean apply(String filter, BitbucketCause cause) { return true; } + @Override + public boolean check(String filter) { return filter.isEmpty() || filter.contains("*") || filter.toLowerCase().contains("any"); } +} + +class OnlyDestFlag extends Filter { + @Override + public boolean apply(String filter, BitbucketCause cause) { + return Pattern.compile( + filter.startsWith(RX_FILTER_FLAG_SINGLE) ? filter.substring(RX_FILTER_FLAG_SINGLE.length()) : Pattern.quote(filter), + Pattern.CASE_INSENSITIVE).matcher(cause.getTargetBranch()).find(); + } + @Override + public boolean check(String filter) { + return !(Pattern.matches("(s:)|(d:)", filter)); + } +} + +class SourceDestFlag extends Filter { + @Override + public boolean apply(String filter, BitbucketCause cause) { + return false; + } + @Override + public boolean check(String filter) { + return Pattern.matches("(s:)|(d:)", filter); + } +} + +/** + * Created by maxvodo + */ +public class BitbucketBuildFilter { + private static final Logger logger = Logger.getLogger(BitbucketBuildTrigger.class.getName()); + + private final String filter; + private Filter currFilter = null; + private static final List<Filter> AvailableFilters; + + static { + ArrayList<Filter> filters = new ArrayList<Filter>(); + filters.add(new AnyFlag()); + filters.add(new OnlyDestFlag()); + filters.add(new EmptyFilter()); + AvailableFilters = filters; + } + + public BitbucketBuildFilter(String f) { + this.filter = f; + this.buildFilter(this.filter); + } + + private void buildFilter(String filter) { + for(Filter f : AvailableFilters) { + if (f.check(filter)) { + this.currFilter = f; + logger.log(Level.INFO, "Using filter: {0}", f.getClass().getSimpleName()); + break; + } + } + logger.warning("No available filters to use ..."); + } + + public boolean approved(BitbucketCause cause) { + return this.currFilter.apply(this.filter, cause); + } +} diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java index 247bd67..254eec3 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java @@ -28,16 +28,17 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { private final String password; private final String repositoryOwner; private final String repositoryName; + private final String branchesFilter; private final String ciKey; private final String ciName; private final String ciSkipPhrases; private final boolean checkDestinationCommit; private final boolean approveIfSuccess; - + transient private BitbucketPullRequestsBuilder bitbucketPullRequestsBuilder; @Extension - public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor(); + public static final BitbucketBuildTriggerDescriptor descriptor = new BitbucketBuildTriggerDescriptor(); @DataBoundConstructor public BitbucketBuildTrigger( @@ -47,6 +48,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { String password, String repositoryOwner, String repositoryName, + String branchesFilter, String ciKey, String ciName, String ciSkipPhrases, @@ -60,6 +62,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { this.password = password; this.repositoryOwner = repositoryOwner; this.repositoryName = repositoryName; + this.branchesFilter = branchesFilter; this.ciKey = ciKey; this.ciName = ciName; this.ciSkipPhrases = ciSkipPhrases; @@ -91,6 +94,10 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { return repositoryName; } + public String getBranchesFilter() { + return branchesFilter; + } + public String getCiKey() { return ciKey; } @@ -102,7 +109,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> { public String getCiSkipPhrases() { return ciSkipPhrases; } - + public boolean getCheckDestinationCommit() { return checkDestinationCommit; } diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java index dd0f5e5..e0a4b19 100644 --- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java +++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java @@ -100,7 +100,7 @@ public class BitbucketRepository { private boolean isBuildTarget(Pullrequest pullRequest) { if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) { - if (isSkipBuild(pullRequest.getTitle())) { + if (isSkipBuild(pullRequest.getTitle()) || !isFilteredBuild(pullRequest)) { return false; } @@ -153,4 +153,21 @@ public class BitbucketRepository { } return false; } + + private boolean isFilteredBuild(Pullrequest pullRequest) { + BitbucketCause cause = new BitbucketCause( + pullRequest.getSource().getBranch().getName(), + pullRequest.getDestination().getBranch().getName(), + pullRequest.getSource().getRepository().getOwnerName(), + pullRequest.getSource().getRepository().getRepositoryName(), + pullRequest.getId(), + pullRequest.getDestination().getRepository().getOwnerName(), + pullRequest.getDestination().getRepository().getRepositoryName(), + pullRequest.getTitle(), + pullRequest.getSource().getCommit().getHash(), + pullRequest.getDestination().getCommit().getHash() + ); + BitbucketBuildFilter filter = new BitbucketBuildFilter(this.trigger.getBranchesFilter()); + return filter.approved(cause); + } } diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly index 82ab08c..845926d 100644 --- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly @@ -14,6 +14,9 @@ <f:entry title="RepositoryName" field="repositoryName"> <f:textbox /> </f:entry> + <f:entry title="BranchesFilter" field="branchesFilter"> + <f:textbox /> + </f:entry> <f:entry title="CI Identifier" field="ciKey"> <f:textbox default="jenkins" /> </f:entry> @@ -29,4 +32,4 @@ <f:entry title="Approve if build success?" field="approveIfSuccess"> <f:checkbox /> </f:entry> -</j:jelly>
\ No newline at end of file +</j:jelly> diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html new file mode 100644 index 0000000..8b801db --- /dev/null +++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html @@ -0,0 +1,5 @@ +Filter option in custom format. Default value is empty or "any". +Available formats: +* any pull requests applied for this project: "any", "*" or empty string. +* filtered by destination branch: "my-branch" or more complex reg-ex filter "r:^master" (must be started with "r:" and case insensitive match). +* filtered by source and destination branches: "s:source-branch d:dest-branch"
\ No newline at end of file |