How to commit using the API ?

636 views
Skip to first unread message

Paulo Henrique Maia soares

unread,
Nov 14, 2017, 9:55:19 AM11/14/17
to GitHub Java API (http://github-api.kohsuke.org/)
Hi everyone,

I would like know how I can use the API to commit an existing file on github?

I don't found any sample to this task. Anyone can help me?

Paulo Henrique Maia soares

unread,
Nov 14, 2017, 10:41:36 AM11/14/17
to GitHub Java API (http://github-api.kohsuke.org/)
So far I do this code:

GitHub github = GitHub.connect("USER", "TOKEN");
GHRepository ghRepo = github.getRepository("REPOSITORY");
GHContent content = ghRepo.getFileContent("/PATH/FILE.txt");

Now, i need change this file and commit in the repository.

Thomas Lefort

unread,
Sep 3, 2018, 1:09:36 PM9/3/18
to GitHub Java API (http://github-api.kohsuke.org/)
Digging into the GitHub API doc and the js version of the API I ended up doing something like that (early and straight to the point version!)

static public String pushFiles(File baseDirectory, String message,
                               
String userName, String password,
                               
String repositoryName, String branchName) throws IOException {
   
GitHub github = GitHub.connectUsingPassword(userName, password);
   
GHRepository repo = github.getRepository(userName + "/" + repositoryName);
   
// get current branch
    GHRef ref = repo.getRef("heads/" + branchName);
   
GHCommit latestCommit = repo.getCommit(ref.getObject().getSha());
   
GHTreeBuilder treeBuilder = repo.createTree().baseTree(latestCommit.getTree().getSha());
   
addFilesToTree(treeBuilder, baseDirectory, baseDirectory);
   
GHTree tree = treeBuilder.create();
   
GHCommit commit = repo.createCommit()
           
.parent(latestCommit.getSHA1())
           
.tree(tree.getSha())
           
.message(message)
           
.create();
 
   ref.updateTo(commit.getSHA1());

   
logger.info("Commit created with on branch " + branchName + " and SHA " + commit.getSHA1() + " and URL " + commit.getHtmlUrl());
   
return commit.getSHA1();
}

private static void addFilesToTree(GHTreeBuilder treeBuilder, File baseDirectory, File currentDirectory) throws IOException {
   
for(File file : currentDirectory.listFiles()) {
       
String relativePath = baseDirectory.toURI().relativize(file.toURI()).getPath();
       
if(file.isFile()) {
            treeBuilder
.textEntry(relativePath, FileUtils.readFileToString(file), false);
       
} else {
           
addFilesToTree(treeBuilder, baseDirectory, file);
       
}
   
}
}



There is probably more elegant or effective. I am grateful for the project and repository but I must say the documentation could do with a few more examples ;-)

符汉泽

unread,
Nov 17, 2019, 5:21:45 AM11/17/19
to GitHub Java API (http://github-api.kohsuke.org/)
Hi Thomas,

I tried your example but it always hit below error, may i know if you have any idea about this? I have already added the text entry.

18:23:45.273 [main] ERROR hanze.lab.githubapitest.github.GithubConnector - org.kohsuke.github.HttpException: {"message":"Must supply tree.sha or tree.content","documentation_url":"https://developer.github.com/v3/git/trees/#create-a-tree"}
Reply all
Reply to author
Forward
0 new messages