How to extract the list of all repositories in Stash or Bitbucket?

I ended up having to do this myself with an on-prem install of Bitbucket which didn't seem to have the REST APIs discussed above accessible, so I came up with a short script to scrape it out of the web page. This workaround has the advantage that there's nothing you need to install, and you don't need to worry about dependencies, certs or logins other than just logging into your Bitbucket server. You can also set this up as a bookmark if you urlencode the script and prefix it with javascript:.

To use this:

  1. Open your bitbucket server project page, where you should see a list of repos.
  2. Open your browser's devtools console. This is usually F12 or ctrl-shift-i.
  3. Paste the following into the command prompt there.
JSON.stringify(Array.from(document.querySelectorAll('[data-repository-id]')).map(aTag => {
  const href = aTag.getAttribute('href');
  let projName = href.match(/\/projects\/(.+)\/repos/)[1].toLowerCase();
  let repoName = href.match(/\/repos\/(.+)\/browse/)[1];
  repoName = repoName.replace(' ', '-');
  const templ = `https://${location.host}/scm/${projName}/${repoName}.git`;
  return {
    href,
    name: aTag.innerText,
    clone: templ
  }
}));

The result is a JSON string containing an array with the repo's URL, name, and clone URL.

[{
  "href": "/projects/FOO/repos/some-repo-here/browse",
  "name": "some-repo-here",
  "clone": "https://mybitbucket.company.com/scm/foo/some-repo-here.git"
}]

  1. Once logged in: on the top right, click on your profile pic and then 'View profile'

enter image description here

  1. Take note of your user (in the example below '[email protected]', but keep in mind it's case sensitive)

enter image description here

  1. Click on profile pic > Manage account > Personal access token > Create a token (choosing 'Read' access type is enough for this functionality)

For all repos in all projects:

  1. Open a CLI and use the command below (remember to fill in your server domain!):
curl -u "[email protected]" -X GET https://<my_server_domain>/rest/api/1.0/projects/?limit=1000
  1. It will ask you for your personal access token, you comply and you get a JSON file with all repos requested

For all repos in a given project:

  1. Pick the project you want to get repos from. In my case, the project URL is: <your_server_domain>/projects/TECH/ and therefore my {projectKey} is 'TECH', which you'll need for the command below.

  2. Open a CLI and use this command (remember to fill in your server domain and projectKey!):

curl -u "[email protected]" -X GET https://<my_server_domain>/rest/api/1.0/projects/{projectKey}/repos?limit=50

Final touches

  1. (optional) If you want just the titles of the repos requested and you have jq installed (for Windows, downloading the exe and adding it to PATH should be enough, but you need to restart your CLI for that new addition to be detected), you can use the command below:
curl -u $BBUSER -X GET <my_server_domain>/rest/api/1.0/projects/TECH/repos?limit=50 | jq '.values|.[]|.name'

(tested with Data Center/Atlassian Bitbucket v7.9.0 and powershell CLI)


For Bitbucket Cloud

You can use their REST API to access and perform queries on your server.

Specifically, you can use this documentation page, provided by Atlassian, to learn how to list you're repositories.


For Bitbucket Server

Edit: As of receiving this tweet from Dan Bennett, I've learnt there is an API/plugin system for Bitbucket Server that could possibly cater for your needs. For docs: See here.

Edit2: Found this reference to listing personal repositories that may serve as a solution.

AFAIK there isn't a solution for you unless you built a little API for yourself that interacted with your Bitbucket Server instance.

Atlassian Documentation does indicate that to list all currently configured repositories you can do git remote -v. However I'm dubious of this as this isn't normally how git remote -v is used; I think it's more likely that Atlassian's documentation is being unclear rather than Atlassian building in this functionality to Bitbucket Server.


Clone ALL Projects & Repositories for a given stash url

    #!/usr/bin/python
    # 
    # @author Jason LeMonier
    #
    # Clone ALL Projects & Repositories for a given stash url
    #
    # Loop through all projects: [P1, P2, ...]
    #    P1 > for each project make a directory with the key "P1"
    #    Then clone every repository inside of directory P1
    #    Backup a directory, create P2, ... 
    # 
    # Added ACTION_FLAG bit so the same logic can run fetch --all on every repository and/or clone.

    import sys
    import os
    import stashy

    ACTION_FLAG = 1     # Bit: +1=Clone, +2=fetch --all 

    url  = os.environ["STASH_URL"]  # "https://mystash.com/stash"
    user = os.environ["STASH_USER"] # joedoe"
    pwd  = os.environ["STASH_PWD"]  # Yay123

    stash = stashy.connect(url, user, pwd)

    def mkdir(xdir):
        if not os.path.exists(xdir):
            os.makedirs(xdir)

    def run_cmd(cmd):
        print ("Directory cwd: %s "%(os.getcwd() ))
        print ("Running Command: \n    %s " %(cmd))
        os.system(cmd)

    start_dir = os.getcwd()

    for project in stash.projects:
        pk = project_key = project["key"]
        mkdir(pk) 
        os.chdir(pk)

        for repo in stash.projects[project_key].repos.list():
            for url in repo["links"]["clone"]:
                href = url["href"]
                repo_dir = href.split("/")[-1].split(".")[0]

                if (url["name"] == "http"):
                    print ("        url.href: %s"% href)  # https://[email protected]/stash/scm/app/ae.git
                    print ("Directory cwd: %s Project: %s"%(os.getcwd(), pk))

                    if ACTION_FLAG & 1 > 0:
                        if not os.path.exists(repo_dir):
                            run_cmd("git clone %s" % url["href"])
                        else:
                            print ("Directory: %s/%s exists already.  Skipping clone. "%(os.getcwd(), repo_dir))

                    if ACTION_FLAG & 2 > 0:
                        # chdir into directory "ae" based on url of this repo, fetch, chdir back
                        cur_dir = os.getcwd()
                        os.chdir(repo_dir)
                        run_cmd("git fetch --all ")
                        os.chdir(cur_dir)

                    break

        os.chdir(start_dir) # avoiding ".." in case of incorrect git directories