Skip to content

Commit 857269a

Browse files
authored
Remove ref-specs cleanup during initialization of BitbucketGitSCMBuilder (#815)
Makes RefSpecsSCMSourceTrait working again. Fixes #814. See also #812. Problem was introduced in #796.
1 parent 613f38e commit 857269a

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ public BitbucketGitSCMBuilder withCredentials(String credentialsId, BitbucketRep
189189
@NonNull
190190
public BitbucketGitSCMBuilder withBitbucketRemote() {
191191
SCMHead head = head();
192-
withoutRefSpecs();
193192
String headName = head.getName();
194193
if (head instanceof PullRequestSCMHead) {
195194
withPullRequestRemote((PullRequestSCMHead) head, headName);

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketGitSCMBuilderTest.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import jenkins.plugins.git.AbstractGitSCMSource;
2626
import jenkins.plugins.git.GitSCMSourceDefaults;
2727
import jenkins.plugins.git.MergeWithGitSCMExtension;
28+
import jenkins.plugins.git.traits.RefSpecsSCMSourceTrait;
2829
import jenkins.scm.api.SCMHead;
2930
import jenkins.scm.api.SCMHeadOrigin;
3031
import jenkins.scm.api.SCMRevision;
@@ -454,6 +455,76 @@ public void given__server_branch_rev_anon__when__build__then__scmBuilt() throws
454455
assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe"));
455456
}
456457

458+
@Test
459+
public void given__server_branch_rev_anon_with_extra_refSpec_when__build__then__scmBuilt() throws Exception {
460+
source.setServerUrl("https://bitbucket.test");
461+
BranchSCMHead head = new BranchSCMHead("test-branch");
462+
AbstractGitSCMSource.SCMRevisionImpl revision =
463+
new AbstractGitSCMSource.SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe");
464+
BitbucketGitSCMBuilder instance = new BitbucketGitSCMBuilder(source,
465+
head, revision, null);
466+
instance.withTrait(new RefSpecsSCMSourceTrait("+refs/heads/*:refs/remotes/@{remote}/*"));
467+
assertThat(instance.credentialsId(), is(nullValue()));
468+
assertThat(instance.head(), is((SCMHead) head));
469+
assertThat(instance.revision(), is((SCMRevision) revision));
470+
assertThat(instance.scmSource(), is(source));
471+
assertThat("expecting dummy value until clone links provided or withBitbucketRemote called",
472+
instance.remote(), is("https://bitbucket.test"));
473+
assertThat(instance.browser(), instanceOf(BitbucketWeb.class));
474+
assertThat(instance.browser().getRepoUrl(), is("https://bitbucket.test/projects/tester/repos/test-repo"));
475+
476+
instance.withCloneLinks(
477+
List.of(
478+
new BitbucketHref("http", "https://bitbucket.test/scm/tester/test-repo.git"),
479+
new BitbucketHref("ssh", "ssh://git@bitbucket.test:7999/tester/test-repo.git")
480+
),
481+
List.of()
482+
);
483+
assertThat(instance.remote(), is("https://bitbucket.test/scm/tester/test-repo.git"));
484+
assertThat(instance.refSpecs(), hasSize(2));
485+
assertThat(instance.refSpecs(), contains(
486+
"+refs/heads/*:refs/remotes/@{remote}/*",
487+
"+refs/heads/test-branch:refs/remotes/@{remote}/test-branch"
488+
));
489+
490+
GitSCM actual = instance.build();
491+
assertThat(actual.getBrowser(), instanceOf(BitbucketWeb.class));
492+
assertThat(actual.getBrowser().getRepoUrl(), is("https://bitbucket.test/projects/tester/repos/test-repo"));
493+
assertThat(actual.getGitTool(), nullValue());
494+
assertThat(actual.getUserRemoteConfigs(), hasSize(1));
495+
UserRemoteConfig config = actual.getUserRemoteConfigs().get(0);
496+
assertThat(config.getName(), is("origin"));
497+
assertThat(config.getRefspec(), is("+refs/heads/*:refs/remotes/origin/* +refs/heads/test-branch:refs/remotes/origin/test-branch"));
498+
assertThat(config.getUrl(), is("https://bitbucket.test/scm/tester/test-repo.git"));
499+
assertThat(config.getCredentialsId(), is(nullValue()));
500+
RemoteConfig origin = actual.getRepositoryByName("origin");
501+
assertThat(origin, notNullValue());
502+
assertThat(origin.getURIs(), hasSize(1));
503+
assertThat(origin.getURIs().get(0).toString(), is("https://bitbucket.test/scm/tester/test-repo.git"));
504+
assertThat(origin.getFetchRefSpecs(), hasSize(2));
505+
assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/*"));
506+
assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/*"));
507+
assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true));
508+
assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(true));
509+
assertThat(origin.getFetchRefSpecs().get(1).getSource(), is("refs/heads/test-branch"));
510+
assertThat(origin.getFetchRefSpecs().get(1).getDestination(), is("refs/remotes/origin/test-branch"));
511+
assertThat(origin.getFetchRefSpecs().get(1).isForceUpdate(), is(true));
512+
assertThat(origin.getFetchRefSpecs().get(1).isWildcard(), is(false));
513+
assertThat(actual.getExtensions(), containsInAnyOrder(
514+
instanceOf(GitSCMSourceDefaults.class),
515+
instanceOf(BuildChooserSetting.class)
516+
));
517+
BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class);
518+
assertThat(chooser.getBuildChooser(), instanceOf(AbstractGitSCMSource.SpecificRevisionBuildChooser.class));
519+
AbstractGitSCMSource.SpecificRevisionBuildChooser revChooser =
520+
(AbstractGitSCMSource.SpecificRevisionBuildChooser) chooser.getBuildChooser();
521+
Collection<Revision> revisions = revChooser
522+
.getCandidateRevisions(false, "test-branch", Mockito.mock(GitClient.class), new LogTaskListener(
523+
Logger.getAnonymousLogger(), Level.FINEST), null, null);
524+
assertThat(revisions, hasSize(1));
525+
assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe"));
526+
}
527+
457528
@Test
458529
public void given__server_withMirror_branch_rev_anon__when__build__then__scmBuilt() throws Exception {
459530
source.setServerUrl("https://bitbucket.test");

0 commit comments

Comments
 (0)