首页 > 解决方案 > 尝试使用 Java JGit 从 Github 中提取日志

问题描述

我正在尝试使用 JGit 提取开发人员使用 Github 安全令牌完成的提交日志。我尝试了很多谷歌搜索,但是这方面的文档很少。我目前正在修改的代码如下所示:

public class GitIntegration {
    public GitIntegration() {
    }
    
    public Iterable<RevCommit> getCommits() throws IOException, InvalidRefNameException, GitAPIException {
        FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
        repositoryBuilder.setMustExist( true );
        repositoryBuilder.setGitDir( new File ("https://oauth2:xyzxyzTOKENxyzxyz@github.com/myuser/myrepo.git"));
        Repository repository = repositoryBuilder.build();

        try (Git git = new Git(repository)) {
            Iterable<RevCommit> commits = git.log().all().call();
            int count = 0;
            for (RevCommit commit : commits) {
                System.out.println("LogCommit: " + commit);
                count++;
            }
            System.out.println(count);
            return commits;
        }
    }
}

但我收到以下错误:

java.nio.file.InvalidPathException: Illegal char <:> at index 5: https:\oauth2:xyzxyzTOKENxyzxyz@github.com\myuser\myrepo.git\config sun.nio.fs.WindowsPathParser.normalize(未知来源)

像这样在 File 构造函数中抛出 git 语法根本不起作用。有谁知道使用令牌进行身份验证然后能够使用 git.log() 方法的方法?

谢谢!

标签: javagithubloggingintegrationjgit

解决方案


经过一番研究,我决定改用 Kohsuke github 库。这样做非常简单。发布基本代码示例以供参考。(请注意,如果用于 Enterprise Git。如果您是为“普通”Git 执行此操作,请查看 Kohsuke 文档,您将需要使用其他方法而不是 connectToEnterpriseWithOAuth。)

import org.kohsuke.github.*;


public class GitIntegration {
    
    
    public GitIntegration() {
        
    }
    
    
    public String getCommits(String repo) {
        String retval = "";
        try {
            GitHub gitHub = GitHub.connectToEnterpriseWithOAuth("https://git.our.company.com/api/v3", "my.email@ourcompany.com", "xyzxyzTokenxyzxyz");
            GHMyself me = gitHub.getMyself();
            
            GHRepository repository = gitHub.getRepository("Gituser/"+repo);
            
            Calendar cal = Calendar.getInstance();
            cal.set(2020, 10, 4);
            Date since = cal.getTime();

            GHCommitQueryBuilder queryBuilder = repository.queryCommits().since(since).until(new Date());
            PagedIterable<GHCommit> commits = queryBuilder.list();
            Iterator<GHCommit> iterator = commits.iterator();

            while (iterator.hasNext()) {
                GHCommit commit = iterator.next();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                GHUser user = commit.getAuthor();
                String name = "";
                if(user != null) name = user.getName();
                String message = commit.getCommitShortInfo().getMessage();
                retval = retval.concat(message + " | " + name + " | " + sdf.format(commit.getCommitDate()) + "\n");
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            retval = e.getCause().getMessage();
            e.printStackTrace();
        }
        return retval;
    }
}

推荐阅读