aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMaxim Epishchev <epishev@garant.ru>2016-01-21 19:52:23 +0300
committerMaxim Epishchev <epishev@garant.ru>2016-01-21 20:01:44 +0300
commit91a85604177f7df2eb204c9e82564142dbe328f8 (patch)
treeb950cc3f5b150005243dd2ed260add5da070d344 /src/main
parent31b3fbaa1926c751b651d58044a5216b64566029 (diff)
downloadbbprb-91a85604177f7df2eb204c9e82564142dbe328f8.tar.gz
Refactoring filters code and add tests
Now we can using more complex filters with source/destination filtration. Also add experimental integration with Git SCM plugin.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java94
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java7
-rw-r--r--src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java15
-rw-r--r--src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly3
-rw-r--r--src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html7
-rw-r--r--src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilterBySCMIncludes.html3
6 files changed, 118 insertions, 11 deletions
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java
index 230432d..1072337 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildFilter.java
@@ -1,11 +1,14 @@
package bitbucketpullrequestbuilder.bitbucketpullrequestbuilder;
+import hudson.ExtensionList;
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;
+import java.util.regex.Matcher;
+import jenkins.plugins.git.AbstractGitSCMSource;
+import jenkins.scm.api.SCMSource;
/**
* Mutable wrapper
@@ -24,8 +27,15 @@ abstract class Filter {
public static final String RX_FILTER_FLAG = "r";
public static final String RX_FILTER_FLAG_SINGLE = RX_FILTER_FLAG + ":";
+ public static final String SRC_RX = "s:(" + RX_FILTER_FLAG_SINGLE + ")?";
+ public static final String DST_RX = "d:(" + RX_FILTER_FLAG_SINGLE + ")?";
+ public static final String BRANCH_FILTER_RX_PART = "([^\\s$]*)";
+
abstract public boolean apply(String filter, BitbucketCause cause);
abstract public boolean check(String filter);
+
+ static final Pattern RX_SRC_DST_PARTS = Pattern.compile("(s:)|(d:)");
+ public static boolean HasSourceOrDestPartsPredicate(String filter) { return RX_SRC_DST_PARTS.matcher(filter).find(); }
}
class EmptyFilter extends Filter {
@@ -42,27 +52,57 @@ class AnyFlag extends Filter {
public boolean check(String filter) { return filter.isEmpty() || filter.contains("*") || filter.toLowerCase().contains("any"); }
}
+class OnlySourceFlag extends Filter {
+ @Override
+ public boolean apply(String filter, BitbucketCause cause) {
+ String selectedRx = filter.startsWith(RX_FILTER_FLAG_SINGLE) ? filter.substring(RX_FILTER_FLAG_SINGLE.length()) : Pattern.quote(filter);
+ logger.log(Level.INFO, "OnlySourceFlag using filter: {0}", selectedRx);
+ Matcher matcher = Pattern.compile(selectedRx, Pattern.CASE_INSENSITIVE).matcher(cause.getSourceBranch());
+ return filter.startsWith(RX_FILTER_FLAG_SINGLE) ? matcher.find() : matcher.matches();
+ }
+ @Override
+ public boolean check(String filter) {
+ return false;
+ }
+}
+
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();
+ String selectedRx = filter.startsWith(RX_FILTER_FLAG_SINGLE) ? filter.substring(RX_FILTER_FLAG_SINGLE.length()) : Pattern.quote(filter);
+ logger.log(Level.INFO, "OnlyDestFlag using filter: {0}", selectedRx);
+ Matcher matcher = Pattern.compile(selectedRx, Pattern.CASE_INSENSITIVE).matcher(cause.getTargetBranch());
+ return filter.startsWith(RX_FILTER_FLAG_SINGLE) ? matcher.find() : matcher.matches();
}
@Override
public boolean check(String filter) {
- return !(Pattern.matches("(s:)|(d:)", filter));
+ return !HasSourceOrDestPartsPredicate(filter);
}
}
-class SourceDestFlag extends Filter {
+class SourceDestFlag extends Filter {
+ static final Pattern SRC_MATCHER_RX = Pattern.compile(SRC_RX + BRANCH_FILTER_RX_PART, Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ);
+ static final Pattern DST_MATCHER_RX = Pattern.compile(DST_RX + BRANCH_FILTER_RX_PART, Pattern.CASE_INSENSITIVE | Pattern.CANON_EQ);
+
+ boolean applyByRx(Pattern rx, Filter usedFilter, String filter, BitbucketCause cause) {
+ Matcher srcMatch = rx.matcher(filter);
+ boolean apply = rx.matcher(filter).matches();
+ while (srcMatch.find()) {
+ String computedFilter = ((srcMatch.group(1) == null ? "" : srcMatch.group(1)) + srcMatch.group(2)).trim();
+ logger.log(Level.INFO, "Apply computed filter: {0}", computedFilter);
+ apply = apply || (computedFilter.isEmpty() ? true : usedFilter.apply(computedFilter, cause));
+ }
+ return apply;
+ }
+
@Override
public boolean apply(String filter, BitbucketCause cause) {
- return false;
+ return this.applyByRx(SRC_MATCHER_RX, new OnlySourceFlag(), filter, cause) &&
+ this.applyByRx(DST_MATCHER_RX, new OnlyDestFlag(), filter, cause);
}
@Override
public boolean check(String filter) {
- return Pattern.matches("(s:)|(d:)", filter);
+ return HasSourceOrDestPartsPredicate(filter);
}
}
@@ -80,12 +120,13 @@ public class BitbucketBuildFilter {
ArrayList<Filter> filters = new ArrayList<Filter>();
filters.add(new AnyFlag());
filters.add(new OnlyDestFlag());
- filters.add(new EmptyFilter());
+ filters.add(new SourceDestFlag());
+ filters.add(new EmptyFilter());
AvailableFilters = filters;
}
public BitbucketBuildFilter(String f) {
- this.filter = f != null ? f : "";
+ this.filter = (f != null ? f : "").trim();
this.buildFilter(this.filter);
}
@@ -104,4 +145,37 @@ public class BitbucketBuildFilter {
logger.log(Level.INFO, "Approve cause: {0}", cause.toString());
return this.currFilter.apply(this.filter, cause);
}
+
+ public static BitbucketBuildFilter InstanceByString(String filter) {
+ logger.log(Level.INFO, "Filter instance by filter string");
+ return new BitbucketBuildFilter(filter);
+ }
+
+ static public String FilterFromGitSCMSource(AbstractGitSCMSource gitscm, String defaultFilter) {
+ if (gitscm == null) {
+ logger.log(Level.INFO, "Git SCMSource unavailable. Using default value: {0}", defaultFilter);
+ return defaultFilter;
+ }
+
+ String filter = defaultFilter;
+ final String includes = gitscm.getIncludes();
+ if (includes != null && !includes.isEmpty()) {
+ for(String part : includes.split("\\s+")) {
+ filter += String.format("%s ", part.replaceAll("\\*\\/", "d:"));
+ }
+ }
+
+ logger.log(Level.INFO, "Git includes transformation to filter result: {1} -> {0}; default: {2}", new Object[]{ filter, includes, defaultFilter });
+ return filter.trim();
+ }
+
+ public static BitbucketBuildFilter InstanceBySCM(ExtensionList<SCMSource> scmSources, String defaultFilter) {
+ logger.log(Level.FINE, "Filter instance by using SCM");
+ AbstractGitSCMSource gitscm = null;
+ for(SCMSource scm : scmSources) {
+ gitscm = (AbstractGitSCMSource)scm;
+ if (gitscm != null) break;
+ }
+ return new BitbucketBuildFilter(FilterFromGitSCMSource(gitscm, defaultFilter));
+ }
}
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java
index 254eec3..cd9345c 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger.java
@@ -29,6 +29,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
private final String repositoryOwner;
private final String repositoryName;
private final String branchesFilter;
+ private final boolean branchesFilterBySCMIncludes;
private final String ciKey;
private final String ciName;
private final String ciSkipPhrases;
@@ -49,6 +50,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
String repositoryOwner,
String repositoryName,
String branchesFilter,
+ boolean branchesFilterBySCMIncludes,
String ciKey,
String ciName,
String ciSkipPhrases,
@@ -63,6 +65,7 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
this.repositoryOwner = repositoryOwner;
this.repositoryName = repositoryName;
this.branchesFilter = branchesFilter;
+ this.branchesFilterBySCMIncludes = branchesFilterBySCMIncludes;
this.ciKey = ciKey;
this.ciName = ciName;
this.ciSkipPhrases = ciSkipPhrases;
@@ -97,6 +100,10 @@ public class BitbucketBuildTrigger extends Trigger<AbstractProject<?, ?>> {
public String getBranchesFilter() {
return branchesFilter;
}
+
+ public boolean getBranchesFilterBySCMIncludes() {
+ return branchesFilterBySCMIncludes;
+ }
public String getCiKey() {
return ciKey;
diff --git a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
index 2686ffd..c71cc99 100644
--- a/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
+++ b/src/main/java/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketRepository.java
@@ -10,7 +10,10 @@ import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClie
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.BuildState;
import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest;
import java.util.logging.Level;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import jenkins.model.Jenkins;
+import jenkins.scm.api.SCMSource;
/**
* Created by nishio
@@ -19,6 +22,7 @@ public class BitbucketRepository {
private static final Logger logger = Logger.getLogger(BitbucketRepository.class.getName());
private static final String BUILD_DESCRIPTION = "%s: %s into %s";
private static final String BUILD_REQUEST_MARKER = "test this please";
+ private static final String BUILD_REQUEST_MARKER_COUNTER_RX = "\\[(\\d+)]\\";
private String projectPath;
private BitbucketPullRequestsBuilder builder;
@@ -99,6 +103,11 @@ public class BitbucketRepository {
this.client.postPullRequestApproval(pullRequestId);
}
+ private Integer extractRebuildTimesFromComment(String content) {
+ Matcher matcher = Pattern.compile(BUILD_REQUEST_MARKER_COUNTER_RX).matcher(content);
+ return matcher.groupCount() >= 1 ? Integer.parseInt(matcher.group(1)) : 0;
+ }
+
private boolean isBuildTarget(Pullrequest pullRequest) {
if (pullRequest.getState() != null && pullRequest.getState().equals("OPEN")) {
if (isSkipBuild(pullRequest.getTitle()) || !isFilteredBuild(pullRequest)) {
@@ -178,7 +187,11 @@ public class BitbucketRepository {
pullRequest.getSource().getCommit().getHash(),
pullRequest.getDestination().getCommit().getHash()
);
- BitbucketBuildFilter filter = new BitbucketBuildFilter(this.trigger.getBranchesFilter());
+
+ BitbucketBuildFilter filter = !this.trigger.getBranchesFilterBySCMIncludes() ?
+ BitbucketBuildFilter.InstanceByString(this.trigger.getBranchesFilter()) :
+ BitbucketBuildFilter.InstanceBySCM(Jenkins.getInstance().getExtensionList(SCMSource.class), 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 845926d..25acac0 100644
--- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly
+++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/config.jelly
@@ -17,6 +17,9 @@
<f:entry title="BranchesFilter" field="branchesFilter">
<f:textbox />
</f:entry>
+ <f:entry title="Using Git SCM 'Branches to build' option to filter pull requests?" field="branchesFilterBySCMIncludes">
+ <f:checkbox />
+ </f:entry>
<f:entry title="CI Identifier" field="ciKey">
<f:textbox default="jenkins" />
</f:entry>
diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html
index f49da40..88af799 100644
--- a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html
+++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilter.html
@@ -3,3 +3,10 @@ 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"
+* filtered by source and destination branches with regex: "s:r:^feature d:r:master$"
+* filtered by many destination/source branches: "s:one s:two s:three d:master d:r:master$"
+* filtered by many sources branches: "s:one s:two s:r:^three d:"
+When you using format with source branch filter "s" or destination filter "d", you must cpecify great than one source and destination filter, eg "s:1 s:2 s:... d:".
+Any sources and any destinations for pull request:
+* filter string: "*"
+* filter string: "s: d:"
diff --git a/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilterBySCMIncludes.html b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilterBySCMIncludes.html
new file mode 100644
index 0000000..3f24419
--- /dev/null
+++ b/src/main/resources/bitbucketpullrequestbuilder/bitbucketpullrequestbuilder/BitbucketBuildTrigger/help-branchesFilterBySCMIncludes.html
@@ -0,0 +1,3 @@
+It is sugar-option for synchronize option "BranchesFilter" to Git SCM option "Branches to build" without manual editing.
+Check this option suppose than your "BranchesFilter" field has logick equal value with Git SCM "Branches to build" option (original value from "BranchesFilter" field will e ignored).
+If "Branches to build" option has values "*/master */feature-master */build-with-jenkins", then "BranchesFilter" field will have value "d:master d:feature-master d:build-with-jenkins".