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

In this article, we will go through very simple example of GitHub REST API to programmatically browse remote GitHub repository without cloning into local.

Example in this article:

  • Make RESTFul call to GitHub content REST API end point for a test repository.
  • Iterate through list of files from test repository.
  • Get content of one of the file & print content of the file.
  • Download file in local with the content of the remote file.

GitHub contents API

GitHub provides access to its repository & its content through REST endpoints.  Here is the link for Documentation for content API.

  • Base URL for GitHub API – https://api.github.com
  • Path for Contents API – /repos/:owner/:repo/contents/:path (GET)
  • Parameter – ref (Branch/Tag/Commit)

We will use Test repository for this example. This is a public repository so authentication won’t be needed.



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

  • “:owner” will be “RaviKharatmal”
  • “:repo” will be “test”.
  • We will not provide “:path” so that we can get list of all files in this repository.
  • We will pass “ref” param as “develop” so that we will get files from “develop” branch.

Note that this API has restrictions. File list from the response is not recursive, so use Tree API to get recursive file list. Contents API also has limit 1000 files & files upto 1 MB so if you need all files or bigger files then also Tree API must be used.

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

Lets Code

For this example, to keep it simple & short, we will use few libraries as given below. But you are free to chose any other library or code to do below things.

  • REST Client library – We will use Spring Framework’s RestTemplate since it provides very easy way to call REST end point.
  • GSON – We use GSON library to parse & pretty print the response of the REST call.
  • Apache commons IO – We will sue this to fetch file or content from the remote URLs.




Here are the dependencies for above libraries.

Here is the java code.



Here is the output of the code.

 

Complete code is also committed & available in GitHub repository.

Further reading

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

 

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



Program will create file as shown below with the content from remote read me file.



One Reply to “GitHub REST API | Get remote repo files list & download file content programmatically without cloning in local”

Leave a Reply

Your email address will not be published. Required fields are marked *