Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

Trying to pull logs from Github using Java JGit

I'm trying to use JGit to pull a log of commits done by developers using a Github security token. I tried googling a lot, but the documentation is scarce on this. The code that I am currently tinkering with looks like this:

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:[email protected]/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;
        }
    }
}

But I am getting the following error:

java.nio.file.InvalidPathException: Illegal char <:> at index 5: https:oauth2:[email protected] sun.nio.fs.WindowsPathParser.normalize(Unknown Source)

It doesn't work to throw git syntax in the File constructor like that at all. Does anyone know of a way to authenticate with tokens and then be able to use the git.log() method?

Thanks!

question from:https://stackoverflow.com/questions/65840603/trying-to-pull-logs-from-github-using-java-jgit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After digging around a bit, I decided to go with the Kohsuke github library instead. When doing that it was quite straightforward. Posting basic code example for reference. (Please note that this if for Enterprise Git. If you are doing it for "normal" Git, look into the Kohsuke documentation, you will need to use another method instead of 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", "[email protected]", "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()) + "
");
            }

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...