用java统计git项目的每个用户变更行数和提交次数--gitlab4j-api - 灰信网(软件开发博客聚合) (freesion.com)https://www.freesion.com/article/6269791470/
org.gitlab4j gitlab4j-api4.14.30 org.glassfish.jersey.core jersey-client2.30.1 org.glassfish.jersey.core jersey-common2.30.1 com.google.guava guava21.0 org.projectlombok lombok1.16.12
package com.example.demo; import com.google.common.collect.Lists; import lombok.AllArgsConstructor; import lombok.Data; 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.Date; import java.util.HashMap; import java.util.List; import java.util.Map; public class GitlabMain { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Data @AllArgsConstructor @NoArgsConstructor static class CommitVo{ private Integer addCount = 0; private Integer deleteCount = 0; private Integer totalCount = 0; } public static void main(String[] args) throws Exception { // GitLabApi gitLabApi = new GitLabApi("http://10.118.128.61","XKDL77z5H8xsWwLPCTnu"); // gitLabApi.getGroupApi().getGroups().forEach(e->{ // System.out.println(e.getId()+"-"+e.getName()); // }); StringBuilder sb = new StringBuilder(gitlab61()).append(gitlab104()).append(gitlab97()); System.out.println(sb.toString()); } public static String gitlab61() throws Exception { ListgroupIds = 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<>(); // 获取连接 // hostUrl:gitLab的域名(或者IP:port) // personalAccessToken:步骤2中的AccessToken GitLabApi gitLabApi = new GitLabApi(url,token); // 条件获取project // nameSpace:项目的命名空间 // projectName:项目名称 groupIds.forEach(e -> { List projects = null; try { projects = gitLabApi.getGroupApi().getProjects(e); } catch (GitLabApiException gitLabApiException) { gitLabApiException.printStackTrace(); } if (!CollectionUtils.isEmpty(projects)) { projects.forEach(p -> { Map map = result.computeIfAbsent(p.getName(), key->new HashMap<>()); //System.out.println(p.getNamespace().getName() + "-" + p.getId() + "-" + p.getName()); List branches = null; try { branches = gitLabApi.getRepositoryApi().getBranches(p.getId()); } catch (GitLabApiException gitLabApiException) { gitLabApiException.printStackTrace(); } if (!CollectionUtils.isEmpty(branches)) { branches.forEach(b -> { try { List commits = gitLabApi.getCommitsApi().getCommits(p.getId(), b.getName(),BEGIN_DATE,END_DATE); if(!CollectionUtils.isEmpty(commits)){ commits.forEach(v->{ try { 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("rn"); }); }); return sb.toString(); } }