From 7ff8cad89d1ff47e74d4046b4830296104552fc2 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Fri, 30 Sep 2022 10:14:56 +0200 Subject: [PATCH] SONAR-17352 Refactor component keys to not include branch suffix --- .../org/sonar/db/component/BranchDto.java | 10 ++++ .../org/sonar/db/component/ComponentDto.java | 9 ---- .../component/ComponentKeyUpdaterDaoTest.java | 6 ++- .../server/issue/index/IssueQueryFactory.java | 25 +++++++--- .../sonar/server/component/ws/AppAction.java | 7 +-- .../ws/ComponentDtoToWsComponent.java | 6 +-- .../ws/ComponentViewerJsonWriter.java | 9 ++-- .../sonar/server/component/ws/ShowAction.java | 17 +++---- .../sonar/server/component/ws/TreeAction.java | 15 +++--- .../server/duplication/ws/ShowAction.java | 15 +++++- .../ws/HotspotWsResponseFormatter.java | 12 ++++- .../sonar/server/hotspot/ws/SearchAction.java | 47 ++++++++++--------- .../server/issue/ws/SearchResponseData.java | 12 +++++ .../server/issue/ws/SearchResponseFormat.java | 29 ++++++++++-- .../server/issue/ws/SearchResponseLoader.java | 10 ++++ .../server/measure/ws/ComponentAction.java | 5 +- .../measure/ws/ComponentDtoToWsComponent.java | 12 +++-- .../measure/ws/ComponentTreeAction.java | 10 ++-- .../server/source/ws/IssueSnippetsAction.java | 15 ++++-- .../server/ce/ws/ActivityActionTest.java | 8 ++-- .../server/component/ws/AppActionTest.java | 8 ++-- .../server/component/ws/ShowActionTest.java | 24 ++++++---- .../ws/SearchEventsActionNewIssuesTest.java | 7 +-- .../ws/DuplicationsParserTest.java | 6 ++- .../server/duplication/ws/ShowActionTest.java | 10 ++-- .../ws/ShowResponseBuilderTest.java | 10 ++-- .../server/hotspot/ws/SearchActionTest.java | 21 ++++----- .../server/hotspot/ws/ShowActionTest.java | 8 ++-- .../issue/ws/SearchActionComponentsTest.java | 13 +++-- ...archResponseFormatFormatOperationTest.java | 5 +- .../ws/ProjectStatusActionTest.java | 7 ++- .../server/source/ws/LinesActionTest.java | 6 ++- 32 files changed, 260 insertions(+), 144 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java index 755b6f8a274..35d5e85fef7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java @@ -200,6 +200,16 @@ public class BranchDto { return this; } + @CheckForNull + public String getBranchKey() { + return branchType == BranchType.BRANCH ? kee : null; + } + + @CheckForNull + public String getPullRequestKey() { + return branchType == BranchType.PULL_REQUEST ? kee : null; + } + @Override public boolean equals(Object o) { if (this == o) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java index cb17c2a859e..0ace5776a85 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDto.java @@ -193,15 +193,6 @@ public class ComponentDto { return split.size() == 2 ? split.get(1) : null; } - /** - * @return the pull request id. It will be null when the component is not on a pull request - */ - @CheckForNull - public String getPullRequest() { - List split = PULL_REQUEST_SPLITTER.splitToList(kee); - return split.size() == 2 ? split.get(1) : null; - } - public String scope() { return scope; } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java index e77ed5dd4e9..89992ed1c3f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentKeyUpdaterDaoTest.java @@ -23,6 +23,7 @@ import com.google.common.base.Strings; import java.util.List; import java.util.Map; import java.util.function.Predicate; +import org.apache.commons.lang.RandomStringUtils; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; @@ -186,7 +187,8 @@ public class ComponentKeyUpdaterDaoTest { @Test public void updateKey_updates_pull_requests_too() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + String pullRequestKey1 = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey1)); db.components().insertComponent(newFileDto(pullRequest)); db.components().insertComponent(newFileDto(pullRequest)); int branchComponentCount = 3; @@ -198,7 +200,7 @@ public class ComponentKeyUpdaterDaoTest { assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, oldBranchKey)).hasSize(branchComponentCount); String newProjectKey = "newKey"; - String newBranchKey = ComponentDto.generatePullRequestKey(newProjectKey, pullRequest.getPullRequest()); + String newBranchKey = ComponentDto.generatePullRequestKey(newProjectKey, pullRequestKey1); underTest.updateKey(dbSession, project.uuid(), newProjectKey); assertThat(dbClient.componentDao().selectAllComponentsFromProjectKey(dbSession, oldProjectKey)).isEmpty(); diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java index e1f6d21285e..22d72c4b921 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/issue/index/IssueQueryFactory.java @@ -50,6 +50,7 @@ import org.sonar.api.utils.log.Loggers; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.rule.RuleDto; @@ -284,11 +285,12 @@ public class IssueQueryFactory { .count() <= 1; } - private void addComponentParameters(IssueQuery.Builder builder, DbSession session, boolean onComponentOnly, List components, SearchRequest request) { + private void addComponentParameters(IssueQuery.Builder builder, DbSession session, boolean onComponentOnly, List components, + SearchRequest request) { builder.onComponentOnly(onComponentOnly); if (onComponentOnly) { builder.componentUuids(components.stream().map(ComponentDto::uuid).collect(toList())); - setBranch(builder, components.get(0), request.getBranch(), request.getPullRequest()); + setBranch(builder, components.get(0), request.getBranch(), request.getPullRequest(), session); return; } @@ -296,7 +298,7 @@ public class IssueQueryFactory { if (projectKeys != null) { List projects = getComponentsFromKeys(session, projectKeys, request.getBranch(), request.getPullRequest()); builder.projectUuids(projects.stream().map(IssueQueryFactory::toProjectUuid).collect(toList())); - setBranch(builder, projects.get(0), request.getBranch(), request.getPullRequest()); + setBranch(builder, projects.get(0), request.getBranch(), request.getPullRequest(), session); } builder.directories(request.getDirectories()); builder.files(request.getFiles()); @@ -316,7 +318,7 @@ public class IssueQueryFactory { Set qualifiers = components.stream().map(ComponentDto::qualifier).collect(toHashSet()); checkArgument(qualifiers.size() == 1, "All components must have the same qualifier, found %s", String.join(",", qualifiers)); - setBranch(builder, components.get(0), request.getBranch(), request.getPullRequest()); + setBranch(builder, components.get(0), request.getBranch(), request.getPullRequest(), dbSession); String qualifier = qualifiers.iterator().next(); switch (qualifier) { case Qualifiers.VIEW: @@ -345,6 +347,12 @@ public class IssueQueryFactory { } } + private BranchDto findComponentBranch(DbSession dbSession, ComponentDto componentDto) { + Optional optionalBranch = dbClient.branchDao().selectByUuid(dbSession, componentDto.branchUuid()); + checkArgument(optionalBranch.isPresent(), "All components must belong to a branch. This error may indicate corrupted data."); + return optionalBranch.get(); + } + private void addProjectUuidsForApplication(IssueQuery.Builder builder, DbSession session, SearchRequest request) { List projectKeys = request.getProjects(); if (projectKeys != null) { @@ -446,11 +454,14 @@ public class IssueQueryFactory { return mainBranchProjectUuid == null ? componentDto.branchUuid() : mainBranchProjectUuid; } - private static void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) { + private void setBranch(IssueQuery.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest, + DbSession session) { + BranchDto branchDto = findComponentBranch(session, component); + String componentBranch = branchDto.isMain() ? null : branchDto.getBranchKey(); builder.branchUuid(branch == null && pullRequest == null ? null : component.branchUuid()); builder.mainBranch(UNKNOWN_COMPONENT.equals(component) || (branch == null && pullRequest == null) - || (branch != null && !branch.equals(component.getBranch())) - || (pullRequest != null && !pullRequest.equals(component.getPullRequest()))); + || (branch != null && !branch.equals(componentBranch)) + || (pullRequest != null && !pullRequest.equals(branchDto.getPullRequestKey()))); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java index 534de8210c9..81a0a19503c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/AppAction.java @@ -88,7 +88,7 @@ public class AppAction implements ComponentsWsAction { try (DbSession session = dbClient.openSession(false)) { ComponentDto component = loadComponent(session, request); userSession.checkComponentPermission(UserRole.USER, component); - writeJsonResponse(response, session, component); + writeJsonResponse(response, session, component, request); } } @@ -100,10 +100,11 @@ public class AppAction implements ComponentsWsAction { return componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, componentKey, branch, pullRequest); } - private void writeJsonResponse(Response response, DbSession session, ComponentDto component) { + private void writeJsonResponse(Response response, DbSession session, ComponentDto component, Request request) { try (JsonWriter json = response.newJsonWriter()) { json.beginObject(); - componentViewerJsonWriter.writeComponent(json, component, userSession, session); + componentViewerJsonWriter.writeComponent(json, component, userSession, session, request.param(PARAM_BRANCH), + request.param(PARAM_PULL_REQUEST)); appendPermissions(json, userSession); componentViewerJsonWriter.writeMeasures(json, component, session); json.endObject(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java index 2af10c66712..5c3120d2c55 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentDtoToWsComponent.java @@ -66,13 +66,13 @@ class ComponentDtoToWsComponent { } public static Components.Component.Builder componentDtoToWsComponent(ComponentDto dto, @Nullable ProjectDto parentProjectDto, - @Nullable SnapshotDto lastAnalysis) { + @Nullable SnapshotDto lastAnalysis, @Nullable String branch, @Nullable String pullRequest) { Components.Component.Builder wsComponent = Components.Component.newBuilder() .setKey(dto.getKey()) .setName(dto.name()) .setQualifier(dto.qualifier()); - ofNullable(emptyToNull(dto.getBranch())).ifPresent(wsComponent::setBranch); - ofNullable(emptyToNull(dto.getPullRequest())).ifPresent(wsComponent::setPullRequest); + ofNullable(emptyToNull(branch)).ifPresent(wsComponent::setBranch); + ofNullable(emptyToNull(pullRequest)).ifPresent(wsComponent::setPullRequest); ofNullable(emptyToNull(dto.path())).ifPresent(wsComponent::setPath); ofNullable(emptyToNull(dto.description())).ifPresent(wsComponent::setDescription); ofNullable(emptyToNull(dto.language())).ifPresent(wsComponent::setLanguage); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentViewerJsonWriter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentViewerJsonWriter.java index dd3cc2629b0..95a95cad00c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentViewerJsonWriter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ComponentViewerJsonWriter.java @@ -62,7 +62,7 @@ public class ComponentViewerJsonWriter { this.dbClient = dbClient; } - public void writeComponentWithoutFav(JsonWriter json, ComponentDto component, DbSession session) { + public void writeComponentWithoutFav(JsonWriter json, ComponentDto component, DbSession session, @Nullable String branch, @Nullable String pullRequest) { json.prop("key", component.getKey()); json.prop("uuid", component.uuid()); json.prop("path", component.path()); @@ -74,18 +74,17 @@ public class ComponentViewerJsonWriter { json.prop("project", project.getKey()); json.prop("projectName", project.longName()); - String branch = project.getBranch(); if (branch != null) { json.prop("branch", branch); } - String pullRequest = project.getPullRequest(); if (pullRequest != null) { json.prop("pullRequest", pullRequest); } } - public void writeComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) { - writeComponentWithoutFav(json, component, session); + public void writeComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session, @Nullable String branch, + @Nullable String pullRequest) { + writeComponentWithoutFav(json, component, session, branch, pullRequest); List propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey("favourite") diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java index 86f340835e0..a4e4746278b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/ShowAction.java @@ -112,7 +112,7 @@ public class ShowAction implements ComponentsWsAction { userSession.checkComponentPermission(UserRole.USER, component); Optional lastAnalysis = dbClient.snapshotDao().selectLastAnalysisByComponentUuid(dbSession, component.branchUuid()); List ancestors = dbClient.componentDao().selectAncestors(dbSession, component); - return buildResponse(dbSession, component, ancestors, lastAnalysis.orElse(null)); + return buildResponse(dbSession, component, ancestors, lastAnalysis.orElse(null), request); } } @@ -125,22 +125,23 @@ public class ShowAction implements ComponentsWsAction { } private ShowWsResponse buildResponse(DbSession dbSession, ComponentDto component, List orderedAncestors, - @Nullable SnapshotDto lastAnalysis) { + @Nullable SnapshotDto lastAnalysis, Request request) { ShowWsResponse.Builder response = ShowWsResponse.newBuilder(); - response.setComponent(toWsComponent(dbSession, component, lastAnalysis)); - addAncestorsToResponse(dbSession, response, orderedAncestors, lastAnalysis); + response.setComponent(toWsComponent(dbSession, component, lastAnalysis, request)); + addAncestorsToResponse(dbSession, response, orderedAncestors, lastAnalysis, request); return response.build(); } private void addAncestorsToResponse(DbSession dbSession, ShowWsResponse.Builder response, List orderedAncestors, - @Nullable SnapshotDto lastAnalysis) { + @Nullable SnapshotDto lastAnalysis, Request request) { // ancestors are ordered from root to leaf, whereas it's the opposite in WS response int size = orderedAncestors.size() - 1; IntStream.rangeClosed(0, size).forEach( - index -> response.addAncestors(toWsComponent(dbSession, orderedAncestors.get(size - index), lastAnalysis))); + index -> response.addAncestors(toWsComponent(dbSession, orderedAncestors.get(size - index), lastAnalysis, request))); } - private Components.Component.Builder toWsComponent(DbSession dbSession, ComponentDto component, @Nullable SnapshotDto lastAnalysis) { + private Components.Component.Builder toWsComponent(DbSession dbSession, ComponentDto component, @Nullable SnapshotDto lastAnalysis, + Request request) { if (isProjectOrApp(component)) { ProjectDto project = dbClient.projectDao().selectProjectOrAppByKey(dbSession, component.getKey()) .orElseThrow(() -> new IllegalStateException("Project is in invalid state.")); @@ -151,7 +152,7 @@ public class ShowAction implements ComponentsWsAction { Optional parentProject = dbClient.projectDao().selectByUuid(dbSession, ofNullable(component.getMainBranchProjectUuid()).orElse(component.branchUuid())); boolean needIssueSync = needIssueSync(dbSession, component, parentProject.orElse(null)); - return componentDtoToWsComponent(component, parentProject.orElse(null), lastAnalysis) + return componentDtoToWsComponent(component, parentProject.orElse(null), lastAnalysis, request.branch, request.pullRequest) .setNeedIssueSync(needIssueSync); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java index 5fd5e5de2a1..246af12da7b 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ws/TreeAction.java @@ -184,9 +184,8 @@ public class TreeAction implements ComponentsWsAction { components = paginateComponents(components, treeRequest); Map referenceComponentsByUuid = searchReferenceComponentsByUuid(dbSession, components); - - return buildResponse(dbSession, baseComponent, components, referenceComponentsByUuid, - Paging.forPageIndex(treeRequest.getPage()).withPageSize(treeRequest.getPageSize()).andTotal(total)); + Paging paging = Paging.forPageIndex(treeRequest.getPage()).withPageSize(treeRequest.getPageSize()).andTotal(total); + return buildResponse(dbSession, baseComponent, components, referenceComponentsByUuid, paging, treeRequest); } } @@ -219,7 +218,7 @@ public class TreeAction implements ComponentsWsAction { } private TreeWsResponse buildResponse(DbSession dbSession, ComponentDto baseComponent, List components, - Map referenceComponentsByUuid, Paging paging) { + Map referenceComponentsByUuid, Paging paging, Request request) { TreeWsResponse.Builder response = TreeWsResponse.newBuilder(); response.getPagingBuilder() .setPageIndex(paging.pageIndex()) @@ -227,16 +226,16 @@ public class TreeAction implements ComponentsWsAction { .setTotal(paging.total()) .build(); - response.setBaseComponent(toWsComponent(dbSession, baseComponent, referenceComponentsByUuid)); + response.setBaseComponent(toWsComponent(dbSession, baseComponent, referenceComponentsByUuid, request)); for (ComponentDto dto : components) { - response.addComponents(toWsComponent(dbSession, dto, referenceComponentsByUuid)); + response.addComponents(toWsComponent(dbSession, dto, referenceComponentsByUuid, request)); } return response.build(); } private Components.Component.Builder toWsComponent(DbSession dbSession, ComponentDto component, - Map referenceComponentsByUuid) { + Map referenceComponentsByUuid, Request request) { Components.Component.Builder wsComponent; if (component.getMainBranchProjectUuid() == null && component.isRootProject() && @@ -246,7 +245,7 @@ public class TreeAction implements ComponentsWsAction { } else { Optional parentProject = dbClient.projectDao().selectByUuid(dbSession, ofNullable(component.getMainBranchProjectUuid()).orElse(component.branchUuid())); - wsComponent = componentDtoToWsComponent(component, parentProject.orElse(null), null); + wsComponent = componentDtoToWsComponent(component, parentProject.orElse(null), null, request.branch, request.pullRequest); } ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid()); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowAction.java index 4369241197e..094f8fe49a6 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/duplication/ws/ShowAction.java @@ -20,6 +20,7 @@ package org.sonar.server.duplication.ws; import java.util.List; +import java.util.Optional; import javax.annotation.CheckForNull; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.server.ws.Change; @@ -29,6 +30,7 @@ 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.ComponentDto; import org.sonar.db.measure.LiveMeasureDto; import org.sonar.server.component.ComponentFinder; @@ -96,15 +98,24 @@ public class ShowAction implements DuplicationsWsAction { public void handle(Request request, Response response) { try (DbSession dbSession = dbClient.openSession(false)) { ComponentDto component = loadComponent(dbSession, request); + BranchDto branchDto = loadBranch(dbSession, component); + String branch = branchDto.isMain() ? null : branchDto.getBranchKey(); + String pullRequest = branchDto.getPullRequestKey(); userSession.checkComponentPermission(UserRole.CODEVIEWER, component); String duplications = findDataFromComponent(dbSession, component); - String branch = component.getBranch(); - String pullRequest = component.getPullRequest(); List blocks = parser.parse(dbSession, component, branch, pullRequest, duplications); writeProtobuf(responseBuilder.build(dbSession, blocks, branch, pullRequest), request, response); } } + private BranchDto loadBranch(DbSession dbSession, ComponentDto component) { + Optional branchDto = dbClient.branchDao().selectByUuid(dbSession, component.branchUuid()); + if (branchDto.isEmpty()) { + throw new IllegalStateException("Could not find a branch for component with " + component.uuid()); + } + return branchDto.get(); + } + private ComponentDto loadComponent(DbSession dbSession, Request request) { String key = request.mandatoryParam(PARAM_KEY); String branch = request.param(PARAM_BRANCH); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java index 5c3490d17d0..2171dfeabb0 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java @@ -20,6 +20,7 @@ package org.sonar.server.hotspot.ws; import javax.annotation.Nullable; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonarqube.ws.Hotspots; @@ -31,7 +32,7 @@ public class HotspotWsResponseFormatter { // nothing to do here } - Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pr) { + Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable String branch, @Nullable String pullRequest) { builder .clear() .setKey(component.getKey()) @@ -39,9 +40,16 @@ public class HotspotWsResponseFormatter { .setName(component.name()) .setLongName(component.longName()); ofNullable(branch).ifPresent(builder::setBranch); - ofNullable(pr).ifPresent(builder::setPullRequest); + ofNullable(pullRequest).ifPresent(builder::setPullRequest); ofNullable(component.path()).ifPresent(builder::setPath); return builder.build(); } + Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, BranchDto branchDto) { + if (branchDto.isMain()) { + return formatComponent(builder, component, null, null); + } + return formatComponent(builder, component, branchDto.getBranchKey(), branchDto.getPullRequestKey()); + } + } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java index b913d82127b..f30266a6636 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java @@ -32,7 +32,6 @@ import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; -import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.lucene.search.TotalHits; import org.elasticsearch.action.search.SearchResponse; @@ -57,7 +56,6 @@ import org.sonar.db.issue.IssueDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.protobuf.DbIssues; import org.sonar.db.rule.RuleDto; -import org.sonar.server.component.ComponentFinder; import org.sonar.server.es.SearchOptions; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.issue.TextRangeResponseFormatter; @@ -348,7 +346,7 @@ public class SearchAction implements HotspotsWsAction { List hotspots = toIssueDtos(dbSession, issueKeys); Paging paging = forPageIndex(wsRequest.getPage()).withPageSize(wsRequest.getIndex()).andTotal((int) getTotalHits(result).value); - return new SearchResponseData(paging, hotspots, wsRequest.getBranch().orElse(null), wsRequest.getPullRequest().orElse(null)); + return new SearchResponseData(paging, hotspots); } private static TotalHits getTotalHits(SearchResponse response) { @@ -505,8 +503,7 @@ public class SearchAction implements HotspotsWsAction { Map leakByProjects = snapshots .stream() .filter(s -> isNullOrEmpty(s.getPeriodMode()) || !s.getPeriodMode().equals(REFERENCE_BRANCH.name())) - .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> - new IssueQuery.PeriodStart(longToDate(s.getPeriodDate() == null ? now : s.getPeriodDate()), false))); + .collect(uniqueIndex(SnapshotDto::getComponentUuid, s -> new IssueQuery.PeriodStart(longToDate(s.getPeriodDate() == null ? now : s.getPeriodDate()), false))); builder.createdAfterByProjectUuids(leakByProjects); builder.newCodeOnReferenceByProjectUuids(newCodeReferenceByProjects); @@ -527,7 +524,12 @@ public class SearchAction implements HotspotsWsAction { .collect(Collectors.toSet()); if (!aggregatedComponentUuids.isEmpty()) { - searchResponseData.addComponents(dbClient.componentDao().selectByUuids(dbSession, aggregatedComponentUuids)); + List componentDtos = dbClient.componentDao().selectByUuids(dbSession, aggregatedComponentUuids); + searchResponseData.addComponents(componentDtos); + + Set branchUuids = componentDtos.stream().map(ComponentDto::branchUuid).collect(Collectors.toSet()); + List branchDtos = dbClient.branchDao().selectByUuids(dbSession, branchUuids); + searchResponseData.addBranches(branchDtos); } } @@ -636,7 +638,11 @@ public class SearchAction implements HotspotsWsAction { Hotspots.Component.Builder builder = Hotspots.Component.newBuilder(); for (ComponentDto component : components) { - responseBuilder.addComponents(responseFormatter.formatComponent(builder, component, searchResponseData.getBranch(), searchResponseData.getPullRequest())); + BranchDto branchDto = searchResponseData.getBranch(component.branchUuid()); + if (branchDto == null) { + throw new IllegalStateException("Could not find a branch for a component " + component.getKey() + " with uuid " + component.uuid()); + } + responseBuilder.addComponents(responseFormatter.formatComponent(builder, component, branchDto)); } } @@ -774,26 +780,13 @@ public class SearchAction implements HotspotsWsAction { private static final class SearchResponseData { private final Paging paging; private final List orderedHotspots; - private final String branch; - private final String pullRequest; private final Map componentsByUuid = new HashMap<>(); private final Map rulesByRuleKey = new HashMap<>(); + private final Map branchesByBranchUuid = new HashMap<>(); - private SearchResponseData(Paging paging, List orderedHotspots, @Nullable String branch, @Nullable String pullRequest) { + private SearchResponseData(Paging paging, List orderedHotspots) { this.paging = paging; this.orderedHotspots = orderedHotspots; - this.branch = branch; - this.pullRequest = pullRequest; - } - - @CheckForNull - public String getBranch() { - return branch; - } - - @CheckForNull - public String getPullRequest() { - return pullRequest; } boolean isEmpty() { @@ -814,6 +807,16 @@ public class SearchAction implements HotspotsWsAction { } } + public void addBranches(List branchDtos) { + for (BranchDto branch : branchDtos) { + branchesByBranchUuid.put(branch.getUuid(), branch); + } + } + + public BranchDto getBranch(String branchUuid) { + return branchesByBranchUuid.get(branchUuid); + } + Collection getComponents() { return componentsByUuid.values(); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseData.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseData.java index 3b4ab79c2db..335aca838a5 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseData.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseData.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Set; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueDto; @@ -53,6 +54,7 @@ public class SearchResponseData { private final ListMultimap actionsByIssueKey = ArrayListMultimap.create(); private final ListMultimap transitionsByIssueKey = ArrayListMultimap.create(); private final Set updatableComments = new HashSet<>(); + private final Map branchesByBranchUuid = new HashMap<>(); public SearchResponseData(IssueDto issue) { checkNotNull(issue); @@ -139,6 +141,16 @@ public class SearchResponseData { } } + public void addBranches(List branchDtos) { + for (BranchDto branch : branchDtos) { + branchesByBranchUuid.put(branch.getUuid(), branch); + } + } + + public BranchDto getBranch(String branchUuid) { + return branchesByBranchUuid.get(branchUuid); + } + void addActions(String issueKey, Iterable actions) { actionsByIssueKey.putAll(issueKey, actions); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java index f62a2077ecb..702f183ffaf 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseFormat.java @@ -35,6 +35,8 @@ import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; import org.sonar.api.utils.Durations; import org.sonar.api.utils.Paging; +import org.sonar.db.component.BranchDto; +import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueDto; @@ -161,8 +163,7 @@ public class SearchResponseFormat { ComponentDto component = data.getComponentByUuid(dto.getComponentUuid()); issueBuilder.setComponent(component.getKey()); - ofNullable(component.getBranch()).ifPresent(issueBuilder::setBranch); - ofNullable(component.getPullRequest()).ifPresent(issueBuilder::setPullRequest); + setBranchOrPr(component, issueBuilder, data); ComponentDto project = data.getComponentByUuid(dto.getProjectUuid()); if (project != null) { issueBuilder.setProject(project.getKey()); @@ -306,8 +307,7 @@ public class SearchResponseFormat { .setName(nullToEmpty(dto.name())) .setLongName(nullToEmpty(dto.longName())) .setEnabled(dto.isEnabled()); - ofNullable(dto.getBranch()).ifPresent(builder::setBranch); - ofNullable(dto.getPullRequest()).ifPresent(builder::setPullRequest); + setBranchOrPr(dto, builder, data); ofNullable(emptyToNull(dto.path())).ifPresent(builder::setPath); result.add(builder.build()); @@ -315,6 +315,27 @@ public class SearchResponseFormat { return result; } + private static void setBranchOrPr(ComponentDto componentDto, Component.Builder builder, SearchResponseData data) { + BranchDto branchDto = data.getBranch(componentDto.branchUuid()); + if (branchDto.isMain()) { + return; + } + builder.setBranch(branchDto.getBranchKey()); + builder.setPullRequest(branchDto.getPullRequestKey()); + } + + private static void setBranchOrPr(ComponentDto componentDto, Issue.Builder builder, SearchResponseData data) { + BranchDto branchDto = data.getBranch(componentDto.branchUuid()); + if (branchDto.isMain()) { + return; + } + if (branchDto.getBranchType() == BranchType.BRANCH) { + builder.setBranch(branchDto.getKey()); + } else if (branchDto.getBranchType() == BranchType.PULL_REQUEST) { + builder.setPullRequest(branchDto.getKey()); + } + } + private Users.Builder formatUsers(SearchResponseData data) { Users.Builder wsUsers = Users.newBuilder(); List users = data.getUsers(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java index 3eab766fcd1..953ec69799c 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/SearchResponseLoader.java @@ -37,6 +37,7 @@ import org.sonar.core.issue.DefaultIssue; import org.sonar.core.util.stream.MoreCollectors; import org.sonar.db.DbClient; import org.sonar.db.DbSession; +import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueChangeDto; import org.sonar.db.issue.IssueDto; @@ -98,6 +99,9 @@ public class SearchResponseLoader { loadComments(collector, dbSession, fields, result); loadUsers(preloadedResponseData, collector, dbSession, result); loadComponents(preloadedResponseData, collector, dbSession, result); + // for all loaded components in result we "join" branches to know to which branch components belong + loadBranches(dbSession, result); + loadActionsAndTransitions(result, fields); completeTotalEffortFromFacet(facets, result); return result; @@ -150,6 +154,12 @@ public class SearchResponseLoader { loadProjects(collector, dbSession, result); } + private void loadBranches(DbSession dbSession, SearchResponseData result) { + Set branchUuids = result.getComponents().stream().map(ComponentDto::branchUuid).collect(Collectors.toSet()); + List branchDtos = dbClient.branchDao().selectByUuids(dbSession, branchUuids); + result.addBranches(branchDtos); + } + private void loadProjects(Collector collector, DbSession dbSession, SearchResponseData result) { Collection loadedComponents = result.getComponents(); for (ComponentDto component : loadedComponents) { diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java index da046ad0211..b645521e72d 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentAction.java @@ -245,9 +245,10 @@ public class ComponentAction implements MeasuresWsAction { ComponentWsResponse.Builder response = ComponentWsResponse.newBuilder(); if (refComponent.isPresent()) { - response.setComponent(componentDtoToWsComponent(component, measuresByMetric, singletonMap(refComponent.get().uuid(), refComponent.get()))); + response.setComponent(componentDtoToWsComponent(component, measuresByMetric, singletonMap(refComponent.get().uuid(), + refComponent.get()), request.getBranch(), request.getPullRequest())); } else { - response.setComponent(componentDtoToWsComponent(component, measuresByMetric, emptyMap())); + response.setComponent(componentDtoToWsComponent(component, measuresByMetric, emptyMap(), request.getBranch(), request.getPullRequest())); } List additionalFields = request.getAdditionalFields(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java index 77ee6758c95..51f07944940 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentDtoToWsComponent.java @@ -20,6 +20,7 @@ package org.sonar.server.measure.ws; import java.util.Map; +import javax.annotation.Nullable; import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.LiveMeasureDto; import org.sonar.db.metric.MetricDto; @@ -34,8 +35,9 @@ class ComponentDtoToWsComponent { } static Component.Builder componentDtoToWsComponent(ComponentDto component, Map measuresByMetric, - Map referenceComponentsByUuid) { - Component.Builder wsComponent = componentDtoToWsComponent(component); + Map referenceComponentsByUuid, @Nullable String branch, + @Nullable String pullRequest) { + Component.Builder wsComponent = componentDtoToWsComponent(component, branch, pullRequest); ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid()); if (referenceComponent != null) { @@ -52,13 +54,13 @@ class ComponentDtoToWsComponent { return wsComponent; } - static Component.Builder componentDtoToWsComponent(ComponentDto component) { + static Component.Builder componentDtoToWsComponent(ComponentDto component, @Nullable String branch, @Nullable String pullRequest) { Component.Builder wsComponent = Component.newBuilder() .setKey(component.getKey()) .setName(component.name()) .setQualifier(component.qualifier()); - ofNullable(component.getBranch()).ifPresent(wsComponent::setBranch); - ofNullable(component.getPullRequest()).ifPresent(wsComponent::setPullRequest); + ofNullable(branch).ifPresent(wsComponent::setBranch); + ofNullable(pullRequest).ifPresent(wsComponent::setPullRequest); ofNullable(component.path()).ifPresent(wsComponent::setPath); ofNullable(component.description()).ifPresent(wsComponent::setDescription); ofNullable(component.language()).ifPresent(wsComponent::setLanguage); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java index 64f179e5350..03a61ca8782 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeAction.java @@ -291,13 +291,13 @@ public class ComponentTreeAction implements MeasuresWsAction { toWsComponent( data.getBaseComponent(), data.getMeasuresByComponentUuidAndMetric().row(data.getBaseComponent().uuid()), - data.getReferenceComponentsByUuid())); + data.getReferenceComponentsByUuid(), request.getBranch(), request.getPullRequest())); for (ComponentDto componentDto : data.getComponents()) { response.addComponents(toWsComponent( componentDto, data.getMeasuresByComponentUuidAndMetric().row(componentDto.uuid()), - data.getReferenceComponentsByUuid())); + data.getReferenceComponentsByUuid(), request.getBranch(), request.getPullRequest())); } if (areMetricsInResponse(request)) { @@ -332,7 +332,7 @@ public class ComponentTreeAction implements MeasuresWsAction { .setPageSize(request.getPageSize()) .setTotal(0); if (baseComponent != null) { - response.setBaseComponent(componentDtoToWsComponent(baseComponent)); + response.setBaseComponent(componentDtoToWsComponent(baseComponent, request.getBranch(), request.getPullRequest())); } return response.build(); } @@ -373,8 +373,8 @@ public class ComponentTreeAction implements MeasuresWsAction { } private static Measures.Component.Builder toWsComponent(ComponentDto component, Map measures, - Map referenceComponentsByUuid) { - Measures.Component.Builder wsComponent = componentDtoToWsComponent(component); + Map referenceComponentsByUuid, @Nullable String branch, @Nullable String pullRequest) { + Measures.Component.Builder wsComponent = componentDtoToWsComponent(component, branch, pullRequest); ComponentDto referenceComponent = referenceComponentsByUuid.get(component.getCopyComponentUuid()); if (referenceComponent != null) { wsComponent.setRefKey(referenceComponent.getKey()); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java index d341ef6a1e4..48dda725e2e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/source/ws/IssueSnippetsAction.java @@ -34,6 +34,7 @@ import org.sonar.api.utils.text.JsonWriter; 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.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.issue.IssueDto; @@ -98,13 +99,19 @@ public class IssueSnippetsAction implements SourcesWsAction { Map> linesPerComponent = getLinesPerComponent(componentUuid, locations); Map componentsByUuid = dbClient.componentDao().selectByUuids(dbSession, linesPerComponent.keySet()) .stream().collect(Collectors.toMap(ComponentDto::uuid, c -> c)); + + Map branches = dbClient.branchDao() + .selectByUuids(dbSession, componentsByUuid.keySet()) + .stream() + .collect(Collectors.toMap(BranchDto::getUuid, b -> b)); + try (JsonWriter jsonWriter = response.newJsonWriter()) { jsonWriter.beginObject(); for (Map.Entry> e : linesPerComponent.entrySet()) { ComponentDto componentDto = componentsByUuid.get(e.getKey()); if (componentDto != null) { - writeSnippet(dbSession, jsonWriter, componentDto, e.getValue()); + writeSnippet(dbSession, jsonWriter, componentDto, e.getValue(), branches.get(componentDto.branchUuid())); } } @@ -114,7 +121,7 @@ public class IssueSnippetsAction implements SourcesWsAction { } } - private void writeSnippet(DbSession dbSession, JsonWriter writer, ComponentDto fileDto, Set lines) { + private void writeSnippet(DbSession dbSession, JsonWriter writer, ComponentDto fileDto, Set lines, BranchDto branchDto) { Optional> lineSourcesOpt = sourceService.getLines(dbSession, fileDto.uuid(), lines); if (lineSourcesOpt.isEmpty()) { return; @@ -129,7 +136,9 @@ public class IssueSnippetsAction implements SourcesWsAction { writer.name(fileDto.getKey()).beginObject(); writer.name("component").beginObject(); - componentViewerJsonWriter.writeComponentWithoutFav(writer, fileDto, dbSession); + String branch = branchDto.isMain() ? null : branchDto.getBranchKey(); + String pullRequest = branchDto.getPullRequestKey(); + componentViewerJsonWriter.writeComponentWithoutFav(writer, fileDto, dbSession, branch, pullRequest); componentViewerJsonWriter.writeMeasures(writer, fileDto, dbSession); writer.endObject(); linesJsonWriter.writeSource(lineSources, writer, periodDateSupplier); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java index 2af87ecd1bc..b1937071caa 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java @@ -23,6 +23,7 @@ import java.util.Date; import java.util.List; import java.util.stream.IntStream; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.server.ws.WebService.Param; @@ -458,10 +459,11 @@ public class ActivityActionTest { logInAsSystemAdministrator(); ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey)); SnapshotDto analysis = db.components().insertSnapshot(pullRequest); CeActivityDto activity = insertActivity("T1", project, SUCCESS, analysis); - insertCharacteristic(activity, PULL_REQUEST, pullRequest.getPullRequest()); + insertCharacteristic(activity, PULL_REQUEST, pullRequestKey); ActivityResponse response = ws.newRequest().executeProtobuf(ActivityResponse.class); @@ -469,7 +471,7 @@ public class ActivityActionTest { .extracting(Task::getId, Ce.Task::getPullRequest, Ce.Task::hasPullRequestTitle, Ce.Task::getStatus, Ce.Task::getComponentKey) .containsExactlyInAnyOrder( // TODO the pull request title must be loaded from db - tuple("T1", pullRequest.getPullRequest(), false, Ce.TaskStatus.SUCCESS, pullRequest.getKey())); + tuple("T1", pullRequestKey, false, Ce.TaskStatus.SUCCESS, pullRequest.getKey())); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java index 2b7e5c30c42..24786cb54af 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/AppActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.component.ws; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.server.ws.WebService; @@ -324,12 +325,13 @@ public class AppActionTest { public void component_and_pull_request_parameters_provided() { ComponentDto project = db.components().insertPrivateProject(); userSession.logIn("john").addProjectPermission(USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); String result = ws.newRequest() .setParam("component", file.getKey()) - .setParam("pullRequest", file.getPullRequest()) + .setParam("pullRequest", pullRequestKey) .execute() .getInput(); @@ -342,7 +344,7 @@ public class AppActionTest { " \"q\": \"" + file.qualifier() + "\",\n" + " \"project\": \"" + project.getKey() + "\",\n" + " \"projectName\": \"" + project.longName() + "\",\n" + - " \"pullRequest\": \"" + file.getPullRequest() + "\",\n" + + " \"pullRequest\": \"" + pullRequestKey + "\",\n" + " \"fav\": false,\n" + " \"canMarkAsFavorite\": true,\n" + " \"measures\": {}\n" + diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java index 6603f2a0197..61720cbfaea 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/component/ws/ShowActionTest.java @@ -22,6 +22,7 @@ package org.sonar.server.component.ws; import java.util.Date; import java.util.Optional; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.resources.Qualifiers; @@ -315,8 +316,10 @@ public class ShowActionTest { ComponentDto portfolio2 = db.components().insertPublicPortfolio(); ComponentDto subview = db.components().insertSubView(portfolio1); + String pullRequestKey1 = RandomStringUtils.randomAlphanumeric(100); ComponentDto project1 = db.components().insertPrivateProject(); - ComponentDto branch1 = db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setNeedIssueSync(true)); + ComponentDto branch1 = db.components().insertProjectBranch(project1, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey1) + .setNeedIssueSync(true)); ComponentDto module = db.components().insertComponent(newModuleDto(branch1)); ComponentDto directory = db.components().insertComponent(newDirectory(module, "dir")); ComponentDto file = db.components().insertComponent(newFileDto(directory)); @@ -326,7 +329,8 @@ public class ShowActionTest { ComponentDto branch3 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(false)); ComponentDto project3 = db.components().insertPrivateProject(); - ComponentDto branch4 = db.components().insertProjectBranch(project3, b -> b.setBranchType(PULL_REQUEST).setNeedIssueSync(false)); + String pullRequestKey4 = RandomStringUtils.randomAlphanumeric(100); + ComponentDto branch4 = db.components().insertProjectBranch(project3, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey4).setNeedIssueSync(false)); ComponentDto moduleOfBranch4 = db.components().insertComponent(newModuleDto(branch4)); ComponentDto directoryOfBranch4 = db.components().insertComponent(newDirectory(moduleOfBranch4, "dir")); ComponentDto fileOfBranch4 = db.components().insertComponent(newFileDto(directoryOfBranch4)); @@ -342,10 +346,10 @@ public class ShowActionTest { // if branch need sync it is propagated to other components assertNeedIssueSyncEqual(null, null, project1, true); - assertNeedIssueSyncEqual(branch1.getPullRequest(), null, branch1, true); - assertNeedIssueSyncEqual(branch1.getPullRequest(), null, module, true); - assertNeedIssueSyncEqual(branch1.getPullRequest(), null, directory, true); - assertNeedIssueSyncEqual(branch1.getPullRequest(), null, file, true); + assertNeedIssueSyncEqual(pullRequestKey1, null, branch1, true); + assertNeedIssueSyncEqual(pullRequestKey1, null, module, true); + assertNeedIssueSyncEqual(pullRequestKey1, null, directory, true); + assertNeedIssueSyncEqual(pullRequestKey1, null, file, true); assertNeedIssueSyncEqual(null, null, project2, true); assertNeedIssueSyncEqual(null, branch2.getBranch(), branch2, true); @@ -353,10 +357,10 @@ public class ShowActionTest { // if all branches are synced, need issue sync on project is is set to false assertNeedIssueSyncEqual(null, null, project3, false); - assertNeedIssueSyncEqual(branch4.getPullRequest(), null, branch4, false); - assertNeedIssueSyncEqual(branch4.getPullRequest(), null, moduleOfBranch4, false); - assertNeedIssueSyncEqual(branch4.getPullRequest(), null, directoryOfBranch4, false); - assertNeedIssueSyncEqual(branch4.getPullRequest(), null, fileOfBranch4, false); + assertNeedIssueSyncEqual(pullRequestKey4, null, branch4, false); + assertNeedIssueSyncEqual(pullRequestKey4, null, moduleOfBranch4, false); + assertNeedIssueSyncEqual(pullRequestKey4, null, directoryOfBranch4, false); + assertNeedIssueSyncEqual(pullRequestKey4, null, fileOfBranch4, false); assertNeedIssueSyncEqual(null, branch5.getBranch(), branch5, false); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java index 0aa1331da6b..7314720e63c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/developers/ws/SearchEventsActionNewIssuesTest.java @@ -211,7 +211,8 @@ public class SearchEventsActionNewIssuesTest { SnapshotDto nonMainBranchAnalysis = insertAnalysis(nonMainBranch, 1_500_000_000_000L); insertIssue(nonMainBranch, nonMainBranchAnalysis); insertIssue(nonMainBranch, nonMainBranchAnalysis); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey("42")); + String pullRequestKey = "42"; + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey)); SnapshotDto pullRequestAnalysis = insertAnalysis(pullRequest, 1_300_000_000_000L); insertIssue(pullRequest, pullRequestAnalysis); issueIndexer.indexAllIssues(); @@ -229,10 +230,10 @@ public class SearchEventsActionNewIssuesTest { format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&branch=%s", nonMainBranch.getKey(), encode(formatDateTime(from + 1_000L)), userSession.getLogin(), nonMainBranch.getBranch()), formatDateTime(nonMainBranchAnalysis.getCreatedAt())), - tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on pull request '%s'", project.name(), pullRequest.getPullRequest()), + tuple("NEW_ISSUES", project.getKey(), format("You have 1 new issue on project '%s' on pull request '%s'", project.name(), pullRequestKey), format("https://sonarcloud.io/project/issues?id=%s&createdAfter=%s&assignees=%s&resolved=false&pullRequest=%s", pullRequest.getKey(), encode(formatDateTime(from + 1_000L)), - userSession.getLogin(), pullRequest.getPullRequest()), + userSession.getLogin(), pullRequestKey), formatDateTime(pullRequestAnalysis.getCreatedAt()))); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java index 629d0ca0e12..a5160407b37 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/DuplicationsParserTest.java @@ -22,6 +22,7 @@ package org.sonar.server.duplication.ws; import com.google.common.collect.Iterables; import java.util.List; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.db.DbTester; @@ -279,10 +280,11 @@ public class DuplicationsParserTest { @Test public void duplication_on_pull_request() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file1 = db.components().insertComponent(newFileDto(pullRequest)); ComponentDto file2 = db.components().insertComponent(newFileDto(pullRequest)); - List blocks = parser.parse(db.getSession(), file1, null, pullRequest.getPullRequest(), + List blocks = parser.parse(db.getSession(), file1, null, pullRequestKey, format("\n" + " \n" + " \n" + diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java index ccfcd558cd2..a48bf77e94c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java @@ -20,6 +20,7 @@ package org.sonar.server.duplication.ws; import java.util.function.Function; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,7 +29,6 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; -import org.sonar.db.component.BranchType; import org.sonar.db.component.ComponentDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.component.TestComponentFinder; @@ -43,6 +43,7 @@ import org.sonar.server.ws.WsActionTester; 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.sonar.db.component.BranchType.PULL_REQUEST; import static org.sonar.db.component.ComponentTesting.newFileDto; import static org.sonar.db.component.SnapshotTesting.newAnalysis; import static org.sonar.test.JsonAssert.assertJson; @@ -157,7 +158,8 @@ public class ShowActionTest { public void duplications_by_file_key_and_pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = db.components().insertComponent(newFileDto(pullRequest)); db.measures().insertLiveMeasure(file, dataMetric, m -> m.setData(format("\n" + " \n" + @@ -168,7 +170,7 @@ public class ShowActionTest { String result = ws.newRequest() .setParam("key", file.getKey()) - .setParam("pullRequest", pullRequest.getPullRequest()) + .setParam("pullRequest", pullRequestKey) .execute() .getInput(); @@ -202,7 +204,7 @@ public class ShowActionTest { " }\n" + " }\n" + "}", - file.getKey(), file.longName(), file.uuid(), pullRequest.getKey(), pullRequest.uuid(), project.longName(), file.getPullRequest())); + file.getKey(), file.longName(), file.uuid(), pullRequest.getKey(), pullRequest.uuid(), project.longName(), pullRequestKey)); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java index 6d7454ee3d0..d764b0f1fe0 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/duplication/ws/ShowResponseBuilderTest.java @@ -23,6 +23,7 @@ import java.io.StringWriter; import java.util.Collections; import java.util.List; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.utils.text.JsonWriter; @@ -247,7 +248,8 @@ public class ShowResponseBuilderTest { @Test public void write_duplications_on_pull_request() { ComponentDto project = db.components().insertPublicProject(); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file1 = db.components().insertComponent(newFileDto(pullRequest)); ComponentDto file2 = db.components().insertComponent(newFileDto(pullRequest)); List blocks = newArrayList(); @@ -255,7 +257,7 @@ public class ShowResponseBuilderTest { Duplication.newComponent(file1, 57, 12), Duplication.newComponent(file2, 73, 12)))); - test(blocks, null, pullRequest.getPullRequest(), + test(blocks, null, pullRequestKey, "{\n" + " \"duplications\": [\n" + " {\n" + @@ -275,14 +277,14 @@ public class ShowResponseBuilderTest { " \"name\": \"" + file1.longName() + "\",\n" + " \"project\": \"" + pullRequest.getKey() + "\",\n" + " \"projectName\": \"" + pullRequest.longName() + "\",\n" + - " \"pullRequest\": \"" + pullRequest.getPullRequest() + "\",\n" + + " \"pullRequest\": \"" + pullRequestKey + "\",\n" + " },\n" + " \"2\": {\n" + " \"key\": \"" + file2.getKey() + "\",\n" + " \"name\": \"" + file2.longName() + "\",\n" + " \"project\": \"" + pullRequest.getKey() + "\",\n" + " \"projectName\": \"" + pullRequest.longName() + "\",\n" + - " \"pullRequest\": \"" + pullRequest.getPullRequest() + "\",\n" + + " \"pullRequest\": \"" + pullRequestKey + "\",\n" + " }\n" + " }" + "}"); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java index 38b9cc4eb41..8ac8fd6d910 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java @@ -36,6 +36,7 @@ import java.util.function.Consumer; import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -684,7 +685,7 @@ public class SearchActionTest { userSessionRule.registerComponents(project); indexPermissions(); ComponentDto branch = dbTester.components().insertProjectBranch(project); - ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST)); + ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST).setKey("prKey")); ComponentDto fileProject = dbTester.components().insertComponent(newFileDto(project)); ComponentDto fileBranch = dbTester.components().insertComponent(newFileDto(branch)); ComponentDto filePR = dbTester.components().insertComponent(newFileDto(pullRequest)); @@ -712,7 +713,7 @@ public class SearchActionTest { .executeProtobuf(SearchWsResponse.class); SearchWsResponse responseBranch = newRequest(branch) .executeProtobuf(SearchWsResponse.class); - SearchWsResponse responsePR = newRequest(pullRequest) + SearchWsResponse responsePR = newRequest(pullRequest, res -> res.setParam(PARAM_PULL_REQUEST, "prKey")) .executeProtobuf(SearchWsResponse.class); assertThat(responseProject.getHotspotsList()) @@ -1121,7 +1122,9 @@ public class SearchActionTest { @Test public void returns_pullRequest_field_of_components_of_pullRequest() { ComponentDto project = dbTester.components().insertPublicProject(); - ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, t -> t.setBranchType(BranchType.PULL_REQUEST) + .setKey(pullRequestKey)); userSessionRule.registerComponents(project, pullRequest); indexPermissions(); ComponentDto directory = dbTester.components().insertComponent(newDirectory(pullRequest, "donut/acme")); @@ -1132,7 +1135,7 @@ public class SearchActionTest { IssueDto projectHotspot = insertHotspot(pullRequest, pullRequest, rule); indexIssues(); - SearchWsResponse response = newRequest(pullRequest) + SearchWsResponse response = newRequest(pullRequest, r -> r.setParam(PARAM_PULL_REQUEST, "pullRequestKey")) .executeProtobuf(SearchWsResponse.class); assertThat(response.getHotspotsList()) @@ -1144,13 +1147,13 @@ public class SearchActionTest { Map componentByKey = response.getComponentsList().stream().collect(uniqueIndex(Component::getKey)); Component actualProject = componentByKey.get(project.getKey()); assertThat(actualProject.hasBranch()).isFalse(); - assertThat(actualProject.getPullRequest()).isEqualTo(pullRequest.getPullRequest()); + assertThat(actualProject.getPullRequest()).isEqualTo(pullRequestKey); Component actualDirectory = componentByKey.get(directory.getKey()); assertThat(actualDirectory.hasBranch()).isFalse(); - assertThat(actualDirectory.getPullRequest()).isEqualTo(pullRequest.getPullRequest()); + assertThat(actualDirectory.getPullRequest()).isEqualTo(pullRequestKey); Component actualFile = componentByKey.get(file.getKey()); assertThat(actualFile.hasBranch()).isFalse(); - assertThat(actualFile.getPullRequest()).isEqualTo(pullRequest.getPullRequest()); + assertThat(actualFile.getPullRequest()).isEqualTo(pullRequestKey); } @Test @@ -2035,10 +2038,6 @@ public class SearchActionTest { if (branch != null) { res.setParam(PARAM_BRANCH, branch); } - String pullRequest = project.getPullRequest(); - if (pullRequest != null) { - res.setParam(PARAM_PULL_REQUEST, pullRequest); - } if (status != null) { res.setParam(PARAM_STATUS, status); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java index eef8d335517..9e6d58a003d 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/ShowActionTest.java @@ -35,6 +35,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.Nullable; +import org.apache.commons.lang.RandomStringUtils; import org.assertj.core.groups.Tuple; import org.junit.Rule; import org.junit.Test; @@ -930,8 +931,9 @@ public class ShowActionTest { @Test public void returns_pullRequest_but_no_branch_on_component_and_project_on_pullRequest() { ComponentDto project = dbTester.components().insertPublicProject(); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); ComponentDto pullRequest = dbTester.components().insertProjectBranch(project, - t -> t.setBranchType(BranchType.PULL_REQUEST)); + t -> t.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = dbTester.components().insertComponent(newFileDto(pullRequest)); userSessionRule.registerComponents(project); RuleDto rule = newRule(SECURITY_HOTSPOT); @@ -944,8 +946,8 @@ public class ShowActionTest { Hotspots.ShowWsResponse response = newRequest(hotspot) .executeProtobuf(Hotspots.ShowWsResponse.class); - verifyComponent(response.getProject(), pullRequest, null, pullRequest.getPullRequest()); - verifyComponent(response.getComponent(), file, null, pullRequest.getPullRequest()); + verifyComponent(response.getProject(), pullRequest, null, pullRequestKey); + verifyComponent(response.getComponent(), file, null, pullRequestKey); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java index 26a262a21e0..acd829d61c5 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionComponentsTest.java @@ -22,6 +22,7 @@ package org.sonar.server.issue.ws; import java.time.Clock; import java.util.Arrays; import java.util.Date; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.resources.Languages; @@ -627,7 +628,9 @@ public class SearchActionComponentsTest { ComponentDto project = db.components().insertPrivateProject(); ComponentDto projectFile = db.components().insertComponent(newFileDto(project)); IssueDto projectIssue = db.issues().insertIssue(rule, project, projectFile); - ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto pullRequestFile = db.components().insertComponent(newFileDto(pullRequest)); IssueDto pullRequestIssue = db.issues().insertIssue(rule, pullRequest, pullRequestFile); allowAnyoneOnProjects(project); @@ -635,17 +638,17 @@ public class SearchActionComponentsTest { SearchWsResponse result = ws.newRequest() .setParam(PARAM_COMPONENT_KEYS, pullRequest.getKey()) - .setParam(PARAM_PULL_REQUEST, pullRequest.getPullRequest()) + .setParam(PARAM_PULL_REQUEST, pullRequestKey) .executeProtobuf(SearchWsResponse.class); assertThat(result.getIssuesList()) .extracting(Issue::getKey, Issue::getComponent, Issue::getPullRequest) - .containsExactlyInAnyOrder(tuple(pullRequestIssue.getKey(), pullRequestFile.getKey(), pullRequestFile.getPullRequest())); + .containsExactlyInAnyOrder(tuple(pullRequestIssue.getKey(), pullRequestFile.getKey(), pullRequestKey)); assertThat(result.getComponentsList()) .extracting(Issues.Component::getKey, Issues.Component::getPullRequest) .containsExactlyInAnyOrder( - tuple(pullRequestFile.getKey(), pullRequestFile.getPullRequest()), - tuple(pullRequest.getKey(), pullRequest.getPullRequest())); + tuple(pullRequestFile.getKey(), pullRequestKey), + tuple(pullRequest.getKey(), pullRequestKey)); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java index 6b98e7d82d8..cf185e9ed75 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchResponseFormatFormatOperationTest.java @@ -176,11 +176,12 @@ public class SearchResponseFormatFormatOperationTest { @Test public void formatOperation_should_add_pullrequest_on_issue() { - componentDto.setKey(randomAlphanumeric(5) + PULL_REQUEST_SEPARATOR + randomAlphanumeric(5)); + String pullRequestKey = randomAlphanumeric(5); + componentDto.setKey(randomAlphanumeric(5) + PULL_REQUEST_SEPARATOR + pullRequestKey); Operation result = searchResponseFormat.formatOperation(searchResponseData); - assertThat(result.getIssue().getPullRequest()).isEqualTo(componentDto.getPullRequest()); + assertThat(result.getIssue().getPullRequest()).isEqualTo(pullRequestKey); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java index a193ae44a12..f3a8d13fe36 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/ProjectStatusActionTest.java @@ -22,6 +22,7 @@ package org.sonar.server.qualitygate.ws; import java.io.IOException; import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.RandomStringUtils; import org.junit.Rule; import org.junit.Test; import org.sonar.api.measures.CoreMetrics; @@ -238,7 +239,9 @@ public class ProjectStatusActionTest { @Test public void return_live_status_when_pull_request_is_referenced_by_its_key() throws IOException { ComponentDto project = db.components().insertPrivateProject(); - ComponentDto pr = db.components().insertProjectBranch(project, branch -> branch.setBranchType(BranchType.PULL_REQUEST)); + String pullRequestKey = RandomStringUtils.randomAlphanumeric(100); + ComponentDto pr = db.components().insertProjectBranch(project, branch -> branch.setBranchType(BranchType.PULL_REQUEST) + .setKey(pullRequestKey)); dbClient.snapshotDao().insert(dbSession, newAnalysis(pr) .setPeriodMode("last_version") @@ -253,7 +256,7 @@ public class ProjectStatusActionTest { String response = ws.newRequest() .setParam(PARAM_PROJECT_KEY, project.getKey()) - .setParam(PARAM_PULL_REQUEST, pr.getPullRequest()) + .setParam(PARAM_PULL_REQUEST, pullRequestKey) .execute().getInput(); assertJson(response).isSimilarTo(getClass().getResource("project_status-example.json")); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java index fbbf4782250..1272acfd8cf 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/source/ws/LinesActionTest.java @@ -47,6 +47,7 @@ import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static java.lang.String.format; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -143,7 +144,8 @@ public class LinesActionTest { public void pull_request() { ComponentDto project = db.components().insertPrivateProject(); userSession.addProjectPermission(UserRole.USER, project); - ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST)); + String pullRequestKey = randomAlphanumeric(100); + ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey)); ComponentDto file = db.components().insertComponent(newFileDto(branch)); db.getDbClient().fileSourceDao().insert(db.getSession(), new FileSourceDto() .setUuid(Uuids.createFast()) @@ -157,7 +159,7 @@ public class LinesActionTest { tester.newRequest() .setParam("key", file.getKey()) - .setParam("pullRequest", file.getPullRequest()) + .setParam("pullRequest", pullRequestKey) .execute() .assertJson(getClass(), "show_source.json"); } -- 2.39.5