使用Jgit编程获取我的Github账户下的所有仓库

4

我在使用JGit客户端时遇到了一些挑战。我将其嵌入到Java应用程序中,希望获取Github账户下的所有存储库并显示它们。此外,我可以使用JGit直接在Github上创建存储库吗?像创建远程存储库这样的操作?

我已经阅读了这个链接link,但是它对我来说似乎太通用了。提前感谢。


请发布您的代码和错误信息。 - Sachith Muhandiram
@Sachith 我本来想做的,但实际上我不太知道怎么做。 - Zuko
@Sachith,你不需要API来检索它。你可以通过简单的Java URL请求来完成。 - Zuko
2个回答

2
列出用户仓库 API 可以从任何语言(包括 Java)调用,与 JGit 无关。请注意保留 HTML 标签。
GET /users/:username/repos

一个使用Java库进行这些调用的例子是"GitHub API for Java",以及它的java.org.kohsuke.github.GHPerson.java#listRepositories()方法。

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

一旦您获得了用户存储库的URL,就可以将它们创建为JGit存储库。

1
是的,您也可以创建存储库:https://github.com/kohsuke/github-api/blob/14dcb37ee1ea225b73b548faae313ab512ae69a4/src/site/markdown/index.md#sample-usage - VonC
现在有意义了,谢谢。甚至没有使用API,直接从URL#openStream()调用。 - Zuko

1

我也在做同样的需求,获取特定用户的存储库列表。尝试使用以下代码,你将获得该用户的所有存储库。 //这里的name表示GitHub账户的用户名

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方法

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类

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;
}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接