使用 github api 批量创建 issues

github api 使用

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

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

Maven 依赖


    org.kohsuke
    github-api
    1.314

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

你可能感兴趣的:(随笔,github)