Tuesday, July 28, 2015

Github API using Java for Corp/Enterprise Github

Other day I was trying to use Github API using Java to do some operation on our github corp account. Though there are bunch of Java Libraries available for Git operation, I didn't find a good documentation for any of them.

I used kohsuke github-api library which is very simple and object oriented.
To use this library in maven project add following dependency:


  • Connect to Corp Git Hub account:
There are 2 ways to connect to enterprise github account either using OAuth Token, or using username/password. Here are the APIs to do that:

org.kohsuke.github.GitHub github = GitHub.connectToEnterprise(
"https://github.corp.<abc>.com/api/v3""<Oauth Token>");

org.kohsuke.github.GitHub github = GitHub.connectToEnterprise(
"https://github.corp.<abc>.com/api/v3""<User Name>","<Password>");

  • Get your information
GHMyself my = github.getMyself();

  • Search a keyword in code
Create GHContentSearchBuilder object from github object. Set keyword in GHContentSearchBuilder object.

GHContentSearchBuilder search = github.searchContent();
GHContentSearchBuilder s = search.q(<keyword>);
PagedSearchIterable<GHContent> res = s.list();

  • Get repositories for each searched code

For each GHContent object use getOwner()

for (GHContent ghContent : res) {

  • Search repositories

Create GHRepositorySearchBuilder object from github object. Set keyword in GHRepositorySearchBuilder object.

GHRepositorySearchBuilder repo = github.searchRepositories();
GHRepositorySearchBuilder repo = repo.q(<keyword>);
PagedSearchIterable<GHRepository> repoList = repo.list();

And there is much more, APIs are simple and you can explore it more to get other information. Feel free to drop a comment if you have any question and till than Happy Coding :)

No comments:

Post a Comment