2

One of my customers is running an instance of Atlassian Bitbucket Server v5.14.0 (not Bitbucket Cloud!) in their intranet. I try to tap into the REST API to get a list of projects and for the one I'm working on, get a list of git repositories:

# first REST API call: returns list of projects on server,
# `?limit=1000` appended to work around / disable pagination:
# https://docs.atlassian.com/bitbucket-server/ ...
#  ... rest/5.14.0/bitbucket-rest.html#idm46783597898304
curl --header "Authorization: Bearer <my access token>" \
     https://<bitbucket hostname>/rest/api/1.0/projects?limit=1000

# second REST API call: returns list of repos in <project ID>
# https://docs.atlassian.com/bitbucket-server/ ...
#  ... rest/5.14.0/bitbucket-rest.html#idm45701776945568
curl --header "Authorization: Bearer <my access token>" \
     https://<bitbucket hostname>/rest/api/1.0/projects/<project key>/repos?limit=1000

In general, this works well. Problem however is that the second call only returns repositories with public visibility and although I am able to see both public and private repos in the web application after logging in, there does not seem to be any way to get the private ones using the REST API.

I also tried

# alternate approach: list repo by name
# https://docs.atlassian.com/bitbucket-server/ ...
#  ... rest/5.14.0/bitbucket-rest.html#idm46783597782384
curl --header "Authorization: Bearer <my access token>" \
     https://<bitbucket hostname>/rest/api/1.0/repos?name=<name of private repo>

but that does not return the repository info either.

I have thoroughly searched the documentation, but so far, this just seems to be a bug in Bitbucket and getting private repositories over the REST API is simply not possible.

Q: Does anyone how to get this to work ?
Q: Is anyone using the Bitbucket Server REST API ? What's your experience / impression ?

ssc
  • 9,528
  • 10
  • 64
  • 94
  • I've used these APIs to successfully return info about private repos, but I used basic AUTH. I wonder if it isn't picking up your auth token and therefore is only showing your public repos... – Rich Duncan Oct 05 '18 at 14:29

2 Answers2

0

It may have something to do with the permissions that your user has. Is it an admin user?

I have used this script to get all repos:

#!/usr/bin/python

import stashy
import os
import sys
import urllib2
import json
import base64

bitbucketBaseUrl = "https://bitbucket.company.com"
bitbucketUserName = "admin"

def validateScriptParameters():
    if len(sys.argv) != 2:
        sys.exit("Usage: {} [Bit Bucket admin password]".format(
            os.path.basename(sys.argv[0])))




validateScriptParameters

bitbucketPassword = sys.argv[1].strip()
bitbucket = stashy.connect(bitbucketBaseUrl, bitbucketUserName, bitbucketPassword)
projectList = bitbucket.projects.list()
total = 0
for project in projectList:
        projectName = project['key']
        repoList = bitbucket.projects[projectName].repos.list()
        for repo in repoList:
            print repo['name']

This script is run as the admin user and you need the stashy lib:

pip install stashy

I find the REST API to be quite good, it can be a bit tricky to figure how the correct request to make, but the documentation is there. It's hard to find though. They release new docs with each release and they tend to be the best:

https://docs.atlassian.com/bitbucket-server/rest/5.15.0/bitbucket-rest.html?utm_source=%2Fstatic%2Frest%2Fbitbucket-server%2Flatest%2Fbitbucket-rest.html&utm_medium=301

There is also a REST API plugin for bitbucket which allows you test requests directly against the server:

REST API Browser

eeijlar
  • 1,232
  • 3
  • 20
  • 53
0

Had the same problem. Basic auth does not return private projects. I had to use OAuth and it now fetches private repositories. I followed these instructions.

https://stackoverflow.com/a/72103398/51759

Jeff Lamb
  • 5,755
  • 4
  • 37
  • 54