Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

/**
Expand All @@ -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("|");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<f:entry title="${%Exclude branches}" field="excludes">
<f:textbox/>
</f:entry>
<f:entry title="${%Include all pull requests}" field="includeAllPullRequests">
<f:checkbox/>
</f:entry>
<f:entry title="${%Checkout Credentials}" field="checkoutCredentialsId">
<c:select default="${descriptor.SAME}"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:l="/lib/layout">
<l:ajax>
<div>
Whether to create jobs for all discovered pull requests.
</div>
</l:ajax>
</j:jelly>