]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19558 Fix usage of indexOnAnalysis and minor fixes
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Tue, 4 Jul 2023 15:35:22 +0000 (17:35 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 5 Jul 2023 20:03:04 +0000 (20:03 +0000)
21 files changed:
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/purge/IndexPurgeListener.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/purge/IndexPurgeListenerTest.java
server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeCommands.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/purge/PurgeListener.java
server/sonar-server-common/src/it/java/org/sonar/server/component/index/EntityDefinitionIndexerIT.java
server/sonar-server-common/src/it/java/org/sonar/server/measure/index/ProjectMeasuresIndexerIT.java
server/sonar-server-common/src/main/java/org/sonar/server/component/index/EntityDefinitionIndexer.java
server/sonar-server-common/src/main/java/org/sonar/server/es/BranchIndexer.java [deleted file]
server/sonar-server-common/src/main/java/org/sonar/server/issue/index/IssueIndexer.java
server/sonar-server-common/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndexer.java
server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java
server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/FooIndexer.java
server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentService.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/issue/ws/AuthorsAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/live/LiveMeasureComputerImpl.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/CreateAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java

index a77b11cf80fd70a1e562efc5969f558aa4138296..93dabb5a7f4d92095edfcefcb887a06d65495e1e 100644 (file)
  */
 package org.sonar.ce.task.projectanalysis.purge;
 
-import java.util.Collection;
 import java.util.List;
 import org.sonar.api.server.ServerSide;
 import org.sonar.db.purge.PurgeListener;
-import org.sonar.server.component.index.EntityDefinitionIndexer;
 import org.sonar.server.issue.index.IssueIndexer;
 
 @ServerSide
 public class IndexPurgeListener implements PurgeListener {
   private final IssueIndexer issueIndexer;
-  private final EntityDefinitionIndexer entityDefinitionIndexer;
 
-  public IndexPurgeListener(IssueIndexer issueIndexer, EntityDefinitionIndexer entityDefinitionIndexer) {
+  public IndexPurgeListener(IssueIndexer issueIndexer) {
     this.issueIndexer = issueIndexer;
-    this.entityDefinitionIndexer = entityDefinitionIndexer;
-  }
-
-  @Override
-  public void onComponentsDisabling(String projectUuid, Collection<String> disabledComponentUuids) {
-    entityDefinitionIndexer.delete(projectUuid, disabledComponentUuids);
   }
 
   @Override
index a5d9ad2e54e64ec785651547784846f87a31c433..0eacdcf2aa3e3695cd39f4b840d403b030fa5a4f 100644 (file)
  */
 package org.sonar.ce.task.projectanalysis.purge;
 
-import java.util.List;
 import org.junit.Test;
 import org.sonar.server.component.index.EntityDefinitionIndexer;
 import org.sonar.server.issue.index.IssueIndexer;
 
 import static java.util.Arrays.asList;
-import static java.util.Collections.singletonList;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 public class IndexPurgeListenerTest {
 
   private IssueIndexer issueIndexer = mock(IssueIndexer.class);
-  private EntityDefinitionIndexer entityDefinitionIndexer = mock(EntityDefinitionIndexer.class);
-
-  private IndexPurgeListener underTest = new IndexPurgeListener(issueIndexer, entityDefinitionIndexer);
-
-  @Test
-  public void test_onComponentDisabling() {
-    String uuid = "123456";
-    String projectUuid = "P789";
-    List<String> uuids = singletonList(uuid);
-    underTest.onComponentsDisabling(projectUuid, uuids);
-
-    verify(entityDefinitionIndexer).delete(projectUuid, uuids);
-  }
+  private IndexPurgeListener underTest = new IndexPurgeListener(issueIndexer);
 
   @Test
   public void test_onIssuesRemoval() {
index 6ddf1800c4d0d8a071cbfc65e9523dcbf021210f..79b85148b6089e9745822a23d1a957b091f8f4b7 100644 (file)
@@ -91,13 +91,11 @@ import org.sonar.db.webhook.WebhookDto;
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.time.ZoneOffset.UTC;
 import static java.util.Arrays.asList;
-import static java.util.Collections.emptySet;
 import static java.util.Collections.singletonList;
 import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.groups.Tuple.tuple;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoInteractions;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.ce.CeTaskTypes.REPORT;
@@ -317,9 +315,6 @@ public class PurgeDaoIT {
       purgeListener, new PurgeProfiler());
     dbSession.commit();
 
-    verify(purgeListener).onComponentsDisabling(mainBranch.uuid(), selectedComponentUuids);
-    verify(purgeListener).onComponentsDisabling(mainBranch.uuid(), ImmutableSet.of(dir.uuid()));
-
     // set purge_status=1 for non-last snapshot
     assertThat(db.countSql("select count(*) from snapshots where purge_status = 1")).isOne();
 
@@ -1266,7 +1261,6 @@ public class PurgeDaoIT {
       .extracting("uuid")
       .containsOnly(mainBranch.uuid(), enabledFileWithIssues.uuid(), disabledFileWithIssues.uuid(),
         enabledFileWithoutIssues.uuid());
-    verify(purgeListener).onComponentsDisabling(mainBranch.uuid(), disabledComponentUuids);
   }
 
   @Test
index 3c03afa23b0a735a9719835cecec92b7affda38c..a366f11c717993fec221ef87d6068a2911f0548b 100644 (file)
@@ -31,11 +31,6 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 public class BranchDto {
   public static final String DEFAULT_MAIN_BRANCH_NAME = "main";
-  /**
-   * The only use of this constant is for applications that were created before SQ 9.8. Sometimes (in example during XML import of
-   * portfolios and applications) we don't have access to default branch name of the application so we default to "master".
-   */
-  public static final String OLD_DEFAULT_MAIN_BRANCH_NAME = "master";
 
   /**
    * Maximum length of column "kee"
index 964059a1164333905ebec0410195f8a297659412..fca4f6b9805a7815eda76a5703268309b99b52c7 100644 (file)
@@ -22,9 +22,7 @@ package org.sonar.db.purge;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Lists;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 import javax.annotation.Nullable;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
@@ -139,46 +137,35 @@ class PurgeCommands {
   }
 
   void purgeDisabledComponents(String rootComponentUuid, Collection<String> disabledComponentUuids, PurgeListener listener) {
-    Set<String> missedDisabledComponentUuids = new HashSet<>();
 
     profiler.start("purgeDisabledComponents (file_sources)");
-    missedDisabledComponentUuids.addAll(
       executeLargeInputs(
         purgeMapper.selectDisabledComponentsWithFileSource(rootComponentUuid),
         input -> {
           purgeMapper.deleteFileSourcesByFileUuid(input);
           return input;
-        }));
+        });
     profiler.stop();
 
     profiler.start("purgeDisabledComponents (unresolved_issues)");
-    missedDisabledComponentUuids.addAll(
       executeLargeInputs(
         purgeMapper.selectDisabledComponentsWithUnresolvedIssues(rootComponentUuid),
         input -> {
           purgeMapper.resolveComponentIssuesNotAlreadyResolved(input, system2.now());
           return input;
-        }));
+        });
     profiler.stop();
 
     profiler.start("purgeDisabledComponents (live_measures)");
-    missedDisabledComponentUuids.addAll(
       executeLargeInputs(
         purgeMapper.selectDisabledComponentsWithLiveMeasures(rootComponentUuid),
         input -> {
           purgeMapper.deleteLiveMeasuresByComponentUuids(input);
           return input;
-        }));
+        });
     profiler.stop();
 
     session.commit();
-
-    // notify listener for any disabled component we found child data for which isn't part of the disabled components
-    // provided
-    missedDisabledComponentUuids.removeAll(disabledComponentUuids);
-    if (!missedDisabledComponentUuids.isEmpty()) {
-      listener.onComponentsDisabling(rootComponentUuid, missedDisabledComponentUuids);
-    }
   }
 
   private void deleteAnalysisDuplications(List<List<String>> snapshotUuidsPartitions) {
index 02fd941f2e75996fe466f1012c8f679109feee33..15a7017d198ad29a8629e4323118da4d93c41c2b 100644 (file)
@@ -104,7 +104,6 @@ public class PurgeDao implements Dao {
 
   private static void purgeDisabledComponents(PurgeCommands commands, PurgeConfiguration conf, PurgeListener listener) {
     String rootUuid = conf.rootUuid();
-    listener.onComponentsDisabling(rootUuid, conf.getDisabledComponentUuids());
     commands.purgeDisabledComponents(rootUuid, conf.getDisabledComponentUuids(), listener);
   }
 
index 3179a171bbe3fb682fba759049c7f440fe9de9e6..05dcce07ffce6b269a041240fc54241650c32500 100644 (file)
  */
 package org.sonar.db.purge;
 
-import java.util.Collection;
 import java.util.List;
 
 public interface PurgeListener {
 
   PurgeListener EMPTY = new PurgeListener() {
-    @Override
-    public void onComponentsDisabling(String uuid, Collection<String> disabledComponentUuids) {
-      // do nothing
-    }
-
     @Override
     public void onIssuesRemoval(String projectUuid, List<String> issueKeys) {
       // do nothing
     }
   };
 
-  void onComponentsDisabling(String uuid, Collection<String> disabledComponentUuids);
-
   void onIssuesRemoval(String projectUuid, List<String> issueKeys);
 }
index a3bdaa863e6d871817c718e2982b5b1e10ebb8dc..ce5527dfef8fe0d078f65e1534c8b5ac730a1367 100644 (file)
@@ -30,7 +30,6 @@ import org.sonar.db.DbClient;
 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.db.component.ProjectData;
 import org.sonar.db.entity.EntityDto;
 import org.sonar.db.es.EsQueueDto;
@@ -47,9 +46,9 @@ import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
 import static org.sonar.api.resources.Qualifiers.PROJECT;
 import static org.sonar.server.component.index.ComponentIndexDefinition.FIELD_NAME;
 import static org.sonar.server.component.index.ComponentIndexDefinition.TYPE_COMPONENT;
-import static org.sonar.server.es.Indexers.EntityEvent.PERMISSION_CHANGE;
 import static org.sonar.server.es.Indexers.EntityEvent.CREATION;
 import static org.sonar.server.es.Indexers.EntityEvent.DELETION;
+import static org.sonar.server.es.Indexers.EntityEvent.PERMISSION_CHANGE;
 import static org.sonar.server.es.Indexers.EntityEvent.PROJECT_TAGS_UPDATE;
 import static org.sonar.server.es.newindex.DefaultIndexSettingsElement.SORTABLE_ANALYZER;
 
@@ -180,16 +179,6 @@ public class EntityDefinitionIndexerIT {
     assertThat(result.getSuccess()).isOne();
   }
 
-  @Test
-  public void delete_some_components() {
-    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
-    indexProject(project, CREATION);
-
-    underTest.delete(project.getUuid(), emptySet());
-
-    assertThatIndexContainsOnly(project);
-  }
-
   @Test
   public void delete_project() {
     ProjectDto project = db.components().insertPrivateProject().getProjectDto();
index c186dc93fac2140bcf0500611d32c6da7fc379ac..cc4f834dce2f4b53c761e35b8988d35033e5024f 100644 (file)
@@ -32,7 +32,9 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.utils.System2;
 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.db.component.ProjectData;
 import org.sonar.db.component.SnapshotDto;
 import org.sonar.db.es.EsQueueDto;
 import org.sonar.db.project.ProjectDto;
@@ -172,22 +174,32 @@ public class ProjectMeasuresIndexerIT {
 
   @Test
   public void indexOnAnalysis_indexes_provisioned_project() {
-    ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
-    ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
+    ProjectData project1 = db.components().insertPrivateProject();
+    ProjectData project2 = db.components().insertPrivateProject();
+
+    underTest.indexOnAnalysis(project1.getMainBranchComponent().uuid());
 
-    underTest.indexOnAnalysis(project1.getUuid());
+    assertThatIndexContainsOnly(project1.getProjectDto());
+  }
+
+  @Test
+  public void indexOnAnalysis_whenPassingANonMainBranch_ShouldNotIndexProject() {
+    ProjectData project1 = db.components().insertPrivateProject();
+    ProjectData project2 = db.components().insertPrivateProject();
+    BranchDto branchDto = db.components().insertProjectBranch(project1.getProjectDto());
+    underTest.indexOnAnalysis(branchDto.getUuid());
 
-    assertThatIndexContainsOnly(project1);
+    assertThatIndexContainsOnly(new ProjectDto[]{});
   }
 
   @Test
   public void indexOnAnalysis_indexes_provisioned_application() {
-    ProjectDto app1 = db.components().insertPrivateApplication().getProjectDto();
-    ProjectDto app2 = db.components().insertPrivateApplication().getProjectDto();
+    ProjectData app1 = db.components().insertPrivateApplication();
+    ProjectData app2 = db.components().insertPrivateApplication();
 
-    underTest.indexOnAnalysis(app1.getUuid());
+    underTest.indexOnAnalysis(app1.getMainBranchComponent().uuid());
 
-    assertThatIndexContainsOnly(app1);
+    assertThatIndexContainsOnly(app1.getProjectDto());
   }
 
   @Test
index 3f7a7f2e9c52131de022a87117396144b528a311..180eae9230febe3108d12b1e189e2a69bfe16ae1 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.component.index;
 
 import com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashSet;
@@ -113,14 +114,18 @@ public class EntityDefinitionIndexer implements EventIndexer, AnalysisIndexer, N
         // measures, tags and permissions does not affect the definition of entities
         emptyList();
       case CREATION, DELETION, PROJECT_KEY_UPDATE -> {
-        List<EsQueueDto> items = entityUuids.stream()
-          .map(entityUuid -> EsQueueDto.create(TYPE_COMPONENT.format(), entityUuid, null, entityUuid))
-          .collect(MoreCollectors.toArrayList(entityUuids.size()));
+        List<EsQueueDto> items = createEsQueueDtosFromEntities(entityUuids);
         yield dbClient.esQueueDao().insert(dbSession, items);
       }
     };
   }
 
+  private static ArrayList<EsQueueDto> createEsQueueDtosFromEntities(Collection<String> entityUuids) {
+    return entityUuids.stream()
+      .map(entityUuid -> EsQueueDto.create(TYPE_COMPONENT.format(), entityUuid, null, entityUuid))
+      .collect(MoreCollectors.toArrayList(entityUuids.size()));
+  }
+
   @Override
   public Collection<EsQueueDto> prepareForRecoveryOnBranchEvent(DbSession dbSession, Collection<String> branchUuids, Indexers.BranchEvent cause) {
     return emptyList();
@@ -190,13 +195,6 @@ public class EntityDefinitionIndexer implements EventIndexer, AnalysisIndexer, N
     bulkIndexer.addDeletion(searchRequest);
   }
 
-  public void delete(String projectUuid, Collection<String> disabledComponentUuids) {
-    BulkIndexer bulk = new BulkIndexer(esClient, TYPE_COMPONENT, Size.REGULAR);
-    bulk.start();
-    disabledComponentUuids.forEach(uuid -> bulk.addDeletion(TYPE_COMPONENT, uuid, AuthorizationDoc.idOf(projectUuid)));
-    bulk.stop();
-  }
-
   @VisibleForTesting
   void index(EntityDto... docs) {
     BulkIndexer bulk = new BulkIndexer(esClient, TYPE_COMPONENT, Size.REGULAR);
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/es/BranchIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/es/BranchIndexer.java
deleted file mode 100644 (file)
index 5aafc90..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2023 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.server.es;
-
-import java.util.Collection;
-import java.util.Set;
-import org.sonar.db.DbSession;
-import org.sonar.db.es.EsQueueDto;
-
-public interface BranchIndexer extends ResilientIndexer {
-
-  enum Cause {
-    CREATION,
-    DELETION,
-    MEASURE_CHANGE
-  }
-
-  /**
-   * This method is called when an analysis must be indexed.
-   *
-   * @param branchUuid UUID of a project or application branch, or the UUID of a portfolio.
-   */
-  void indexBranchOnAnalysis(String branchUuid);
-
-  void indexBranchOnAnalysis(String branchUuid, Set<String> unchangedComponentUuids);
-
-  Collection<EsQueueDto> prepareBranchIndexForRecovery(DbSession dbSession, Collection<String> branchUuids, BranchIndexer.Cause cause);
-}
index a058dfdb287f79d7346a08455ce52bce425b6f33..fdab6b148351bb6fe32429e4cb42ff6ba3082e22 100644 (file)
@@ -27,7 +27,6 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
-import java.util.stream.Collectors;
 import org.elasticsearch.action.index.IndexRequest;
 import org.elasticsearch.action.search.SearchRequest;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
@@ -169,16 +168,16 @@ public class IssueIndexer implements EventIndexer, AnalysisIndexer, NeedAuthoriz
     };
   }
 
-  private List<EsQueueDto> createProjectDeleteRecoveryItems(Collection<String> entityUuids) {
+  private static List<EsQueueDto> createProjectDeleteRecoveryItems(Collection<String> entityUuids) {
     return entityUuids.stream()
       .map(entityUuid -> createQueueDto(entityUuid, ID_TYPE_DELETE_PROJECT_UUID, entityUuid))
-      .collect(Collectors.toList());
+      .toList();
   }
 
-  private List<EsQueueDto> createBranchRecoveryItems(Collection<String> branchUuids) {
+  private static List<EsQueueDto> createBranchRecoveryItems(Collection<String> branchUuids) {
     return branchUuids.stream()
       .map(branchUuid -> createQueueDto(branchUuid, ID_TYPE_BRANCH_UUID, branchUuid))
-      .collect(Collectors.toList());
+      .toList();
   }
 
   /**
index 7bedc634001b53054e394f859a346936f1be23a6..9bf340831665754f88b05bb16877b47269a3c7ea 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
 import javax.annotation.Nullable;
@@ -107,8 +108,7 @@ public class ProjectMeasuresIndexer implements EventIndexer, AnalysisIndexer, Ne
         // nothing to do, permissions are not used in index type projectmeasures/projectmeasure
         Collections.emptyList();
       case PROJECT_KEY_UPDATE, CREATION, PROJECT_TAGS_UPDATE, DELETION ->
-        // when MEASURE_CHANGE or PROJECT_KEY_UPDATE project must be re-indexed because key is used in this index
-        // when PROJECT_CREATION provisioned projects are supported by WS api/components/search_projects
+        // when CREATION provisioned projects are supported by WS api/components/search_projects
         prepareForRecovery(dbSession, entityUuids);
     };
   }
@@ -118,6 +118,7 @@ public class ProjectMeasuresIndexer implements EventIndexer, AnalysisIndexer, Ne
     return switch (cause) {
       case DELETION -> Collections.emptyList();
       case MEASURE_CHANGE -> {
+        // when MEASURE_CHANGE or PROJECT_KEY_UPDATE project must be re-indexed because key is used in this index
         Set<String> entityUuids = dbClient.branchDao().selectByUuids(dbSession, branchUuids)
           .stream().map(BranchDto::getProjectUuid)
           .collect(Collectors.toSet());
@@ -171,17 +172,29 @@ public class ProjectMeasuresIndexer implements EventIndexer, AnalysisIndexer, Ne
     return bulkIndexer.stop();
   }
 
-  private void doIndex(Size size, @Nullable String projectUuid) {
-    try (DbSession dbSession = dbClient.openSession(false);
-      ProjectMeasuresIndexerIterator rowIt = ProjectMeasuresIndexerIterator.create(dbSession, projectUuid)) {
+  private void doIndex(Size size, @Nullable String branchUuid) {
+
 
-      BulkIndexer bulkIndexer = createBulkIndexer(size, IndexingListener.FAIL_ON_ERROR);
-      bulkIndexer.start();
-      while (rowIt.hasNext()) {
-        ProjectMeasures doc = rowIt.next();
-        bulkIndexer.add(toProjectMeasuresDoc(doc).toIndexRequest());
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      String projectUuid = null;
+      if (branchUuid != null) {
+        Optional<BranchDto> branchDto = dbClient.branchDao().selectByUuid(dbSession, branchUuid);
+        if (branchDto.isEmpty() || !branchDto.get().isMain()) {
+          return;
+        } else {
+          projectUuid = branchDto.get().getProjectUuid();
+        }
+      }
+
+      try (ProjectMeasuresIndexerIterator rowIt = ProjectMeasuresIndexerIterator.create(dbSession, projectUuid)) {
+        BulkIndexer bulkIndexer = createBulkIndexer(size, IndexingListener.FAIL_ON_ERROR);
+        bulkIndexer.start();
+        while (rowIt.hasNext()) {
+          ProjectMeasures doc = rowIt.next();
+          bulkIndexer.add(toProjectMeasuresDoc(doc).toIndexRequest());
+        }
+        bulkIndexer.stop();
       }
-      bulkIndexer.stop();
     }
   }
 
index 4ea1228322ce89aa8dcc517ae85ce9fcb063fd12..8d829f0bb931a9c9496d804360ddbed9cf016188 100644 (file)
@@ -102,7 +102,7 @@ public class PermissionIndexer implements EventIndexer {
   public Collection<EsQueueDto> prepareForRecoveryOnEntityEvent(DbSession dbSession, Collection<String> entityUuids, Indexers.EntityEvent cause) {
     return switch (cause) {
       case PROJECT_KEY_UPDATE, PROJECT_TAGS_UPDATE ->
-        // nothing to change. Measures, project key and tags are not part of this index
+        // nothing to change. project key and tags are not part of this index
         emptyList();
       case CREATION, DELETION, PERMISSION_CHANGE -> insertIntoEsQueue(dbSession, entityUuids);
     };
index 4e20afb0870f512e79d46d7600766de15c9d7985..cd8e1c4c2623713fa5ad81289f427733bcd085e6 100644 (file)
  */
 package org.sonar.server.permission.index;
 
+import java.util.Optional;
 import java.util.Set;
 import org.elasticsearch.action.index.IndexRequest;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
 import org.sonar.server.es.AnalysisIndexer;
 import org.sonar.server.es.BaseDoc;
 import org.sonar.server.es.EsClient;
@@ -32,9 +36,11 @@ public class FooIndexer implements AnalysisIndexer, NeedAuthorizationIndexer {
   private static final AuthorizationScope AUTHORIZATION_SCOPE = new AuthorizationScope(TYPE_FOO, p -> true);
 
   private final EsClient esClient;
+  private final DbClient dbClient;
 
-  public FooIndexer(EsClient esClient) {
+  public FooIndexer(EsClient esClient, DbClient dbClient) {
     this.esClient = esClient;
+    this.dbClient = dbClient;
   }
 
   @Override
@@ -49,8 +55,19 @@ public class FooIndexer implements AnalysisIndexer, NeedAuthorizationIndexer {
 
   @Override
   public void indexOnAnalysis(String branchUuid, Set<String> unchangedComponentUuids) {
-    addToIndex(branchUuid, "bar");
-    addToIndex(branchUuid, "baz");
+    try(DbSession dbSession = dbClient.openSession(true)){
+      Optional<BranchDto> branchDto = dbClient.branchDao().selectByUuid(dbSession, branchUuid);
+      if (branchDto.isEmpty()) {
+        //For portfolio, adding branchUuid directly
+        addToIndex(branchUuid, "bar");
+        addToIndex(branchUuid, "baz");
+      }else{
+        addToIndex(branchDto.get().getProjectUuid(), "bar");
+        addToIndex(branchDto.get().getProjectUuid(), "baz");
+      }
+    }
+
+
   }
 
   private void addToIndex(String projectUuid, String name) {
index afbcfbc64b008e646c38c9094e009e8c01eb0afd..279b63ce578d342845d046caafe42e9064b47dc6 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Test;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.component.ProjectData;
 import org.sonar.db.entity.EntityDto;
 import org.sonar.db.es.EsQueueDto;
 import org.sonar.db.portfolio.PortfolioDto;
@@ -59,7 +60,7 @@ public class PermissionIndexerTest {
   public UserSessionRule userSession = UserSessionRule.standalone();
 
   private FooIndex fooIndex = new FooIndex(es.client(), new WebAuthorizationTypeSupport(userSession));
-  private FooIndexer fooIndexer = new FooIndexer(es.client());
+  private FooIndexer fooIndexer = new FooIndexer(es.client(), db.getDbClient());
   private PermissionIndexer underTest = new PermissionIndexer(db.getDbClient(), es.client(), fooIndexer);
 
   @Test
@@ -394,15 +395,15 @@ public class PermissionIndexerTest {
   }
 
   private ProjectDto createAndIndexPrivateProject() {
-    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
-    fooIndexer.indexOnAnalysis(project.getUuid());
-    return project;
+    ProjectData project = db.components().insertPrivateProject();
+    fooIndexer.indexOnAnalysis(project.getMainBranchDto().getUuid());
+    return project.getProjectDto();
   }
 
   private ProjectDto createAndIndexPublicProject() {
-    ProjectDto project = db.components().insertPublicProject().getProjectDto();
-    fooIndexer.indexOnAnalysis(project.getUuid());
-    return project;
+    ProjectData project = db.components().insertPublicProject();
+    fooIndexer.indexOnAnalysis(project.getMainBranchDto().getUuid());
+    return project.getProjectDto();
   }
 
   private PortfolioDto createAndIndexPortfolio() {
index d8e6792b1e9962ac8efa99924bcb10cb13bd687a..e0026c1ce439637130b0c6ac6cb1ad5c36d21520 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.pushevent.PushEventDto;
-import org.sonar.server.es.EventIndexer;
 import org.sonar.server.es.Indexers;
 import org.sonar.server.project.Project;
 import org.sonar.server.project.ProjectLifeCycleListeners;
index c75a637fd32510063ab1cfbe0cdab08ccafa46cb..0d2365d87658c6d9aaf7ccb1900da432a4d70229 100644 (file)
@@ -71,8 +71,8 @@ public class AuthorsAction implements IssuesWsAction {
     NewAction action = controller.createAction("authors")
       .setSince("5.1")
       .setDescription("Search SCM accounts which match a given query.<br/>" +
-        "Requires authentication."
-        + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
+                      "Requires authentication."
+                      + "<br/>When issue indexation is in progress returns 503 service unavailable HTTP code.")
       .setResponseExample(Resources.getResource(this.getClass(), "authors-example.json"))
       .setChangelog(new Change("7.4", "The maximum size of 'ps' is set to 100"))
       .setHandler(this);
@@ -119,17 +119,16 @@ public class AuthorsAction implements IssuesWsAction {
 
   private List<String> getAuthors(DbSession session, @Nullable EntityDto entity, Request request) {
     IssueQuery.Builder issueQueryBuilder = IssueQuery.builder();
-    ofNullable(entity).ifPresent(e -> {
-      switch (e.getQualifier()) {
-        case Qualifiers.PROJECT -> issueQueryBuilder.projectUuids(Set.of(e.getUuid()));
-        case Qualifiers.VIEW -> issueQueryBuilder.viewUuids(Set.of(e.getUuid()));
+    ofNullable(entity).ifPresent(p -> {
+      switch (p.getQualifier()) {
+        case Qualifiers.PROJECT -> issueQueryBuilder.projectUuids(Set.of(p.getUuid()));
+        case Qualifiers.VIEW -> issueQueryBuilder.viewUuids(Set.of(p.getUuid()));
         case Qualifiers.APP -> {
-          BranchDto appMainBranch = dbClient.branchDao().selectMainBranchByProjectUuid(session, e.getUuid())
-            .orElseThrow(() -> new IllegalStateException("Couldn't find main branch for APP " + e.getUuid()));
-          issueQueryBuilder.viewUuids(Set.of(entity.getUuid()));
-          issueQueryBuilder.branchUuid(appMainBranch.getUuid());
+          BranchDto appMainBranch = dbClient.branchDao().selectMainBranchByProjectUuid(session, entity.getUuid())
+            .orElseThrow(() -> new IllegalStateException("Couldn't find main branch for APP " + entity.getUuid()));
+          issueQueryBuilder.viewUuids(Set.of(appMainBranch.getUuid()));
         }
-        default -> throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", e.getQualifier()));
+        default -> throw new IllegalArgumentException(String.format("Component of type '%s' is not supported", p.getQualifier()));
       }
     });
     return issueIndex.searchAuthors(
index 9c346e2cfecd31c9d145b9df1f2452086d42f5ee..0a2d0bf7cd85e2014ef604107a3732f61708e1d3 100644 (file)
@@ -27,9 +27,9 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import javax.annotation.CheckForNull;
+import org.slf4j.LoggerFactory;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.measures.Metric;
-import org.slf4j.LoggerFactory;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.BranchDto;
@@ -39,7 +39,6 @@ import org.sonar.db.measure.LiveMeasureComparator;
 import org.sonar.db.measure.LiveMeasureDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.project.ProjectDto;
-import org.sonar.server.es.EventIndexer;
 import org.sonar.server.es.Indexers;
 import org.sonar.server.qualitygate.EvaluatedQualityGate;
 import org.sonar.server.qualitygate.QualityGate;
index 9c67047cf25286cd0b66e677006bfb9f88b80158..8efa5b4218f537943e1b9ec0cbe75074609a8edd 100644 (file)
@@ -29,7 +29,6 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.entity.EntityDto;
-import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentCreationData;
 import org.sonar.server.component.ComponentUpdater;
 import org.sonar.server.newcodeperiod.NewCodeDefinitionResolver;
index 0510d1fd95653a25c1794919a6d12b529609ff73..ca2ea9bdffea4db8c77f1cbee3e621842939180d 100644 (file)
@@ -34,7 +34,6 @@ import org.sonar.db.permission.GroupPermissionDto;
 import org.sonar.db.permission.UserPermissionDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserId;
-import org.sonar.server.es.EventIndexer;
 import org.sonar.server.es.Indexers;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.project.Visibility;