GitLab统计代码提交行数

Posted OkidoGreen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GitLab统计代码提交行数相关的知识,希望对你有一定的参考价值。

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

<dependency>
			<groupId>org.gitlab4j</groupId>
			<artifactId>gitlab4j-api</artifactId>
			<version>4.14.30</version>
		</dependency>

		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-client</artifactId>
			<version>2.30.1</version>
		</dependency>

		<dependency>
			<groupId>org.glassfish.jersey.core</groupId>
			<artifactId>jersey-common</artifactId>
			<version>2.30.1</version>
		</dependency>

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>21.0</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.12</version>
		</dependency>
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;
    

    /**
     *
     * @throws GitLabApiException
     */
    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());
    

    /**
     * 2-DC
     * @throws GitLabApiException
     */
    public static String gitlab61() throws Exception 
        List<Integer> groupIds = Lists.newArrayList(2);
        return gitlab("xxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
    

    /**
     * 3-dfh
     * @throws GitLabApiException
     */
    public static String gitlab97() throws Exception 
        List<Integer> groupIds = Lists.newArrayList(3);
        return gitlab("xxxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
    

    /**
     * #13 dfh
     * #34 dfham-smart-research
     * #59 nibp
     * #58 pms
     */
    public static String gitlab104() throws Exception
        List<Integer> 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<Integer> groupIds) throws ParseException 
        Date BEGIN_DATE = sdf.parse(startDate);
        Date END_DATE = sdf.parse(endDate);
        Map<String,Map<String,CommitVo>> result = new HashMap<>();
        // 获取连接
        // hostUrl:gitLab的域名(或者IP:port)
        // personalAccessToken:步骤2中的AccessToken
        GitLabApi gitLabApi = new GitLabApi(url,token);
        // 条件获取project
        // nameSpace:项目的命名空间
        // projectName:项目名称
        groupIds.forEach(e -> 
            List<Project> projects = null;
            try 
                projects = gitLabApi.getGroupApi().getProjects(e);
             catch (GitLabApiException gitLabApiException) 
                gitLabApiException.printStackTrace();
            
            if (!CollectionUtils.isEmpty(projects)) 
                projects.forEach(p -> 
                    Map<String,CommitVo> map = result.computeIfAbsent(p.getName(), key->new HashMap<>());
                    //System.out.println(p.getNamespace().getName() + "-" + p.getId() + "-" + p.getName());
                    List<Branch> branches = null;
                    try 
                        branches = gitLabApi.getRepositoryApi().getBranches(p.getId());
                     catch (GitLabApiException gitLabApiException) 
                        gitLabApiException.printStackTrace();
                    
                    if (!CollectionUtils.isEmpty(branches)) 
                        branches.forEach(b -> 
                            try 
                                List<Commit> 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("\\r\\n");
            );
        );

        return sb.toString();
    



以上是关于GitLab统计代码提交行数的主要内容,如果未能解决你的问题,请参考以下文章

python-gitlab 统计代码行数

git 查看代码提交行数

git 怎么查询提交了代码行数

撤回提交到 gitlab 上的代码

如何查看代码行数 intellij

(转)通过gitlab统计git提交的代码量