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

michael_giroux July 31, 2015

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

1 vote
Mibex_Software
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2015

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

1 vote
Mark McCormack _Adaptavist_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2015

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.

0 votes
michael_giroux July 31, 2015

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

Mibex_Software
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2015

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...

michael_giroux July 31, 2015

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.

Mibex_Software
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 31, 2015

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
TAGS
AUG Leaders

Atlassian Community Events