• GitLab统计代码提交行数


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

    1. <dependency>
    2. <groupId>org.gitlab4jgroupId>
    3. <artifactId>gitlab4j-apiartifactId>
    4. <version>4.14.30version>
    5. dependency>
    6. <dependency>
    7. <groupId>org.glassfish.jersey.coregroupId>
    8. <artifactId>jersey-clientartifactId>
    9. <version>2.30.1version>
    10. dependency>
    11. <dependency>
    12. <groupId>org.glassfish.jersey.coregroupId>
    13. <artifactId>jersey-commonartifactId>
    14. <version>2.30.1version>
    15. dependency>
    16. <dependency>
    17. <groupId>com.google.guavagroupId>
    18. <artifactId>guavaartifactId>
    19. <version>21.0version>
    20. dependency>
    21. <dependency>
    22. <groupId>org.projectlombokgroupId>
    23. <artifactId>lombokartifactId>
    24. <version>1.16.12version>
    25. dependency>
    1. package com.example.demo;
    2. import com.google.common.collect.Lists;
    3. import lombok.AllArgsConstructor;
    4. import lombok.Data;
    5. import lombok.NoArgsConstructor;
    6. import org.gitlab4j.api.GitLabApi;
    7. import org.gitlab4j.api.GitLabApiException;
    8. import org.gitlab4j.api.models.Branch;
    9. import org.gitlab4j.api.models.Commit;
    10. import org.gitlab4j.api.models.Project;
    11. import org.springframework.util.CollectionUtils;
    12. import java.text.ParseException;
    13. import java.text.SimpleDateFormat;
    14. import java.util.Date;
    15. import java.util.HashMap;
    16. import java.util.List;
    17. import java.util.Map;
    18. public class GitlabMain {
    19. private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    20. @Data
    21. @AllArgsConstructor
    22. @NoArgsConstructor
    23. static class CommitVo{
    24. private Integer addCount = 0;
    25. private Integer deleteCount = 0;
    26. private Integer totalCount = 0;
    27. }
    28. /**
    29. *
    30. * @throws GitLabApiException
    31. */
    32. public static void main(String[] args) throws Exception {
    33. // GitLabApi gitLabApi = new GitLabApi("http://10.118.128.61","XKDL77z5H8xsWwLPCTnu");
    34. // gitLabApi.getGroupApi().getGroups().forEach(e->{
    35. // System.out.println(e.getId()+"-"+e.getName());
    36. // });
    37. StringBuilder sb = new StringBuilder(gitlab61()).append(gitlab104()).append(gitlab97());
    38. System.out.println(sb.toString());
    39. }
    40. /**
    41. * 2-DC
    42. * @throws GitLabApiException
    43. */
    44. public static String gitlab61() throws Exception {
    45. List groupIds = Lists.newArrayList(2);
    46. return gitlab("xxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
    47. }
    48. /**
    49. * 3-dfh
    50. * @throws GitLabApiException
    51. */
    52. public static String gitlab97() throws Exception {
    53. List groupIds = Lists.newArrayList(3);
    54. return gitlab("xxxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
    55. }
    56. /**
    57. * #13 dfh
    58. * #34 dfham-smart-research
    59. * #59 nibp
    60. * #58 pms
    61. */
    62. public static String gitlab104() throws Exception{
    63. List groupIds = Lists.newArrayList(13, 34, 59, 58);
    64. return gitlab("xxxxx","xxxxx","2022-07-01 00:00:00","2022-07-31 23:59:59",groupIds);
    65. }
    66. public static String gitlab(String url,String token,String startDate,String endDate,List groupIds) throws ParseException {
    67. Date BEGIN_DATE = sdf.parse(startDate);
    68. Date END_DATE = sdf.parse(endDate);
    69. Map> result = new HashMap<>();
    70. // 获取连接
    71. // hostUrl:gitLab的域名(或者IP:port)
    72. // personalAccessToken:步骤2中的AccessToken
    73. GitLabApi gitLabApi = new GitLabApi(url,token);
    74. // 条件获取project
    75. // nameSpace:项目的命名空间
    76. // projectName:项目名称
    77. groupIds.forEach(e -> {
    78. List projects = null;
    79. try {
    80. projects = gitLabApi.getGroupApi().getProjects(e);
    81. } catch (GitLabApiException gitLabApiException) {
    82. gitLabApiException.printStackTrace();
    83. }
    84. if (!CollectionUtils.isEmpty(projects)) {
    85. projects.forEach(p -> {
    86. Map map = result.computeIfAbsent(p.getName(), key->new HashMap<>());
    87. //System.out.println(p.getNamespace().getName() + "-" + p.getId() + "-" + p.getName());
    88. List branches = null;
    89. try {
    90. branches = gitLabApi.getRepositoryApi().getBranches(p.getId());
    91. } catch (GitLabApiException gitLabApiException) {
    92. gitLabApiException.printStackTrace();
    93. }
    94. if (!CollectionUtils.isEmpty(branches)) {
    95. branches.forEach(b -> {
    96. try {
    97. List commits = gitLabApi.getCommitsApi().getCommits(p.getId(), b.getName(),BEGIN_DATE,END_DATE);
    98. if(!CollectionUtils.isEmpty(commits)){
    99. commits.forEach(v->{
    100. try {
    101. v = gitLabApi.getCommitsApi().getCommit(p.getId(), v.getShortId());
    102. } catch (GitLabApiException gitLabApiException) {
    103. gitLabApiException.printStackTrace();
    104. }
    105. CommitVo vo = map.computeIfAbsent(v.getAuthorName() + "-" + v.getAuthorEmail(),key-> new CommitVo());
    106. vo.setAddCount(vo.getAddCount()+v.getStats().getAdditions());
    107. vo.setDeleteCount(vo.getDeleteCount()+v.getStats().getDeletions());
    108. vo.setTotalCount(vo.getTotalCount()+v.getStats().getTotal());
    109. });
    110. }
    111. } catch (GitLabApiException ioException) {
    112. ioException.printStackTrace();
    113. }
    114. });
    115. }
    116. });
    117. }
    118. });
    119. StringBuilder sb = new StringBuilder();
    120. result.entrySet().forEach(e->{
    121. e.getValue().entrySet().forEach(ee->{
    122. sb.append(e.getKey() + "\t" + ee.getKey() + "\t" + ee.getValue().getAddCount() + "\t" + ee.getValue().getDeleteCount() + "\t" + ee.getValue().getTotalCount() ).append("\r\n");
    123. });
    124. });
    125. return sb.toString();
    126. }
    127. }

  • 相关阅读:
    Spring Security是什么?(二)
    数据库与缓存更新一致性四种策略
    提升APP的用户体验的方法
    day24-selenium基本操作
    vuedraggable遇到的问题
    【Linux】守护进程
    Java阻塞队列中的异类,SynchronousQueue底层实现原理剖析
    Redeis缓存查询基于元注解与AOP结合使用——不过时的优雅
    【Linux-网络编程】
    1.mysql安装及基础
  • 原文地址:https://blog.csdn.net/z69183787/article/details/126249538