Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EGit / JGit » JGIT LS Remote Repositor using SSH(JGIT with SSH Keys)
JGIT LS Remote Repositor using SSH [message #1854208] Sat, 06 August 2022 18:39 Go to next message
Alfredo Espana is currently offline Alfredo EspanaFriend
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
Re: JGIT LS Remote Repositor using SSH [message #1854209 is a reply to message #1854208] Sat, 06 August 2022 20:19 Go to previous message
Thomas Wolf is currently offline Thomas WolfFriend
Messages: 493
Registered: August 2016
Senior Member
Don't know about the GitSshdSessionFactory from Apache MINA SSHD. Probably you have to configure the key to use somewhere. Also note that it is built against JGit 5.13. The current version of JGit is 6.2.

JGit has its own SSH support based on Apache MINA SSHD in bundle org.eclipse.jgit.ssh.apache. Example uses are in the test bundle available in the JGit repository, or a very simple setup is also in org.eclipse.jgit.pgm.TextBuiltin.execute().

Bundle org.eclipse.jgit.ssh.apache.agent adds support for SSH agents.
Previous Topic:Cherry pick a range of commit
Next Topic:Suddenly getting "No more authentication methods available" with ssh auth to git repo
Goto Forum:
  


Current Time: Sat Sep 24 19:45:35 GMT 2022

Powered by FUDForum. Page generated in 0.01952 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top