diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java index 64be0e3f9..068ddf3a3 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java @@ -136,6 +136,11 @@ public class BitbucketSCMSource extends SCMSource { */ private String excludes = ""; + /** + * Whether to include all pull requests. + */ + private boolean includeAllPullRequests; + /** * If true, a webhook will be auto-registered in the repository managed by this source. */ @@ -218,6 +223,15 @@ public void setExcludes(@NonNull String excludes) { this.excludes = excludes; } + public boolean isIncludeAllPullRequests() { + return includeAllPullRequests; + } + + @DataBoundSetter + public void setIncludeAllPullRequests(boolean includeAllPullRequests) { + this.includeAllPullRequests = includeAllPullRequests; + } + public String getRepoOwner() { return repoOwner; } @@ -400,7 +414,7 @@ private void retrieveBranches(SCMSourceCriteria criteria, @NonNull final SCMHead private void observe(SCMSourceCriteria criteria, SCMHeadObserver observer, final TaskListener listener, final String owner, final String repositoryName, final String branchName, final String hash, BitbucketPullRequest pr) throws IOException, InterruptedException { - if (isExcluded(branchName)) { + if (isExcluded(branchName, pr != null)) { return; } final BitbucketApi bitbucket = BitbucketApiFactory.newInstance(bitbucketServerUrl, getScanCredentials(), owner, repositoryName); @@ -612,14 +626,16 @@ public String getRemoteName() { } /** - * Returns true if the branchName isn't matched by includes or is matched by excludes. + * Returns true if the branchName isn't matched by includes or is matched by excludes + * unless it is a pull requests and all pull requests should be built. * * @param branchName + * @param isPullRequest * @return true if branchName is excluded or is not included */ - private boolean isExcluded(String branchName) { - return !Pattern.matches(getPattern(getIncludes()), branchName) - || Pattern.matches(getPattern(getExcludes()), branchName); + private boolean isExcluded(String branchName, boolean isPullRequest) { + return !(isPullRequest && isIncludeAllPullRequests()) && + (!Pattern.matches(getPattern(getIncludes()), branchName) || Pattern.matches(getPattern(getExcludes()), branchName)); } /** @@ -633,14 +649,12 @@ private String getPattern(String branches) { StringBuilder quotedBranches = new StringBuilder(); for (String wildcard : branches.split(" ")) { StringBuilder quotedBranch = new StringBuilder(); - for (String branch : wildcard.split("\\*")) { - if (wildcard.startsWith("*") || quotedBranches.length() > 0) { + for (String branch : wildcard.split("((?<=\\*)|(?=\\*))")) { + if ("*".equals(branch)) { quotedBranch.append(".*"); + } else { + quotedBranch.append(Pattern.quote(branch)); } - quotedBranch.append(Pattern.quote(branch)); - } - if (wildcard.endsWith("*")) { - quotedBranch.append(".*"); } if (quotedBranches.length() > 0) { quotedBranches.append("|"); diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/config-detail.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/config-detail.jelly index c1166e092..dac6d61f7 100644 --- a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/config-detail.jelly +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/config-detail.jelly @@ -20,6 +20,9 @@ + + + diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-includeAllPullRequests.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-includeAllPullRequests.jelly new file mode 100644 index 000000000..99db35d6d --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-includeAllPullRequests.jelly @@ -0,0 +1,8 @@ + + + +
+ Whether to create jobs for all discovered pull requests. +
+
+
\ No newline at end of file