Use credentials provider as the URIish password auth is broken Because of the merged change in JGit [1] that introduces the ability to use the ~/.netrc credentials, all the JGit commands on private repositories are broken [2]. In order to get around this problem we need to avoid using the URIish credentials and explicitly provide a not-null CredentialsProvider that uses the underlying GitHub OAuth credentials for the HTTP BasicAuth handshake. [1] https://git.eclipse.org/r/#/c/22194/ [2] https://groups.google.com/forum/#!topic/repo-discuss/t_yYD2IGYS0 Change-Id: I6723990da79ebeec96e80389d81d6f3233668694
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java index 464f252..b1302f1 100644 --- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java +++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitCloneStep.java
@@ -79,6 +79,7 @@ public void doImport(ProgressMonitor progress) throws GitCloneFailedException, GitDestinationAlreadyExistsException, GitDestinationNotWritableException { CloneCommand clone = new CloneCommand(); + clone.setCredentialsProvider(getRepository().getCredentialsProvider()); String sourceUri = getSourceUri(); clone.setURI(sourceUri); clone.setBare(true);
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitHubRepository.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitHubRepository.java index a677958..9544984 100644 --- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitHubRepository.java +++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/GitHubRepository.java
@@ -17,6 +17,10 @@ import lombok.experimental.Delegate; +import org.eclipse.jgit.errors.UnsupportedCredentialItem; +import org.eclipse.jgit.transport.CredentialItem; +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.URIish; import org.kohsuke.github.GHRepository; import com.google.inject.Inject; @@ -41,8 +45,7 @@ private GHRepository ghRepository; public String getCloneUrl() { - return cloneUrl.replace("://", "://" + ghLogin.getMyself().getLogin() + ":" - + ghLogin.getToken().accessToken + "@"); + return cloneUrl.replace("://", "://" + ghLogin.getMyself().getLogin() + "@"); } public String getOrganisation() { @@ -65,4 +68,56 @@ this.ghRepository = ghLogin.getHub().getRepository(organisation + "/" + repository); } + + public CredentialsProvider getCredentialsProvider() { + return new CredentialsProvider() { + + @Override + public boolean supports(CredentialItem... items) { + for (CredentialItem i : items) { + if (i instanceof CredentialItem.Username) { + continue; + } else if (i instanceof CredentialItem.Password) { + continue; + } else { + return false; + } + } + return true; + } + + @Override + public boolean isInteractive() { + return false; + } + + @Override + public boolean get(URIish uri, CredentialItem... items) + throws UnsupportedCredentialItem { + String username = uri.getUser(); + if (username == null) { + username = ghLogin.getMyself().getLogin(); + } + if (username == null) { + return false; + } + + String password = ghLogin.getToken().accessToken; + if (password == null) { + return false; + } + + for (CredentialItem i : items) { + if (i instanceof CredentialItem.Username) { + ((CredentialItem.Username) i).setValue(username); + } else if (i instanceof CredentialItem.Password) { + ((CredentialItem.Password) i).setValue(password.toCharArray()); + } else { + throw new UnsupportedCredentialItem(uri, i.getPromptText()); + } + } + return true; + } + }; + } }
diff --git a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java index 3f199be..89f3454 100644 --- a/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java +++ b/github-plugin/src/main/java/com/googlesource/gerrit/plugins/github/git/PullRequestImportJob.java
@@ -246,6 +246,7 @@ fetch.setRefSpecs(new RefSpec("+refs/pull/" + pr.getNumber() + "/head:refs/remotes/origin/pr/" + pr.getNumber())); fetch.setProgressMonitor(this); + fetch.setCredentialsProvider(ghRepository.getCredentialsProvider()); fetch.call(); }