用java统计git项目的每个用户变更行数和提交次数--gitlab4j-api - 灰信网(软件开发博客聚合) (freesion.com)
https://www.freesion.com/article/6269791470/
<groupId>org.gitlab4jgroupId>
<artifactId>gitlab4j-apiartifactId>
<groupId>org.glassfish.jersey.coregroupId>
<artifactId>jersey-clientartifactId>
<groupId>org.glassfish.jersey.coregroupId>
<artifactId>jersey-commonartifactId>
<groupId>com.google.guavagroupId>
<artifactId>guavaartifactId>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
package com.example.demo;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;
import org.gitlab4j.api.models.Branch;
import org.gitlab4j.api.models.Commit;
import org.gitlab4j.api.models.Project;
import org.springframework.util.CollectionUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
public class GitlabMain {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private Integer addCount = 0;
private Integer deleteCount = 0;
private Integer totalCount = 0;
public static void main(String[] args) throws Exception {
StringBuilder sb = new StringBuilder(gitlab61()).append(gitlab104()).append(gitlab97());
System.out.println(sb.toString());
public static String gitlab61() throws Exception {
List groupIds = Lists.newArrayList(2);
return gitlab("xxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
public static String gitlab97() throws Exception {
List groupIds = Lists.newArrayList(3);
return gitlab("xxxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
public static String gitlab104() throws Exception{
List groupIds = Lists.newArrayList(13, 34, 59, 58);
return gitlab("xxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
public static String gitlab(String url,String token,String startDate,String endDate,List groupIds) throws ParseException {
Date BEGIN_DATE = sdf.parse(startDate);
Date END_DATE = sdf.parse(endDate);
Map> result = new HashMap<>();
GitLabApi gitLabApi = new GitLabApi(url,token);
projects = gitLabApi.getGroupApi().getProjects(e);
} catch (GitLabApiException gitLabApiException) {
gitLabApiException.printStackTrace();
if (!CollectionUtils.isEmpty(projects)) {
Map map = result.computeIfAbsent(p.getName(), key->new HashMap<>());
branches = gitLabApi.getRepositoryApi().getBranches(p.getId());
} catch (GitLabApiException gitLabApiException) {
gitLabApiException.printStackTrace();
if (!CollectionUtils.isEmpty(branches)) {
List commits = gitLabApi.getCommitsApi().getCommits(p.getId(), b.getName(),BEGIN_DATE,END_DATE);
if(!CollectionUtils.isEmpty(commits)){
v = gitLabApi.getCommitsApi().getCommit(p.getId(), v.getShortId());
} catch (GitLabApiException gitLabApiException) {
gitLabApiException.printStackTrace();
CommitVo vo = map.computeIfAbsent(v.getAuthorName() + "-" + v.getAuthorEmail(),key-> new CommitVo());
vo.setAddCount(vo.getAddCount()+v.getStats().getAdditions());
vo.setDeleteCount(vo.getDeleteCount()+v.getStats().getDeletions());
vo.setTotalCount(vo.getTotalCount()+v.getStats().getTotal());
} catch (GitLabApiException ioException) {
ioException.printStackTrace();
StringBuilder sb = new StringBuilder();
result.entrySet().forEach(e->{
e.getValue().entrySet().forEach(ee->{
sb.append(e.getKey() + "\t" + ee.getKey() + "\t" + ee.getValue().getAddCount() + "\t" + ee.getValue().getDeleteCount() + "\t" + ee.getValue().getTotalCount() ).append("\r\n");
