Skip to content

Instantly share code, notes, and snippets.

@Detelca
Created April 8, 2012 14:55
Show Gist options
  • Save Detelca/2337731 to your computer and use it in GitHub Desktop.
Save Detelca/2337731 to your computer and use it in GitHub Desktop.
How to create/update file with github api v3 from android
package com.krama.testgithub;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.TypedResource;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.RepositoryService;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TestgithubActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.buttonUpdateGit);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
updateFile(v);
}
});
};
private void updateFile(View v){
try {
// based on http://swanson.github.com/blog/2011/07/23/digging-around-the-github-api-take-2.html
// initialize github client
GitHubClient client = new GitHubClient();
TextView password = (TextView)findViewById(R.id.textViewPassword);
client.setCredentials("Detelca", password.getText().toString());
// create needed services
RepositoryService repositoryService = new RepositoryService();
CommitService commitService = new CommitService(client);
DataService dataService = new DataService(client);
// get some sha's from current state in git
Repository repository = repositoryService.getRepository("Detelca", "Detelca");
String baseCommitSha = repositoryService.getBranches(repository).get(0).getCommit().getSha();
RepositoryCommit baseCommit = commitService.getCommit(repository, baseCommitSha);
String treeSha = baseCommit.getSha();
// create new blob with data
Blob blob = new Blob();
blob.setContent("[\"" + System.currentTimeMillis() + "\"]").setEncoding(Blob.ENCODING_UTF8);
String blob_sha = dataService.createBlob(repository, blob);
Tree baseTree = dataService.getTree(repository, treeSha);
// create new tree entry
TreeEntry treeEntry = new TreeEntry();
treeEntry.setPath("data/data.json");
treeEntry.setMode(TreeEntry.MODE_BLOB);
treeEntry.setType(TreeEntry.TYPE_BLOB);
treeEntry.setSha(blob_sha);
treeEntry.setSize(blob.getContent().length());
Collection<TreeEntry> entries = new ArrayList<TreeEntry>();
entries.add(treeEntry);
Tree newTree = dataService.createTree(repository, entries, baseTree.getSha());
// create commit
Commit commit = new Commit();
commit.setMessage("Android commit at " + new Date(System.currentTimeMillis()).toLocaleString());
commit.setTree(newTree);
List<Commit> listOfCommits = new ArrayList<Commit>();
listOfCommits.add(new Commit().setSha(baseCommitSha));
// listOfCommits.containsAll(base_commit.getParents());
commit.setParents(listOfCommits);
// commit.setSha(base_commit.getSha());
Commit newCommit = dataService.createCommit(repository, commit);
// create resource
TypedResource commitResource = new TypedResource();
commitResource.setSha(newCommit.getSha());
commitResource.setType(TypedResource.TYPE_COMMIT);
commitResource.setUrl(newCommit.getUrl());
// get master reference and update it
Reference reference = dataService.getReference(repository, "heads/master");
reference.setObject(commitResource);
dataService.editReference(repository, reference, true);
// success
Toast.makeText(v.getContext(), "Updating github", Toast.LENGTH_LONG).show();
} catch (Exception e) {
// error
e.printStackTrace();
Toast.makeText(v.getContext(), "Updating github error " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
@Detelca
Copy link
Author

Detelca commented Apr 8, 2012

@Maratonec
Copy link

Maratonec commented Mar 26, 2020

Hi, nice script, should I download GSON, org.eclipse.egot.github.core and Junit(gson dependency) when I am not using maven? Does this code overwrite the file and delete it’s old content or does it add the new information? Thanks for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment