From c5b4530bd921fa15e7c666d131bc32b460993468 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Thu, 17 Aug 2023 14:20:27 +0200 Subject: [PATCH] SONAR-20157 New field previousUncompliantValue in api/new_code_periods/list --- .../server/newcodeperiod/ws/ListActionIT.java | 64 +++++++++++++++++++ .../server/newcodeperiod/ws/ListAction.java | 59 ++++++----------- 2 files changed, 85 insertions(+), 38 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/ListActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/ListActionIT.java index f6fc8303537..1e6feffe7f6 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/ListActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/newcodeperiod/ws/ListActionIT.java @@ -72,6 +72,7 @@ import org.sonarqube.ws.NewCodePeriods.ShowWSResponse; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -408,6 +409,69 @@ public class ListActionIT { assertThat(result.getEffectiveValue()).isEqualTo(DateUtils.formatDateTime(analysis.getCreatedAt())); } + @Test + public void list_branch_with_its_own_previous_non_compliant_value() { + ProjectDto project = db.components().insertPublicProject().getProjectDto(); + BranchDto branchWithOwnSettings = db.components().insertProjectBranch(project, branchDto -> branchDto.setKey("OWN_SETTINGS")); + + var ncd = tester.insert(new NewCodePeriodDto() + .setProjectUuid(project.getUuid()) + .setBranchUuid(branchWithOwnSettings.getUuid()) + .setType(NewCodePeriodType.NUMBER_OF_DAYS) + .setValue("90") + .setPreviousNonCompliantValue("120")); + + var updatedAt = ncd.getUpdatedAt(); + + userSession.registerProjects(project); + + ListWSResponse response = ws.newRequest() + .setParam("project", project.getKey()) + .executeProtobuf(ListWSResponse.class); + + assertThat(response).isNotNull(); + assertThat(response.getNewCodePeriodsCount()).isEqualTo(2); // main branch + OWN_SETTINGS branch + assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey) + .contains(DEFAULT_MAIN_BRANCH_NAME, "OWN_SETTINGS"); + + assertThat(response.getNewCodePeriodsList()) + .filteredOn(resp -> !resp.getInherited()) + .hasSize(1) + .extracting(ShowWSResponse::getType, ShowWSResponse::getValue, ShowWSResponse::getPreviousNonCompliantValue, ShowWSResponse::getUpdatedAt) + .containsOnly(tuple(NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "90", "120", updatedAt)); + } + + @Test + public void list_branch_with_inherited_previous_non_compliant_value() { + ProjectDto project = db.components().insertPublicProject().getProjectDto(); + db.components().insertProjectBranch(project, branchDto -> branchDto.setKey("PROJECT_SETTINGS")); + + tester.insert(new NewCodePeriodDto() + .setProjectUuid(project.getUuid()) + .setType(NewCodePeriodType.NUMBER_OF_DAYS) + .setValue("90") + .setPreviousNonCompliantValue("100")); + + userSession.registerProjects(project); + + ListWSResponse response = ws.newRequest() + .setParam("project", project.getKey()) + .executeProtobuf(ListWSResponse.class); + + assertThat(response).isNotNull(); + assertThat(response.getNewCodePeriodsCount()).isEqualTo(2); // main branch + PROJECT_SETTINGS branch + assertThat(response.getNewCodePeriodsList()).extracting(ShowWSResponse::getBranchKey) + .contains(DEFAULT_MAIN_BRANCH_NAME, "PROJECT_SETTINGS"); + + assertThat(response.getNewCodePeriodsList()) + .hasSize(2) + .extracting(ShowWSResponse::getType, ShowWSResponse::getValue, ShowWSResponse::getPreviousNonCompliantValue) + .containsExactlyInAnyOrder( + tuple(NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "90", ""), + tuple(NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "90", "") + ); + } + private void createBranches(ProjectDto project, int numberOfBranches, BranchType branchType) { for (int branchCount = 0; branchCount < numberOfBranches; branchCount++) { String branchKey = String.format("%s_%d", branchType.name(), branchCount); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java index 1bdce819d89..c16fd477c13 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ListAction.java @@ -24,6 +24,7 @@ import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import javax.annotation.Nullable; import org.sonar.api.server.ws.Request; @@ -71,7 +72,7 @@ public class ListAction implements NewCodePeriodsWsAction { @Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction("list") - .setDescription("Lists the " + createHtmlExternalLink(newCodeDefinitionDocumentationUrl, "new code definition") + + .setDescription("Lists the " + createHtmlExternalLink(newCodeDefinitionDocumentationUrl, "new code definition") + " for all branches in a project.
" + "Requires the permission to browse the project") .setSince("8.0") @@ -97,11 +98,11 @@ public class ListAction implements NewCodePeriodsWsAction { List newCodePeriods = newCodePeriodDao.selectAllByProject(dbSession, project.getUuid()); - Map newCodePeriodByBranchUuid = newCodePeriods + Map newCodePeriodByBranchUuid = newCodePeriods .stream() - .collect(Collectors.toMap(NewCodePeriodDto::getBranchUuid, dto -> new InheritedNewCodePeriod(dto, dto.getBranchUuid() == null))); + .collect(Collectors.toMap(NewCodePeriodDto::getBranchUuid, Function.identity())); - InheritedNewCodePeriod projectDefault = newCodePeriodByBranchUuid.getOrDefault(null, getGlobalOrDefault(dbSession)); + NewCodePeriodDto projectDefault = newCodePeriodByBranchUuid.getOrDefault(null, getGlobalOrDefault(dbSession)); Map analysis = newCodePeriods.stream() .filter(newCodePeriodDto -> newCodePeriodDto.getType().equals(NewCodePeriodType.SPECIFIC_ANALYSIS)) @@ -113,44 +114,47 @@ public class ListAction implements NewCodePeriodsWsAction { ListWSResponse.Builder builder = ListWSResponse.newBuilder(); for (BranchDto branch : branches) { - InheritedNewCodePeriod inherited = newCodePeriodByBranchUuid.getOrDefault(branch.getUuid(), projectDefault); + NewCodePeriodDto newCodePeriod = newCodePeriodByBranchUuid.getOrDefault(branch.getUuid(), projectDefault); String effectiveValue = null; //handles specific analysis only - Long analysisDate = analysisUuidDateMap.get(analysis.get(inherited.getUuid())); + Long analysisDate = analysisUuidDateMap.get(analysis.get(newCodePeriod.getUuid())); if (analysisDate != null) { effectiveValue = DateUtils.formatDateTime(analysisDate); } builder.addNewCodePeriods( - build(projectKey, branch.getKey(), inherited.getType(), inherited.getValue(), inherited.inherited, effectiveValue)); + build(projectKey, branch.getKey(), newCodePeriod, effectiveValue)); } writeProtobuf(builder.build(), request, response); } } - private InheritedNewCodePeriod getGlobalOrDefault(DbSession dbSession) { - return newCodePeriodDao.selectGlobal(dbSession) - .map(dto -> new InheritedNewCodePeriod(dto, true)) - .orElse(new InheritedNewCodePeriod(NewCodePeriodDto.defaultInstance(), true)); + private NewCodePeriodDto getGlobalOrDefault(DbSession dbSession) { + return newCodePeriodDao.selectGlobal(dbSession).orElse(NewCodePeriodDto.defaultInstance()); } - private static NewCodePeriods.ShowWSResponse build(String projectKey, String branchKey, NewCodePeriodType newCodePeriodType, - @Nullable String value, boolean inherited, @Nullable String effectiveValue) { + private static NewCodePeriods.ShowWSResponse build(String projectKey, String branchKey, NewCodePeriodDto ncd, @Nullable String effectiveValue) { + boolean inherited = ncd.getBranchUuid() == null; NewCodePeriods.ShowWSResponse.Builder builder = newBuilder() - .setType(convertType(newCodePeriodType)) + .setType(convertType(ncd.getType())) .setInherited(inherited) .setBranchKey(branchKey) - .setProjectKey(projectKey); + .setProjectKey(projectKey) + .setUpdatedAt(ncd.getUpdatedAt()); if (effectiveValue != null) { builder.setEffectiveValue(effectiveValue); } - if (value != null) { - builder.setValue(value); + if (ncd.getValue() != null) { + builder.setValue(ncd.getValue()); + } + + if (!inherited && ncd.getPreviousNonCompliantValue() != null) { + builder.setPreviousNonCompliantValue(ncd.getPreviousNonCompliantValue()); } return builder.build(); @@ -171,25 +175,4 @@ public class ListAction implements NewCodePeriodsWsAction { } } - private static class InheritedNewCodePeriod { - NewCodePeriodDto newCodePeriod; - boolean inherited; - - InheritedNewCodePeriod(NewCodePeriodDto newCodePeriod, boolean inherited) { - this.newCodePeriod = newCodePeriod; - this.inherited = inherited; - } - - NewCodePeriodType getType() { - return newCodePeriod.getType(); - } - - String getValue() { - return newCodePeriod.getValue(); - } - - String getUuid() { - return newCodePeriod.getUuid(); - } - } } -- 2.39.5