How do I filter repos rest api for a specific repo id

I'm attempting to write a script to audit existing repositories for specific requirements and violations.  Easy enough to doe with a bash script, except that the repositories have directory names that represent the stash repository ID, which is mapped to a project/repository by Stash, but not obvious to my script.

Given a repository ID (id = 1124) how do I obtain the associated clone url?

I tried http:server/rest/api/latest/repos?id=1124 but the id parameter is ignored and I get the first page of all repos.

I could cycle through all repos to find it, but that is pretty expensive.  Is there a specific rest api to retrieve the clone url?

3 answers

Michael,

Rather than write your script from scratch, you might want to try our latest Add-on in the Atlassian Marketplace called ScriptRunner for Stash.

It has a built-in repository size report that lists all of your repositories or you could write your own script with some help from the examples in the documentation. The nearest example would be the one that lists all the repository sizes. As well as the project name and size, you could return the clone URL fairly quickly.

Hi,

The repository ID's are not public, so there is no way to do a lookup by repository ID through the REST interface. You can retrieve the clone URL if you have the repository slug by parsing the output of  

/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}

for

 "links": {
        "clone": [
            {
                "href": "https://<baseURL>/scm/PRJ/my-repo.git",
                "name": "http"
            },

 

but that's probably not what you wanted to hear.

 

Cheers,

Michael

I was hoping to drive the script with unix find:

cd repositories

find . -maxdepth 1 -type d -exec audit_repo {} \;

But I suppose I could add some additional logic to the audit_repo to use rest api to retrieve the projects and repos:

for project in (get all stash projects

do

  for repo in (get all project repos)

  do

    something

  done

done

You can receive all repositories at once with /rest/api/1.0/repos, so you don't need to iterate over all projects first. Note that you will get the repository ID in the JSON output: "values": [ { "slug": "my-repo", "id": 1, but that will probably not help you much...

Unless I'm overlooking something, repos returns 25 entries by default, and appears to have a hard limit of 1000 entries per request. But I can work with that.

The hard limit you mention is indeed 1000, but configurable (Stash config property page.max.repositories=1000, see here https://confluence.atlassian.com/display/STASH/Stash+config+properties). But you can always page over the results to get all repositories. Just use "?start=XXX" in your REST requests.

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 06, 2018 in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

216 views 3 7
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you