Github_API
What is Github?
GitHub is a web-based Git repository hosting service, which offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features.
What is Git?
Git is a free and open source distributed version control system, a tool to manage your source code history.
You can follow the links to know more about Github:
https://en.wikipedia.org/wiki/GitHub
https://www.howtogeek.com/180167/htg-explains-what-is-github-and-what-do-geeks-use-it-for/
At this point I hope everybody is familiar about github.
Here I will explain you about how to interact with your github repository through java program.
First of all you need two jar files and you can download it from the below links:
org.eclipse.egit.github.core.jar: http://www.java2s.com/Code/Jar/o/Downloadorgeclipseegitgithubcore215jar.htm
gson.jar : http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
Code for reading the contents of the repository:
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryContents;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.ContentsService;
import org.eclipse.egit.github.core.service.RepositoryService;
public class readgithub {
static GitHubClient client = new GitHubClient(); //this by default connects to open source github(github.com)
ContentsService content = new ContentsService(client);
static RepositoryService service = new RepositoryService(client);
public void readContent() throws IOException {
Repository repo = null;
repo = service.getRepository("username", "repository");
List<RepositoryContents> contentList = null;
contentList = content.getContents(repo);
for (RepositoryContents content2 : contentList) {
System.out.println(content2.getName());
}
}
public static void main(String[] args) {
try {
new readgithub().readContent();
} catch (IOException e) {
e.printStackTrace();
}
}
}
If you want to try for enterprise github then send the host name as parameter to the githubclient.
static GitHubClient client = new GitHubClient("hostname");
Similarly you can check for the file type(whether it is the folder or file and so on)
for (RepositoryContents content2 : contentList) {
System.out.println(content2.getType().toString());
if(content2.getType().toString().trim().contentEquals("file")){
System.out.println(content2.getName() + " is a file");
}
if(content2.getType().toString().trim().contentEquals("dir")){
System.out.println(content2.getName() + " is a folder");
}
}