From 4bcc72d0042112612d1f94f90d2e16f6b9adc1fc Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 20 Oct 2016 12:14:59 +0200 Subject: [PATCH] SONAR-8227 Add DbSession parameter in AuthorizationIndexer index methods --- .../step/ApplyPermissionsStep.java | 4 ++-- .../server/permission/PermissionService.java | 8 ++++---- .../server/permission/PermissionUpdater.java | 2 +- .../index/AuthorizationIndexer.java | 20 ++++++++----------- .../ComponentCleanerServiceTest.java | 2 +- .../index/AuthorizationIndexerTest.java | 6 +++--- .../server/view/index/ViewIndexerTest.java | 4 ++-- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java index f6c27656f65..b46059d147f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/task/projectanalysis/step/ApplyPermissionsStep.java @@ -47,7 +47,7 @@ public class ApplyPermissionsStep implements ComputationStep { private final TreeRootHolder treeRootHolder; public ApplyPermissionsStep(DbClient dbClient, DbIdsRepository dbIdsRepository, AuthorizationIndexer indexer, PermissionRepository permissionRepository, - TreeRootHolder treeRootHolder) { + TreeRootHolder treeRootHolder) { this.dbClient = dbClient; this.dbIdsRepository = dbIdsRepository; this.indexer = indexer; @@ -77,7 +77,7 @@ public class ApplyPermissionsStep implements ComputationStep { if (hasNoPermissions(dbSession, projectId)) { permissionRepository.applyDefaultPermissionTemplate(dbSession, projectId); dbSession.commit(); - indexer.index(project.getUuid()); + indexer.index(dbSession, project.getUuid()); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java index fcda00f76a7..9f3c15d95ae 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionService.java @@ -87,7 +87,7 @@ public class PermissionService { Long userId = Qualifiers.PROJECT.equals(component.qualifier()) && currentUserId != null ? currentUserId.longValue() : null; permissionRepository.applyDefaultPermissionTemplate(session, component, userId); session.commit(); - indexProjectPermissions(asList(component.uuid())); + indexProjectPermissions(session, asList(component.uuid())); } public boolean wouldCurrentUserHavePermissionWithDefaultTemplate(DbSession dbSession, String permission, @Nullable String branch, String projectKey, String qualifier) { @@ -110,10 +110,10 @@ public class PermissionService { permissionRepository.apply(dbSession, template, project, null); } dbSession.commit(); - indexProjectPermissions(projects.stream().map(ComponentDto::uuid).collect(Collectors.toList())); + indexProjectPermissions(dbSession, projects.stream().map(ComponentDto::uuid).collect(Collectors.toList())); } - private void indexProjectPermissions(List projectUuids) { - authorizationIndexer.index(projectUuids); + private void indexProjectPermissions(DbSession dbSession, List projectUuids) { + authorizationIndexer.index(dbSession, projectUuids); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionUpdater.java index 47a426a2736..07f5813776b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionUpdater.java @@ -66,7 +66,7 @@ public class PermissionUpdater { dbSession.commit(); if (!projectIds.isEmpty()) { - authorizationIndexer.index(projectUuids); + authorizationIndexer.index(dbSession, projectUuids); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/index/AuthorizationIndexer.java b/server/sonar-server/src/main/java/org/sonar/server/permission/index/AuthorizationIndexer.java index d2ebc57d28e..502b254acd4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/index/AuthorizationIndexer.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/index/AuthorizationIndexer.java @@ -101,12 +101,10 @@ public class AuthorizationIndexer implements Startable { BulkIndexer.delete(esClient, index, esClient.prepareSearch(index).setTypes(type).setQuery(matchAllQuery())); } - public void index(List projectUuids) { + public void index(DbSession dbSession, List projectUuids) { checkArgument(!projectUuids.isEmpty(), "ProjectUuids cannot be empty"); - try (DbSession dbSession = dbClient.openSession(false)) { - AuthorizationDao dao = new AuthorizationDao(); - index(dao.selectByProjects(dbClient, dbSession, projectUuids)); - } + AuthorizationDao dao = new AuthorizationDao(); + index(dao.selectByProjects(dbClient, dbSession, projectUuids)); } private void index(Collection authorizations) { @@ -130,13 +128,11 @@ public class AuthorizationIndexer implements Startable { esClient.prepareRefresh(ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES).get(); } - public void index(String projectUuid) { - try (DbSession dbSession = dbClient.openSession(false)) { - AuthorizationDao dao = new AuthorizationDao(); - List dtos = dao.selectByProjects(dbClient, dbSession, singletonList(projectUuid)); - if (dtos.size() == 1) { - index(dtos.get(0)); - } + public void index(DbSession dbSession, String projectUuid) { + AuthorizationDao dao = new AuthorizationDao(); + List dtos = dao.selectByProjects(dbClient, dbSession, singletonList(projectUuid)); + if (dtos.size() == 1) { + index(dtos.get(0)); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java index ede7d441018..e2e0b138b51 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ComponentCleanerServiceTest.java @@ -225,7 +225,7 @@ public class ComponentCleanerServiceTest { dbClient.componentDao().insert(dbSession, project); dbSession.commit(); projectMeasuresIndexer.index(); - authorizationIndexer.index(project.uuid()); + authorizationIndexer.index(dbSession, project.uuid()); String issueKey = "issue-key-" + suffix; es.putDocuments(IssueIndexDefinition.INDEX, TYPE_ISSUE, IssueTesting.newDoc(issueKey, project)); diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/index/AuthorizationIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/index/AuthorizationIndexerTest.java index 1e5fcae3e18..dd366beaf97 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/index/AuthorizationIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/index/AuthorizationIndexerTest.java @@ -133,7 +133,7 @@ public class AuthorizationIndexerTest { ComponentDto project2 = componentDbTester.insertProject(); userDbTester.insertProjectPermissionOnGroup(group, USER, project2); - underTest.index(project1.uuid()); + underTest.index(dbTester.getSession(), project1.uuid()); authorizationIndexerTester.verifyProjectExistsWithAuthorization(project1.uuid(), asList(group.getName(), ANYONE), emptyList()); authorizationIndexerTester.verifyProjectDoesNotExist(project2.uuid()); @@ -150,7 +150,7 @@ public class AuthorizationIndexerTest { userDbTester.insertProjectPermissionOnGroup(group, USER, project3); // Only index projects 1 and 2 - underTest.index(asList(project1.uuid(), project2.uuid())); + underTest.index(dbTester.getSession(), asList(project1.uuid(), project2.uuid())); authorizationIndexerTester.verifyProjectExistsWithAuthorization(project1.uuid(), asList(group.getName(), ANYONE), emptyList()); authorizationIndexerTester.verifyProjectExistsWithAuthorization(project2.uuid(), asList(group.getName(), ANYONE), emptyList()); @@ -170,6 +170,6 @@ public class AuthorizationIndexerTest { @Test public void fail_when_trying_to_index_empty_project_uuids() throws Exception { expectedException.expect(IllegalArgumentException.class); - underTest.index(Collections.emptyList()); + underTest.index(dbTester.getSession(), Collections.emptyList()); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java index 4b145f94906..c9a3a254e69 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/view/index/ViewIndexerTest.java @@ -151,7 +151,7 @@ public class ViewIndexerTest { dbClient.ruleDao().insert(dbSession, rule); ComponentDto project1 = addProjectWithIssue(rule); issueIndexer.indexAll(); - authorizationIndexer.index(project1.uuid()); + authorizationIndexer.index(dbSession, project1.uuid()); ComponentDto view = ComponentTesting.newView("ABCD"); ComponentDto techProject1 = ComponentTesting.newProjectCopy("CDEF", project1, view); @@ -168,7 +168,7 @@ public class ViewIndexerTest { // Add a project to the view and index it again ComponentDto project2 = addProjectWithIssue(rule); issueIndexer.indexAll(); - authorizationIndexer.index(project2.uuid()); + authorizationIndexer.index(dbSession, project2.uuid()); ComponentDto techProject2 = ComponentTesting.newProjectCopy("EFGH", project2, view); dbClient.componentDao().insert(dbSession, techProject2); -- 2.39.5