如何使用Java访问GitHub中的存储库名称



我有一个应用程序,在GitHub中有许多不同的存储库。在我的应用程序中,我需要获得每个存储库的名称。我在互联网上搜索了很多,当然也在Stack Overflow中搜索了很多来访问GitHub repos,但我没有找到适合我的解决方案。

希望有人有一个好主意或有经验。

@Value("${github.githubUrl}")
String url;
public void getEach() {
try {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(this.url);
request.addHeader("content-type", "application/json");
HttpResponse result = httpClient.execute(request);
String json = EntityUtils.toString(result.getEntity(), "UTF-8");
System.out.println("Json kommmt=============================  "+ json);
JsonElement jelement = new JsonParser().parse(json);
JsonArray jarr = jelement.getAsJsonArray();
for (int i = 0; i < jarr.size(); i++) {
JsonObject jo = (JsonObject) jarr.get(i);
String fullName = jo.get("full_name").toString();
fullName = fullName.substring(1, fullName.length()-1);
System.out.println("fullname kommt ================  " + fullName);
}
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
}

我可以在这里看到一个现有的代码。

https://github.com/hub4j/github-api/blob/9ab6d570193dc381a0cda7cc4991f471499dcf24/src/main/java/org/kohsuke/github/GHPerson.java#L70-L89

public PagedIterable<GHRepository> listRepositories(final int pageSize) {
return new PagedIterable<GHRepository>() {
public PagedIterator<GHRepository> _iterator(int pageSize) {
return new PagedIterator<GHRepository>(root.retrieve().asIterator("/users/" + login + "/repos?per_page=" + pageSize, GHRepository[].class, pageSize)) {
@Override
protected void wrapUp(GHRepository[] page) {
for (GHRepository c : page)
c.wrap(root);
}
};
}
};
}

根据github文档,存储库列表如下:

List organization repositories
Lists repositories for the specified organization.
GET /orgs/{org}/repos

注意:
或者,如果只是要求列出回购,请尝试此-

curl "https://api.github.com/users/$GHUSER/repos?access_token=$GITHUB_API_TOKEN" | grep -w clone_url

步骤1,2,3将帮助您创建令牌,如https://crunchify.com/how-to-access-github-content-with-basic-oauth-authentication-in-java-httpclient-or-urlconnection-method/

如果真的依赖java,你可以试试java中的curl!(你应该找到grep的方法(

URL url = new URL("https://api.github.com/users/" + GHUSER + "/repos?access_token=" + GITHUB_API_TOKEN); //pagesize too
try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
for (String line; (line = reader.readLine()) != null;) {
System.out.println(line);
}
}

退房https://github.com/hub4j/github-api,它拥有你所需要的一切。这是一个代码示例,我认为可以执行您想要的操作——获取指定组织内的repo列表。

import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.io.IOException;
@SpringBootApplication
public class GhScanner implements CommandLineRunner {
@Value("${github.oauthToken}")
private String oauthToken;
public static void main(String[] args) {
SpringApplication.run(GhScanner.class, args);
}
@Override
public void run(String... args) throws Exception {
if (args.length < 1) {
throw new IllegalArgumentException("Expected organization name as the 1st argument");
}
var githubOrganization = args[0];
System.out.printf("Fetching repos for organization '%s':%n", githubOrganization);
var iterator = getRepos(githubOrganization).iterator();
while (iterator.hasNext()) {
iterator.nextPage().stream()
.map(GHRepository::getName)
.forEach(System.out::println);
}
}
private PagedIterable<GHRepository> getRepos(String githubOrganization) throws IOException {
GitHub gitHub = GitHub.connectUsingOAuth(oauthToken);
return gitHub.getOrganization(githubOrganization).listRepositories();
}
}

最新更新