GitHub REST API | Tree API to get remote repo files list & metadata recursively programmatically without cloning in local

In this article we will go through simple example for Tree API from GitHub REST API using Java. In earlier article we went through content API. Here is the link for the same.

GitHub REST API | Get remote repo files list & download file content programmatically without cloning in local

Example in this article

  • Get branches list using branches REST end point.
  • Fetch Tree REST URL from branches response for ‘develop’ branch.
  • Call Tree REST endpoint with recursive parameter & get list of all files recursively & print files names & sizes.

GitHub Branches & Tree API

You can find documentation here for Branches API & Tree API.

  • Base URL for GitHub API – https://api.github.com
  • Path for Branches API – /repos/:owner/:repo/branches/:branch (GET)
  • Path for Branches API – /repos/:owner/:repo/git/trees/:tree_sha (GET)
    • Parameter – recursive (Set to 1 to indicate to get files recursively)

Test Repo Web URL = https://github.com/Ravikharatmal/test

  • “:owner” will be “RaviKharatmal”
  • “:repo” will be “test”.
  • “:branch” as “develop” so that we will get files from “develop” branch.
  • “:tree_sha” will be received in response of branches API, so use that value.



Lets code

In earlier article for Content API, we used Spring Framework’s RestTemplate. For this example, we will use Apache commons HTTP Client library with Apache Fluent httpcomponents library. We will still use GSON for JSON parsing. In this article we will just fetch file metadata. To get file content you can refer Content API.

Here is the Java code.



Here is the output of above program.

 

Complete code is also committed & available in GitHub repository.



Further Reading

Git commands from Java using JGIT | Programmatically git clone, checkout -b, commit -a, log, status, branch

GitHub REST API | Get remote repo files list & download file content programmatically without cloning in local

GitHub REST API | Search files, content, pull requests, commits programmatically using Java without cloning

Leave a Reply

Your email address will not be published.