java - 将 GitHub 问题下载到 .csv 文件

标签 java csv github egit github-api

GitHub 问题下载使用 eclipse egit 不返回任何内容。

最近,我一直在尝试创建一个 java 桌面应用程序(适用于 Windows),它将从特定的 GitHub 问题存储库下载 GitHub 问题,并将它们保存在 .csv 文件中。

我使用 Swing 创建了一个简单的 GUI 来启用存储库名称的输入。我还使用 eclipse 的 egit 库与 GitHub 建立连接以下载问题。我使用身份验证,使用 .properties 文件输入以验证 egit 与 GitHub 的连接。

这是我的应用程序用来下载问题并将它们写入 .csv 文件的主要代码:

package io.github.psgs.issuesdownload;

import io.github.psgs.issuesdownload.gui.GUI;
import org.eclipse.egit.github.core.Issue;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.IssueService;

import java.io.FileWriter;
import java.io.IOException;

public class IssuesDownload {

    public static void main(String[] args) {
        try {
        Config.loadConfiguration();
        } catch(IOException ex) {

        }
        GUI.main(args);
    }

    public static String saveIssues(String repoDetails) {
        String[] repoInfo = repoDetails.split("/");
        String repoOwner = repoInfo[0];
        String repoName = repoInfo[1];

        GitHubClient client = new GitHubClient();
        client.setCredentials(Config.githubuser, Config.githubpass);

        IssueService issueService = new IssueService(client);

        try {
            FileWriter writer = new FileWriter("issues.csv");
            //String[] header = {"Id", "Title", "Creator", "Assignee", "Milestone", "State", "Body Text"};
            writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text");
            writer.append("\n");

            for (Issue issue : issueService.getIssues(repoOwner, repoName, null)) {
                //String[] data = {String.valueOf(issue.getId()), issue.getTitle(), issue.getUser().getName(), issue.getAssignee().getName(), issue.getMilestone().getTitle(), issue.getState(), issue.getBodyText()};
                writer.append(String.valueOf(issue.getId()) + ",");
                writer.append(issue.getTitle() + ",");
                writer.append(issue.getUser().getName() + ",");
                writer.append(issue.getAssignee().getName() + ",");
                writer.append(issue.getMilestone().getTitle() + ",");
                writer.append(issue.getState() + ",");
                writer.append(issue.getBodyText());
                writer.append("\n");
            }
            writer.flush();
            writer.close();
            return "Download Complete!";
        } catch (IOException ex) {
            System.out.println("An IOException has occured!");
            ex.printStackTrace();
            if (ex.getMessage().equalsIgnoreCase("api.github.com")) {
                return "An error has occured, reaching " + ex.getMessage() + "! Please check your network connection.";
            }
        }
        return "An error has occured!";
    }
}

此代码也可在以下位置获得:https://gist.github.com/psgs/9048602

可以在以下位置找到整个存储库:https://github.com/psgs/IssuesDownload

当我运行此代码时,.properties 文件与编译 .jar 文件位于同一目录中,GitHub 问题不会出现在 .csv 文件中。我已经测试了 .csv 文件输出,当我删除下载代码时, header 写入正确。

有人知道为什么会这样吗?也许这是我错过的身份验证问题?

最佳答案

在尝试了一些新的 API 包装器之后,我找到了一个可用的 API 库。我现在正在使用 Kohsuke Kawaguchi's GitHub API for Java连接到 GitHub。

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

我的代码现在如下所示:

package io.github.psgs.issuesdownload;

import io.github.psgs.issuesdownload.gui.GUI;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;

import java.io.FileWriter;
import java.io.IOException;

public class IssuesDownload {

    public static void main(String[] args) {
        try {
            Config.loadConfiguration();
        } catch (IOException ex) {
            System.out.println("An IOException had occured while loading the configuration!");
            ex.printStackTrace();
        }
        GUI.main(args);
    }

    public static String saveIssues(String repoDetails, GHIssueState issueState) {

        String[] repoInfo = repoDetails.split("/");

        try {
            GitHub github = GitHub.connectUsingOAuth(Config.githubtoken);
            GHRepository repository = github.getUser(repoInfo[0]).getRepository(repoInfo[1]);

            FileWriter writer = new FileWriter("issues.csv");
            writer.append("Id, Title, Creator, Assignee, Milestone, State, Body Text");
            writer.append("\n");

            for (GHIssue issue : repository.getIssues(issueState)) {
                writer.append(String.valueOf(issue.getNumber()) + ",");
                writer.append(issue.getTitle() + ",");
                writer.append(issue.getUser().getLogin() + ",");
                if (issue.getAssignee() != null) {
                    writer.append(issue.getAssignee().getName() + ",");
                } else {
                    writer.append(" ,");
                }
                if (issue.getMilestone() != null) {
                    writer.append(issue.getMilestone().getTitle() + ",");
                } else {
                    writer.append(" ,");
                }
                writer.append(issue.getState() + ",");
                writer.append(issue.getBody() + ",");
                writer.append("\n");
            }
            writer.flush();
            writer.close();
            return "Download Complete!";
        } catch (IOException ex) {
            System.out.println("An IOException has occured!");
            ex.printStackTrace();
            if (ex.getMessage().equalsIgnoreCase("api.github.com")) {
            return "An error has occurred reaching " + ex.getMessage() + "! Please check your network connection.";
            }
        }
        return "An error has occured!";
    }
}

关于java - 将 GitHub 问题下载到 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21827958/

相关文章:

java - 带有 ADT 插件的 Eclipse - 布局编辑器不显示内容(卡在 "Loading editor"上)

git pull origin master 给出 merge 冲突 : what should I do?

git - 如何创建个人 git 函数,每次启动 Bash 时都可用?

python - 如何使用 python 比较两个不同的 csv 文件?

c++ - 为什么这些算法运行得比它们应该的要快?

unix - 如何查看所有命令的日志?

java - 从内向外在正方形中绘制像素 - Java

java - 将背景颜色更改为 ListView 的项目范围

java - 替换String中的$符号

python - pyodbc.DataError : ('22018' , “[22018] [Microsoft][ODBC SQL Server 驱动程序][SQL Server]转换失败] 错误