Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,358
Community Members
 
Community Events
165
Community Groups

How to get a list of files in a given repo with the rest api?

Edited

Hi, I have a project with 20 repos. I would like to get a list of file names for each repo (in the main branch -some are called 'master', some 'main'-).

How do I go about doing this?

I saw this answer in another question, but it didn't really work for me. This is specifically what I tried (to get all the files in one repo's master branch):

/rest/api/1.0/projects/{projectName}/repos/{repoName}/master/files?limit=1000

Where am I going wrong? Ideally I would like to get all files under all repos inside a given project in one call, but if that's not possible I can settle for making an API call per repo to get each of their list of files. I don't need the content, just the file names.

 

Thanks!

1 answer

Alright, in the end I just devised a way to do it via JS from the browser (couldn't find any way to do this directly with the Bitbucket API):

//The way to run this script is through DevTools (Ctrl+Shift+i) > Sources > '>>' > Snippets > New snippet > copy paste the script there 
//> run it (while in https://<your-bitbucket-domain>/projects/<your-project-name>

//STEP 1: extracts the urls of all repos under <your-project-name>
let array = document.getElementsByClassName("repository-name");
let links = Array.from(array).map(x=>x.outerHTML.match(/<a[^>]+href=\"(.*?)\"[^>]*>(.*)?<\/a>/)[1]);
// console.log(links); // (25) ["/projects/<your-proj>/repos/<repo-name>/browse"...]

let repoNames = links.map(x=>x.split("/").slice(-2,-1)).flat(); //["repoA", "repoB", "repoC", ...]
console.log( repoNames)
links.forEach((x,idx)=>fetchEachRepo("https://<your-bitbucket-domain>"+x,repoNames[idx]));

//STEP 2: fetches the html of a given url and extracts repo file names 
function fetchEachRepo(url,repoName){
let parser = new DOMParser();
let htmlDoc;
let res;
fetch(url)
.then(response => response.text())
.then(data=>{
htmlDoc = parser.parseFromString(data, 'text/html');
res=Array.from(htmlDoc.getElementsByClassName("file file-row")).map(x=>x.dataset.itemName);
res.unshift(repoName);
console.log(res);
})
.catch((err)=>console.log(err));
}


//NOTES
// $('') is an alias for document.querySelector
// $$('') is an alias for document.querySelectorAll

 

The end result looks like:

['repoA', 'readme.md', 'contributing.md'...]

['repoB', '.gitignore', ...]

...

 

Hope it helps

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,229 views 2 9
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you