diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2023-07-04 17:35:22 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-07-05 20:03:04 +0000 |
commit | 9d242f176cef4f978560eb1fde917523be967f9d (patch) | |
tree | 5a68f2491c6c305653e378ea0b32fe34bf819c0c /server/sonar-webserver-es | |
parent | 0b24d4b1fa9066719f974624545ef447a6698d2d (diff) | |
download | sonarqube-9d242f176cef4f978560eb1fde917523be967f9d.tar.gz sonarqube-9d242f176cef4f978560eb1fde917523be967f9d.zip |
SONAR-19558 Fix usage of indexOnAnalysis and minor fixes
Diffstat (limited to 'server/sonar-webserver-es')
3 files changed, 29 insertions, 11 deletions
diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java index 4ea1228322c..8d829f0bb93 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/permission/index/PermissionIndexer.java @@ -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); }; diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/FooIndexer.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/FooIndexer.java index 4e20afb0870..cd8e1c4c262 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/FooIndexer.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/FooIndexer.java @@ -19,8 +19,12 @@ */ 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) { diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java index afbcfbc64b0..279b63ce578 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/permission/index/PermissionIndexerTest.java @@ -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() { |