Gitlab统计

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class StatisticsResultVO {

private String name;
private String type;
private Long value;

private Long count;
private String date;
/**
* 用于折线图,第一值为X轴坐标,第二个值为Y轴坐标
*/
private List<List<Object>> data;

}

贡献度统计

import cn.hutool.core.date.DateUtil;
import org.gitlab4j.api.Constants;
import org.gitlab4j.api.GitLabApi;
import org.gitlab4j.api.GitLabApiException;

import java.util.*;
import java.util.stream.Collectors;

UserInfoDTO userInfo = userInfoBO.selectByPrimaryKey(SecurityIDUtil.decryptId(securityUserId));
HashMap<String, Long> totalByDay = new HashMap<>();
try (GitLabApi gitLabApi = new GitLabApi(hostUrl, apiToken)) {
gitLabApi.getEventsApi()
.getUserEventsStream(userInfo.getGitUserId(), Constants.ActionType.PUSHED, null, endDate, startDate, null)
.forEach(event -> {
String dayDate = DateUtil.formatDate(event.getCreatedAt());
totalByDay.put(dayDate, totalByDay.getOrDefault(dayDate, 0L) + 1);
});
} catch (GitLabApiException e) {
throw new BizException("GitlabId: {}, 获取事件失败", userInfo.getGitUserId());
}

List<StatisticsResultVO> heapMapData = new ArrayList<>();
totalByDay.forEach((date, events) ->
heapMapData.add(StatisticsResultVO.builder().date(date).count(events).build()));

return heapMapData.stream()
.sorted(Comparator.comparing(o -> DateUtil.parseDate(o.getDate())))
.collect(Collectors.toList());

提交次数 / 编辑行数 统计

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GitlabUserStatisticsVO {

private String nickname;
private String email;
private Date commitDate;
private Long totalAddLineNumber = 0L;
private Long totalDeleteLineNumber = 0L;
/**
* 累计变更行数
*/
private Long totalEditLineNumber = 0L;
/**
* 累计提交次数
*/
private Long totalCommitNumber = 0L;

public void addLine(long additions) {
this.totalAddLineNumber += additions;
}

public void deleteLine(long deletions) {
this.totalDeleteLineNumber += deletions;
}

public void totalLine(long total) {
this.totalEditLineNumber += total;
}

public void commitAdd() {
this.totalCommitNumber += 1;
}

}

Date endDate = new Date();
Date startDate = DateUtil.offset(endDate, DateField.YEAR, -1).toJdkDate();

Map<String, Map<String, GitlabUserStatisticsVO>> statisticsMap = new HashMap<>();

String hostUrl = "http://gitlab.xxx.com";

String apiToken = "MdAja379_NrfR-123456";

try (GitLabApi gitLabApi = new GitLabApi(hostUrl, apiToken)) {
List<Project> projects = gitLabApi.getProjectApi().getProjects();
projects.forEach(project -> {
// 统计代码量
try {
gitLabApi.getCommitsApi()
.getCommitsStream(project, "master", startDate, endDate, null, true, true, null)
.forEach(commit -> {
String email = commit.getCommitterEmail();
UserInfoDTO userInfoDTO = userInfoByEmail.get(email);
if (CommonUtil.isNotEmpty(userInfoDTO)) {
String nickname = userInfoDTO.getNickname();
String monthDate = DateUtil.format(commit.getCommittedDate(), "yyyy-MM");

Map<String, GitlabUserStatisticsVO> gitUserStatisticsByNickname;
if (statisticsMap.containsKey(monthDate)) {
gitUserStatisticsByNickname = statisticsMap.get(monthDate);
} else {
gitUserStatisticsByNickname = new HashMap<>();
statisticsMap.put(monthDate, gitUserStatisticsByNickname);
}

final GitlabUserStatisticsVO gitlabUserStatisticsVO;
if (gitUserStatisticsByNickname.containsKey(nickname)) {
gitlabUserStatisticsVO = gitUserStatisticsByNickname.get(nickname);
} else {
gitlabUserStatisticsVO = new GitlabUserStatisticsVO();
gitlabUserStatisticsVO.setEmail(email);
gitlabUserStatisticsVO.setNickname(nickname);
gitUserStatisticsByNickname.put(nickname, gitlabUserStatisticsVO);
}

// 累计变更行数
Optional.ofNullable(commit.getStats())
.ifPresent(commitStats -> {
gitlabUserStatisticsVO.addLine(commitStats.getAdditions());
gitlabUserStatisticsVO.deleteLine(commitStats.getDeletions());
gitlabUserStatisticsVO.totalLine(commitStats.getTotal());
});
// 累计提交次数
gitlabUserStatisticsVO.commitAdd();
}
});
} catch (GitLabApiException e) {
throw new BizException("{}项目获取commits失败", project.getName());
}
});
}