Search code examples
gitbashrepositorybitbucketbitbucket-api

How to get a list of git repositories from bit bucket to be used as variables in bash script?


I am trying to write a bash script that takes all of the repositories I have on bitbucket and backs them up locally.

I am stuck trying to get a list of repos from bitbucket. I have read a little bit of their api that does this, and I even tested it. But it's just so massive that I just don't know where to start.

Any help would be greatly appreciated


Solution

  • I think you should check "GET a list of repositories for an account" API. It is pretty straightforward to use it with curl:

    curl -u '<user>:<password>' https://api.bitbucket.org/2.0/repositories/<owner>
    

    The owner may be the same as user. Note, that if you are going to query only public repositories, you don't need any authorization (-u option). The response is paginated, so you may need to make multiple calls.

    This API returns a JSON object describing all visible (for authenticated user) repos of owner. One of the ways to parse it in Bash is to use jsawk. But you are free to use any tool you are comfortable with like NodeJS or Python. You'll just need to read from stdin and parse it as JSON. Simple example, just to start with. Fetches all public repos of jespern:

    curl https://api.bitbucket.org/2.0/repositories/jespern | jsawk 'return this.values.map(value => value.full_name)'
    
    ["jespern/cx","jespern/cx-patches","jespern/ldap2vcard","jespern/pyetsy","jespern/puck","jespern/hgswitch","jespern/smart-oxe","jespern/pygments-anchorlinenos","jespern/emptyrepo","jespern/help"]
    

    At this point you can use sed or awk to extract them from the array.