Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to extract the list of all repos under all projects in Bitbucket. Is there a REST API for the same? I couldn't find one.

I have both on-premise and cloud Bitbucket.

like image 626
Jeel Avatar asked Mar 18 '16 16:03

Jeel


People also ask

How do I list all repositories in Bitbucket?

To get all repositories, including private, you need to authenticate with api. bitbucket . org using any of the following: your regular credentials, username and app password, or OAuth token. Let me know if you have any questions.

How do I view repositories in Bitbucket?

By adding the "site: https://bitbucket.org" string to your search, you'll be able to find public repositories.

How do I download everything from Bitbucket?

Go to either the Source view, Commit view, or Branches list of a repository. Using the branch selector to choose a branch. Click the actions dropdown next to the branch selector, then select Download.


2 Answers

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
like image 62
Jason LeMonier Avatar answered Sep 29 '22 16:09

Jason LeMonier


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.

like image 43
Harmelodic Avatar answered Sep 29 '22 17:09

Harmelodic