]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15092 Apply new code filter for application branches
authorJacek <jacek.poreda@sonarsource.com>
Tue, 29 Jun 2021 12:03:49 +0000 (14:03 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 30 Jun 2021 20:02:59 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/SearchAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/hotspot/ws/SearchActionTest.java

index befed43c4949806f047c63059e1969fe76c7a9f7..3ac7d24f7f95ff46193086bd1631c385d154e6ab 100644 (file)
@@ -49,6 +49,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.issue.IssueDto;
@@ -81,7 +82,6 @@ import static org.sonar.api.utils.DateUtils.formatDateTime;
 import static org.sonar.api.utils.DateUtils.longToDate;
 import static org.sonar.api.utils.Paging.forPageIndex;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
-import static org.sonar.core.util.stream.MoreCollectors.toSet;
 import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
 import static org.sonar.server.security.SecurityStandards.SANS_TOP_25_INSECURE_INTERACTION;
 import static org.sonar.server.security.SecurityStandards.SANS_TOP_25_POROUS_DEFENSES;
@@ -330,7 +330,9 @@ public class SearchAction implements HotspotsWsAction {
       String projectUuid = firstNonNull(project.getMainBranchProjectUuid(), project.uuid());
       if (Qualifiers.APP.equals(project.qualifier())) {
         builder.viewUuids(singletonList(projectUuid));
-        addCreatedAfterByProjects(builder, dbSession, wsRequest, project);
+        if (wsRequest.isSinceLeakPeriod() && !wsRequest.getPullRequest().isPresent()) {
+          addCreatedAfterByProjects(builder, dbSession, project);
+        }
       } else {
         builder.projectUuids(singletonList(projectUuid));
         if (wsRequest.isSinceLeakPeriod() && !wsRequest.getPullRequest().isPresent()) {
@@ -380,18 +382,21 @@ public class SearchAction implements HotspotsWsAction {
     return issueIndex.search(query, searchOptions);
   }
 
-  private void addCreatedAfterByProjects(IssueQuery.Builder builder, DbSession dbSession, WsRequest wsRequest, ComponentDto application) {
-    if (!wsRequest.isSinceLeakPeriod() || wsRequest.getPullRequest().isPresent()) {
-      return;
+  private void addCreatedAfterByProjects(IssueQuery.Builder builder, DbSession dbSession, ComponentDto application) {
+    Set<String> projectUuids;
+    if (application.getMainBranchProjectUuid() == null) {
+      projectUuids = dbClient.applicationProjectsDao().selectProjects(dbSession, application.uuid()).stream()
+        .map(ProjectDto::getUuid)
+        .collect(Collectors.toSet());
+    } else {
+      projectUuids = dbClient.applicationProjectsDao().selectProjectBranchesFromAppBranch(dbSession, application.uuid()).stream()
+        .map(BranchDto::getUuid)
+        .collect(Collectors.toSet());
     }
 
-    Set<String> projectUuids = dbClient.applicationProjectsDao().selectProjects(dbSession, application.uuid()).stream()
-      .map(ProjectDto::getUuid)
-      .collect(Collectors.toSet());
     long now = system2.now();
     Map<String, IssueQuery.PeriodStart> leakByProjects = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, projectUuids).stream()
-      .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);
   }
index 953350fa4e6eb19f3e950c464566a87af6c2659f..7cebd52f827262f0eb0b1912f7e8077bd30f433d 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
@@ -1523,7 +1524,7 @@ public class SearchActionTest {
   }
 
   @Test
-  public void returns_issues_when_sinceLeakPeriod_is_true_and_is_application() {
+  public void returns_issues_when_sinceLeakPeriod_is_true_and_is_application_for_main_branch() {
     long referenceDate = 800_996_999_332L;
 
     system2.setNow(referenceDate + 10_000);
@@ -1561,11 +1562,69 @@ public class SearchActionTest {
 
     SearchWsResponse responseOnLeak = newRequest(application,
       t -> t.setParam("sinceLeakPeriod", "true"))
-      .executeProtobuf(SearchWsResponse.class);
+        .executeProtobuf(SearchWsResponse.class);
     assertThat(responseOnLeak.getHotspotsList())
       .extracting(SearchWsResponse.Hotspot::getKey)
       .containsExactlyInAnyOrder(afterRef.getKey());
+  }
+
+  @Test
+  public void returns_issues_when_sinceLeakPeriod_is_true_and_is_application_for_branch_other_than_main() {
+    long referenceDate = 800_996_999_332L;
+
+    system2.setNow(referenceDate + 10_000);
+    ProjectDto application = dbTester.components().insertPublicApplicationDto();
+    BranchDto applicationBranch = dbTester.components().insertProjectBranch(application, branchDto -> branchDto.setKey("application_branch_1"));
+    ProjectDto project = dbTester.components().insertPublicProjectDto();
+    BranchDto projectBranch = dbTester.components().insertProjectBranch(project, branchDto -> branchDto.setKey("project_1_branch_1"));
+
+    ProjectDto project2 = dbTester.components().insertPublicProjectDto();
+    BranchDto project2Branch = dbTester.components().insertProjectBranch(project2, branchDto -> branchDto.setKey("project_2_branch_1"));
+
+    dbTester.components().addApplicationProject(application, project);
+    dbTester.components().addApplicationProject(application, project2);
+
+    dbTester.components().addProjectBranchToApplicationBranch(applicationBranch, projectBranch, project2Branch);
+
+    ComponentDto applicationBranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), applicationBranch.getUuid()).get();
+    ComponentDto projectBranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), projectBranch.getUuid()).get();
+    ComponentDto project2BranchComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), project2Branch.getUuid()).get();
+
+    dbTester.components().insertComponent(ComponentTesting.newProjectCopy(projectBranchComponentDto, applicationBranchComponentDto));
+    dbTester.components().insertComponent(ComponentTesting.newProjectCopy(project2BranchComponentDto, applicationBranchComponentDto));
+
+    indexViews();
 
+    userSessionRule.registerProjects(application, project, project2);
+    indexPermissions();
+
+    ComponentDto file = dbTester.components().insertComponent(newFileDto(projectBranchComponentDto));
+    dbTester.components().insertSnapshot(projectBranch, t -> t.setPeriodDate(referenceDate).setLast(true));
+
+    RuleDefinitionDto rule = newRule(SECURITY_HOTSPOT);
+    IssueDto afterRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setIssueCreationTime(referenceDate + 1000));
+    IssueDto atRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setType(SECURITY_HOTSPOT).setIssueCreationTime(referenceDate));
+    IssueDto beforeRef = dbTester.issues().insertHotspot(rule, projectBranchComponentDto, file, t -> t.setIssueCreationTime(referenceDate - 1000));
+
+    ComponentDto file2 = dbTester.components().insertComponent(newFileDto(project2BranchComponentDto));
+    IssueDto project2Issue = dbTester.issues().insertHotspot(rule, project2BranchComponentDto, file2, t -> t.setIssueCreationTime(referenceDate - 1000));
+
+    indexIssues();
+
+    ComponentDto applicationComponentDto = dbClient.componentDao().selectByUuid(dbTester.getSession(), application.getUuid()).get();
+    SearchWsResponse responseAll = newRequest(applicationComponentDto,
+      t -> t.setParam("branch", applicationBranch.getKey()))
+        .executeProtobuf(SearchWsResponse.class);
+    assertThat(responseAll.getHotspotsList())
+      .extracting(SearchWsResponse.Hotspot::getKey)
+      .containsExactlyInAnyOrder(afterRef.getKey(), atRef.getKey(), beforeRef.getKey(), project2Issue.getKey());
+
+    SearchWsResponse responseOnLeak = newRequest(applicationComponentDto,
+      t -> t.setParam("sinceLeakPeriod", "true").setParam("branch", applicationBranch.getKey()))
+        .executeProtobuf(SearchWsResponse.class);
+    assertThat(responseOnLeak.getHotspotsList())
+      .extracting(SearchWsResponse.Hotspot::getKey)
+      .containsExactlyInAnyOrder(afterRef.getKey());
   }
 
   @Test