How can I retrieve the clone url for ssh protocol?

ABA July 11, 2016

How can I retrieve ALL the ssh urls for the Bitbucket repositories that I have access to, using either a command-line Git command ( CLI )  or  using REST call ? I have unsuccessfully sad tried to search for the solution for the last week.

Currently, I am in process of moving the source code to bitbucket.  Our Atlassian admins are setting up the repositories for our group and we will be using command-line Git commands to add the source code there. 

 

For one of the command line commands - git remote add origin -

  • I have to log-on to the local Bitbucket web interface,
  • click on clone,
  • select SSH 
  • copy the url and then
  • paste it in my command. 

 

I am hoping to avoid having to log-on and access the web interface.

Is it possible?  How would I do it? I would prefer command-line, but REST call would also be ok

Thank you

 

2 answers

1 accepted

1 vote
Answer accepted
ThiagoBomfim
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 11, 2016

Hello there!

You could achieve that by using one of our Core REST API's:

If you expand the 200 sample response we have on this section of the documentation, you will see that the SSH URL will be on the result:

{
    "size": 1,
    "limit": 25,
    "isLastPage": true,
    "values": [
        {
            "slug": "my-repo",
            "id": 1,
            "name": "My repo",
            "scmId": "git",
            "state": "AVAILABLE",
            "statusMessage": "Available",
            "forkable": true,
            "project": {
                "key": "PRJ",
                "id": 1,
                "name": "My Cool Project",
                "description": "The description for my cool project.",
                "public": true,
                "type": "NORMAL",
                "links": {
                    "self": [
                        {
                            "href": "http://link/to/project"
                        }
                    ]
                }
            },
            "public": true,
            "links": {
                "clone": [
                    {
                        "href": "ssh://git@<baseURL>/PRJ/my-repo.git",
                        "name": "ssh"
                    },
                    {
                        "href": "https://<baseURL>/scm/PRJ/my-repo.git",
                        "name": "http"
                    }
                ],
                "self": [
                    {
                        "href": "http://link/to/repository"
                    }
                ]
            }
        }
    ],
    "start": 0
}

 

From the REST API above, though, you will need to pass a {projectKey}. You could get the {projectKey} beforehand by using this REST first. Right after that is the sample for a HTTP 200 response:

  • /rest/api/1.0/projects

    {
        "size": 1,
        "limit": 25,
        "isLastPage": true,
        "values": [
            {
                "key": "PRJ",
                "id": 1,
                "name": "My Cool Project",
                "description": "The description for my cool project.",
                "public": true,
                "type": "NORMAL",
                "links": {
                    "self": [
                        {
                            "href": "http://link/to/project"
                        }
                    ]
                }
            }
        ],
        "start": 0
    }

Programmatically you could retrieve the results of any of the REST endpoints above by using a simple commands like the ones below. Notice that mine is a "bad" example (just because I didn't have SSH keys uploaded to my profile, you can't see the SSH URL):

  • To retrieve all project ids from my user/instance: 
$ curl -u charlie:charlie http://<BBS_URL>/rest/api/1.0/projects
 
{  
   "size":1,
   "limit":25,
   "isLastPage":true,
   "values":[  
      {  
         "key":"EH",
         "id":1,
         "name":"Expert Hour",
         "public":false,
         "type":"NORMAL",
         "links":{  
            "self":[  
               {  
                  "href":"http://192.168.123.102/bitbucket/projects/EH"
               }
            ]
         }
      }
   ],
   "start":0
}
  • To retrieve the repos that returned from my example project above

 

$ curl -u charlie:charlie http://192.168.123.102/bitbucket/rest/api/1.0/projects/EH/repos
 
{  
   "size":1,
   "limit":25,
   "isLastPage":true,
   "values":[  
      {  
         "slug":"mirror-mirror",
         "id":1,
         "name":"Mirror Mirror",
         "scmId":"git",
         "state":"AVAILABLE",
         "statusMessage":"Available",
         "forkable":true,
         "project":{  
            "key":"EH",
            "id":1,
            "name":"Expert Hour",
            "public":false,
            "type":"NORMAL",
            "links":{  
               "self":[  
                  {  
                     "href":"http://192.168.123.102/bitbucket/projects/EH"
                  }
               ]
            }
         },
         "public":true,
         "links":{  
            "clone":[  
               {  
                  "href":"ssh://git@192.168.123.102:7999/eh/mirror-mirror.git",
                  "name":"ssh"
               },
               {  
                  "href":"http://charlie@192.168.123.102/bitbucket/scm/eh/mirror-mirror.git",
                  "name":"http"
               }
            ],
            "self":[  
               {  
                  "href":"http://192.168.123.102/bitbucket/projects/EH/repos/mirror-mirror/browse"
               }
            ]
         }
      }
   ],
   "start":0
}

Let us know if that helps.

Best regards,
Thiago Bomfim
DevTools

0 votes
SidduAngadi July 11, 2016

1st step you need add Access key ( sshkey ) of the user from which you want clone.  Add the ssh key under project-->repository-->settings–>Access Key.

Then execute below command:

git clone ${url}

${url}  -  SSH protocol ex:-  ssh://git@localhost:7999/cit/rlmcqweb.

ABA July 11, 2016

Hi,  thanks.  But I have done the adding the sshkey part.

 

I don't want to clone form the command line.  I want to get the ssh url that is for clone and use THAT url with

git remote add origin

 

I just want to avoid the steps above of having to access the web interface that I mentioned to get the url.  

Josh Wheeler
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2016

@Thiago Bomfim's answer is what you want.

ThiagoBomfim
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 12, 2016

Hi Team,

On the answer above I provided you with the REST endpoint from which you can obtain exactly this information by simply calling curl commands from your client (i.e. no need to use a browser and click through the pages you mentioned).

The response on the JSON above contains the following part which is exactly what you're looking for:

"clone":[  
               {  
                  "href":"ssh://git@192.168.123.102:7999/eh/mirror-mirror.git",
                  "name":"ssh"
               },
               {  
                  "href":"http://charlie@192.168.123.102/bitbucket/scm/eh/mirror-mirror.git",
                  "name":"http"
               }
            ],

I hope that helps.

Thiago

ABA July 12, 2016

Thanks,  Thiago.

I am unable to test it for next two weeks, but this is exactly what I am looking for.

I will post a comment, if I run into any issues when I test.

Thanks again.

 

ThiagoBomfim
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 12, 2016

Not a problem.

All the best with your test.

Cheers!

ABA July 12, 2016

Hi, this may seem to be a very basic question, but we just started using Atlassian suite and everyone including the admins are learning as we go. Do the admins need to set-up something to allow us to use the REST APIs?  

Our Bitbucket version is 4.6.2.

When I tried both https://git-dev.company.com/bitbucket/rest/api/1.0/     OR

https://git-dev.company.com/bitbucket/rest/api/2.0/ 

I get a 404

The repositories are definitely defined at https://git-dev.company.com/repos?visibility=public as I see a list of test repositories at above url

Thanks

ThiagoBomfim
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 12, 2016

Hi team,

If you type on your browser:

https://git-dev.company.com/bitbucket/rest/api/1.0/projects

That should enable you to get results.

The best way though is by using curl as I indicated above.

ABA July 12, 2016

Thanks for a quick response.

I will be using curl for getting the information.  I just tried with curl but didn't get the result, so I thought I will just verify the url and that is why I typed it in browser to verify my url is not invalid.  I tried with https://git-dev.company.com/bitbucket/rest/api/1.0/projects, but still get a 404.  That is why I am wondering, if the admins need to do anything before I can use the REST calls for any of the Atlassian suite products - I am starting with BitBucket, but once I get a handle on it, I will try to automate my work using REST calls for the others - Bamboo, Confluence and JIRA

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events