How to pass authentication context to clone a git repo using RepositoryCloneLinksRequest.Builder & GitCloneBuilder

bitbucket-server

#1

Hi,

I am stuck with a seemingly simple task, “How to clone a git repo using RepositoryCloneLinksRequest.Builder / GitCloneBuilder” through my plugin.

I am able to get the link of the URL that I want to clone as below
RepositoryCloneLinksRequest repoclonelinkreq = new RepositoryCloneLinksRequest.Builder().repository(repo).protocol("http").user(null).build();

How do I proceed next to clone the repo to some /tmp directory on my sever ?

I also tried using the below

gitCommandBuilderFactory.builder(repo).workingDirectory(tmpDir).clone().normal().branch(targetBranch).checkout(true).build(); unsuccessfully.

Are there some useful links of a sample/example code that I can see to figure out how to achieve this?


#2

Ok so, I think below is the right way of doing it.

gitCommandBuilderFactory.builder().clone().normal().directory(dir).branch(targetBranch).origin(repositoryURL).checkout(true).build().call();

I wonder how to pass the authentication context ?


#3

This is what I have got so far, I am able to trigger a clone for a repo/branch. But I am not able to pass the authentication context i.e the password.

Below results in a prompt…
Password for ‘http://admin@localhost:7990’: << Prompts me for password on the console and the command times out giving me an exception.

RepositoryCloneLinksRequest rclr =  new RepositoryCloneLinksRequest.Builder().repository(getRepository(req)).protocol("http").
user(authenticationContext.getCurrentUser()).build();

String repositoryURL = repositoryService.getCloneLinks(rclr).iterator().next().getHref();

gitCommandBuilderFactory.builder().clone().normal().directory("/XYZ").branch(targetBranch).origin(repositoryURL).checkout(true).build().call();

Result:

Then I came across the below link, I wonder if it is possible to pass the authentication context of the logged in user for cloning a repository…
https://confluence.atlassian.com/bitbucketserver/personal-access-tokens-939515499.html


#4

@sylvie any hint that you can give ?


#5

Ok, so I think the HTTP/HTTPs would always ask for git password for cloning. Using SSH works fine with the authentication context provided and does not prompt for password.

RepositoryCloneLinksRequest rclr =  new RepositoryCloneLinksRequest.Builder().repository(getRepository(req)).protocol("ssh").
user(authenticationContext.getCurrentUser()).build();