Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adm 851[Backend] fix Snoar refactor issues #1181

Merged
merged 8 commits into from
Mar 15, 2024
74 changes: 45 additions & 29 deletions backend/src/main/java/heartbeat/service/board/jira/JiraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,7 @@ private JiraCardWithFields getAllCards(BoardType boardType, URI baseUrl, BoardRe

private AllCardsResponseDTO formatAllCards(String allCardResponse, List<TargetField> targetFields,
List<TargetField> overrideFields) {
Gson gson = new Gson();
AllCardsResponseDTO allCardsResponseDTO = gson.fromJson(allCardResponse, AllCardsResponseDTO.class);
AllCardsResponseDTO allCardsResponseDTO = new Gson().fromJson(allCardResponse, AllCardsResponseDTO.class);
List<JiraCard> jiraCards = allCardsResponseDTO.getIssues();

JsonArray elements = JsonParser.parseString(allCardResponse).getAsJsonObject().get("issues").getAsJsonArray();
Expand All @@ -440,59 +439,76 @@ private AllCardsResponseDTO formatAllCards(String allCardResponse, List<TargetFi
jiraCards.get(index).getFields().setStoryPoints(storyPointList.get(index));
}
}
customFieldMapList.add(getCustomfieldMap(gson, sprintMap, resultMap, element, jsonElement));
customFieldMapList.add(getCustomFieldMap(element, resultMap, jsonElement, sprintMap));
}
for (int index = 0; index < customFieldMapList.size(); index++) {
jiraCards.get(index).getFields().setCustomFields(customFieldMapList.get(index));
}

for (JiraCard jiraCard : jiraCards) {
String key = jiraCard.getKey();
jiraCard.getFields().setSprint(sprintMap.get(key));
}
jiraCards.forEach(jiraCard -> {
Sprint sprint = sprintMap.get(jiraCard.getKey());
jiraCard.getFields().setSprint(sprint);
});
return allCardsResponseDTO;
}

private static Map<String, JsonElement> getCustomfieldMap(Gson gson, Map<String, Sprint> sprintMap,
Map<String, String> resultMap, JsonElement element, JsonObject jsonElement) {
private Map<String, JsonElement> getCustomFieldMap(JsonElement element, Map<String, String> resultMap,
JsonObject jsonElement, Map<String, Sprint> sprintMap) {
Map<String, JsonElement> customFieldMap = new HashMap<>();
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
String customFieldKey = entry.getKey();
String customFieldValue = entry.getValue();
resultMap.forEach((customFieldKey, customFieldValue) -> {
if (jsonElement.has(customFieldKey)) {
JsonElement fieldValue = jsonElement.get(customFieldKey);
if (customFieldValue.equals("Sprint") && !fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
JsonArray jsonArray = fieldValue.getAsJsonArray();
if (!jsonArray.isJsonNull() && !jsonArray.isEmpty()) {
Type listType = new TypeToken<List<Sprint>>() {
}.getType();
List<Sprint> sprints = gson.fromJson(jsonArray, listType);
sprints.sort(Comparator.comparing(Sprint::getCompleteDate,
Comparator.nullsLast(Comparator.comparing(ZonedDateTime::parse))));
sprintMap.put(element.getAsJsonObject().get("key").getAsString(),
sprints.get(sprints.size() - 1));
}
}
else if (customFieldValue.equals("Story point estimate") && !fieldValue.isJsonNull()
&& fieldValue.isJsonPrimitive()) {
fieldValue = mapFieldValue(element, sprintMap, customFieldValue, fieldValue);
customFieldMap.put(customFieldKey, fieldValue);
}
});
return customFieldMap;
}

private JsonElement mapFieldValue(JsonElement element, Map<String, Sprint> sprintMap, String customFieldValue,
JsonElement fieldValue) {
switch (customFieldValue) {
case "Sprint" -> Optional.ofNullable(getSprint(fieldValue))
.ifPresentOrElse(it -> sprintMap.put(element.getAsJsonObject().get("key").getAsString(), it), () -> {
});
case "Story point estimate" -> {
if (!fieldValue.isJsonNull() && fieldValue.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = fieldValue.getAsJsonPrimitive();
if (jsonPrimitive.isNumber()) {
Number numberValue = jsonPrimitive.getAsNumber();
double doubleValue = numberValue.doubleValue();
fieldValue = new JsonPrimitive(doubleValue);
}
}
else if (customFieldValue.equals("Flagged") && !fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
}
case "Flagged" -> {
if (!fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
JsonArray jsonArray = fieldValue.getAsJsonArray();
if (!jsonArray.isJsonNull() && !jsonArray.isEmpty()) {
JsonElement targetField = jsonArray.get(jsonArray.size() - 1);
fieldValue = targetField.getAsJsonObject().get("value");
}
}
customFieldMap.put(customFieldKey, fieldValue);
}
default -> {
}
}
return customFieldMap;
return fieldValue;
}

private Sprint getSprint(JsonElement fieldValue) {
if (!fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
JsonArray jsonArray = fieldValue.getAsJsonArray();
if (!jsonArray.isJsonNull() && !jsonArray.isEmpty()) {
Type listType = new TypeToken<List<Sprint>>() {
}.getType();
List<Sprint> sprints = new Gson().fromJson(jsonArray, listType);
sprints.sort(Comparator.comparing(Sprint::getCompleteDate,
Comparator.nullsLast(Comparator.comparing(ZonedDateTime::parse))));
return sprints.get(sprints.size() - 1);
}
}
return null;
}

private String parseJiraJql(BoardType boardType, List<String> doneColumns, BoardRequestParam boardRequestParam) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@ private int parseTotalPage(@Nullable List<String> linkHeader) {
return 1;
}
String lastLink = linkHeader.stream().map(link -> link.replaceAll("per_page=\\d+", "")).findFirst().orElse("");
Matcher matcher = Pattern.compile("page=(\\d+)[^>]*>.*?rel=\"last\"").matcher(lastLink);
if (matcher.find()) {
return Integer.parseInt(matcher.group(1));
int lastIndex = lastLink.indexOf("rel=\"last\"");
if (lastIndex == -1) {
return 1;
}
String beforeLastRel = lastLink.substring(0, lastIndex);
Matcher matcher = Pattern.compile("page=(\\d+)").matcher(beforeLastRel);

String lastNumber = null;
while (matcher.find()) {
lastNumber = matcher.group(1); // 每次找到匹配项时更新lastNumber
}
if (lastNumber != null) {
return Integer.parseInt(lastNumber);
}
else {
return 1;
}
return 1;
}

}
132 changes: 68 additions & 64 deletions backend/src/main/java/heartbeat/service/report/CSVFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,43 +101,7 @@ public void convertPipelineDataToCSV(List<PipelineCSVInfo> leadTimeData, String

csvWriter.writeNext(headers);

for (PipelineCSVInfo csvInfo : leadTimeData) {
String committerName = null;
String commitDate = null;
String creatorName = null;
String organization = csvInfo.getOrganizationName();
String pipelineName = csvInfo.getPipeLineName();
String stepName = csvInfo.getStepName();
String valid = String.valueOf(csvInfo.getValid()).toLowerCase();
String buildNumber = String.valueOf(csvInfo.getBuildInfo().getNumber());
String state = csvInfo.getPiplineStatus().equals(CANCELED_STATUS) ? CANCELED_STATUS
: csvInfo.getDeployInfo().getState();
String branch = csvInfo.getBuildInfo().getBranch();
if (csvInfo.getCommitInfo() != null) {
committerName = csvInfo.getCommitInfo().getCommit().getAuthor().getName();
commitDate = csvInfo.getCommitInfo().getCommit().getAuthor().getDate();
}

if (csvInfo.getBuildInfo().getCreator() != null
&& csvInfo.getBuildInfo().getCreator().getName() != null) {
creatorName = csvInfo.getBuildInfo().getCreator().getName();
}

LeadTimeInfo leadTimeInfo = csvInfo.getLeadTimeInfo();
String firstCommitTimeInPr = leadTimeInfo.getFirstCommitTimeInPr();
String prCreatedTime = leadTimeInfo.getPrCreatedTime();
String prMergedTime = leadTimeInfo.getPrMergedTime();
String jobFinishTime = csvInfo.getDeployInfo().getJobFinishTime();
String totalTime = leadTimeInfo.getTotalTime();
String prLeadTime = leadTimeInfo.getPrLeadTime();
String pipelineLeadTime = leadTimeInfo.getPipelineLeadTime();

String[] rowData = { organization, pipelineName, stepName, valid, buildNumber, committerName,
creatorName, firstCommitTimeInPr, commitDate, prCreatedTime, prMergedTime, jobFinishTime,
totalTime, prLeadTime, pipelineLeadTime, state, branch };

csvWriter.writeNext(rowData);
}
leadTimeData.stream().map(this::getRowData).forEach(csvWriter::writeNext);
}
catch (IOException e) {
log.error("Failed to write pipeline file", e);
Expand All @@ -149,6 +113,42 @@ public void convertPipelineDataToCSV(List<PipelineCSVInfo> leadTimeData, String
}
}

private String[] getRowData(PipelineCSVInfo csvInfo) {
String committerName = null;
String commitDate = null;
if (csvInfo.getCommitInfo() != null) {
committerName = csvInfo.getCommitInfo().getCommit().getAuthor().getName();
commitDate = csvInfo.getCommitInfo().getCommit().getAuthor().getDate();
}

String creatorName = null;
if (csvInfo.getBuildInfo().getCreator() != null && csvInfo.getBuildInfo().getCreator().getName() != null) {
creatorName = csvInfo.getBuildInfo().getCreator().getName();
}

String organization = csvInfo.getOrganizationName();
String pipelineName = csvInfo.getPipeLineName();
String stepName = csvInfo.getStepName();
String valid = String.valueOf(csvInfo.getValid()).toLowerCase();
String buildNumber = String.valueOf(csvInfo.getBuildInfo().getNumber());
String state = csvInfo.getPiplineStatus().equals(CANCELED_STATUS) ? CANCELED_STATUS
: csvInfo.getDeployInfo().getState();
String branch = csvInfo.getBuildInfo().getBranch();

LeadTimeInfo leadTimeInfo = csvInfo.getLeadTimeInfo();
String firstCommitTimeInPr = leadTimeInfo.getFirstCommitTimeInPr();
String prCreatedTime = leadTimeInfo.getPrCreatedTime();
String prMergedTime = leadTimeInfo.getPrMergedTime();
String jobFinishTime = csvInfo.getDeployInfo().getJobFinishTime();
String totalTime = leadTimeInfo.getTotalTime();
String prLeadTime = leadTimeInfo.getPrLeadTime();
String pipelineLeadTime = leadTimeInfo.getPipelineLeadTime();

return new String[] { organization, pipelineName, stepName, valid, buildNumber, committerName, creatorName,
firstCommitTimeInPr, commitDate, prCreatedTime, prMergedTime, jobFinishTime, totalTime, prLeadTime,
pipelineLeadTime, state, branch };
}

public InputStreamResource getDataFromCSV(ReportType reportDataType, long csvTimeStamp) {
return switch (reportDataType) {
case METRIC -> readStringFromCsvFile(
Expand Down Expand Up @@ -289,33 +289,7 @@ private String[] getFixedDataPerRow(JiraCardDTO cardDTO, int columnCount) {
rowData[0] = cardDTO.getBaseInfo().getKey();

if (cardDTO.getBaseInfo().getFields() != null) {
rowData[1] = cardDTO.getBaseInfo().getFields().getSummary();
rowData[2] = cardDTO.getBaseInfo().getFields().getIssuetype().getName();
rowData[3] = cardDTO.getBaseInfo().getFields().getStatus().getName();
if (cardDTO.getBaseInfo().getFields().getLastStatusChangeDate() != null) {
rowData[4] = convertToSimpleISOFormat(cardDTO.getBaseInfo().getFields().getLastStatusChangeDate());
}
rowData[5] = String.valueOf(cardDTO.getBaseInfo().getFields().getStoryPoints());
if (cardDTO.getBaseInfo().getFields().getAssignee() != null) {
rowData[6] = cardDTO.getBaseInfo().getFields().getAssignee().getDisplayName();
}
if (cardDTO.getBaseInfo().getFields().getReporter() != null) {
rowData[7] = cardDTO.getBaseInfo().getFields().getReporter().getDisplayName();
}

rowData[8] = cardDTO.getBaseInfo().getFields().getProject().getKey();
rowData[9] = cardDTO.getBaseInfo().getFields().getProject().getName();
rowData[10] = cardDTO.getBaseInfo().getFields().getPriority().getName();

if (cardDTO.getBaseInfo().getFields().getParent() != null) {
rowData[11] = cardDTO.getBaseInfo().getFields().getParent().getFields().getSummary();
}

if (cardDTO.getBaseInfo().getFields().getSprint() != null) {
rowData[12] = cardDTO.getBaseInfo().getFields().getSprint().getName();
}

rowData[13] = String.join(",", cardDTO.getBaseInfo().getFields().getLabels());
fixDataWithFields(cardDTO, rowData);
}

}
Expand All @@ -332,6 +306,36 @@ private String[] getFixedDataPerRow(JiraCardDTO cardDTO, int columnCount) {
return rowData;
}

private void fixDataWithFields(JiraCardDTO cardDTO, String[] rowData) {
rowData[1] = cardDTO.getBaseInfo().getFields().getSummary();
rowData[2] = cardDTO.getBaseInfo().getFields().getIssuetype().getName();
rowData[3] = cardDTO.getBaseInfo().getFields().getStatus().getName();
if (cardDTO.getBaseInfo().getFields().getLastStatusChangeDate() != null) {
rowData[4] = convertToSimpleISOFormat(cardDTO.getBaseInfo().getFields().getLastStatusChangeDate());
}
rowData[5] = String.valueOf(cardDTO.getBaseInfo().getFields().getStoryPoints());
if (cardDTO.getBaseInfo().getFields().getAssignee() != null) {
rowData[6] = cardDTO.getBaseInfo().getFields().getAssignee().getDisplayName();
}
if (cardDTO.getBaseInfo().getFields().getReporter() != null) {
rowData[7] = cardDTO.getBaseInfo().getFields().getReporter().getDisplayName();
}

rowData[8] = cardDTO.getBaseInfo().getFields().getProject().getKey();
rowData[9] = cardDTO.getBaseInfo().getFields().getProject().getName();
rowData[10] = cardDTO.getBaseInfo().getFields().getPriority().getName();

if (cardDTO.getBaseInfo().getFields().getParent() != null) {
rowData[11] = cardDTO.getBaseInfo().getFields().getParent().getFields().getSummary();
}

if (cardDTO.getBaseInfo().getFields().getSprint() != null) {
rowData[12] = cardDTO.getBaseInfo().getFields().getSprint().getName();
}

rowData[13] = String.join(",", cardDTO.getBaseInfo().getFields().getLabels());
}

private String getExtraDataPerRow(Object object, BoardCSVConfig extraField) {
Map<String, JsonElement> elementMap = (Map<String, JsonElement>) object;
if (elementMap == null) {
Expand Down
Loading
Loading