栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Java

GitLab统计代码提交行数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

GitLab统计代码提交行数

用java统计git项目的每个用户变更行数和提交次数--gitlab4j-api - 灰信网(软件开发博客聚合) (freesion.com)https://www.freesion.com/article/6269791470/


			org.gitlab4j
			gitlab4j-api
			4.14.30
		

		
			org.glassfish.jersey.core
			jersey-client
			2.30.1
		

		
			org.glassfish.jersey.core
			jersey-common
			2.30.1
		

		
			com.google.guava
			guava
			21.0
		

		
			org.projectlombok
			lombok
			1.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 {
        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<>();
        // 获取连接
        // 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();
    }


}

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1041201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号