Skip to content

Commit

Permalink
ADM-851:[backend] refactor: use other way to get page number
Browse files Browse the repository at this point in the history
  • Loading branch information
BoBoDai committed Mar 15, 2024
1 parent cbbba0d commit 988e1f5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 35 deletions.
65 changes: 32 additions & 33 deletions backend/src/main/java/heartbeat/service/board/jira/JiraService.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,44 @@ private Map<String, JsonElement> getCustomFieldMap(JsonElement element, Map<Stri
resultMap.forEach((customFieldKey, customFieldValue) -> {
if (jsonElement.has(customFieldKey)) {
JsonElement fieldValue = jsonElement.get(customFieldKey);
switch (customFieldValue) {
case "Sprint" -> Optional.ofNullable(getSprint(fieldValue))
.ifPresentOrElse(it -> sprintMap.put(element.getAsJsonObject().get("key").getAsString(), it),
() -> {
});
case "Story point estimate" -> fieldValue = setStoryPointEstimate(fieldValue);
case "Flagged" -> fieldValue = setFlagged(fieldValue);
default -> {
}
}
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);
}
}
}
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");
}
}
}
default -> {
}
}
return fieldValue;
}

private Sprint getSprint(JsonElement fieldValue) {
if (!fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
JsonArray jsonArray = fieldValue.getAsJsonArray();
Expand All @@ -489,29 +511,6 @@ private Sprint getSprint(JsonElement fieldValue) {
return null;
}

private JsonElement setStoryPointEstimate(JsonElement fieldValue) {
if (!fieldValue.isJsonNull() && fieldValue.isJsonPrimitive()) {
JsonPrimitive jsonPrimitive = fieldValue.getAsJsonPrimitive();
if (jsonPrimitive.isNumber()) {
Number numberValue = jsonPrimitive.getAsNumber();
double doubleValue = numberValue.doubleValue();
return new JsonPrimitive(doubleValue);
}
}
return fieldValue;
}

private JsonElement setFlagged(JsonElement fieldValue) {
if (!fieldValue.isJsonNull() && fieldValue.isJsonArray()) {
JsonArray jsonArray = fieldValue.getAsJsonArray();
if (!jsonArray.isJsonNull() && !jsonArray.isEmpty()) {
JsonElement targetField = jsonArray.get(jsonArray.size() - 1);
return targetField.getAsJsonObject().get("value");
}
}
return fieldValue;
}

private String parseJiraJql(BoardType boardType, List<String> doneColumns, BoardRequestParam boardRequestParam) {
if (boardType == BoardType.JIRA) {
return String.format("status in ('%s') AND status changed during (%s, %s)", String.join("','", doneColumns),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ private int parseTotalPage(@Nullable List<String> linkHeader) {
return 1;
}
String beforeLastRel = lastLink.substring(0, lastIndex);
Matcher matcher = Pattern.compile("page=(\\d+)[^>]*>").matcher(beforeLastRel);
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 {
}
else {
return 1;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class CachePageServiceTest {
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?page=3&per_page=100>; rel="last"
""";

public static final String NONE_PAGE_HEADER = """
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?pages=1&per_page=100>; rel="first",
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?pages=1&per_page=100>; rel="prev",
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?per_page=100&pages=2>; rel="next",
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?pages=3&per_page=100>; rel="last"
""";

public static final String NONE_TOTAL_PAGE_HEADER = """
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?page=1&per_page=100>; rel="first",
<https://api.buildkite.com/v2/organizations/test_org_id/pipelines/test_pipeline_id/builds?page=1&per_page=100>; rel="prev",
Expand Down Expand Up @@ -131,4 +138,27 @@ void shouldReturnPageStepsInfoDtoWhenFetchPageStepsInfoSuccessGivenExistButNotMa
assertThat(pageStepsInfoDto.getTotalPage()).isEqualTo(1);
}

@Test
void shouldReturnPageStepsInfoDtoWhenFetchPageStepsInfoSuccessGivenExistButNotMatchedPageLinkHeader() {
List<String> linkHeader = new ArrayList<>();
linkHeader.add(NONE_PAGE_HEADER);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.addAll(HttpHeaders.LINK, linkHeader);
List<BuildKiteBuildInfo> buildKiteBuildInfoList = new ArrayList<>();
BuildKiteJob testJob = BuildKiteJob.builder().name(TEST_JOB_NAME).build();
buildKiteBuildInfoList.add(BuildKiteBuildInfo.builder().jobs(List.of(testJob)).build());
ResponseEntity<List<BuildKiteBuildInfo>> responseEntity = new ResponseEntity<>(buildKiteBuildInfoList,
httpHeaders, HttpStatus.OK);
when(buildKiteFeignClient.getPipelineSteps(anyString(), anyString(), anyString(), anyString(), anyString(),
anyString(), anyString(), any()))
.thenReturn(responseEntity);

PageStepsInfoDto pageStepsInfoDto = cachePageService.fetchPageStepsInfo(MOCK_TOKEN, TEST_ORG_ID,
TEST_PIPELINE_ID, "1", "100", MOCK_START_TIME, MOCK_END_TIME, List.of("main"));

assertNotNull(pageStepsInfoDto);
assertThat(pageStepsInfoDto.getFirstPageStepsInfo()).isEqualTo(responseEntity.getBody());
assertThat(pageStepsInfoDto.getTotalPage()).isEqualTo(1);
}

}

0 comments on commit 988e1f5

Please sign in to comment.