JGIT LS Remote Repositor using SSH [message #1854208] |
Sat, 06 August 2022 18:39  |
Alfredo Espana Messages: 1 Registered: August 2022 |
Junior Member |
|
|
Hello.
I am working in an open source project that uses JGIT to get the tags from repositories in github, bitbucket, gitlab and azure devops using a personal access token and without authentication and it works really great.
Example Code:
public List<String> getVersions(RequestScope requestScope) {
List<String> versionList = new ArrayList<>();
try {
CredentialsProvider credentialsProvider = null;
if (vcs != null) {
log.info("vcs using {}", vcs.getVcsType());
switch (vcs.getVcsType()) {
case GITHUB:
credentialsProvider = new UsernamePasswordCredentialsProvider(vcs.getAccessToken(), "");
break;
case BITBUCKET:
credentialsProvider = new UsernamePasswordCredentialsProvider("x-token-auth", vcs.getAccessToken());
break;
case GITLAB:
credentialsProvider = new UsernamePasswordCredentialsProvider("oauth2", vcs.getAccessToken());
break;
case AZURE_DEVOPS:
credentialsProvider = new UsernamePasswordCredentialsProvider("dummy", vcs.getAccessToken());
break;
default:
credentialsProvider = null;
break;
}
}
Map<String, Ref> tags = Git.lsRemoteRepository()
.setTags(true)
.setRemote(source)
.setCredentialsProvider(credentialsProvider)
.callAsMap();
tags.forEach((key, value) -> {
versionList.add(key.replace("refs/tags/", ""));
});
} catch (GitAPIException e) {
log.error(e.getMessage());
}
return versionList;
}
I would like to add support to use ssh key for my application using JGIT and Apache Mina.
I checked https://github.com/apache/mina-sshd/blob/master/docs/git.md but I am getting "Unable to connect" with the following code:
SshClient client = SshClient.setUpDefaultClient();
try {
client.start();
GitSshdSessionFactory sshdFactory = new GitSshdSessionFactory(client); // re-use the same client for all SSH sessions
org.eclipse.jgit.transport.SshSessionFactory.setInstance(sshdFactory); // replace the JSCH-based factory
Git.lsRemoteRepository().setRemote("git@github.com:alfespa17/terraform-module-private.git").call();
} finally {
client.stop();
}
I guess I am missing something but I am not really sure how to make the code work because I can't find examples and I am new using apache mina
Can someone guide me or maybe share some small example?
I would appreciate any help, thanks
|
|
|
|
Powered by
FUDForum. Page generated in 0.02107 seconds