From: Zipeng WU Date: Fri, 23 Sep 2022 10:19:22 +0000 (+0200) Subject: SONAR-14484 Move 'api/project_pull_requests' WS to DE X-Git-Tag: 9.7.0.61563~170 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1a42877ea036f650f691caf1a00a4892604f7ff6;p=sonarqube.git SONAR-14484 Move 'api/project_pull_requests' WS to DE --- diff --git a/server/sonar-web/src/main/js/app/components/ComponentContainer.tsx b/server/sonar-web/src/main/js/app/components/ComponentContainer.tsx index 2220ec14d6c..1e788618770 100644 --- a/server/sonar-web/src/main/js/app/components/ComponentContainer.tsx +++ b/server/sonar-web/src/main/js/app/components/ComponentContainer.tsx @@ -155,6 +155,10 @@ export class ComponentContainer extends React.PureComponent { }; fetchBranches = async (componentWithQualifier: Component) => { + const { + appState: { branchesEnabled } + } = this.props; + const breadcrumb = componentWithQualifier.breadcrumbs.find(({ qualifier }) => { return ([ComponentQualifier.Application, ComponentQualifier.Project] as string[]).includes( qualifier @@ -168,7 +172,7 @@ export class ComponentContainer extends React.PureComponent { const { key } = breadcrumb; const [branches, pullRequests] = await Promise.all([ getBranches(key), - breadcrumb.qualifier === ComponentQualifier.Application + !branchesEnabled || breadcrumb.qualifier === ComponentQualifier.Application ? Promise.resolve([]) : getPullRequests(key) ]); diff --git a/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx index e7822a0a90c..45c4bfe9c6e 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/ComponentContainer-test.tsx @@ -141,6 +141,7 @@ it("doesn't load branches portfolio", async () => { it('updates branches on change', async () => { const updateBranchStatus = jest.fn(); const wrapper = shallowRender({ + appState: mockAppState({ branchesEnabled: true }), location: mockLocation({ query: { id: 'portfolioKey' } }), updateBranchStatus }); diff --git a/server/sonar-webserver-webapi/build.gradle b/server/sonar-webserver-webapi/build.gradle index 4bd3f52a205..77956eacc4e 100644 --- a/server/sonar-webserver-webapi/build.gradle +++ b/server/sonar-webserver-webapi/build.gradle @@ -43,5 +43,6 @@ dependencies { testCompile testFixtures(project(':server:sonar-webserver-es')) testCompile testFixtures(project(':server:sonar-webserver-ws')) testCompile project(':sonar-testing-harness') + testFixturesApi testFixtures(project(':server:sonar-db-dao')) } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/DeleteAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/DeleteAction.java deleted file mode 100644 index 01d343bc76a..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/DeleteAction.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -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.server.ws.WebService.NewController; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.component.BranchDto; -import org.sonar.db.project.ProjectDto; -import org.sonar.server.component.ComponentCleanerService; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.user.UserSession; - -import static org.sonar.db.component.BranchType.PULL_REQUEST; -import static org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam; -import static org.sonar.server.branch.pr.ws.PullRequestsWs.addPullRequestParam; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PULL_REQUEST; -import static org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_DELETE; - -public class DeleteAction implements PullRequestWsAction { - private final DbClient dbClient; - private final UserSession userSession; - private final ComponentCleanerService componentCleanerService; - private final ComponentFinder componentFinder; - - public DeleteAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession, ComponentCleanerService componentCleanerService) { - this.dbClient = dbClient; - this.componentFinder = componentFinder; - this.userSession = userSession; - this.componentCleanerService = componentCleanerService; - } - - @Override - public void define(NewController context) { - WebService.NewAction action = context.createAction(ACTION_DELETE) - .setSince("7.1") - .setDescription("Delete a pull request.
" + - "Requires 'Administer' rights on the specified project.") - .setPost(true) - .setHandler(this); - - addProjectParam(action); - addPullRequestParam(action); - } - - @Override - public void handle(Request request, Response response) throws Exception { - userSession.checkLoggedIn(); - String projectKey = request.mandatoryParam(PARAM_PROJECT); - String pullRequestId = request.mandatoryParam(PARAM_PULL_REQUEST); - - try (DbSession dbSession = dbClient.openSession(false)) { - ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey); - checkPermission(project); - - BranchDto pullRequest = dbClient.branchDao().selectByPullRequestKey(dbSession, project.getUuid(), pullRequestId) - .filter(branch -> branch.getBranchType() == PULL_REQUEST) - .orElseThrow(() -> new NotFoundException(String.format("Pull request '%s' is not found for project '%s'", pullRequestId, projectKey))); - - componentCleanerService.deleteBranch(dbSession, pullRequest); - response.noContent(); - } - } - - private void checkPermission(ProjectDto project) { - userSession.checkProjectPermission(UserRole.ADMIN, project); - } - -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/ListAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/ListAction.java deleted file mode 100644 index cd4dfb86220..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/ListAction.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Optional; -import java.util.function.Function; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.server.ws.Change; -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.BranchDto; -import org.sonar.db.component.SnapshotDto; -import org.sonar.db.measure.LiveMeasureDto; -import org.sonar.db.permission.GlobalPermission; -import org.sonar.db.project.ProjectDto; -import org.sonar.db.protobuf.DbProjectBranches; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.issue.index.IssueIndex; -import org.sonar.server.issue.index.PrStatistics; -import org.sonar.server.user.UserSession; -import org.sonarqube.ws.ProjectPullRequests; - -import static com.google.common.base.Strings.emptyToNull; -import static java.util.Collections.singletonList; -import static java.util.Objects.requireNonNull; -import static java.util.Optional.ofNullable; -import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; -import static org.sonar.api.utils.DateUtils.formatDateTime; -import static org.sonar.api.web.UserRole.USER; -import static org.sonar.core.util.stream.MoreCollectors.toList; -import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; -import static org.sonar.db.component.BranchType.PULL_REQUEST; -import static org.sonar.server.branch.pr.ws.PullRequestsWs.addProjectParam; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT; -import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException; -import static org.sonar.server.ws.WsUtils.writeProtobuf; - -public class ListAction implements PullRequestWsAction { - - private final DbClient dbClient; - private final UserSession userSession; - private final ComponentFinder componentFinder; - private final IssueIndex issueIndex; - - public ListAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder, IssueIndex issueIndex) { - this.dbClient = dbClient; - this.userSession = userSession; - this.componentFinder = componentFinder; - this.issueIndex = issueIndex; - } - - @Override - public void define(WebService.NewController context) { - WebService.NewAction action = context.createAction("list") - .setSince("7.1") - .setDescription("List the pull requests of a project.
" + - "One of the following permissions is required: " + - "
    " + - "
  • 'Browse' rights on the specified project
  • " + - "
  • 'Execute Analysis' rights on the specified project
  • " + - "
") - .setResponseExample(getClass().getResource("list-example.json")) - .setHandler(this) - .setChangelog( - new Change("8.4", "Response fields: 'bugs', 'vulnerabilities', 'codeSmells' are deprecated.")); - addProjectParam(action); - } - - @Override - public void handle(Request request, Response response) throws Exception { - String projectKey = request.mandatoryParam(PARAM_PROJECT); - - try (DbSession dbSession = dbClient.openSession(false)) { - ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey); - checkPermission(project); - - List pullRequests = dbClient.branchDao().selectByProject(dbSession, project).stream() - .filter(b -> b.getBranchType() == PULL_REQUEST) - .collect(toList()); - List pullRequestUuids = pullRequests.stream().map(BranchDto::getUuid).collect(toList()); - - Map mergeBranchesByUuid = dbClient.branchDao() - .selectByUuids(dbSession, pullRequests.stream().map(BranchDto::getMergeBranchUuid).filter(Objects::nonNull).collect(toList())) - .stream().collect(uniqueIndex(BranchDto::getUuid)); - - Map branchStatisticsByBranchUuid = issueIndex.searchBranchStatistics(project.getUuid(), pullRequestUuids).stream() - .collect(uniqueIndex(PrStatistics::getBranchUuid, Function.identity())); - Map qualityGateMeasuresByComponentUuids = dbClient.liveMeasureDao() - .selectByComponentUuidsAndMetricKeys(dbSession, pullRequestUuids, singletonList(ALERT_STATUS_KEY)).stream() - .collect(uniqueIndex(LiveMeasureDto::getComponentUuid)); - Map analysisDateByBranchUuid = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, pullRequestUuids).stream() - .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> formatDateTime(s.getCreatedAt()))); - - ProjectPullRequests.ListWsResponse.Builder protobufResponse = ProjectPullRequests.ListWsResponse.newBuilder(); - pullRequests - .forEach(b -> addPullRequest(protobufResponse, b, mergeBranchesByUuid, qualityGateMeasuresByComponentUuids.get(b.getUuid()), branchStatisticsByBranchUuid.get(b.getUuid()), - analysisDateByBranchUuid.get(b.getUuid()))); - writeProtobuf(protobufResponse.build(), request, response); - } - } - - private void checkPermission(ProjectDto project) { - if (userSession.hasProjectPermission(USER, project) || - userSession.hasProjectPermission(UserRole.SCAN, project) || - userSession.hasPermission(GlobalPermission.SCAN)) { - return; - } - throw insufficientPrivilegesException(); - } - - private static void addPullRequest(ProjectPullRequests.ListWsResponse.Builder response, BranchDto branch, Map mergeBranchesByUuid, - @Nullable LiveMeasureDto qualityGateMeasure, PrStatistics prStatistics, @Nullable String analysisDate) { - Optional mergeBranch = Optional.ofNullable(mergeBranchesByUuid.get(branch.getMergeBranchUuid())); - - ProjectPullRequests.PullRequest.Builder builder = ProjectPullRequests.PullRequest.newBuilder(); - builder.setKey(branch.getKey()); - - DbProjectBranches.PullRequestData pullRequestData = requireNonNull(branch.getPullRequestData(), "Pull request data should be available for branch type PULL_REQUEST"); - builder.setBranch(pullRequestData.getBranch()); - ofNullable(emptyToNull(pullRequestData.getUrl())).ifPresent(builder::setUrl); - ofNullable(emptyToNull(pullRequestData.getTitle())).ifPresent(builder::setTitle); - - if (mergeBranch.isPresent()) { - String mergeBranchKey = mergeBranch.get().getKey(); - builder.setBase(mergeBranchKey); - } else { - builder.setIsOrphan(true); - } - - if (StringUtils.isNotEmpty(pullRequestData.getTarget())) { - builder.setTarget(pullRequestData.getTarget()); - } else if (mergeBranch.isPresent()) { - builder.setTarget(mergeBranch.get().getKey()); - } - - ofNullable(analysisDate).ifPresent(builder::setAnalysisDate); - setQualityGate(builder, qualityGateMeasure, prStatistics); - response.addPullRequests(builder); - } - - private static void setQualityGate(ProjectPullRequests.PullRequest.Builder builder, @Nullable LiveMeasureDto qualityGateMeasure, @Nullable PrStatistics prStatistics) { - ProjectPullRequests.Status.Builder statusBuilder = ProjectPullRequests.Status.newBuilder(); - if (qualityGateMeasure != null) { - ofNullable(qualityGateMeasure.getDataAsString()).ifPresent(statusBuilder::setQualityGateStatus); - } - statusBuilder.setBugs(prStatistics == null ? 0L : prStatistics.getBugs()); - statusBuilder.setVulnerabilities(prStatistics == null ? 0L : prStatistics.getVulnerabilities()); - statusBuilder.setCodeSmells(prStatistics == null ? 0L : prStatistics.getCodeSmells()); - builder.setStatus(statusBuilder); - } -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsAction.java deleted file mode 100644 index f75c2554a19..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsAction.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.sonar.server.ws.WsAction; - -public interface PullRequestWsAction extends WsAction { - // marker interface -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsModule.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsModule.java deleted file mode 100644 index 9a1e3fcdd04..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestWsModule.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.sonar.core.platform.Module; - -public class PullRequestWsModule extends Module { - @Override - protected void configureModule() { - add( - ListAction.class, - DeleteAction.class, - PullRequestsWs.class); - } -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWs.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWs.java deleted file mode 100644 index b4b37e27cbc..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWs.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.sonar.api.server.ws.WebService; - -import static java.util.Arrays.stream; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PULL_REQUEST; -import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; - -public class PullRequestsWs implements WebService { - private final PullRequestWsAction[] actions; - - public PullRequestsWs(PullRequestWsAction... actions) { - this.actions = actions; - } - - @Override - public void define(Context context) { - NewController controller = context.createController("api/project_pull_requests") - .setSince("7.1") - .setDescription("Manage pull request"); - stream(actions).forEach(action -> action.define(controller)); - controller.done(); - } - - static void addProjectParam(NewAction action) { - action - .createParam(PARAM_PROJECT) - .setDescription("Project key") - .setExampleValue(KEY_PROJECT_EXAMPLE_001) - .setRequired(true); - } - - static void addPullRequestParam(NewAction action) { - action - .createParam(PARAM_PULL_REQUEST) - .setDescription("Pull request id") - .setExampleValue("1543") - .setRequired(true); - } - -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWsParameters.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWsParameters.java deleted file mode 100644 index b2091a153eb..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/PullRequestsWsParameters.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -public class PullRequestsWsParameters { - - public static final String PARAM_PROJECT = "project"; - public static final String PARAM_COMPONENT = "component"; - public static final String PARAM_PULL_REQUEST = "pullRequest"; - - private PullRequestsWsParameters() { - // static utility class - } -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/package-info.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/package-info.java deleted file mode 100644 index d41a9187853..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/branch/pr/ws/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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. - */ -@ParametersAreNonnullByDefault -package org.sonar.server.branch.pr.ws; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/branch/pr/ws/list-example.json b/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/branch/pr/ws/list-example.json deleted file mode 100644 index 877d25c17ef..00000000000 --- a/server/sonar-webserver-webapi/src/main/resources/org/sonar/server/branch/pr/ws/list-example.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "pullRequests": [ - { - "key": "123", - "title": "Add feature X", - "branch": "feature/bar", - "base": "feature/foo", - "status": { - "qualityGateStatus": "OK", - "bugs": 0, - "vulnerabilities": 0, - "codeSmells": 0 - }, - "analysisDate": "2017-04-01T02:15:42+0200", - "url": "https://github.com/SonarSource/sonar-core-plugins/pull/32", - "target": "feature/foo" - } - ] -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/DeleteActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/DeleteActionTest.java deleted file mode 100644 index 778ad2d13cd..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/DeleteActionTest.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.junit.Rule; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.System2; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import org.sonar.db.component.BranchDto; -import org.sonar.db.component.ComponentDto; -import org.sonar.server.component.ComponentCleanerService; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.component.TestComponentFinder; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.exceptions.UnauthorizedException; -import org.sonar.server.tester.UserSessionRule; -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.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.sonar.db.component.BranchType.PULL_REQUEST; - -public class DeleteActionTest { - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - - private ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class); - private ComponentFinder componentFinder = TestComponentFinder.from(db); - - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - public WsActionTester ws = new WsActionTester(new DeleteAction(db.getDbClient(), componentFinder, userSession, componentCleanerService)); - - @Test - public void definition() { - WebService.Action definition = ws.getDef(); - - assertThat(definition.key()).isEqualTo("delete"); - assertThat(definition.isPost()).isTrue(); - assertThat(definition.isInternal()).isFalse(); - assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project", "pullRequest"); - assertThat(definition.since()).isEqualTo("7.1"); - } - - @Test - public void delete_pull_request() { - ComponentDto project = db.components().insertPublicProject(); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("1984").setBranchType(PULL_REQUEST)); - - userSession.logIn().addProjectPermission(UserRole.ADMIN, project); - - ws.newRequest() - .setParam("project", project.getKey()) - .setParam("pullRequest", "1984") - .execute(); - verifyDeletedKey("1984"); - } - - @Test - public void fail_if_missing_project_parameter() { - userSession.logIn(); - - assertThatThrownBy(() -> ws.newRequest().execute()) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("The 'project' parameter is missing"); - } - - @Test - public void fail_if_missing_pull_request_parameter() { - userSession.logIn(); - - assertThatThrownBy(() -> ws.newRequest().setParam("project", "projectName").execute()) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("The 'pullRequest' parameter is missing"); - } - - @Test - public void fail_if_not_logged_in() { - assertThatThrownBy(() -> ws.newRequest().execute()) - .isInstanceOf(UnauthorizedException.class) - .hasMessageContaining("Authentication is required"); - } - - @Test - public void fail_if_pull_request_does_not_exist() { - ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("orwell")); - userSession.logIn().addProjectPermission(UserRole.ADMIN, project); - - assertThatThrownBy(() -> ws.newRequest() - .setParam("project", project.getDbKey()) - .setParam("pullRequest", "1984") - .execute()) - .isInstanceOf(NotFoundException.class) - .hasMessageContaining("Pull request '1984' is not found for project 'orwell'"); - } - - @Test - public void fail_if_project_does_not_exist() { - userSession.logIn(); - - assertThatThrownBy(() -> ws.newRequest() - .setParam("project", "foo") - .setParam("pullRequest", "123") - .execute()) - .isInstanceOf(NotFoundException.class) - .hasMessageContaining("Project 'foo' not found"); - } - - private void verifyDeletedKey(String key) { - ArgumentCaptor argument = ArgumentCaptor.forClass(BranchDto.class); - verify(componentCleanerService).deleteBranch(any(DbSession.class), argument.capture()); - assertThat(argument.getValue().getKey()).isEqualTo(key); - } - -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/ListActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/ListActionTest.java deleted file mode 100644 index a362a6527ff..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/ListActionTest.java +++ /dev/null @@ -1,455 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.resources.ResourceTypes; -import org.sonar.api.server.ws.Change; -import org.sonar.api.server.ws.WebService; -import org.sonar.api.utils.DateUtils; -import org.sonar.api.utils.System2; -import org.sonar.api.web.UserRole; -import org.sonar.db.DbTester; -import org.sonar.db.component.BranchType; -import org.sonar.db.component.ComponentDto; -import org.sonar.db.component.ComponentTesting; -import org.sonar.db.component.ResourceTypesRule; -import org.sonar.db.metric.MetricDto; -import org.sonar.db.permission.GlobalPermission; -import org.sonar.db.protobuf.DbProjectBranches; -import org.sonar.db.rule.RuleDto; -import org.sonar.server.component.ComponentFinder; -import org.sonar.server.es.EsTester; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.issue.index.IssueIndex; -import org.sonar.server.issue.index.IssueIndexer; -import org.sonar.server.issue.index.IssueIteratorFactory; -import org.sonar.server.permission.index.PermissionIndexerTester; -import org.sonar.server.permission.index.WebAuthorizationTypeSupport; -import org.sonar.server.tester.UserSessionRule; -import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.MediaTypes; -import org.sonarqube.ws.ProjectPullRequests.ListWsResponse; -import org.sonarqube.ws.ProjectPullRequests.PullRequest; - -import static java.lang.String.format; -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.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE; -import static org.sonar.api.issue.Issue.RESOLUTION_FIXED; -import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; -import static org.sonar.api.resources.Qualifiers.PROJECT; -import static org.sonar.api.rules.RuleType.BUG; -import static org.sonar.api.rules.RuleType.CODE_SMELL; -import static org.sonar.api.rules.RuleType.VULNERABILITY; -import static org.sonar.api.utils.DateUtils.dateToLong; -import static org.sonar.api.utils.DateUtils.parseDateTime; -import static org.sonar.api.web.UserRole.CODEVIEWER; -import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION; -import static org.sonar.db.component.BranchType.BRANCH; -import static org.sonar.db.component.BranchType.PULL_REQUEST; -import static org.sonar.db.component.SnapshotTesting.newAnalysis; -import static org.sonar.server.branch.pr.ws.PullRequestsWsParameters.PARAM_PROJECT; -import static org.sonar.test.JsonAssert.assertJson; -import static org.sonarqube.ws.ProjectPullRequests.Status; - -public class ListActionTest { - - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); - @Rule - public EsTester es = EsTester.create(); - @Rule - public UserSessionRule userSession = UserSessionRule.standalone(); - - private MetricDto qualityGateStatus; - - private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(PROJECT); - private IssueIndexer issueIndexer = new IssueIndexer(es.client(), db.getDbClient(), new IssueIteratorFactory(db.getDbClient()), null); - private IssueIndex issueIndex = new IssueIndex(es.client(), System2.INSTANCE, userSession, new WebAuthorizationTypeSupport(userSession)); - private PermissionIndexerTester permissionIndexerTester = new PermissionIndexerTester(es, issueIndexer); - - public WsActionTester ws = new WsActionTester(new ListAction(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), resourceTypes), issueIndex)); - - @Before - public void setUp() { - qualityGateStatus = db.measures().insertMetric(m -> m.setKey(ALERT_STATUS_KEY)); - } - - @Test - public void definition() { - WebService.Action definition = ws.getDef(); - assertThat(definition.key()).isEqualTo("list"); - assertThat(definition.isPost()).isFalse(); - assertThat(definition.isInternal()).isFalse(); - assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("project"); - assertThat(definition.since()).isEqualTo("7.1"); - - assertThat(definition.changelog()) - .extracting(Change::getVersion, Change::getDescription) - .contains(tuple("8.4", "Response fields: 'bugs', 'vulnerabilities', 'codeSmells' are deprecated.")); - } - - @Test - public void json_example() { - ComponentDto project = db.components().insertPrivateProject(p -> p.setDbKey("sonarqube")); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature/foo").setBranchType(BRANCH)); - ComponentDto pullRequest = db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(branch.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder() - .setBranch("feature/bar") - .setTitle("Add feature X") - .setUrl("https://github.com/SonarSource/sonar-core-plugins/pull/32") - .build())); - db.getDbClient().snapshotDao().insert(db.getSession(), newAnalysis(pullRequest).setLast(true).setCreatedAt(DateUtils.parseDateTime("2017-04-01T01:15:42+0100").getTime())); - db.measures().insertLiveMeasure(pullRequest, qualityGateStatus, m -> m.setData("OK")); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - db.commit(); - - String json = ws.newRequest() - .setParam(PARAM_PROJECT, project.getKey()) - .execute() - .getInput(); - - assertJson(json).isSimilarTo(ws.getDef().responseExampleAsString()); - assertJson(ws.getDef().responseExampleAsString()).isSimilarTo(json); - } - - @Test - public void pull_request() { - ComponentDto project = db.components().insertPrivateProject(); - db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getDbKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey, PullRequest::getBranch, PullRequest::getIsOrphan, PullRequest::hasUrl, PullRequest::hasTitle) - .containsExactlyInAnyOrder(tuple("123", "feature/bar", false, false, false)); - } - - @Test - public void project_with_zero_branches() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - String json = ws.newRequest() - .setParam("project", project.getDbKey()) - .setMediaType(MediaTypes.JSON) - .execute() - .getInput(); - - assertJson(json).isSimilarTo("{\"pullRequests\": []}"); - } - - @Test - public void pull_requests() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto nonMainBranch = db.components().insertProjectBranch(project, - b -> b.setKey("branch1").setBranchType(BranchType.BRANCH)); - ComponentDto pullRequestOnNonMainBranch = db.components().insertProjectBranch(project, - b -> b.setKey("pull_request_on_branch1") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(nonMainBranch.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - ComponentDto pullRequestOnMaster = db.components().insertProjectBranch(project, - b -> b.setKey("pull_request_on_master") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey, PullRequest::getBase) - .containsExactlyInAnyOrder( - tuple(pullRequestOnNonMainBranch.getPullRequest(), nonMainBranch.getBranch()), - tuple(pullRequestOnMaster.getPullRequest(), "master")); - } - - @Test - public void base_branch_is_using_default_main_name_when_main_branch_has_no_name() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto pullRequest = db.components().insertProjectBranch(project, - b -> b.setKey("pr-123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder() - .setBranch("feature123").build())); - - ListWsResponse response = ws.newRequest() - .setParam("project", pullRequest.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequests(0)) - .extracting(PullRequest::getKey, PullRequest::getBase) - .containsExactlyInAnyOrder(pullRequest.getPullRequest(), "master"); - } - - @Test - public void pull_request_on_removed_branch() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto pullRequest = db.components().insertProjectBranch(project, - b -> b.setKey("pr-123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid("unknown") - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey, PullRequest::hasBase, PullRequest::getIsOrphan) - .containsExactlyInAnyOrder( - tuple(pullRequest.getPullRequest(), false, true)); - } - - @Test - public void status_on_pull_requests() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto nonMainBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); - ComponentDto pullRequest = db.components().insertProjectBranch(project, - b -> b.setKey("pr-123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(nonMainBranch.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - db.measures().insertLiveMeasure(pullRequest, qualityGateStatus, m -> m.setData("ERROR")); - RuleDto rule = db.rules().insert(); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(BUG).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(BUG).setResolution(RESOLUTION_FIXED)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(VULNERABILITY).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(VULNERABILITY).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(CODE_SMELL).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(CODE_SMELL).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(CODE_SMELL).setResolution(null)); - db.issues().insert(rule, pullRequest, pullRequest, i -> i.setType(CODE_SMELL).setResolution(RESOLUTION_FALSE_POSITIVE)); - indexIssues(); - permissionIndexerTester.allowOnlyAnyone(project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList().stream().map(PullRequest::getStatus)) - .extracting(Status::getQualityGateStatus, Status::hasBugs, Status::getBugs, Status::hasVulnerabilities, Status::getVulnerabilities, Status::hasCodeSmells, - Status::getCodeSmells) - .containsExactlyInAnyOrder(tuple("ERROR", true, 1L, true, 2L, true, 3L)); - } - - @Test - public void status_on_pull_request_with_no_issue() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto nonMainBranch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); - db.components().insertProjectBranch(project, - b -> b.setKey("pr-123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(nonMainBranch.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - indexIssues(); - permissionIndexerTester.allowOnlyAnyone(project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList().stream().map(PullRequest::getStatus)) - .extracting(Status::getBugs, Status::getVulnerabilities, Status::getCodeSmells) - .containsExactlyInAnyOrder(tuple(0L, 0L, 0L)); - } - - @Test - public void response_contains_date_of_last_analysis() { - Long lastAnalysisNonMainBranch = dateToLong(parseDateTime("2017-04-01T00:00:00+0100")); - Long previousAnalysisPullRequest = dateToLong(parseDateTime("2017-04-02T00:00:00+0100")); - Long lastAnalysisPullRequest = dateToLong(parseDateTime("2017-04-03T00:00:00+0100")); - - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - ComponentDto pullRequest1 = db.components().insertProjectBranch(project, - b -> b.setKey("pr1") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/pr1").build())); - - ComponentDto nonMainBranch2 = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH)); - - ComponentDto pullRequest2 = db.components().insertProjectBranch(project, - b -> b.setKey("pr2") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(nonMainBranch2.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/pr2").build())); - - db.getDbClient().snapshotDao().insert(db.getSession(), - newAnalysis(nonMainBranch2).setCreatedAt(lastAnalysisNonMainBranch)); - db.getDbClient().snapshotDao().insert(db.getSession(), - newAnalysis(pullRequest2).setCreatedAt(previousAnalysisPullRequest).setLast(false)); - db.getDbClient().snapshotDao().insert(db.getSession(), - newAnalysis(pullRequest2).setCreatedAt(lastAnalysisPullRequest)); - db.commit(); - indexIssues(); - permissionIndexerTester.allowOnlyAnyone(project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::hasAnalysisDate, b -> "".equals(b.getAnalysisDate()) ? null : dateToLong(parseDateTime(b.getAnalysisDate()))) - .containsExactlyInAnyOrder( - tuple(false, null), - tuple(true, lastAnalysisPullRequest)); - } - - private void indexIssues() { - issueIndexer.indexAllIssues(); - } - - @Test - public void does_not_fail_when_only_browse_permission_on_project() { - ComponentDto project = db.components().insertPrivateProject(); - db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey) - .containsExactlyInAnyOrder("123"); - } - - @Test - public void does_not_fail_when_only_scan_permission_on_project() { - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - userSession.logIn().addProjectPermission(SCAN_EXECUTION, project); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey) - .containsExactlyInAnyOrder("123"); - } - - @Test - public void does_not_fail_when_only_scan_permission() { - userSession.logIn().addPermission(GlobalPermission.SCAN); - ComponentDto project = db.components().insertPublicProject(); - db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - - ListWsResponse response = ws.newRequest() - .setParam("project", project.getKey()) - .executeProtobuf(ListWsResponse.class); - - assertThat(response.getPullRequestsList()) - .extracting(PullRequest::getKey) - .containsExactlyInAnyOrder("123"); - } - - @Test - public void fail_when_using_branch_db_key() { - ComponentDto project = db.components().insertPrivateProject(); - userSession.logIn().addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project); - - assertThatThrownBy(() -> ws.newRequest() - .setParam("project", branch.getDbKey()) - .execute()) - .isInstanceOf(NotFoundException.class) - .hasMessageContaining(format("Project '%s' not found", branch.getDbKey())); - } - - @Test - public void fail_if_missing_project_parameter() { - assertThatThrownBy(() -> ws.newRequest().execute()) - .isInstanceOf(IllegalArgumentException.class) - .hasMessageContaining("The 'project' parameter is missing"); - } - - @Test - public void fail_if_not_a_reference_on_project() { - ComponentDto project = db.components().insertPrivateProject(); - ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project)); - userSession.logIn().addProjectPermission(UserRole.USER, project); - - assertThatThrownBy(() -> ws.newRequest().setParam("project", file.getDbKey()).execute()) - .isInstanceOf(NotFoundException.class) - .hasMessageContaining("Project '" + file.getDbKey() + "' not found"); - } - - @Test - public void fail_if_project_does_not_exist() { - assertThatThrownBy(() -> ws.newRequest().setParam("project", "foo").execute()) - .isInstanceOf(NotFoundException.class) - .hasMessageContaining("Project 'foo' not found"); - } - - @Test - public void fail_when_not_having_right_permission() { - ComponentDto project = db.components().insertPrivateProject(); - db.components().insertProjectBranch(project, - b -> b.setKey("123") - .setBranchType(PULL_REQUEST) - .setMergeBranchUuid(project.uuid()) - .setPullRequestData(DbProjectBranches.PullRequestData.newBuilder().setBranch("feature/bar").build())); - userSession.logIn().addProjectPermission(CODEVIEWER, project); - - assertThatThrownBy(() -> ws.newRequest().setParam("project", project.getDbKey()).execute()) - .isInstanceOf(ForbiddenException.class); - } - -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java deleted file mode 100644 index 5439eaf98ec..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestWsModuleTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.junit.Test; -import org.sonar.core.platform.ListContainer; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PullRequestWsModuleTest { - @Test - public void verify_count_of_added_components() { - ListContainer container = new ListContainer(); - new PullRequestWsModule().configure(container); - assertThat(container.getAddedObjects()).hasSize(3); - } -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsParametersTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsParametersTest.java deleted file mode 100644 index f2ee5a29e0f..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsParametersTest.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.junit.Test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.sonar.test.TestUtils.hasOnlyPrivateConstructors; - -public class PullRequestsWsParametersTest { - - @Test - public void private_method() { - assertThat(hasOnlyPrivateConstructors(PullRequestsWsParameters.class)).isTrue(); - } -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsTest.java deleted file mode 100644 index 7f36884cfa4..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/pr/ws/PullRequestsWsTest.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.branch.pr.ws; - -import org.junit.Test; -import org.sonar.api.server.ws.Request; -import org.sonar.api.server.ws.Response; -import org.sonar.api.server.ws.WebService; - -import static org.assertj.core.api.Assertions.assertThat; - -public class PullRequestsWsTest { - - @Test - public void define_ws() { - PullRequestsWs underTest = new PullRequestsWs(new PullRequestWsAction() { - @Override - public void define(WebService.NewController context) { - context.createAction("foo").setHandler(this); - } - - @Override - public void handle(Request request, Response response) { - - } - }); - - WebService.Context context = new WebService.Context(); - underTest.define(context); - - assertThat(context.controller("api/project_pull_requests").action("foo")).isNotNull(); - } - -} diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/TestComponentFinder.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/TestComponentFinder.java deleted file mode 100644 index 0f631b5e5b6..00000000000 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/TestComponentFinder.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.component; - -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.ResourceTypes; -import org.sonar.db.DbClient; -import org.sonar.db.DbTester; -import org.sonar.db.component.ResourceTypesRule; - -public class TestComponentFinder extends ComponentFinder { - private TestComponentFinder(DbClient dbClient, ResourceTypes resourceTypes) { - super(dbClient, resourceTypes); - } - - public static TestComponentFinder from(DbTester dbTester) { - return new TestComponentFinder(dbTester.getDbClient(), new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT)); - } -} diff --git a/server/sonar-webserver-webapi/src/testFixtures/java/org/sonar/server/component/TestComponentFinder.java b/server/sonar-webserver-webapi/src/testFixtures/java/org/sonar/server/component/TestComponentFinder.java new file mode 100644 index 00000000000..0f631b5e5b6 --- /dev/null +++ b/server/sonar-webserver-webapi/src/testFixtures/java/org/sonar/server/component/TestComponentFinder.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2022 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.component; + +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.ResourceTypes; +import org.sonar.db.DbClient; +import org.sonar.db.DbTester; +import org.sonar.db.component.ResourceTypesRule; + +public class TestComponentFinder extends ComponentFinder { + private TestComponentFinder(DbClient dbClient, ResourceTypes resourceTypes) { + super(dbClient, resourceTypes); + } + + public static TestComponentFinder from(DbTester dbTester) { + return new TestComponentFinder(dbTester.getDbClient(), new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT)); + } +} diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 3e689002d2a..96ae215c715 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -68,7 +68,6 @@ import org.sonar.server.authentication.ws.AuthenticationWsModule; import org.sonar.server.badge.ws.ProjectBadgesWsModule; import org.sonar.server.batch.BatchWsModule; import org.sonar.server.branch.BranchFeatureProxyImpl; -import org.sonar.server.branch.pr.ws.PullRequestWsModule; import org.sonar.server.branch.ws.BranchWsModule; import org.sonar.server.ce.CeModule; import org.sonar.server.ce.projectdump.ProjectExportWsModule; @@ -413,7 +412,6 @@ public class PlatformLevel4 extends PlatformLevel { // components new BranchWsModule(), - new PullRequestWsModule(), new ProjectsWsModule(), new ProjectsEsModule(), new ProjectTagsWsModule(),