4

I'm having a bit of a challenge with the JGit client. I'm embedding it in a Java App & I'd like to fetch all the repositories under an account on Github and display them. Also to add on, can I create a repository direct on Github using JGit. Like Create a Remote Repository?

I've gone through this link but it seems generic to me. Thanks in advance

Zuko
  • 2,764
  • 30
  • 30

2 Answers2

2

The List user repositories API is something you can call from any language (including Java) and is not related to JGit.

GET /users/:username/repos

An example of Java library making those calls would be "GitHub API for Java ", and its java.org.kohsuke.github.GHPerson.java#listRepositories() method

new PagedIterator<GHRepository>(
   root.retrieve().asIterator("/users/" + login + "/repos?per_page=" + pageSize, 
                              GHRepository[].class, pageSize))

Once you get the url for those user repos, you can create JGit repos out of them.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    And yes, you can create repos too: https://github.com/kohsuke/github-api/blob/14dcb37ee1ea225b73b548faae313ab512ae69a4/src/site/markdown/index.md#sample-usage – VonC Jul 24 '16 at 12:21
  • That makes sense now. thanks. Didnt even use the API, just called directly from URL#openStream() – Zuko Jul 25 '16 at 04:30
1

I also doing that same Requirement To get List of repositories of a particular user Try this one you will get All repositories of that user //Here name means username of GitHub account

public Collection<AuthMsg> getRepos(String name) {

    String url = "https://api.github.com/users/"+name+"/repos";

    String data = getJSON(url);
    System.out.println(data);

    Type collectionType = new TypeToken<Collection<AuthMsg>>(){}.getType();
    Collection<AuthMsg> enums = new Gson().fromJson(data, collectionType);

    return enums;

}

//getJson method

public String getJSON(String url) {
    HttpURLConnection c = null;
    try {
        URL u = new URL(url);
        c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);
        c.connect();
        int status = c.getResponseCode();

        switch (status) {                                      
            case 200:
            case 201:
                BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                br.close();
                return sb.toString();
        }

    } catch (MalformedURLException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
    } finally {
       if (c != null) {
          try {
              c.disconnect();
          } catch (Exception ex) {
             Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
          }
       }
    }
    return null;
}

//AuthMsg Class

public class AuthMsg {
//"name" which is in json what we get from url
@SerializedName("name")
private String repository;

/**
 * @return the repository
 */
public String getRepository() {
    return repository;
}

/**
 * @param repository the repository to set
 */
public void setRepository(String repository) {
    this.repository = repository;
}

}

G.Raju
  • 41
  • 2