diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-05-12 14:14:38 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-06-11 20:04:56 +0000 |
commit | 3352e9f378dfb2929a19d362f4b5ae21bd33f0db (patch) | |
tree | 561d899ce84e2faaffc8bfca6718c1158443c613 /server/sonar-webserver-webapi | |
parent | 46a49f0b5ef205f5632b44dc07221eed79ec803d (diff) | |
download | sonarqube-3352e9f378dfb2929a19d362f4b5ae21bd33f0db.tar.gz sonarqube-3352e9f378dfb2929a19d362f4b5ae21bd33f0db.zip |
SONAR-13390 SONAR-13391 New Code Reference Branch
Diffstat (limited to 'server/sonar-webserver-webapi')
7 files changed, 91 insertions, 40 deletions
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java index 541a0302568..4f0214825a2 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/SnapshotDtoToWsPeriod.java @@ -32,15 +32,11 @@ class SnapshotDtoToWsPeriod { } static Optional<Measures.Period> snapshotToWsPeriods(@Nullable SnapshotDto snapshot) { - if (snapshot == null) { + if (snapshot == null || snapshot.getPeriodMode() == null) { return Optional.empty(); } - if (snapshot.getPeriodDate() != null) { - return Optional.of(snapshotDtoToWsPeriod(snapshot)); - } - - return Optional.empty(); + return Optional.of(snapshotDtoToWsPeriod(snapshot)); } private static Measures.Period snapshotDtoToWsPeriod(SnapshotDto snapshot) { 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 161afe2d542..13326b68047 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 @@ -157,6 +157,8 @@ public class ListAction implements NewCodePeriodsWsAction { return NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION; case SPECIFIC_ANALYSIS: return NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS; + case REFERENCE_BRANCH: + return NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH; default: throw new IllegalStateException("Unexpected type: " + type); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java index b79f029d3a0..c82efac609b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/SetAction.java @@ -47,6 +47,7 @@ import static com.google.common.base.Preconditions.checkArgument; import static java.lang.String.format; import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS; import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION; +import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH; import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS; public class SetAction implements NewCodePeriodsWsAction { @@ -60,8 +61,8 @@ public class SetAction implements NewCodePeriodsWsAction { private static final String END_ITEM_LIST = "</li>"; private static final Set<NewCodePeriodType> OVERALL_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS); - private static final Set<NewCodePeriodType> PROJECT_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS); - private static final Set<NewCodePeriodType> BRANCH_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, SPECIFIC_ANALYSIS); + private static final Set<NewCodePeriodType> PROJECT_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH); + private static final Set<NewCodePeriodType> BRANCH_TYPES = EnumSet.of(PREVIOUS_VERSION, NUMBER_OF_DAYS, SPECIFIC_ANALYSIS, REFERENCE_BRANCH); private final DbClient dbClient; private final UserSession userSession; @@ -105,7 +106,8 @@ public class SetAction implements NewCodePeriodsWsAction { BEGIN_LIST + BEGIN_ITEM_LIST + SPECIFIC_ANALYSIS.name() + " - can be set at branch level only" + END_ITEM_LIST + BEGIN_ITEM_LIST + PREVIOUS_VERSION.name() + " - can be set at any level (global, project, branch)" + END_ITEM_LIST + - BEGIN_ITEM_LIST + NUMBER_OF_DAYS.name() + " - can be set can be set at any level (global, project, branch)" + END_ITEM_LIST + + BEGIN_ITEM_LIST + NUMBER_OF_DAYS.name() + " - can be set at any level (global, project, branch)" + END_ITEM_LIST + + BEGIN_ITEM_LIST + REFERENCE_BRANCH.name() + " - can only be set for projects and branches" + END_ITEM_LIST + END_LIST ); action.createParam(PARAM_VALUE) @@ -115,6 +117,7 @@ public class SetAction implements NewCodePeriodsWsAction { BEGIN_ITEM_LIST + "the uuid of an analysis, when type is " + SPECIFIC_ANALYSIS.name() + END_ITEM_LIST + BEGIN_ITEM_LIST + "no value, when type is " + PREVIOUS_VERSION.name() + END_ITEM_LIST + BEGIN_ITEM_LIST + "a number, when type is " + NUMBER_OF_DAYS.name() + END_ITEM_LIST + + BEGIN_ITEM_LIST + "a string, when type is " + REFERENCE_BRANCH.name() + END_ITEM_LIST + END_LIST ); } @@ -182,6 +185,10 @@ public class SetAction implements NewCodePeriodsWsAction { SnapshotDto analysis = getAnalysis(dbSession, value, project, branch); dto.setValue(analysis.getUuid()); break; + case REFERENCE_BRANCH: + requireValue(type, value); + dto.setValue(value); + break; default: throw new IllegalStateException("Unexpected type: " + type); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java index 43163e09d5d..ca79dfe3966 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/newcodeperiod/ws/ShowAction.java @@ -38,6 +38,8 @@ import org.sonar.server.user.UserSession; import org.sonarqube.ws.NewCodePeriods; import static java.lang.String.format; +import static org.sonar.db.permission.OrganizationPermission.SCAN; +import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.NewCodePeriods.ShowWSResponse; @@ -60,11 +62,12 @@ public class ShowAction implements NewCodePeriodsWsAction { @Override public void define(WebService.NewController context) { WebService.NewAction action = context.createAction("show") - .setDescription("Shows a setting for the New Code Period.<br>" + - "Requires one of the following permissions: " + + .setDescription("Shows a setting for the New Code Period.<br> " + + "If the component requested doesn't exist or if no new code period is set for it, a value is inherited from the project or from the global setting." + + "Requires one of the following permissions if a component is specified: " + "<ul>" + - "<li>'Administer System'</li>" + "<li>'Administer' rights on the specified component</li>" + + "<li>'Execute analysis' rights on the specified component</li>" + "</ul>") .setSince("8.0") .setResponseExample(getClass().getResource("show-example.json")) @@ -88,16 +91,25 @@ public class ShowAction implements NewCodePeriodsWsAction { try (DbSession dbSession = dbClient.openSession(false)) { ProjectDto project = null; BranchDto branch = null; + boolean inherited = false; if (projectKey != null) { - project = getProject(dbSession, projectKey); - userSession.checkProjectPermission(UserRole.ADMIN, project); - if (branchKey != null) { - branch = getBranch(dbSession, project, branchKey); + try { + project = getProject(dbSession, projectKey); + checkPermission(project); + if (branchKey != null) { + try { + branch = getBranch(dbSession, project, branchKey); + } catch (NotFoundException e) { + inherited = true; + } + } + } catch (NotFoundException e) { + inherited = true; } } - ShowWSResponse.Builder builder = get(dbSession, project, branch, false); + ShowWSResponse.Builder builder = get(dbSession, project, branch, inherited); if (project != null) { builder.setProjectKey(project.getKey()); @@ -109,6 +121,15 @@ public class ShowAction implements NewCodePeriodsWsAction { } } + private void checkPermission(ProjectDto project) { + if (userSession.hasProjectPermission(UserRole.SCAN, project) || + userSession.hasProjectPermission(UserRole.ADMIN, project) || + userSession.hasPermission(SCAN, project.getOrganizationUuid())) { + return; + } + throw insufficientPrivilegesException(); + } + private ShowWSResponse.Builder get(DbSession dbSession, @Nullable ProjectDto project, @Nullable BranchDto branch, boolean inherited) { if (project == null) { Optional<NewCodePeriodDto> dto = newCodePeriodDao.selectGlobal(dbSession); @@ -151,6 +172,8 @@ public class ShowAction implements NewCodePeriodsWsAction { return NewCodePeriods.NewCodePeriodType.PREVIOUS_VERSION; case SPECIFIC_ANALYSIS: return NewCodePeriods.NewCodePeriodType.SPECIFIC_ANALYSIS; + case REFERENCE_BRANCH: + return NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH; default: throw new IllegalStateException("Unexpected type: " + type); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/QualityGateDetailsFormatter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/QualityGateDetailsFormatter.java index 8928346d324..391c97d91c3 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/QualityGateDetailsFormatter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/QualityGateDetailsFormatter.java @@ -49,8 +49,7 @@ public class QualityGateDetailsFormatter { return newResponseWithoutQualityGateDetails(); } - JsonParser parser = new JsonParser(); - JsonObject json = parser.parse(optionalMeasureData.get()).getAsJsonObject(); + JsonObject json = JsonParser.parseString(optionalMeasureData.get()).getAsJsonObject(); ProjectStatusResponse.Status qualityGateStatus = measureLevelToQualityGateStatus(json.get("level").getAsString()); projectStatusBuilder.setStatus(qualityGateStatus); @@ -80,8 +79,8 @@ public class QualityGateDetailsFormatter { periodBuilder.clear(); SnapshotDto snapshot = this.optionalSnapshot.get(); - String periodMode = snapshot.getPeriodMode(); - if (isNullOrEmpty(periodMode)) { + + if (isNullOrEmpty(snapshot.getPeriodMode())) { return; } periodBuilder.setIndex(1); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java index 2ebdbdbf1a5..122414be00f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/SetActionTest.java @@ -126,7 +126,7 @@ public class SetActionTest { logInAsProjectAdministrator(project); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Invalid type 'SPECIFIC_ANALYSIS'. Projects can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS]"); + expectedException.expectMessage("Invalid type 'SPECIFIC_ANALYSIS'. Projects can only be set with types: [PREVIOUS_VERSION, NUMBER_OF_DAYS, REFERENCE_BRANCH]"); ws.newRequest() .setParam("project", project.getKey()) @@ -322,7 +322,8 @@ public class SetActionTest { return new Object[][]{ {NewCodePeriodType.NUMBER_OF_DAYS, "5"}, {NewCodePeriodType.SPECIFIC_ANALYSIS, "analysis-uuid"}, - {NewCodePeriodType.PREVIOUS_VERSION, null} + {NewCodePeriodType.PREVIOUS_VERSION, null}, + {NewCodePeriodType.REFERENCE_BRANCH, "master"} }; } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java index e2cbfdea650..b67fe6cb374 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/newcodeperiod/ws/ShowActionTest.java @@ -38,7 +38,6 @@ import org.sonar.db.newcodeperiod.NewCodePeriodType; import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import org.sonarqube.ws.NewCodePeriods; @@ -88,31 +87,21 @@ public class ShowActionTest { } @Test - public void throw_NFE_if_project_not_found() { - expectedException.expect(NotFoundException.class); - expectedException.expectMessage("Project 'unknown' not found"); - - ws.newRequest() - .setParam("project", "unknown") - .execute(); - } - - @Test - public void throw_NFE_if_branch_not_found() { + public void throw_FE_if_no_project_permission() { ComponentDto project = componentDb.insertPublicProject(); - logInAsProjectAdministrator(project); - expectedException.expect(NotFoundException.class); - expectedException.expectMessage("Branch 'unknown' in project '" + project.getKey() + "' not found"); + expectedException.expect(ForbiddenException.class); + expectedException.expectMessage("Insufficient privileges"); ws.newRequest() .setParam("project", project.getKey()) - .setParam("branch", "unknown") .execute(); } @Test - public void throw_FE_if_no_project_permission() { + public void throw_FE_if_project_issue_admin() { ComponentDto project = componentDb.insertPublicProject(); + logInAsProjectIssueAdmin(project); + expectedException.expect(ForbiddenException.class); expectedException.expectMessage("Insufficient privileges"); @@ -217,6 +206,32 @@ public class ShowActionTest { assertResponse(response, project.getKey(), "branch", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true); } + @Test + public void show_inherited_if_project_not_found() { + tester.insert(new NewCodePeriodDto().setType(NewCodePeriodType.NUMBER_OF_DAYS).setValue("3")); + + ShowWSResponse response = ws.newRequest() + .setParam("project", "unknown") + .executeProtobuf(ShowWSResponse.class); + + assertResponse(response, "", "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true); + } + + @Test + public void show_inherited_if_branch_not_found() { + ComponentDto project = componentDb.insertPublicProject(); + logInAsProjectScan(project); + + tester.insert(project.projectUuid(), NewCodePeriodType.NUMBER_OF_DAYS, "3"); + + ShowWSResponse response = ws.newRequest() + .setParam("project", project.getKey()) + .setParam("branch", "unknown") + .executeProtobuf(ShowWSResponse.class); + + assertResponse(response, project.getKey(), "", NewCodePeriods.NewCodePeriodType.NUMBER_OF_DAYS, "3", true); + } + private void assertResponse(ShowWSResponse response, String projectKey, String branchKey, NewCodePeriods.NewCodePeriodType type, String value, boolean inherited) { assertThat(response.getBranchKey()).isEqualTo(branchKey); assertThat(response.getProjectKey()).isEqualTo(projectKey); @@ -229,4 +244,12 @@ public class ShowActionTest { userSession.logIn().addProjectPermission(UserRole.ADMIN, project); } + private void logInAsProjectScan(ComponentDto project) { + userSession.logIn().addProjectPermission(UserRole.SCAN, project); + } + + private void logInAsProjectIssueAdmin(ComponentDto project) { + userSession.logIn().addProjectPermission(UserRole.ISSUE_ADMIN, project); + } + } |