使用 github api 批量创建 issues

github api 使用

Github 支持通过 github api 操作创建 issues 等操作。

有一个开源库 https://github-api.kohsuke.org/ 做了封装,可以通过 Java api 方式直接调用。

Maven 依赖

<dependency>
    <groupId>org.kohsuke</groupId>
    <artifactId>github-api</artifactId>
    <version>1.314</version>
</dependency>

Demo

每个issue发完建议sleep一两秒,太快会被限流。

org.kohsuke.github.HttpException: {"message":"You have exceeded a secondary rate limit and have been temporarily blocked from content creation. Please retry your request again later.","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#secondary-rate-limits"}
    public static final String repositoryOwner = "apache"; // 仓库所有者
    
    public static final String repositoryName = "xx"; // 仓库名称

    // 客户端认证
    public static final GitHub github;

    static {
        try {
            // TODO 这个地方需要去github上申请自己的 token 填进去即可
            github = new GitHubBuilder().withOAuthToken("").build();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

@Test
public void testCreateOneIssue() throws Exception {
    // 获取指定仓库
    GHRepository repository = github.getRepository(repositoryOwner + "/" + repositoryName);

    // 可以通过 Map<Integer, GHMilestone> ms = repository.getMilestones();
    GHMilestone milestone = repository.getMilestone(27);

    // Issue标题
    String title = "Your title";

    // Issue内容
    String body = "Your issue body";

    createOneIssue(repository, milestone, title, body);
}

private static void createOneIssue(GHRepository repository, GHMilestone milestone, String title, String body) throws IOException {
    // 创建一个新的Issue
    GHIssue issue = repository.createIssue(title)
            .body(body)
            // 指定 milestone
            .milestone(milestone)
            // 指定多个tags
            .label("db: Oracle")
            .label("in: SQL parse")
            .label("type: enhancement")
            .label("good first issue")
            .create();

    // 输出Issue的URL
    System.out.println(issue.getHtmlUrl());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个Java代码示例,使用GitHub APIGitHub仓库中读取文件内容: ```java import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Base64; public class GitHubFileReader { public static void main(String[] args) throws IOException { // GitHub API URL String apiUrl = "https://api.github.com/repos/{owner}/{repo}/contents/{path}"; // GitHub repository information String owner = "github"; String repo = "gitignore"; String path = "Java.gitignore"; // Replace placeholders with actual values apiUrl = apiUrl.replace("{owner}", owner) .replace("{repo}", repo) .replace("{path}", path); // Set up HTTP connection URL url = new URL(apiUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/vnd.github.v3+json"); // Set up authentication if needed String username = "username"; String token = "token"; String auth = username + ":" + token; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8)); String authHeaderValue = "Basic " + new String(encodedAuth); connection.setRequestProperty("Authorization", authHeaderValue); // Read response InputStream responseStream = connection.getInputStream(); String response = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8); // Extract content from response int start = response.indexOf("\"content\": \"") + 12; int end = response.indexOf("\",", start); String content = response.substring(start, end); // Decode content String decodedContent = new String(Base64.getDecoder().decode(content), StandardCharsets.UTF_8); // Print content System.out.println(decodedContent); } } ``` 这个代码示例假设你已经有了GitHub API的访问令牌,用于身份验证。如果你没有访问令牌,你需要创建一个并将其传递到HTTP请求中,以便进行身份验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlyingZCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值