From eba0aead51982e036bd3cf829d8cbc47aeea68e2 Mon Sep 17 00:00:00 2001 From: Andrew Wright and John Youkhana Date: Thu, 18 May 2017 11:09:41 +1000 Subject: [PATCH] Allow exclusions to be specified at the start of the branch name --- .../plugins/bitbucket/BitbucketSCMSource.java | 4 +-- .../bitbucket/BitbucketSCMSourceTest.java | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceTest.java 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 68573021e..5fab99236 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java @@ -635,7 +635,7 @@ public String getRemoteName() { * @param branchName * @return true if branchName is excluded or is not included */ - private boolean isExcluded(String branchName) { + boolean isExcluded(String branchName) { return !Pattern.matches(getPattern(getIncludes()), branchName) || Pattern.matches(getPattern(getExcludes()), branchName); } @@ -652,7 +652,7 @@ private String getPattern(String branches) { for (String wildcard : branches.split(" ")) { StringBuilder quotedBranch = new StringBuilder(); for (String branch : wildcard.split("\\*")) { - if (wildcard.startsWith("*") || quotedBranches.length() > 0) { + if (wildcard.startsWith("*") || quotedBranch.length() > 0) { quotedBranch.append(".*"); } quotedBranch.append(Pattern.quote(branch)); diff --git a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceTest.java b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceTest.java new file mode 100644 index 000000000..9c62f4382 --- /dev/null +++ b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSourceTest.java @@ -0,0 +1,25 @@ +package com.cloudbees.jenkins.plugins.bitbucket; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Before; +import org.junit.Test; + +public class BitbucketSCMSourceTest { + + public BitbucketSCMSource bitbucketSCMSource; + + @Before + public void setUp() throws Exception { + bitbucketSCMSource = new BitbucketSCMSource("1", "owner", "repo"); + } + + @Test + public void shouldAllowExclusionsAtStartOfBranchName() throws Exception { + String includes = "master branch-*"; + bitbucketSCMSource.setIncludes(includes); + assertFalse("Should not exclude branch-* at start of name", bitbucketSCMSource.isExcluded("branch-1.0.0")); + assertTrue("Should exclude branch in the middle of the branch name", bitbucketSCMSource.isExcluded("FOO_branch-1.0.0")); + } +}