B4J Library GitHub API Library

This is a work in progress library that wraps the GitHub API for Java library;

http://github-api.kohsuke.org/

File is too large to upload so you can download it here;


The minimum methods are exposed to;

1. Create a repo
2. Connect to a repo
3. Create files in a repo
4. Update files in a repo
5. Delete files in a repo
6. List all repos for the current user

Small example to connect to a repo;

B4X:
Dim hub As githubapi
  
'hub.Initialize("email","password") 'Use email and password (had mixed luck with this)
hub.Initialize2("XXX") 'Use oauth token (set up in your github)
Log(hub.isCredentialValid)
  
Dim repo As GHRepository
repo.Initialize

'NB important note you have to fully qualify the repo with your org or user name (this is the same as repo.FullName)
repo = hub.getRepository(user_or_org_name & "/" & ProjectName)
  
Log(repo.Name)
Log(repo.Description)
Log(repo.FullName)

Once you have a repo you can create files like this;

B4X:
repo.createContent(File.ReadString(File.DirApp,"Somefile.txt"),"CommitMessage","SomeFile.txt")

To update a file you need to get it first;

B4X:
Dim c As GHContent
c = repo.getFileContent("Somefile.txt")

'if c isnt initialised then the file doesnt exist in github

c.update(File.ReadString(File.DirApp,"Somefile.txt"),"Update")

And to delete a file;

B4X:
Dim c As GHContent
c = repo.getFileContent("Somefile.txt")
c.delete("Deleted")

To create a new repo do this;

B4X:
Dim repo As GHRepository
repo.Initialize
repo = hub.createRepository(ProjectName,"","",False)
 
Last edited:

tchart

Well-Known Member
Licensed User
Longtime User
Does one need to Initialize c then in case it does not exists?

Hadn´t look at the library yet (but surely i´ll do). It is just a question which came in mind when reading the post.

No, you should use repo.createContent to create the file. The GHContent object is for items that already exist as it contains the SHA key etc which are required for updates and deletes.

In other words GHContent isnt for creating new content. The is initialised is just the way to see if the content exists in GitHub or not.
 

tchart

Well-Known Member
Licensed User
Longtime User
@Mashiane library has been updated.

You can use the AllRepositories method to get a map of all repos. The key is the repo name the value is a GHRepository object

B4X:
Dim m As Map
m = hub.AllRepositories

For Each name As String In m.Keys
    Log(name)
    Dim repo As GHRepository = m.Get(name)
    Log(repo.Description)
Next
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…