From a21c6949476c27febeedc84ac450d17b40247961 Mon Sep 17 00:00:00 2001 From: Stefan Winkler Date: Sat, 21 Jan 2017 17:05:08 +0100 Subject: [PATCH] Add options for scanning branches/PRs Add two options to individually include/exclude branches and pull requests from scanning. --- .../plugins/bitbucket/BitbucketSCMSource.java | 36 +++++++++++++++++-- .../BitbucketSCMSource/config-detail.jelly | 6 ++++ .../help-scanBranches.jelly | 11 ++++++ .../help-scanPullRequests.jelly | 11 ++++++ .../plugins/bitbucket/BranchScanningTest.java | 24 +++++++++++++ 5 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanBranches.jelly create mode 100644 src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanPullRequests.jelly 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 b6de37768..82788939e 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java @@ -132,6 +132,16 @@ public class BitbucketSCMSource extends SCMSource { */ private String excludes = ""; + /** + * If true, branches are included when scanning in the retrieve process. + */ + private boolean scanBranches = true; + + /** + * If true, pull requests are included when scanning in the retrieve process. + */ + private boolean scanPullRequests = true; + /** * If true, a webhook will be auto-registered in the repository managed by this source. */ @@ -213,6 +223,24 @@ public void setExcludes(@NonNull String excludes) { Pattern.compile(getPattern(excludes)); this.excludes = excludes; } + + @DataBoundSetter + public void setScanBranches(boolean scanBranches) { + this.scanBranches = scanBranches; + } + + public boolean getScanBranches() { + return scanBranches; + } + + @DataBoundSetter + public void setScanPullRequests(boolean scanPullRequests) { + this.scanPullRequests = scanPullRequests; + } + + public boolean getScanPullRequests() { + return scanPullRequests; + } public String getRepoOwner() { return repoOwner; @@ -304,9 +332,13 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe } // Search branches - retrieveBranches(criteria, observer, listener); + if (getScanBranches()) { + retrieveBranches(criteria, observer, listener); + } // Search pull requests - retrievePullRequests(criteria, observer, listener); + if (getScanPullRequests()) { + retrievePullRequests(criteria, observer, listener); + } } private void retrievePullRequests(SCMSourceCriteria criteria, SCMHeadObserver observer, final TaskListener listener) 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..e60a30f22 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,12 @@ + + + + + + diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanBranches.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanBranches.jelly new file mode 100644 index 000000000..bd888e138 --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanBranches.jelly @@ -0,0 +1,11 @@ + + + +
+

+ If this option is activated, branches are included for scanning. Deactivate + it if only pull requests should be returned by scanning. +

+
+
+
\ No newline at end of file diff --git a/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanPullRequests.jelly b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanPullRequests.jelly new file mode 100644 index 000000000..62cb1f252 --- /dev/null +++ b/src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource/help-scanPullRequests.jelly @@ -0,0 +1,11 @@ + + + +
+

+ If this option is activated, pull requests are included for scanning. + Deactivate it if pull request should should not be returned by scanning. +

+
+
+
\ No newline at end of file diff --git a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BranchScanningTest.java b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BranchScanningTest.java index a4fc9b67d..4de477d5e 100644 --- a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BranchScanningTest.java +++ b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BranchScanningTest.java @@ -114,6 +114,30 @@ public void scanTestPullRequests() throws IOException, InterruptedException { assertEquals("branch1", observer.getBranches().get(0)); assertEquals("PR-23", observer.getBranches().get(1)); } + + @Test + public void scanTestDeactivatedPullRequests() throws IOException, InterruptedException { + BitbucketSCMSource source = getBitbucketSCMSourceMock(RepositoryType.GIT, true); + source.setScanPullRequests(false); + SCMHeadObserverImpl observer = new SCMHeadObserverImpl(); + source.fetch(observer, BitbucketClientMockUtils.getTaskListenerMock()); + + // Only branch1 must be observed + assertEquals(1, observer.getBranches().size()); + assertEquals("branch1", observer.getBranches().get(0)); + } + + @Test + public void scanTestDeactivatedBranches() throws IOException, InterruptedException { + BitbucketSCMSource source = getBitbucketSCMSourceMock(RepositoryType.GIT, true); + source.setScanBranches(false); + SCMHeadObserverImpl observer = new SCMHeadObserverImpl(); + source.fetch(observer, BitbucketClientMockUtils.getTaskListenerMock()); + + // Only my-feature-branch PR must be observed + assertEquals(1, observer.getBranches().size()); + assertEquals("PR-23", observer.getBranches().get(0)); + } @Test public void gitSCMTest() {