diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java index 41c1ad985..4fd866ba0 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilder.java @@ -62,11 +62,11 @@ public class GitHubSCMBuilder extends GitSCMBuilder { /** * Singleton instance of {@link HttpsRepositoryUriResolver}. */ - private static final HttpsRepositoryUriResolver HTTPS = new HttpsRepositoryUriResolver(); + static final HttpsRepositoryUriResolver HTTPS = new HttpsRepositoryUriResolver(); /** * Singleton instance of {@link SshRepositoryUriResolver}. */ - private static final SshRepositoryUriResolver SSH = new SshRepositoryUriResolver(); + static final SshRepositoryUriResolver SSH = new SshRepositoryUriResolver(); /** * The GitHub API suffix for GitHub Server. */ @@ -96,6 +96,11 @@ public class GitHubSCMBuilder extends GitSCMBuilder { */ @CheckForNull private final URL repositoryUrl; + /** + * The repository name. + */ + @NonNull + private RepositoryUriResolver uriResolver = GitHubSCMBuilder.HTTPS; /** * Constructor. @@ -131,6 +136,7 @@ public GitHubSCMBuilder(@NonNull GitHubSCMSource source, if (repoUrl != null) { withBrowser(new GithubWeb(repoUrl)); } + withCredentials(credentialsId(), null); } /** @@ -181,8 +187,29 @@ public final String repositoryUrl(String owner, String repo) { */ @NonNull public final RepositoryUriResolver uriResolver() { - String credentialsId = credentialsId(); - return uriResolver(context, apiUri, credentialsId); + return uriResolver; + } + + /** + * Configures the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to the + * {@link #remote()} + * + * @param credentialsId the {@link IdCredentials#getId()} of the {@link Credentials} to use when connecting to + * the {@link #remote()} or {@code null} to let the git client choose between providing its own + * credentials or connecting anonymously. + * @param uriResolver the {@link RepositoryUriResolver} of the {@link Credentials} to use or {@code null} + * to detect the the protocol based on the credentialsId. Defaults to HTTP if credentials are + * {@code null}. Enables support for blank SSH credentials. + * @return {@code this} for method chaining. + */ + @NonNull + public GitHubSCMBuilder withCredentials(String credentialsId, RepositoryUriResolver uriResolver) { + if (uriResolver == null) { + uriResolver = uriResolver(context, apiUri, credentialsId); + } + + this.uriResolver = uriResolver; + return withCredentials(credentialsId); } /** @@ -193,6 +220,7 @@ public final RepositoryUriResolver uriResolver() { * @param credentialsId the credentials. * @return a {@link RepositoryUriResolver} */ + @NonNull public static RepositoryUriResolver uriResolver(@CheckForNull Item context, @NonNull String apiUri, @CheckForNull String credentialsId) { if (credentialsId == null) { diff --git a/src/main/java/org/jenkinsci/plugins/github_branch_source/SSHCheckoutTrait.java b/src/main/java/org/jenkinsci/plugins/github_branch_source/SSHCheckoutTrait.java index 146fb56a8..9f2683cfd 100644 --- a/src/main/java/org/jenkinsci/plugins/github_branch_source/SSHCheckoutTrait.java +++ b/src/main/java/org/jenkinsci/plugins/github_branch_source/SSHCheckoutTrait.java @@ -102,7 +102,7 @@ public final String getCredentialsId() { */ @Override protected void decorateBuilder(SCMBuilder builder) { - ((GitSCMBuilder)builder).withCredentials(credentialsId); + ((GitHubSCMBuilder)builder).withCredentials(credentialsId, GitHubSCMBuilder.SSH); } /** diff --git a/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilderTest.java b/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilderTest.java index 3674a47a8..57a8af305 100644 --- a/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilderTest.java +++ b/src/test/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMBuilderTest.java @@ -64,11 +64,11 @@ public void setUp() throws IOException { source = new GitHubSCMSource( "tester", "test-repo"); owner.setSourcesList(Collections.singletonList(new BranchSource(source))); source.setOwner(owner); + Credentials userPasswordCredential = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "user-pass", null, "git-user", "git-secret"); + Credentials sshPrivateKeyCredential = new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "user-key", "git", + new BasicSSHUserPrivateKey.UsersPrivateKeySource(), null, null); SystemCredentialsProvider.getInstance().setDomainCredentialsMap(Collections.singletonMap(Domain.global(), - Arrays.asList( - new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, "user-pass", null, "git-user", - "git-secret"), new BasicSSHUserPrivateKey(CredentialsScope.GLOBAL, "user-key", "git", - new BasicSSHUserPrivateKey.UsersPrivateKeySource(), null, null)))); + Arrays.asList(userPasswordCredential, sshPrivateKeyCredential))); } @After @@ -237,6 +237,361 @@ public void given__cloud_branch_rev_userkey__when__build__then__scmBuilt() throw assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); } + @Test + public void given__cloud_branch_rev_anon_sshtrait_anon__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId(null); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is(nullValue())); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("https://github.com/tester/test-repo.git")); + + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait(null); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is(nullValue())); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is(nullValue())); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + + @Test + public void given__cloud_branch_rev_userpass_sshtrait_anon__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId("user-pass"); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is("user-pass")); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("https://github.com/tester/test-repo.git")); + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait(null); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is(nullValue())); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is(nullValue())); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + + @Test + public void given__cloud_branch_rev_userkey_sshtrait_anon__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId("user-key"); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is("user-key")); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait(null); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is(nullValue())); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is(nullValue())); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + + @Test + public void given__cloud_branch_rev_anon_sshtrait_userkey__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId(null); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is(nullValue())); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("https://github.com/tester/test-repo.git")); + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait("user-key"); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is("user-key")); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is("user-key")); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + + @Test + public void given__cloud_branch_rev_userpass_sshtrait_userkey__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId("user-pass"); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is("user-pass")); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("https://github.com/tester/test-repo.git")); + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait("user-key"); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is("user-key")); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is("user-key")); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + + @Test + public void given__cloud_branch_rev_userkey_sshtrait_userkey__when__build__then__scmBuilt() throws Exception { + BranchSCMHead head = new BranchSCMHead("test-branch"); + SCMRevisionImpl revision = + new SCMRevisionImpl(head, "cafebabedeadbeefcafebabedeadbeefcafebabe"); + source.setCredentialsId("user-key"); + GitHubSCMBuilder instance = new GitHubSCMBuilder(source, head, revision); + assertThat(instance.credentialsId(), is("user-key")); + assertThat(instance.head(), is((SCMHead) head)); + assertThat(instance.revision(), is((SCMRevision) revision)); + assertThat(instance.refSpecs(), contains("+refs/heads/test-branch:refs/remotes/@{remote}/test-branch")); + assertThat("expecting guess value until withGitHubRemote called", + instance.remote(), is("https://github.com/tester/test-repo.git")); + assertThat(instance.browser(), instanceOf(GithubWeb.class)); + assertThat(instance.browser().getRepoUrl(), is("https://github.com/tester/test-repo")); + + instance.withGitHubRemote(); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + SSHCheckoutTrait sshTrait = new SSHCheckoutTrait("user-key"); + sshTrait.decorateBuilder(instance); + + GitSCM actual = instance.build(); + assertThat(instance.credentialsId(), is("user-key")); + assertThat(instance.remote(), is("git@github.com:tester/test-repo.git")); + + assertThat(actual.getBrowser(), instanceOf(GithubWeb.class)); + assertThat(actual.getBrowser().getRepoUrl(), is("https://github.com/tester/test-repo")); + assertThat(actual.getGitTool(), nullValue()); + assertThat(actual.getUserRemoteConfigs(), hasSize(1)); + UserRemoteConfig config = actual.getUserRemoteConfigs().get(0); + assertThat(config.getName(), is("origin")); + assertThat(config.getRefspec(), is("+refs/heads/test-branch:refs/remotes/origin/test-branch")); + assertThat(config.getUrl(), is("git@github.com:tester/test-repo.git")); + assertThat(config.getCredentialsId(), is("user-key")); + RemoteConfig origin = actual.getRepositoryByName("origin"); + assertThat(origin, notNullValue()); + assertThat(origin.getURIs(), hasSize(1)); + assertThat(origin.getURIs().get(0).toString(), is("git@github.com:tester/test-repo.git")); + assertThat(origin.getFetchRefSpecs(), hasSize(1)); + assertThat(origin.getFetchRefSpecs().get(0).getSource(), is("refs/heads/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).getDestination(), is("refs/remotes/origin/test-branch")); + assertThat(origin.getFetchRefSpecs().get(0).isForceUpdate(), is(true)); + assertThat(origin.getFetchRefSpecs().get(0).isWildcard(), is(false)); + assertThat(actual.getExtensions(), containsInAnyOrder( + instanceOf(BuildChooserSetting.class), + instanceOf(GitSCMSourceDefaults.class)) + ); + BuildChooserSetting chooser = getExtension(actual, BuildChooserSetting.class); + assertThat(chooser.getBuildChooser(), instanceOf(SpecificRevisionBuildChooser.class)); + SpecificRevisionBuildChooser revChooser = + (SpecificRevisionBuildChooser) chooser.getBuildChooser(); + Collection revisions = revChooser + .getCandidateRevisions(false, "test-branch", mock(GitClient.class), new LogTaskListener( + getAnonymousLogger(), FINEST), null, null); + assertThat(revisions, hasSize(1)); + assertThat(revisions.iterator().next().getSha1String(), is("cafebabedeadbeefcafebabedeadbeefcafebabe")); + } + @Test public void given__cloud_branch_norev_anon__when__build__then__scmBuilt() throws Exception { BranchSCMHead head = new BranchSCMHead("test-branch");