From: Benjamin Campomenosi <109955405+benjamin-campomenosi-sonarsource@users.noreply.github.com> Date: Thu, 26 Jan 2023 16:31:24 +0000 (+0100) Subject: SONAR-17705 drop project_analyses/set_baseline & project_analyses/unset_baseline X-Git-Tag: 10.0.0.68432~274 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9f4942bc70aa61e6e69dc1da9b98f194e6e3416d;p=sonarqube.git SONAR-17705 drop project_analyses/set_baseline & project_analyses/unset_baseline --- diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModule.java index 7f6eb18f3cd..c187d38ff93 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModule.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModule.java @@ -32,9 +32,7 @@ public class ProjectAnalysisWsModule extends Module { UpdateEventAction.class, DeleteEventAction.class, DeleteAction.class, - SearchAction.class, - SetBaselineAction.class, - UnsetBaselineAction.class); + SearchAction.class); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java deleted file mode 100644 index d2f77a28661..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/SetBaselineAction.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.projectanalysis.ws; - -import com.google.protobuf.Empty; -import javax.annotation.Nullable; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.web.UserRole; -import org.sonar.core.util.Uuids; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.BranchDao; -import org.sonar.db.component.BranchDto; -import org.sonar.db.component.SnapshotDto; -import org.sonar.db.newcodeperiod.NewCodePeriodDto; -import org.sonar.db.newcodeperiod.NewCodePeriodType; -import org.sonar.db.project.ProjectDto; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.user.UserSession; - -import static com.google.common.base.Preconditions.checkArgument; -import static java.lang.String.format; -import static org.apache.commons.lang.StringUtils.trimToNull; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT; -import static org.sonar.server.ws.WsUtils.writeProtobuf; - -public class SetBaselineAction implements ProjectAnalysesWsAction { - private final DbClient dbClient; - private final UserSession userSession; - private final ComponentFinder componentFinder; - private final BranchDao branchDao; - - public SetBaselineAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, BranchDao branchDao) { - this.dbClient = dbClient; - this.userSession = userSession; - this.componentFinder = componentFinder; - this.branchDao = branchDao; - } - - @Override - public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("set_baseline") - .setDescription("Set an analysis as the baseline of the New Code Period on a project or a branch.
" + - "This manually set baseline.
" + - "Requires one of the following permissions:" + - "") - .setSince("7.7") - .setDeprecatedSince("8.0") - .setPost(true) - .setHandler(this); - - action.createParam(PARAM_PROJECT) - .setDescription("Project key") - .setRequired(true); - - action.createParam(PARAM_BRANCH) - .setDescription("Branch key"); - - action.createParam(PARAM_ANALYSIS) - .setDescription("Analysis key") - .setExampleValue(Uuids.UUID_EXAMPLE_01) - .setRequired(true); - } - - @Override - public void handle(Request httpRequest, Response httpResponse) throws Exception { - doHandle(httpRequest); - - writeProtobuf(Empty.newBuilder().build(), httpRequest, httpResponse); - } - - private void doHandle(Request request) { - String projectKey = request.mandatoryParam(PARAM_PROJECT); - String branchKey = trimToNull(request.param(PARAM_BRANCH)); - String analysisUuid = request.mandatoryParam(PARAM_ANALYSIS); - - try (DbSession dbSession = dbClient.openSession(false)) { - ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); - BranchDto branch = loadBranch(dbSession, project, branchKey); - SnapshotDto analysis = getAnalysis(dbSession, analysisUuid); - checkRequest(project, branch, analysis, branchKey); - - dbClient.newCodePeriodDao().upsert(dbSession, new NewCodePeriodDto() - .setProjectUuid(project.getUuid()) - .setBranchUuid(branch.getUuid()) - .setType(NewCodePeriodType.SPECIFIC_ANALYSIS) - .setValue(analysisUuid) - ); - dbSession.commit(); - } - } - - private BranchDto loadBranch(DbSession dbSession, ProjectDto project, @Nullable String branchKey) { - if (branchKey != null) { - return branchDao.selectByBranchKey(dbSession, project.getUuid(), branchKey) - .orElseThrow(() -> new NotFoundException(String.format("Branch '%s' in project '%s' not found", branchKey, project.getKey()))); - } - - return branchDao.selectByUuid(dbSession, project.getUuid()) - .orElseThrow(() -> new NotFoundException(String.format("Main branch in project '%s' not found", project.getKey()))); - } - - private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid) { - return dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid) - .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", analysisUuid))); - } - - private void checkRequest(ProjectDto project, BranchDto branchDto, SnapshotDto analysis, @Nullable String branchKey) { - userSession.checkProjectPermission(UserRole.ADMIN, project); - - boolean analysisMatchesBranch = analysis.getComponentUuid().equals(branchDto.getUuid()); - if (branchKey != null) { - checkArgument(analysisMatchesBranch, - "Analysis '%s' does not belong to branch '%s' of project '%s'", - analysis.getUuid(), branchKey, project.getKey()); - } else { - checkArgument(analysisMatchesBranch, - "Analysis '%s' does not belong to main branch of project '%s'", - analysis.getUuid(), project.getKey()); - } - } -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/UnsetBaselineAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/UnsetBaselineAction.java deleted file mode 100644 index 2f2c5c7b14f..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectanalysis/ws/UnsetBaselineAction.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.projectanalysis.ws; - -import com.google.protobuf.Empty; -import javax.annotation.Nullable; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.BranchDao; -import org.sonar.db.component.BranchDto; -import org.sonar.db.project.ProjectDto; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.user.UserSession; - -import static org.apache.commons.lang.StringUtils.trimToNull; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT; -import static org.sonar.server.ws.WsUtils.writeProtobuf; - -public class UnsetBaselineAction implements ProjectAnalysesWsAction { - private final DbClient dbClient; - private final UserSession userSession; - private final ComponentFinder componentFinder; - private final BranchDao branchDao; - - public UnsetBaselineAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, BranchDao branchDao) { - this.dbClient = dbClient; - this.userSession = userSession; - this.componentFinder = componentFinder; - this.branchDao = branchDao; - } - - @Override - public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("unset_baseline") - .setDescription("Unset any manually-set New Code Period baseline on a project or a branch.
" + - "Unsetting a manual baseline restores the use of the default new code period setting.
" + - "Requires one of the following permissions:" + - "") - .setSince("7.7") - .setPost(true) - .setDeprecatedSince("8.0") - .setHandler(this); - - action.createParam(PARAM_PROJECT) - .setDescription("Project key") - .setRequired(true); - - action.createParam(PARAM_BRANCH) - .setDescription("Branch key"); - } - - @Override - public void handle(Request httpRequest, Response httpResponse) throws Exception { - doHandle(httpRequest); - - writeProtobuf(Empty.newBuilder().build(), httpRequest, httpResponse); - } - - private void doHandle(Request request) { - String projectKey = request.mandatoryParam(PARAM_PROJECT); - String branchKey = trimToNull(request.param(PARAM_BRANCH)); - - try (DbSession dbSession = dbClient.openSession(false)) { - ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey); - userSession.checkProjectPermission(UserRole.ADMIN, project); - - BranchDto branch = loadBranch(dbSession, project, branchKey); - dbClient.newCodePeriodDao().delete(dbSession, project.getUuid(), branch.getUuid()); - dbSession.commit(); - } - } - - private BranchDto loadBranch(DbSession dbSession, ProjectDto project, @Nullable String branchKey) { - if (branchKey != null) { - return branchDao.selectByBranchKey(dbSession, project.getUuid(), branchKey) - .orElseThrow(() -> new NotFoundException(String.format("Branch '%s' in project '%s' not found", branchKey, project.getKey()))); - } - - return branchDao.selectByUuid(dbSession, project.getUuid()) - .orElseThrow(() -> new NotFoundException(String.format("Main branch in project '%s' not found", project.getKey()))); - } - -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java index b37a38cbb27..248eafc6960 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/ProjectAnalysisWsModuleTest.java @@ -29,6 +29,6 @@ public class ProjectAnalysisWsModuleTest { public void verify_count_of_added_components() { ListContainer container = new ListContainer(); new ProjectAnalysisWsModule().configure(container); - assertThat(container.getAddedObjects()).hasSize(8); + assertThat(container.getAddedObjects()).hasSize(6); } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java deleted file mode 100644 index 902288d6ab7..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/SetBaselineActionTest.java +++ /dev/null @@ -1,277 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.projectanalysis.ws; - -import com.google.common.collect.ImmutableMap; -import com.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; -import com.tngtech.java.junit.dataprovider.UseDataProvider; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import javax.annotation.Nullable; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import org.sonar.db.component.BranchDao; -import org.sonar.db.component.BranchDto; -import org.sonar.db.component.ComponentDbTester; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; -import org.sonar.db.component.SnapshotDto; -import org.sonar.db.newcodeperiod.NewCodePeriodDto; -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.TestRequest; -import org.sonar.server.ws.WsActionTester; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME; -import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT; -import static org.sonarqube.ws.client.WsRequest.Method.POST; - -@RunWith(DataProviderRunner.class) -public class SetBaselineActionTest { - - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - private DbClient dbClient = db.getDbClient(); - private DbSession dbSession = db.getSession(); - private BranchDao branchDao = db.getDbClient().branchDao(); - private ComponentDbTester tester = new ComponentDbTester(db); - private WsActionTester ws = new WsActionTester(new SetBaselineAction(dbClient, userSession, TestComponentFinder.from(db), branchDao)); - - @Test - @UseDataProvider("nullOrEmpty") - public void set_baseline_on_main_branch(@Nullable String branchName) { - ComponentDto project = tester.insertPrivateProject(); - SnapshotDto analysis = db.components().insertSnapshot(project); - logInAsProjectAdministrator(project); - - call(project.getKey(), branchName, analysis.getUuid()); - - NewCodePeriodDto loaded = dbClient.newCodePeriodDao().selectByBranch(dbSession, project.uuid(), project.uuid()).get(); - assertThat(loaded.getValue()).isEqualTo(analysis.getUuid()); - assertThat(loaded.getType()).isEqualTo(SPECIFIC_ANALYSIS); - } - - @DataProvider - public static Object[][] nullOrEmpty() { - return new Object[][] { - {null}, - {""}, - {" "}, - }; - } - - @Test - public void set_baseline_on_non_main_branch() { - ComponentDto project = tester.insertPrivateProject(); - ComponentDto branchComponent = tester.insertProjectBranch(project); - SnapshotDto analysis = db.components().insertSnapshot(branchComponent); - BranchDto branch = branchDao.selectByUuid(dbSession, branchComponent.uuid()).get(); - logInAsProjectAdministrator(project); - - call(project.getKey(), branch.getKey(), analysis.getUuid()); - - NewCodePeriodDto loaded = dbClient.newCodePeriodDao().selectByBranch(dbSession, project.uuid(), branch.getUuid()).get(); - assertThat(loaded.getValue()).isEqualTo(analysis.getUuid()); - assertThat(loaded.getType()).isEqualTo(SPECIFIC_ANALYSIS); - } - - @Test - public void fail_when_user_is_not_admin() { - ComponentDto project = tester.insertPrivateProject(); - SnapshotDto analysis = db.components().insertSnapshot(project); - - assertThatThrownBy(() -> call(project.getKey(), DEFAULT_MAIN_BRANCH_NAME, analysis.getUuid())) - .isInstanceOf(ForbiddenException.class) - .hasMessage("Insufficient privileges"); - } - - @Test - @UseDataProvider("missingOrEmptyParamsAndFailureMessage") - public void fail_with_IAE_when_required_param_missing_or_empty(Map params, String message) { - assertThatThrownBy(() -> call(params)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(message); - } - - @DataProvider - public static Object[][] missingOrEmptyParamsAndFailureMessage() { - MapBuilder builder = new MapBuilder() - .put(PARAM_PROJECT, "project key") - .put(PARAM_BRANCH, "branch key") - .put(PARAM_ANALYSIS, "analysis uuid"); - - return new Object[][] { - {builder.put(PARAM_PROJECT, null).map, "The 'project' parameter is missing"}, - {builder.put(PARAM_PROJECT, "").map, "The 'project' parameter is missing"}, - {builder.put(PARAM_ANALYSIS, null).map, "The 'analysis' parameter is missing"}, - {builder.put(PARAM_ANALYSIS, "").map, "The 'analysis' parameter is missing"}, - }; - } - - @Test - @UseDataProvider("nonexistentParamsAndFailureMessage") - public void fail_with_IAE_when_required_param_nonexistent(Map nonexistentParams, String regex) { - ComponentDto project = tester.insertPrivateProject(); - SnapshotDto analysis = db.components().insertSnapshot(project); - logInAsProjectAdministrator(project); - - Map params = new HashMap<>(); - params.put(PARAM_PROJECT, project.getKey()); - params.put(PARAM_BRANCH, "master"); - params.put(PARAM_ANALYSIS, analysis.getUuid()); - params.putAll(nonexistentParams); - - assertThatThrownBy(() -> call(params)) - .isInstanceOf(NotFoundException.class); - } - - @DataProvider - public static Object[][] nonexistentParamsAndFailureMessage() { - MapBuilder builder = new MapBuilder(); - - return new Object[][] { - {builder.put(PARAM_PROJECT, "nonexistent").map, "Project 'nonexistent' not found"}, - {builder.put(PARAM_BRANCH, "nonexistent").map, "Branch 'nonexistent' in project .* not found"}, - {builder.put(PARAM_ANALYSIS, "nonexistent").map, "Analysis 'nonexistent' is not found"}, - }; - } - - @Test - public void fail_when_branch_does_not_belong_to_project() { - ComponentDto project = tester.insertPrivateProject(); - SnapshotDto analysis = db.components().insertSnapshot(project); - logInAsProjectAdministrator(project); - - ComponentDto otherProject = tester.insertPrivateProjectWithCustomBranch("develop"); - BranchDto branchOfOtherProject = branchDao.selectByUuid(dbSession, otherProject.uuid()).get(); - - assertThatThrownBy(() -> call(project.getKey(), branchOfOtherProject.getKey(), analysis.getUuid())) - .isInstanceOf(NotFoundException.class) - .hasMessage(String.format("Branch '%s' in project '%s' not found", branchOfOtherProject.getKey(), project.getKey())); - } - - @Test - public void fail_when_analysis_does_not_belong_to_main_branch_of_project() { - ComponentDto project = tester.insertPrivateProjectWithCustomBranch("branch1"); - logInAsProjectAdministrator(project); - - ComponentDto otherProject = ComponentTesting.newPrivateProjectDto(); - SnapshotDto otherAnalysis = db.components().insertProjectAndSnapshot(otherProject); - - assertThatThrownBy(() -> call(project.getKey(), "branch1", otherAnalysis.getUuid())) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(String.format("Analysis '%s' does not belong to branch '%s' of project '%s'", - otherAnalysis.getUuid(), "branch1", project.getKey())); - } - - @Test - public void fail_when_analysis_does_not_belong_to_non_main_branch_of_project() { - ComponentDto project = tester.insertPrivateProject(); - tester.insertProjectBranch(project, b -> b.setKey("branch1")); - logInAsProjectAdministrator(project); - - ComponentDto otherProject = ComponentTesting.newPrivateProjectDto(); - SnapshotDto otherAnalysis = db.components().insertProjectAndSnapshot(otherProject); - - assertThatThrownBy(() -> call(project.getKey(), "branch1", otherAnalysis.getUuid())) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(String.format("Analysis '%s' does not belong to branch '%s' of project '%s'", - otherAnalysis.getUuid(), "branch1", project.getKey())); - } - - @Test - public void ws_parameters() { - WebService.Action definition = ws.getDef(); - - assertThat(definition.isPost()).isTrue(); - assertThat(definition.key()).isEqualTo("set_baseline"); - assertThat(definition.since()).isEqualTo("7.7"); - assertThat(definition.isInternal()).isFalse(); - } - - private void logInAsProjectAdministrator(ComponentDto project) { - userSession.logIn().addProjectPermission(UserRole.ADMIN, project); - } - - private void call(Map params) { - TestRequest httpRequest = ws.newRequest().setMethod(POST.name()); - - for (Map.Entry param : params.entrySet()) { - httpRequest.setParam(param.getKey(), param.getValue()); - } - - httpRequest.execute(); - } - - private void call(String projectKey, @Nullable String branchKey, String analysisUuid) { - if (branchKey == null) { - call(ImmutableMap.of( - PARAM_PROJECT, projectKey, - PARAM_ANALYSIS, analysisUuid)); - } else { - call(ImmutableMap.of( - PARAM_PROJECT, projectKey, - PARAM_BRANCH, branchKey, - PARAM_ANALYSIS, analysisUuid)); - } - } - - private static class MapBuilder { - private final Map map; - - private MapBuilder() { - this.map = Collections.emptyMap(); - } - - private MapBuilder(Map map) { - this.map = map; - } - - public MapBuilder put(String key, @Nullable String value) { - Map copy = new HashMap<>(map); - if (value == null) { - copy.remove(key); - } else { - copy.put(key, value); - } - return new MapBuilder(copy); - } - } -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java deleted file mode 100644 index 327c05c09cd..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java +++ /dev/null @@ -1,270 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.projectanalysis.ws; - -import com.google.common.collect.ImmutableMap; -import com.tngtech.java.junit.dataprovider.DataProvider; -import com.tngtech.java.junit.dataprovider.DataProviderRunner; -import com.tngtech.java.junit.dataprovider.UseDataProvider; -import java.util.Optional; -import javax.annotation.Nullable; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import org.sonar.db.component.BranchDao; -import org.sonar.db.component.BranchDto; -import org.sonar.db.component.BranchType; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.SnapshotDto; -import org.sonar.db.newcodeperiod.NewCodePeriodDto; -import org.sonar.db.newcodeperiod.NewCodePeriodType; -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.TestRequest; -import org.sonar.server.ws.WsActionTester; - -import static java.util.Optional.ofNullable; -import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; -import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH; -import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT; -import static org.sonarqube.ws.client.WsRequest.Method.POST; - -@RunWith(DataProviderRunner.class) -public class UnsetBaselineActionTest { - - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - - private DbClient dbClient = db.getDbClient(); - private DbSession dbSession = db.getSession(); - private BranchDao branchDao = db.getDbClient().branchDao(); - private WsActionTester ws = new WsActionTester(new UnsetBaselineAction(dbClient, userSession, TestComponentFinder.from(db), branchDao)); - - @Test - public void does_not_fail_and_has_no_effect_when_there_is_no_baseline_on_main_branch() { - ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); - SnapshotDto analysis = db.components().insertSnapshot(project); - logInAsProjectAdministrator(project); - - call(project.getKey(), null); - - verifyManualBaseline(project, null); - } - - @Test - public void does_not_fail_and_has_no_effect_when_there_is_no_baseline_on_non_main_branch() { - ComponentDto project = db.components().insertPublicProject(); - String branchName = randomAlphanumeric(248); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); - SnapshotDto analysis = db.components().insertSnapshot(project); - logInAsProjectAdministrator(project); - - call(project.getKey(), branchName); - - verifyManualBaseline(branch, null); - } - - @Test - public void unset_baseline_when_it_is_set_on_main_branch() { - ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); - SnapshotDto projectAnalysis = db.components().insertSnapshot(project); - SnapshotDto branchAnalysis = db.components().insertSnapshot(project); - db.newCodePeriods().insert(project.branchUuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, projectAnalysis.getUuid()); - logInAsProjectAdministrator(project); - - call(project.getKey(), null); - - verifyManualBaseline(project, null); - } - - @Test - public void unset_baseline_when_it_is_set_non_main_branch() { - ComponentDto project = db.components().insertPublicProject(); - String branchName = randomAlphanumeric(248); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName)); - db.components().insertSnapshot(branch); - SnapshotDto branchAnalysis = db.components().insertSnapshot(project); - db.newCodePeriods().insert(project.branchUuid(), branch.uuid(), NewCodePeriodType.SPECIFIC_ANALYSIS, branchAnalysis.getUuid()); - - logInAsProjectAdministrator(project); - - call(project.getKey(), branchName); - - verifyManualBaseline(branch, null); - } - - @Test - public void fail_when_user_is_not_admin_on_project() { - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project); - - assertThatThrownBy(() -> call(project.getKey(), null)) - .isInstanceOf(ForbiddenException.class) - .hasMessage("Insufficient privileges"); - } - - @Test - public void fail_when_user_is_not_admin_on_project_of_branch() { - ComponentDto project = db.components().insertPublicProject(); - String branchName = randomAlphanumeric(248); - db.components().insertProjectBranch(project, b -> b.setKey(branchName)); - - String key = project.getKey(); - assertThatThrownBy(() -> call(key, branchName)) - .isInstanceOf(ForbiddenException.class) - .hasMessage("Insufficient privileges"); - } - - @Test - @UseDataProvider("nullOrEmptyOrValue") - public void fail_with_IAE_when_missing_project_parameter(@Nullable String branchParam) { - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project); - logInAsProjectAdministrator(project); - - assertThatThrownBy(() -> call(null, branchParam)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("The 'project' parameter is missing"); - } - - @Test - @UseDataProvider("nullOrEmptyOrValue") - public void fail_with_IAE_when_project_parameter_empty(@Nullable String branchParam) { - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project); - logInAsProjectAdministrator(project); - - assertThatThrownBy(() -> call("", branchParam)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage("The 'project' parameter is missing"); - } - - @DataProvider - public static Object[][] nullOrEmptyOrValue() { - return new Object[][] { - {null}, - {""}, - {randomAlphabetic(10)}, - }; - } - - @Test - @UseDataProvider("nullOrEmpty") - public void does_not_fail_with_IAE_when_missing_branch_parameter(@Nullable String branchParam) { - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project); - logInAsProjectAdministrator(project); - - call(project.getKey(), branchParam); - } - - @DataProvider - public static Object[][] nullOrEmpty() { - return new Object[][] { - {null}, - {""}, - }; - } - - @DataProvider - public static Object[][] nonexistentParamsAndFailureMessage() { - return new Object[][] { - {ImmutableMap.of(PARAM_PROJECT, "nonexistent"), "Component 'nonexistent' on branch .* not found"}, - {ImmutableMap.of(PARAM_BRANCH, "nonexistent"), "Component .* on branch 'nonexistent' not found"} - }; - } - - @Test - public void fail_when_branch_does_not_belong_to_project() { - ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project); - ComponentDto otherProject = db.components().insertPublicProject(); - ComponentDto otherBranch = db.components().insertProjectBranch(otherProject); - logInAsProjectAdministrator(project); - - assertThatThrownBy(() -> call(project.getKey(), otherBranch.getKey())) - .isInstanceOf(NotFoundException.class) - .hasMessage(String.format("Branch '%s' in project '%s' not found", otherBranch.getKey(), project.getKey())); - } - - @Test - public void fail_with_NotFoundException_when_branch_is_pull_request() { - ComponentDto project = db.components().insertPrivateProject(); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); - logInAsProjectAdministrator(project); - - assertThatThrownBy(() -> call(project.getKey(), pullRequest.getKey())) - .isInstanceOf(NotFoundException.class) - .hasMessage(String.format("Branch '%s' in project '%s' not found", pullRequest.getKey(), project.getKey())); - } - - @Test - public void verify_ws_parameters() { - WebService.Action definition = ws.getDef(); - - assertThat(definition.isPost()).isTrue(); - assertThat(definition.key()).isEqualTo("unset_baseline"); - assertThat(definition.since()).isEqualTo("7.7"); - assertThat(definition.isInternal()).isFalse(); - } - - private void logInAsProjectAdministrator(ComponentDto project) { - userSession.logIn().addProjectPermission(UserRole.ADMIN, project); - } - - private void call(@Nullable String project, @Nullable String branchName) { - TestRequest httpRequest = ws.newRequest().setMethod(POST.name()); - ofNullable(project).ifPresent(t -> httpRequest.setParam(PARAM_PROJECT, t)); - ofNullable(branchName).ifPresent(t -> httpRequest.setParam(PARAM_BRANCH, t)); - - httpRequest.execute(); - } - - private void verifyManualBaseline(ComponentDto project, @Nullable SnapshotDto projectAnalysis) { - BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(dbSession, project.uuid()).get(); - Optional newCodePeriod = db.getDbClient().newCodePeriodDao().selectByBranch(dbSession, branchDto.getProjectUuid(), branchDto.getUuid()); - if (projectAnalysis == null) { - assertThat(newCodePeriod).isNotNull(); - assertThat(newCodePeriod).isEmpty(); - } else { - assertThat(newCodePeriod).isNotNull(); - assertThat(newCodePeriod).isNotEmpty(); - assertThat(newCodePeriod.get().getValue()).isEqualTo(projectAnalysis.getUuid()); - } - } - -} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/ProjectAnalysesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/ProjectAnalysesService.java index 35a54c7a7b3..d8896097ba8 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/ProjectAnalysesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/ProjectAnalysesService.java @@ -20,7 +20,6 @@ package org.sonarqube.ws.client.projectanalyses; import javax.annotation.Generated; -import javax.annotation.Nullable; import org.sonarqube.ws.MediaTypes; import org.sonarqube.ws.ProjectAnalyses.CreateEventResponse; import org.sonarqube.ws.ProjectAnalyses.SearchResponse; @@ -30,9 +29,6 @@ import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsConnector; -import static java.util.Objects.requireNonNull; -import static java.util.Optional.ofNullable; - /** * @see Further information about this web service online */ @@ -123,21 +119,4 @@ public class ProjectAnalysesService extends BaseService { UpdateEventResponse.parser()); } - public void set_baseline(String projectKey, @Nullable String branchName, String analysisUuid) { - requireNonNull(projectKey, "projectKey can't be null"); - requireNonNull(analysisUuid, "analysisUuid can't be null"); - PostRequest request = new PostRequest(path("set_baseline")) - .setParam("project", projectKey) - .setParam("analysis", analysisUuid); - ofNullable(branchName).ifPresent(t -> request.setParam("branch", t)); - call(request); - } - - public void unset_baseline(String projectKey, @Nullable String branchName) { - requireNonNull(projectKey, "projectKey can't be null"); - PostRequest request = new PostRequest(path("unset_baseline")) - .setParam("project", projectKey); - ofNullable(branchName).ifPresent(t -> request.setParam("branch", t)); - call(request); - } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/SetBaselineRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/SetBaselineRequest.java deleted file mode 100644 index 39fa8d24ba2..00000000000 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/SetBaselineRequest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarqube.ws.client.projectanalyses; - -import javax.annotation.Generated; - -/** - * This is part of the internal API. - * This is a POST request. - * @see Further information about this action online (including a response example) - * @since 7.7 - */ -@Generated("sonar-ws-generator") -public class SetBaselineRequest { - - private String analysis; - private String branch; - private String project; - - /** - * This is a mandatory parameter. - * Example value: "AU-Tpxb--iU5OvuD2FLy" - */ - public SetBaselineRequest setAnalysis(String analysis) { - this.analysis = analysis; - return this; - } - - public String getAnalysis() { - return analysis; - } - - /** - */ - public SetBaselineRequest setBranch(String branch) { - this.branch = branch; - return this; - } - - public String getBranch() { - return branch; - } - - /** - * This is a mandatory parameter. - */ - public SetBaselineRequest setProject(String project) { - this.project = project; - return this; - } - - public String getProject() { - return project; - } -} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/UnsetBaselineRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/UnsetBaselineRequest.java deleted file mode 100644 index 7069e06b50d..00000000000 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectanalyses/UnsetBaselineRequest.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonarqube.ws.client.projectanalyses; - -import javax.annotation.Generated; - -/** - * This is part of the internal API. - * This is a POST request. - * @see Further information about this action online (including a response example) - * @since 7.7 - */ -@Generated("sonar-ws-generator") -public class UnsetBaselineRequest { - - private String branch; - private String project; - - /** - */ - public UnsetBaselineRequest setBranch(String branch) { - this.branch = branch; - return this; - } - - public String getBranch() { - return branch; - } - - /** - * This is a mandatory parameter. - */ - public UnsetBaselineRequest setProject(String project) { - this.project = project; - return this; - } - - public String getProject() { - return project; - } -}