]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-19445 Review and improve the endpoints in WebAPI that rely on ComponentDto
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Tue, 6 Jun 2023 07:21:12 +0000 (09:21 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 19 Jun 2023 20:03:14 +0000 (20:03 +0000)
34 files changed:
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/notification/NotificationDbTester.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java
server/sonar-server-common/src/main/java/org/sonar/server/component/index/ComponentIndexer.java
server/sonar-webserver-auth/src/main/java/org/sonar/server/user/AbstractUserSession.java
server/sonar-webserver-auth/src/main/java/org/sonar/server/user/ThreadLocalUserSession.java
server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserSession.java
server/sonar-webserver-auth/src/testFixtures/java/org/sonar/server/tester/UserSessionRule.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/AddActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/ListActionIT.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/notification/ws/RemoveActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/ActivityStatusAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/CancelAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentFinder.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/favorite/ws/SearchAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AddCommentAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/AssignAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ChangeStatusAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/DeleteCommentAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/EditCommentAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsSupport.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/ShowAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/AddAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/ListAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/NotificationUpdater.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/notification/ws/RemoveAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/PermissionWsSupport.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/permission/ws/template/ApplyTemplateAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/projectdump/ws/ProjectDumpWsSupport.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/SettingValidations.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java

index 107084fdb81ac02be4dae7c34d3294c779ae464c..020853338604948190f4084ce453ef833784b920 100644 (file)
@@ -275,8 +275,12 @@ public class ComponentDbTester {
   }
 
   public final ComponentDto insertSubportfolio(ComponentDto parentPortfolio) {
-    ComponentDto subPortfolioComponent = ComponentTesting.newSubPortfolio(parentPortfolio);
-    return insertComponentAndPortfolio(subPortfolioComponent, true, defaults(), sp -> sp.setParentUuid(sp.getRootUuid()));
+    return insertSubportfolio(parentPortfolio, defaults());
+  }
+
+  public final ComponentDto insertSubportfolio(ComponentDto parentPortfolio, Consumer<ComponentDto> consumer) {
+    ComponentDto subPortfolioComponent = ComponentTesting.newSubPortfolio(parentPortfolio).setPrivate(true);
+    return insertComponentAndPortfolio(subPortfolioComponent, true, consumer, sp -> sp.setParentUuid(sp.getRootUuid()));
   }
 
   public void addPortfolioReference(String portfolioUuid, String... referencerUuids) {
@@ -484,6 +488,11 @@ public class ComponentDbTester {
     return insertSnapshot(snapshotDto);
   }
 
+  public SnapshotDto insertSnapshot(PortfolioDto portfolio) {
+    SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(portfolio.getUuid());
+    return insertSnapshot(snapshotDto);
+  }
+
   public SnapshotDto insertSnapshot(BranchDto branchDto, Consumer<SnapshotDto> consumer) {
     SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(branchDto);
     consumer.accept(snapshotDto);
index f7ca67843974c16c9313b07a968a5c2d44c06f57..9f8d04071b8182a389765d5c38cebe88a944a095 100644 (file)
@@ -25,7 +25,7 @@ import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.property.PropertyDto;
 import org.sonar.db.property.PropertyQuery;
 
@@ -42,22 +42,22 @@ public class NotificationDbTester {
     this.dbSession = db.getSession();
   }
 
-  public void assertExists(String channel, String dispatcher, String userUuid, @Nullable ComponentDto component) {
+  public void assertExists(String channel, String dispatcher, String userUuid, @Nullable ProjectDto project) {
     List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
-      .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel))
-      .setEntityUuid(component == null ? null : component.uuid())
-      .setUserUuid(userUuid)
-      .build(), dbSession).stream()
-      .filter(prop -> component == null ? prop.getEntityUuid() == null : prop.getEntityUuid() != null)
+        .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel))
+        .setEntityUuid(project == null ? null : project.getUuid())
+        .setUserUuid(userUuid)
+        .build(), dbSession).stream()
+      .filter(prop -> project == null ? prop.getEntityUuid() == null : prop.getEntityUuid() != null)
       .collect(MoreCollectors.toList());
     assertThat(result).hasSize(1);
     assertThat(result.get(0).getValue()).isEqualTo("true");
   }
 
-  public void assertDoesNotExist(String channel, String dispatcher, String userUuid, @Nullable ComponentDto component) {
+  public void assertDoesNotExist(String channel, String dispatcher, String userUuid, @Nullable ProjectDto project) {
     List<PropertyDto> result = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
       .setKey(String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel))
-      .setEntityUuid(component == null ? null : component.uuid())
+      .setEntityUuid(project == null ? null : project.getUuid())
       .setUserUuid(userUuid)
       .build(), dbSession);
     assertThat(result).isEmpty();
index 132681d52b2c016f87d8f3a2768132c7a1216fcc..4d08a86d15642d3f0c84cae8009141e3aa49820d 100644 (file)
@@ -300,11 +300,6 @@ public class UserDbTester {
     return dto;
   }
 
-  public void deleteProjectPermissionFromAnyone(ComponentDto project, String permission) {
-    db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, null, project.uuid(), project);
-    db.commit();
-  }
-
   public void deleteProjectPermissionFromAnyone(EntityDto project, String permission) {
     db.getDbClient().groupPermissionDao().delete(db.getSession(), permission, null, null, project.getUuid(), project);
     db.commit();
@@ -389,11 +384,6 @@ public class UserDbTester {
     db.commit();
   }
 
-  public void deletePermissionFromUser(ComponentDto project, UserDto user, String permission) {
-    db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user, permission, project);
-    db.commit();
-  }
-
   public void deletePermissionFromUser(EntityDto project, UserDto user, String permission) {
     db.getDbClient().userPermissionDao().deleteProjectPermission(db.getSession(), user, permission, project);
     db.commit();
@@ -406,10 +396,10 @@ public class UserDbTester {
     checkArgument(project.isPrivate() || !PUBLIC_PERMISSIONS.contains(permission),
       "%s can't be granted on a public project", permission);
     EntityDto entityDto;
-    if (project.qualifier().equals(Qualifiers.VIEW) || project.qualifier().equals(Qualifiers.SUBVIEW)){
+    if (project.qualifier().equals(Qualifiers.VIEW) || project.qualifier().equals(Qualifiers.SUBVIEW)) {
       entityDto = db.getDbClient().portfolioDao().selectByUuid(db.getSession(), project.uuid())
         .orElseThrow();
-    }else{
+    } else {
       BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), project.branchUuid())
         .orElseThrow();
       // I don't know if this check is worth it
index 5b47fb11c8909565b781032b88f47d069e46c01a..396c5149b999cc8353f5ccf4581f5a9ce64094ff 100644 (file)
@@ -201,12 +201,12 @@ public class ComponentIndexer implements ProjectIndexer, NeedAuthorizationIndexe
     bulk.stop();
   }
 
-  public static ComponentDoc toDocument(EntityDto component) {
+  public static ComponentDoc toDocument(EntityDto entity) {
     return new ComponentDoc()
-      .setId(component.getUuid())
-      .setAuthUuid(component.getAuthUuid())
-      .setName(component.getName())
-      .setKey(component.getKey())
-      .setQualifier(component.getQualifier());
+      .setId(entity.getUuid())
+      .setAuthUuid(entity.getAuthUuid())
+      .setName(entity.getName())
+      .setKey(entity.getKey())
+      .setQualifier(entity.getQualifier());
   }
 }
index e34ca857a739c4a9a61db446adf37762f742da90..cf79bdbf547fa11bfd2f3224cedc99546ff3eea0 100644 (file)
@@ -113,7 +113,7 @@ public abstract class AbstractUserSession implements UserSession {
   }
 
   @Override
-  public final boolean hasChildProjectsPermission(String permission, ProjectDto application) {
+  public final boolean hasChildProjectsPermission(String permission, EntityDto application) {
     return hasChildProjectsPermission(permission, application.getUuid());
   }
 
@@ -211,7 +211,7 @@ public abstract class AbstractUserSession implements UserSession {
   }
 
   @Override
-  public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
+  public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) {
     if (!APP.equals(application.getQualifier()) || hasChildProjectsPermission(projectPermission, application)) {
       return this;
     }
index f5a3119f673ac0336fcae14887af0ab7badba5c3..4be97dd5e967cb31439c7d5767ea7c1229c8bc8d 100644 (file)
@@ -142,7 +142,7 @@ public class ThreadLocalUserSession implements UserSession {
   }
 
   @Override
-  public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
+  public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) {
     get().checkChildProjectsPermission(projectPermission, application);
     return this;
   }
@@ -190,8 +190,8 @@ public class ThreadLocalUserSession implements UserSession {
   }
 
   @Override
-  public boolean hasChildProjectsPermission(String permission, ProjectDto project) {
-    return get().hasChildProjectsPermission(permission, project);
+  public boolean hasChildProjectsPermission(String permission, EntityDto application) {
+    return get().hasChildProjectsPermission(permission, application);
   }
 
   @Override
index 4e4bdc05b2fa38ae434bf46db363c4ab8471185c..12c5bf460d79bab68cf3ebc05d3608da0c97e830 100644 (file)
@@ -153,7 +153,7 @@ public interface UserSession {
 
   boolean hasChildProjectsPermission(String permission, ComponentDto component);
 
-  boolean hasChildProjectsPermission(String permission, ProjectDto component);
+  boolean hasChildProjectsPermission(String permission, EntityDto application);
 
   boolean hasPortfolioChildProjectsPermission(String permission, ComponentDto component);
 
@@ -185,7 +185,7 @@ public interface UserSession {
   UserSession checkComponentPermission(String projectPermission, ComponentDto component);
 
   /**
-   * Ensures that {@link #hasEntityPermission(String, ProjectDto)} is {@code true},
+   * Ensures that {@link #hasEntityPermission(String, EntityDto)} is {@code true},
    * otherwise throws a {@link org.sonar.server.exceptions.ForbiddenException}.
    */
   UserSession checkEntityPermission(String projectPermission, EntityDto entity);
@@ -197,10 +197,10 @@ public interface UserSession {
   UserSession checkChildProjectsPermission(String projectPermission, ComponentDto project);
 
   /**
-   * Ensures that {@link #hasChildProjectsPermission(String, ProjectDto)} is {@code true}
+   * Ensures that {@link #hasChildProjectsPermission(String, EntityDto)} is {@code true}
    * otherwise throws a {@link org.sonar.server.exceptions.ForbiddenException}.
    */
-  UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application);
+  UserSession checkChildProjectsPermission(String projectPermission, EntityDto application);
 
   /**
    * Ensures that {@link #hasComponentUuidPermission(String, String)} is {@code true},
index 946bf0f5f749525b337582b8a5eeadc412ac94f7..3d4f35ac0f39fd4be19f940353488b2159ed4146 100644 (file)
@@ -180,6 +180,11 @@ public class UserSessionRule implements TestRule, UserSession {
     return this;
   }
 
+  public UserSessionRule registerPortfolios(PortfolioDto... portfolioDtos) {
+    ensureAbstractMockUserSession().registerPortfolios(portfolioDtos);
+    return this;
+  }
+
   public UserSessionRule registerProjects(ProjectDto... projectDtos) {
     ensureAbstractMockUserSession().registerProjects(projectDtos);
     return this;
@@ -274,8 +279,8 @@ public class UserSessionRule implements TestRule, UserSession {
   }
 
   @Override
-  public boolean hasChildProjectsPermission(String permission, ProjectDto component) {
-    return currentUserSession.hasChildProjectsPermission(permission, component);
+  public boolean hasChildProjectsPermission(String permission, EntityDto application) {
+    return currentUserSession.hasChildProjectsPermission(permission, application);
   }
 
   @Override
@@ -383,7 +388,7 @@ public class UserSessionRule implements TestRule, UserSession {
   }
 
   @Override
-  public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
+  public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) {
     currentUserSession.checkChildProjectsPermission(projectPermission, application);
     return this;
   }
index 7fcb3ea1688ae7785c2c7bd1c874c227867ca8b3..e2a8ab14bfeec6fc58ca8dfa18df83f28fd46606 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.notifications.NotificationChannel;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
@@ -62,18 +63,18 @@ public class AddActionIT {
   @Rule
   public UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public DbTester db = DbTester.create();
+  public DbTester db = DbTester.create(true);
 
-  private DbClient dbClient = db.getDbClient();
+  private final DbClient dbClient = db.getDbClient();
 
-  private NotificationChannel emailChannel = new FakeNotificationChannel("EmailChannel");
-  private NotificationChannel twitterChannel = new FakeNotificationChannel("TwitterChannel");
+  private final NotificationChannel emailChannel = new FakeNotificationChannel("EmailChannel");
+  private final NotificationChannel twitterChannel = new FakeNotificationChannel("TwitterChannel");
   // default channel, based on class simple name
-  private NotificationChannel defaultChannel = new FakeNotificationChannel("EmailNotificationChannel");
+  private final NotificationChannel defaultChannel = new FakeNotificationChannel("EmailNotificationChannel");
 
-  private Dispatchers dispatchers = mock(Dispatchers.class);
+  private final Dispatchers dispatchers = mock(Dispatchers.class);
 
-  private WsActionTester ws = new WsActionTester(new AddAction(new NotificationCenter(
+  private final WsActionTester ws = new WsActionTester(new AddAction(new NotificationCenter(
     new NotificationDispatcherMetadata[] {},
     new NotificationChannel[] {emailChannel, twitterChannel, defaultChannel}),
     new NotificationUpdater(dbClient), dispatchers, dbClient, TestComponentFinder.from(db), userSession));
@@ -104,7 +105,7 @@ public class AddActionIT {
   public void add_notification_on_private_with_USER_permission() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.addProjectPermission(USER, project);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
@@ -118,8 +119,8 @@ public class AddActionIT {
   public void add_notification_on_public_project() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    userSession.registerComponents(project);
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    userSession.registerProjects(project);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
 
@@ -134,7 +135,7 @@ public class AddActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     userSession.addProjectPermission(USER, project);
     call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
 
@@ -150,7 +151,7 @@ public class AddActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     call(NOTIF_MY_NEW_ISSUES, null, null, null);
 
     userSession.addProjectPermission(USER, project);
@@ -166,8 +167,8 @@ public class AddActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
-    userSession.registerComponents(project);
+    ProjectDto project = db.components().insertPublicProject().getProjectDto();
+    userSession.registerProjects(project);
     call(NOTIF_MY_NEW_ISSUES, null, null, null);
 
     call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null);
@@ -210,7 +211,7 @@ public class AddActionIT {
   }
 
   @Test
-  public void fail_if_login_provided_and_not_system_administrsator() {
+  public void fail_if_login_provided_and_not_system_administrator() {
     UserDto user = db.users().insertUser();
     userSession.logIn(user).setNonSystemAdministrator();
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
@@ -219,6 +220,17 @@ public class AddActionIT {
       .isInstanceOf(ForbiddenException.class);
   }
 
+  @Test
+  public void fail_if_project_provided_and_not_project_user() {
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+    when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
+
+    assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), null))
+      .isInstanceOf(ForbiddenException.class);
+  }
+
   @Test
   public void fail_when_notification_already_exists() {
     UserDto user = db.users().insertUser();
@@ -299,8 +311,8 @@ public class AddActionIT {
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
 
     assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, "VIEW_1", null))
-      .isInstanceOf(BadRequestException.class)
-      .hasMessageContaining("Component 'VIEW_1' must be a project");
+      .isInstanceOf(NotFoundException.class)
+      .hasMessageContaining("Project 'VIEW_1' not found");
   }
 
   @Test
index 49eefadb1c94553f64a7c82eb96f4e981a0d3f2d..b07d27ced1cdb88a9c0c064c51ed29af95d7e202 100644 (file)
@@ -26,7 +26,8 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
@@ -56,7 +57,7 @@ public class ListActionIT {
   @Rule
   public final UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public final DbTester db = DbTester.create();
+  public final DbTester db = DbTester.create(true);
 
   private final DbClient dbClient = db.getDbClient();
   private final DbSession dbSession = db.getSession();
@@ -110,16 +111,16 @@ public class ListActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.users().insertProjectPermissionOnUser(user, USER, project);
-    ComponentDto anotherProject = db.components().insertPrivateProject().getMainBranchComponent();
-    notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project);
+    ProjectData project = db.components().insertPrivateProject();
+    db.users().insertProjectPermissionOnUser(user, USER, project.getMainBranchComponent());
+    ProjectDto anotherProject = db.components().insertPrivateProject().getProjectDto();
+    notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project.getProjectDto());
     notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, anotherProject);
     dbSession.commit();
 
     ListResponse result = call();
 
-    assertThat(result.getNotificationsList()).extracting(Notification::getProject).containsOnly(project.getKey());
+    assertThat(result.getNotificationsList()).extracting(Notification::getProject).containsOnly(project.getProjectDto().getKey());
   }
 
   @Test
@@ -155,10 +156,10 @@ public class ListActionIT {
     UserDto user = db.users().insertUser();
     userSession.logIn(user);
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
-    db.users().insertProjectPermissionOnUser(user, USER, project);
-    notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project);
-    notificationUpdater.add(dbSession, emailChannel.getKey(), "Unknown Notification", user, project);
+    ProjectData project = db.components().insertPrivateProject();
+    db.users().insertProjectPermissionOnUser(user, USER, project.getMainBranchComponent());
+    notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project.getProjectDto());
+    notificationUpdater.add(dbSession, emailChannel.getKey(), "Unknown Notification", user, project.getProjectDto());
     dbSession.commit();
 
     ListResponse result = call();
@@ -174,7 +175,7 @@ public class ListActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS));
     when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     db.users().insertProjectPermissionOnUser(user, USER, project);
     notificationUpdater.add(dbSession, twitterChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null);
     notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null);
@@ -241,8 +242,9 @@ public class ListActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS));
     when(dispatchers.getProjectDispatchers()).thenReturn(asList(NOTIF_MY_NEW_ISSUES, NOTIF_NEW_QUALITY_GATE_STATUS));
-    ComponentDto project = db.components().insertPrivateProject(p -> p.setKey(KEY_PROJECT_EXAMPLE_001).setName("My Project")).getMainBranchComponent();
-    db.users().insertProjectPermissionOnUser(user, USER, project);
+    ProjectData projectData = db.components().insertPrivateProject(p -> p.setKey(KEY_PROJECT_EXAMPLE_001).setName("My Project"));
+    ProjectDto project = projectData.getProjectDto();
+    db.users().insertProjectPermissionOnUser(user, USER, projectData.getMainBranchComponent());
     notificationUpdater.add(dbSession, twitterChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null);
     notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, null);
     notificationUpdater.add(dbSession, emailChannel.getKey(), NOTIF_NEW_ISSUES, user, null);
index f135134a2c425799512f0c7d329bf7eb43736fa1..b89a124e92e9b64e42cb0c344ca66af8f0e47bf9 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.exceptions.BadRequestException;
@@ -61,7 +62,7 @@ public class RemoveActionIT {
   @Rule
   public final UserSessionRule userSession = UserSessionRule.standalone();
   @Rule
-  public final DbTester db = DbTester.create();
+  public final DbTester db = DbTester.create(true);
   private final DbClient dbClient = db.getDbClient();
   private final DbSession dbSession = db.getSession();
 
@@ -111,7 +112,7 @@ public class RemoveActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     notificationUpdater.add(dbSession, defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project);
     dbSession.commit();
 
@@ -126,7 +127,7 @@ public class RemoveActionIT {
     userSession.logIn(user);
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
     notificationUpdater.add(dbSession, defaultChannel.getKey(), NOTIF_MY_NEW_ISSUES, user, project);
     dbSession.commit();
 
@@ -287,8 +288,8 @@ public class RemoveActionIT {
 
     RemoveRequest request = this.request.setProject("VIEW_1");
     assertThatThrownBy(() -> call(request))
-      .isInstanceOf(BadRequestException.class)
-      .hasMessage("Component 'VIEW_1' must be a project");
+      .isInstanceOf(NotFoundException.class)
+      .hasMessage("Project 'VIEW_1' not found");
   }
 
   @Test
index 3be91be3c748155568e225426bce0166319fe234..472514a8c103d0f24477b73d6f335a5720388410 100644 (file)
@@ -76,12 +76,7 @@ public class ProjectBadgesSupport {
       String branchName = request.param(PARAM_BRANCH);
       ProjectDto project = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey);
 
-      BranchDto branch;
-      if (branchName == null) {
-        branch = componentFinder.getMainBranch(dbSession, project);
-      } else {
-        branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null);
-      }
+      BranchDto branch = componentFinder.getBranchOrPullRequest(dbSession, project, branchName, null);
 
       if (!branch.getBranchType().equals(BRANCH)) {
         throw generateInvalidProjectException();
index f30975adf1d5da11dc3aec1e3463fcb802d4d0f5..be3c1414890a598ed2cfd7b6815dba9be2cc5431 100644 (file)
@@ -47,6 +47,7 @@ import org.sonar.db.ce.CeTaskQuery;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentQuery;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.Ce;
 import org.sonarqube.ws.Ce.ActivityResponse;
@@ -167,8 +168,8 @@ public class ActivityAction implements CeWsAction {
   private ActivityResponse doHandle(Request request) {
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      ComponentDto component = loadComponent(dbSession, request);
-      checkPermission(component);
+      EntityDto entity = loadEntity(dbSession, request);
+      checkPermission(entity);
       // if a task searched by uuid is found all other parameters are ignored
       Optional<Ce.Task> taskSearchedById = searchTaskByUuid(dbSession, request);
       if (taskSearchedById.isPresent()) {
@@ -178,7 +179,7 @@ public class ActivityAction implements CeWsAction {
           Paging.forPageIndex(1).withPageSize(1).andTotal(1), 0);
       }
 
-      CeTaskQuery query = buildQuery(dbSession, request, component);
+      CeTaskQuery query = buildQuery(dbSession, request, entity);
 
       return buildPaginatedResponse(dbSession, query, parseInt(request.getP()), parseInt(request.getPs()));
     }
@@ -230,26 +231,26 @@ public class ActivityAction implements CeWsAction {
   }
 
   @CheckForNull
-  private ComponentDto loadComponent(DbSession dbSession, Request request) {
+  private EntityDto loadEntity(DbSession dbSession, Request request) {
     String componentKey = request.getComponent();
 
     if (componentKey != null) {
-      Optional<ComponentDto> foundComponent;
-      foundComponent = dbClient.componentDao().selectByKey(dbSession, componentKey);
+      Optional<EntityDto> foundComponent;
+      foundComponent = dbClient.entityDao().selectByKey(dbSession, componentKey);
       return checkFoundWithOptional(foundComponent, "Component '%s' does not exist", componentKey);
     } else {
       return null;
     }
   }
 
-  private void checkPermission(@Nullable ComponentDto component) {
+  private void checkPermission(@Nullable EntityDto entity) {
     // fail fast if not logged in
     userSession.checkLoggedIn();
 
-    if (component == null) {
+    if (entity == null) {
       userSession.checkIsSystemAdministrator();
     } else {
-      userSession.checkComponentPermission(UserRole.ADMIN, component);
+      userSession.checkEntityPermission(UserRole.ADMIN, entity);
     }
   }
 
@@ -268,7 +269,7 @@ public class ActivityAction implements CeWsAction {
     return activity.map(ceActivityDto -> formatter.formatActivity(dbSession, ceActivityDto, null));
   }
 
-  private CeTaskQuery buildQuery(DbSession dbSession, Request request, @Nullable ComponentDto component) {
+  private CeTaskQuery buildQuery(DbSession dbSession, Request request, @Nullable EntityDto entity) {
     CeTaskQuery query = new CeTaskQuery();
     query.setType(request.getType());
     query.setOnlyCurrents(parseBoolean(request.getOnlyCurrents()));
@@ -283,8 +284,8 @@ public class ActivityAction implements CeWsAction {
     }
 
     String componentQuery = request.getQ();
-    if (component != null) {
-      query.setMainComponentUuid(component.uuid());
+    if (entity != null) {
+      query.setMainComponentUuid(entity.getUuid());
     } else if (componentQuery != null) {
       query.setMainComponentUuids(loadComponents(dbSession, componentQuery).stream()
         .map(ComponentDto::uuid)
index d1ee942d2f8bf5ca1488920ba2a5f6c8fe32da9b..f14be61276977f6bc1cbe0abfe67b8103e5c49e9 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.DbSession;
 import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
 import org.sonar.server.ws.KeyExamples;
@@ -83,8 +84,8 @@ public class ActivityStatusAction implements CeWsAction {
 
   private ActivityStatusWsResponse doHandle(Request request) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      Optional<ComponentDto> component = searchComponent(dbSession, request);
-      String componentUuid = component.map(ComponentDto::uuid).orElse(null);
+      Optional<EntityDto> component = searchComponent(dbSession, request);
+      String componentUuid = component.map(EntityDto::getUuid).orElse(null);
       checkPermissions(component.orElse(null));
       int pendingCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.PENDING, componentUuid);
       int inProgressCount = dbClient.ceQueueDao().countByStatusAndMainComponentUuid(dbSession, CeQueueDto.Status.IN_PROGRESS, componentUuid);
@@ -106,17 +107,17 @@ public class ActivityStatusAction implements CeWsAction {
     }
   }
 
-  private Optional<ComponentDto> searchComponent(DbSession dbSession, Request request) {
-    ComponentDto component = null;
+  private Optional<EntityDto> searchComponent(DbSession dbSession, Request request) {
+    EntityDto entity = null;
     if (request.getComponentKey() != null) {
-      component = componentFinder.getByKey(dbSession, request.getComponentKey());
+      entity = componentFinder.getEntityByKey(dbSession, request.getComponentKey());
     }
-    return Optional.ofNullable(component);
+    return Optional.ofNullable(entity);
   }
 
-  private void checkPermissions(@Nullable ComponentDto component) {
-    if (component != null) {
-      userSession.checkComponentPermission(UserRole.ADMIN, component);
+  private void checkPermissions(@Nullable EntityDto entity) {
+    if (entity != null) {
+      userSession.checkEntityPermission(UserRole.ADMIN, entity);
     } else {
       userSession.checkIsSystemAdministrator();
     }
index a30f0057069fcf4e5ebee9412c728f5591af8a03..697d7b67efcea4c1e7e7afd8f9615b65408d1951 100644 (file)
  */
 package org.sonar.server.ce.ws;
 
-import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
-
 import java.util.Optional;
-
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
@@ -35,12 +32,14 @@ import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.user.UserSession;
 
+import static org.sonar.server.user.AbstractUserSession.insufficientPrivilegesException;
+
 public class CancelAction implements CeWsAction {
 
   public static final String PARAM_TASK_ID = "id";
 
   private final UserSession userSession;
-  private DbClient dbClient;
+  private final DbClient dbClient;
   private final CeQueue queue;
 
   public CancelAction(UserSession userSession, DbClient dbClient, CeQueue queue) {
index 2d3cc1b1000fb396eab3f890cca45c274b6715b6..44190a174710bba505fec3db23df41155eaa41bf 100644 (file)
@@ -32,6 +32,7 @@ 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.entity.EntityDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.server.exceptions.NotFoundException;
 
@@ -45,6 +46,7 @@ public class ComponentFinder {
   private static final String LABEL_PROJECT = "Project";
   private static final String LABEL_COMPONENT = "Component";
   private static final String LABEL_PROJECT_NOT_FOUND = "Project '%s' not found";
+  private static final String LABEL_ENTITY_NOT_FOUND = "Component '%s' not found";
 
   private final DbClient dbClient;
   private final ResourceTypes resourceTypes;
@@ -64,6 +66,25 @@ public class ComponentFinder {
     return getByKey(dbSession, checkParamNotEmpty(componentKey, parameterNames.getKeyParam()));
   }
 
+  public EntityDto getEntityByUuidOrKey(DbSession dbSession, @Nullable String entityUuid, @Nullable String entityKey, ParamNames parameterNames) {
+    checkByUuidOrKey(entityUuid, entityKey, parameterNames);
+
+    if (entityUuid != null) {
+      return getEntityByUuid(dbSession, checkParamNotEmpty(entityUuid, parameterNames.getUuidParam()));
+    }
+    return getEntityByKey(dbSession, checkParamNotEmpty(entityKey, parameterNames.getKeyParam()));
+  }
+
+  public EntityDto getEntityByKey(DbSession dbSession, String entityKey) {
+    return dbClient.entityDao().selectByKey(dbSession, entityKey)
+      .orElseThrow(() -> new NotFoundException(String.format(LABEL_ENTITY_NOT_FOUND, entityKey)));
+  }
+
+  public EntityDto getEntityByUuid(DbSession dbSession, String entityUuid) {
+    return dbClient.entityDao().selectByUuid(dbSession, entityUuid)
+      .orElseThrow(() -> new NotFoundException(String.format(LABEL_ENTITY_NOT_FOUND, entityUuid)));
+  }
+
   public ProjectDto getProjectByKey(DbSession dbSession, String projectKey) {
     return dbClient.projectDao().selectProjectByKey(dbSession, projectKey)
       .orElseThrow(() -> new NotFoundException(String.format(LABEL_PROJECT_NOT_FOUND, projectKey)));
@@ -105,12 +126,8 @@ public class ComponentFinder {
       .orElseThrow(() -> new NotFoundException(String.format("Branch uuid '%s' not found", branchUuid)));
   }
 
-  public BranchDto getBranchOrPullRequest(DbSession dbSession, ComponentDto project, @Nullable String branchKey, @Nullable String pullRequestKey) {
-    return getBranchOrPullRequest(dbSession, project.uuid(), project.getKey(), branchKey, pullRequestKey);
-  }
-
-  public BranchDto getBranchOrPullRequest(DbSession dbSession, ProjectDto project, @Nullable String branchKey, @Nullable String pullRequestKey) {
-    return getBranchOrPullRequest(dbSession, project.getUuid(), project.getKey(), branchKey, pullRequestKey);
+  public BranchDto getBranchOrPullRequest(DbSession dbSession, EntityDto entity, @Nullable String branchKey, @Nullable String pullRequestKey) {
+    return getBranchOrPullRequest(dbSession, entity.getUuid(), entity.getKey(), branchKey, pullRequestKey);
   }
 
   public ProjectAndBranch getAppOrProjectAndBranch(DbSession dbSession, String projectKey, @Nullable String branchKey, @Nullable String pullRequestKey) {
index 39db8a79ccc74aea2b15845f0500919dad2acc4b..8344bba97553925811a20b2ca002c774425a8bf1 100644 (file)
@@ -124,12 +124,12 @@ public class SearchAction implements FavoritesWsAction {
       .forEach(builder::addFavorites);
   }
 
-  private static Favorite toWsFavorite(Favorite.Builder builder, EntityDto componentDto) {
+  private static Favorite toWsFavorite(Favorite.Builder builder, EntityDto entity) {
     builder
       .clear()
-      .setKey(componentDto.getKey());
-    ofNullable(componentDto.getName()).ifPresent(builder::setName);
-    ofNullable(componentDto.getQualifier()).ifPresent(builder::setQualifier);
+      .setKey(entity.getKey());
+    ofNullable(entity.getName()).ifPresent(builder::setName);
+    ofNullable(entity.getQualifier()).ifPresent(builder::setQualifier);
     return builder.build();
   }
 
index 6277973a6dcd4c8f81a627d4bc469a2417a320b4..1db5aba3ef1780d0fe6d4e0eb69a6559489c9b73 100644 (file)
@@ -80,7 +80,7 @@ public class AddCommentAction implements HotspotsWsAction {
     String comment = request.mandatoryParam(PARAM_COMMENT);
     try (DbSession dbSession = dbClient.openSession(false)) {
       IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey);
-      hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.USER);
+      hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.USER);
 
       DefaultIssue defaultIssue = hotspot.toDefaultIssue();
       IssueChangeContext context = hotspotWsSupport.newIssueChangeContextWithoutMeasureRefresh();
index 7c7438b8b19f69a6766c91689381d20d99034328..b7547af68ae3d877525290d4f768c1b66e30b447 100644 (file)
@@ -110,7 +110,7 @@ public class AssignAction implements HotspotsWsAction {
       IssueDto hotspotDto = hotspotWsSupport.loadHotspot(dbSession, hotspotKey);
 
       checkHotspotStatusAndResolution(hotspotDto);
-      hotspotWsSupport.loadAndCheckProject(dbSession, hotspotDto, UserRole.USER);
+      hotspotWsSupport.loadAndCheckBranch(dbSession, hotspotDto, UserRole.USER);
       UserDto assignee = isNullOrEmpty(login) ? null : getAssignee(dbSession, login);
 
       IssueChangeContext context = hotspotWsSupport.newIssueChangeContextWithoutMeasureRefresh();
index 37f23646e29f2b2981d0fafe150cabd2ff298868..7111c7ab1cbe9d56eb28e6d5330108ebda083a54 100644 (file)
@@ -111,7 +111,7 @@ public class ChangeStatusAction implements HotspotsWsAction {
     String newResolution = resolutionParam(request, newStatus);
     try (DbSession dbSession = dbClient.openSession(false)) {
       IssueDto hotspot = hotspotWsSupport.loadHotspot(dbSession, hotspotKey);
-      hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.SECURITYHOTSPOT_ADMIN);
+      hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.SECURITYHOTSPOT_ADMIN);
 
       if (needStatusUpdate(hotspot, newStatus, newResolution)) {
         String transitionKey = toTransitionKey(newStatus, newResolution);
index 7be6e76825b94559c02d1615409939c9be3aa0d9..daba316cf6fccf958abe41ea0cee735ede4f9b0b 100644 (file)
@@ -83,7 +83,7 @@ public class DeleteCommentAction implements HotspotsWsAction {
   }
 
   private void validate(DbSession dbSession, IssueChangeDto issueChangeDto) {
-    hotspotWsSupport.loadAndCheckProject(dbSession, issueChangeDto.getIssueKey());
+    hotspotWsSupport.loadAndCheckBranch(dbSession, issueChangeDto.getIssueKey());
     checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only delete your own comments");
   }
 
index 47ada036db1a1e81820e47e8d61f58c5ab9886da..01eaffe2c2ca71927494e70b2574017bfc0c59fc 100644 (file)
@@ -102,7 +102,7 @@ public class EditCommentAction implements HotspotsWsAction {
   }
 
   private void validate(DbSession dbSession, IssueChangeDto issueChangeDto) {
-    hotspotWsSupport.loadAndCheckProject(dbSession, issueChangeDto.getIssueKey());
+    hotspotWsSupport.loadAndCheckBranch(dbSession, issueChangeDto.getIssueKey());
     checkArgument(Objects.equals(issueChangeDto.getUserUuid(), userSession.getUuid()), "You can only edit your own comments");
   }
 
index f33bdbfbb541f9adeec4b68c66383a9269a09722..dfc7c1ce6b9c3f487f270bc5063f6c51b5697ba5 100644 (file)
@@ -27,8 +27,10 @@ import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueChangeContext;
 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.IssueDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.user.UserSession;
 
@@ -51,9 +53,9 @@ public class HotspotWsSupport {
     return userSession.checkLoggedIn().getUuid();
   }
 
-  ComponentDto loadAndCheckProject(DbSession dbSession, String hotspotKey) {
+  BranchDto loadAndCheckBranch(DbSession dbSession, String hotspotKey) {
     IssueDto hotspot = loadHotspot(dbSession, hotspotKey);
-    return loadAndCheckProject(dbSession, hotspot, UserRole.USER);
+    return loadAndCheckBranch(dbSession, hotspot, UserRole.USER);
   }
 
   IssueDto loadHotspot(DbSession dbSession, String hotspotKey) {
@@ -63,15 +65,17 @@ public class HotspotWsSupport {
       .orElseThrow(() -> new NotFoundException(format("Hotspot '%s' does not exist", hotspotKey)));
   }
 
-  ComponentDto loadAndCheckProject(DbSession dbSession, IssueDto hotspot, String userRole) {
-    String projectUuid = hotspot.getProjectUuid();
-    checkArgument(projectUuid != null, "Hotspot '%s' has no project", hotspot.getKee());
+  BranchDto loadAndCheckBranch(DbSession dbSession, IssueDto hotspot, String userRole) {
+    String branchUuid = hotspot.getProjectUuid();
+    checkArgument(branchUuid != null, "Hotspot '%s' has no branch", hotspot.getKee());
 
-    ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, projectUuid)
-      .orElseThrow(() -> new NotFoundException(format("Project with uuid '%s' does not exist", projectUuid)));
-    userSession.checkComponentPermission(userRole, project);
+    BranchDto branch = dbClient.branchDao().selectByUuid(dbSession, branchUuid)
+      .orElseThrow(() -> new NotFoundException(format("Branch with uuid '%s' does not exist", branchUuid)));
+    ProjectDto project = dbClient.projectDao().selectByUuid(dbSession, branch.getProjectUuid())
+      .orElseThrow(() -> new NotFoundException(format("Project with uuid '%s' does not exist", branch.getProjectUuid())));
 
-    return project;
+    userSession.checkEntityPermission(userRole, project);
+    return branch;
   }
 
   boolean canChangeStatus(ComponentDto project) {
index f5c82402406730947250e3250608dc9ad2bd0111..5af876ed042ba2fd0d6e8595d654cfba99eff474 100644 (file)
@@ -177,9 +177,9 @@ public class ShowAction implements HotspotsWsAction {
 
   private void formatComponents(Components components, ShowWsResponse.Builder responseBuilder) {
     responseBuilder
-      .setProject(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getProject(), components.getBranch(), components.getPullRequest()))
+      .setProject(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getBranchComponent(), components.getBranch(), components.getPullRequest()))
       .setComponent(responseFormatter.formatComponent(Hotspots.Component.newBuilder(), components.getComponent(), components.getBranch(), components.getPullRequest()));
-    responseBuilder.setCanChangeStatus(hotspotWsSupport.canChangeStatus(components.getProject()));
+    responseBuilder.setCanChangeStatus(hotspotWsSupport.canChangeStatus(components.getBranchComponent()));
   }
 
   private static void formatRule(ShowWsResponse.Builder responseBuilder, RuleDto ruleDto) {
@@ -266,7 +266,7 @@ public class ShowAction implements HotspotsWsAction {
       .filter(Optional::isPresent)
       .map(Optional::get)
       .collect(toSet());
-    Set<ComponentDto> preloadedComponents = ImmutableSet.of(components.project, components.component);
+    Set<ComponentDto> preloadedComponents = ImmutableSet.of(components.branchComponent, components.component);
     FormattingContext formattingContext = issueChangeSupport
       .newFormattingContext(dbSession, singleton(hotspot), Load.ALL, preloadedUsers, preloadedComponents);
 
@@ -298,27 +298,26 @@ public class ShowAction implements HotspotsWsAction {
 
   private Components loadComponents(DbSession dbSession, IssueDto hotspot) {
     String componentUuid = hotspot.getComponentUuid();
-
-    ComponentDto project = hotspotWsSupport.loadAndCheckProject(dbSession, hotspot, UserRole.USER);
-    BranchDto branch = dbClient.branchDao().selectByUuid(dbSession, project.branchUuid()).orElseThrow(() -> new IllegalStateException("Can't find branch " + project.branchUuid()));
-
     checkArgument(componentUuid != null, "Hotspot '%s' has no component", hotspot.getKee());
-    boolean hotspotOnProject = Objects.equals(project.uuid(), componentUuid);
-    ComponentDto component = hotspotOnProject ? project
-      : dbClient.componentDao().selectByUuid(dbSession, componentUuid)
+
+    BranchDto branch = hotspotWsSupport.loadAndCheckBranch(dbSession, hotspot, UserRole.USER);
+    ComponentDto component = dbClient.componentDao().selectByUuid(dbSession, componentUuid)
         .orElseThrow(() -> new NotFoundException(format("Component with uuid '%s' does not exist", componentUuid)));
+    boolean hotspotOnBranch = Objects.equals(branch.getUuid(), componentUuid);
+    ComponentDto branchComponent = hotspotOnBranch ? component : dbClient.componentDao().selectByUuid(dbSession, branch.getUuid())
+      .orElseThrow(() -> new NotFoundException(format("Component with uuid '%s' does not exist", componentUuid)));
 
-    return new Components(project, component, branch);
+    return new Components(branchComponent, component, branch);
   }
 
   private static final class Components {
-    private final ComponentDto project;
+    private final ComponentDto branchComponent;
     private final ComponentDto component;
     private final String branch;
     private final String pullRequest;
 
-    private Components(ComponentDto project, ComponentDto component, BranchDto branch) {
-      this.project = project;
+    private Components(ComponentDto branchComponent, ComponentDto component, BranchDto branch) {
+      this.branchComponent = branchComponent;
       this.component = component;
       if (branch.isMain()) {
         this.branch = null;
@@ -342,8 +341,8 @@ public class ShowAction implements HotspotsWsAction {
       return pullRequest;
     }
 
-    public ComponentDto getProject() {
-      return project;
+    public ComponentDto getBranchComponent() {
+      return branchComponent;
     }
 
     public ComponentDto getComponent() {
index 66ebe4bd5654d5241729c2f977085e36020eb11a..a67178a38d9c404e6f9b056a908241fafa24705d 100644 (file)
@@ -22,15 +22,13 @@ package org.sonar.server.notification.ws;
 import java.util.List;
 import java.util.Optional;
 import org.sonar.api.notifications.NotificationChannel;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 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.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.issue.notification.MyNewIssuesNotificationHandler;
@@ -40,13 +38,13 @@ import org.sonar.server.ws.KeyExamples;
 
 import static java.util.Optional.empty;
 import static java.util.Optional.ofNullable;
+import static org.sonar.server.exceptions.BadRequestException.checkRequest;
+import static org.sonar.server.exceptions.NotFoundException.checkFound;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.ACTION_ADD;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_CHANNEL;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_LOGIN;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_PROJECT;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_TYPE;
-import static org.sonar.server.exceptions.NotFoundException.checkFound;
-import static org.sonar.server.exceptions.BadRequestException.checkRequest;
 
 public class AddAction implements NotificationsWsAction {
   private final NotificationCenter notificationCenter;
@@ -91,10 +89,10 @@ public class AddAction implements NotificationsWsAction {
 
     action.createParam(PARAM_TYPE)
       .setDescription("Notification type. Possible values are for:" +
-        "<ul>" +
-        "  <li>Global notifications: %s</li>" +
-        "  <li>Per project notifications: %s</li>" +
-        "</ul>",
+          "<ul>" +
+          "  <li>Global notifications: %s</li>" +
+          "  <li>Per project notifications: %s</li>" +
+          "</ul>",
         String.join(", ", dispatchers.getGlobalDispatchers()),
         String.join(", ", dispatchers.getProjectDispatchers()))
       .setRequired(true)
@@ -117,7 +115,7 @@ public class AddAction implements NotificationsWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       checkPermissions(request);
       UserDto user = getUser(dbSession, request);
-      Optional<ComponentDto> project = searchProject(dbSession, request);
+      Optional<ProjectDto> project = searchProject(dbSession, request);
       notificationUpdater.add(dbSession, request.getChannel(), request.getType(), user, project.orElse(null));
       dbSession.commit();
     }
@@ -128,11 +126,9 @@ public class AddAction implements NotificationsWsAction {
     return checkFound(dbClient.userDao().selectByLogin(dbSession, login), "User '%s' not found", login);
   }
 
-  private Optional<ComponentDto> searchProject(DbSession dbSession, AddRequest request) {
-    Optional<ComponentDto> project = request.getProject() == null ? empty() : Optional.of(componentFinder.getByKey(dbSession, request.getProject()));
-    project.ifPresent(p -> checkRequest(Qualifiers.PROJECT.equals(p.qualifier()) && Scopes.PROJECT.equals(p.scope()),
-      "Component '%s' must be a project", request.getProject()));
-    project.ifPresent(p -> userSession.checkComponentPermission(UserRole.USER, p));
+  private Optional<ProjectDto> searchProject(DbSession dbSession, AddRequest request) {
+    Optional<ProjectDto> project = request.getProject() == null ? empty() : Optional.of(componentFinder.getProjectByKey(dbSession, request.getProject()));
+    project.ifPresent(p -> userSession.checkEntityPermission(UserRole.USER, p));
     return project;
   }
 
index 983e91e7b7f024c98581e644d5fac7ed015cdf1a..4e3c468c358475eb2c7c54209fb4b344846980ca 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.db.property.PropertyDto;
 import org.sonar.db.property.PropertyQuery;
 import org.sonar.db.user.UserDto;
@@ -120,10 +121,10 @@ public class ListAction implements NotificationsWsAction {
   private UnaryOperator<ListResponse.Builder> addNotifications(DbSession dbSession, UserDto user) {
     return response -> {
       List<PropertyDto> properties = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setUserUuid(user.getUuid()).build(), dbSession);
-      Map<String, ComponentDto> componentsByUuid = searchProjects(dbSession, properties);
+      Map<String, EntityDto> entitiesByUuid = searchProjects(dbSession, properties);
 
       Predicate<PropertyDto> isNotification = prop -> prop.getKey().startsWith("notification.");
-      Predicate<PropertyDto> isComponentInDb = prop -> prop.getEntityUuid() == null || componentsByUuid.containsKey(prop.getEntityUuid());
+      Predicate<PropertyDto> isComponentInDb = prop -> prop.getEntityUuid() == null || entitiesByUuid.containsKey(prop.getEntityUuid());
 
       Notification.Builder notification = Notification.newBuilder();
 
@@ -131,7 +132,7 @@ public class ListAction implements NotificationsWsAction {
         .filter(isNotification)
         .filter(channelAndDispatcherAuthorized())
         .filter(isComponentInDb)
-        .map(toWsNotification(notification, componentsByUuid))
+        .map(toWsNotification(notification, entitiesByUuid))
         .sorted(comparing(Notification::getProject, nullsFirst(naturalOrder()))
           .thenComparing(Notification::getChannel)
           .thenComparing(Notification::getType))
@@ -154,19 +155,19 @@ public class ListAction implements NotificationsWsAction {
     return (prop.getEntityUuid() != null && dispatchers.getProjectDispatchers().contains(dispatcher)) || dispatchers.getGlobalDispatchers().contains(dispatcher);
   }
 
-  private Map<String, ComponentDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) {
-    Set<String> componentUuids = properties.stream()
+  private Map<String, EntityDto> searchProjects(DbSession dbSession, List<PropertyDto> properties) {
+    Set<String> entityUuids = properties.stream()
       .map(PropertyDto::getEntityUuid)
       .filter(Objects::nonNull)
       .collect(MoreCollectors.toSet(properties.size()));
-    Set<String> authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedEntityUuids(dbSession, componentUuids, userSession.getUuid(), UserRole.USER);
-    return dbClient.componentDao().selectByUuids(dbSession, componentUuids)
+    Set<String> authorizedProjectUuids = dbClient.authorizationDao().keepAuthorizedEntityUuids(dbSession, entityUuids, userSession.getUuid(), UserRole.USER);
+    return dbClient.entityDao().selectByUuids(dbSession, entityUuids)
       .stream()
-      .filter(c -> authorizedProjectUuids.contains(c.uuid()))
-      .collect(MoreCollectors.uniqueIndex(ComponentDto::uuid));
+      .filter(c -> authorizedProjectUuids.contains(c.getUuid()))
+      .collect(MoreCollectors.uniqueIndex(EntityDto::getUuid));
   }
 
-  private static Function<PropertyDto, Notification> toWsNotification(Notification.Builder notification, Map<String, ComponentDto> projectsByUuid) {
+  private static Function<PropertyDto, Notification> toWsNotification(Notification.Builder notification, Map<String, EntityDto> projectsByUuid) {
     return property -> {
       notification.clear();
       List<String> propertyKey = Splitter.on(".").splitToList(property.getKey());
@@ -178,12 +179,11 @@ public class ListAction implements NotificationsWsAction {
     };
   }
 
-  private static void populateProjectFields(Builder notification, String componentUuid,
-    Map<String, ComponentDto> projectsByUuid) {
-    ComponentDto project = projectsByUuid.get(componentUuid);
+  private static void populateProjectFields(Builder notification, String componentUuid, Map<String, EntityDto> projectsByUuid) {
+    EntityDto project = projectsByUuid.get(componentUuid);
     notification
       .setProject(project.getKey())
-      .setProjectName(project.name());
+      .setProjectName(project.getName());
   }
 
   private void checkPermissions(Request request) {
index 9256dd903cdab65ad9cc06f904c314cc386b5c17..5b08dd4e292ffd9ef27fb277ed07e68f9b4a65eb 100644 (file)
@@ -25,7 +25,7 @@ import javax.annotation.Nullable;
 import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.entity.EntityDto;
 import org.sonar.db.property.PropertyDto;
 import org.sonar.db.property.PropertyQuery;
 import org.sonar.db.user.UserDto;
@@ -45,50 +45,50 @@ public class NotificationUpdater {
   /**
    * Add a notification to a user.
    */
-  public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) {
+  public void add(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable EntityDto project) {
     String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel);
-    String projectUuid = project == null ? null : project.uuid();
+    String projectUuid = project == null ? null : project.getUuid();
     String projectKey = project == null ? null : project.getKey();
-    String projectName = project == null ? null : project.name();
-    String qualifier = project == null ? null : project.qualifier();
+    String projectName = project == null ? null : project.getName();
+    String qualifier = project == null ? null : project.getQualifier();
 
     List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery(
-      PropertyQuery.builder()
-        .setKey(key)
-        .setEntityUuid(projectUuid)
-        .setUserUuid(user.getUuid())
-        .build(),
-      dbSession).stream()
+        PropertyQuery.builder()
+          .setKey(key)
+          .setEntityUuid(projectUuid)
+          .setUserUuid(user.getUuid())
+          .build(),
+        dbSession).stream()
       .filter(notificationScope(project))
       .collect(MoreCollectors.toList());
     checkArgument(existingNotification.isEmpty()
       || !PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification already added");
 
     dbClient.propertiesDao().saveProperty(dbSession, new PropertyDto()
-      .setKey(key)
-      .setUserUuid(user.getUuid())
-      .setValue(PROP_NOTIFICATION_VALUE)
-      .setEntityUuid(projectUuid),
+        .setKey(key)
+        .setUserUuid(user.getUuid())
+        .setValue(PROP_NOTIFICATION_VALUE)
+        .setEntityUuid(projectUuid),
       user.getLogin(), projectKey, projectName, qualifier);
   }
 
   /**
    * Remove a notification from a user.
    */
-  public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable ComponentDto project) {
+  public void remove(DbSession dbSession, String channel, String dispatcher, UserDto user, @Nullable EntityDto project) {
     String key = String.join(".", PROP_NOTIFICATION_PREFIX, dispatcher, channel);
-    String projectUuid = project == null ? null : project.uuid();
+    String projectUuid = project == null ? null : project.getUuid();
     String projectKey = project == null ? null : project.getKey();
-    String projectName = project == null ? null : project.name();
-    String qualifier = project == null ? null : project.qualifier();
+    String projectName = project == null ? null : project.getName();
+    String qualifier = project == null ? null : project.getQualifier();
 
     List<PropertyDto> existingNotification = dbClient.propertiesDao().selectByQuery(
-      PropertyQuery.builder()
-        .setKey(key)
-        .setEntityUuid(projectUuid)
-        .setUserUuid(user.getUuid())
-        .build(),
-      dbSession).stream()
+        PropertyQuery.builder()
+          .setKey(key)
+          .setEntityUuid(projectUuid)
+          .setUserUuid(user.getUuid())
+          .build(),
+        dbSession).stream()
       .filter(notificationScope(project))
       .collect(MoreCollectors.toList());
     checkArgument(!existingNotification.isEmpty() && PROP_NOTIFICATION_VALUE.equals(existingNotification.get(0).getValue()), "Notification doesn't exist");
@@ -100,7 +100,7 @@ public class NotificationUpdater {
       .setEntityUuid(projectUuid), user.getLogin(), projectKey, projectName, qualifier);
   }
 
-  private static Predicate<PropertyDto> notificationScope(@Nullable ComponentDto project) {
+  private static Predicate<PropertyDto> notificationScope(@Nullable EntityDto project) {
     return prop -> project == null ? (prop.getEntityUuid() == null) : (prop.getEntityUuid() != null);
   }
 }
index f1dcdf2205046af203a8aca18a46d443cc32838e..dd9ee79cf83f98ab3924034edc094ea43f369bfb 100644 (file)
@@ -23,14 +23,12 @@ import java.util.List;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import org.sonar.api.notifications.NotificationChannel;
-import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Scopes;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.db.user.UserDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.issue.notification.MyNewIssuesNotificationHandler;
@@ -40,13 +38,13 @@ import org.sonar.server.ws.KeyExamples;
 
 import static java.util.Optional.empty;
 import static java.util.Optional.ofNullable;
+import static org.sonar.server.exceptions.BadRequestException.checkRequest;
+import static org.sonar.server.exceptions.NotFoundException.checkFound;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.ACTION_REMOVE;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_CHANNEL;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_LOGIN;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_PROJECT;
 import static org.sonar.server.notification.ws.NotificationsWsParameters.PARAM_TYPE;
-import static org.sonar.server.exceptions.NotFoundException.checkFound;
-import static org.sonar.server.exceptions.BadRequestException.checkRequest;
 
 public class RemoveAction implements NotificationsWsAction {
   private final NotificationCenter notificationCenter;
@@ -91,10 +89,10 @@ public class RemoveAction implements NotificationsWsAction {
 
     action.createParam(PARAM_TYPE)
       .setDescription("Notification type. Possible values are for:" +
-        "<ul>" +
-        "  <li>Global notifications: %s</li>" +
-        "  <li>Per project notifications: %s</li>" +
-        "</ul>",
+          "<ul>" +
+          "  <li>Global notifications: %s</li>" +
+          "  <li>Per project notifications: %s</li>" +
+          "</ul>",
         dispatchers.getGlobalDispatchers().stream().sorted().collect(Collectors.joining(", ")),
         dispatchers.getProjectDispatchers().stream().sorted().collect(Collectors.joining(", ")))
       .setRequired(true)
@@ -117,7 +115,7 @@ public class RemoveAction implements NotificationsWsAction {
     try (DbSession dbSession = dbClient.openSession(false)) {
       checkPermissions(request);
       UserDto user = getUser(dbSession, request);
-      Optional<ComponentDto> project = searchProject(dbSession, request);
+      Optional<ProjectDto> project = searchProject(dbSession, request);
       notificationUpdater.remove(dbSession, request.getChannel(), request.getType(), user, project.orElse(null));
       dbSession.commit();
     }
@@ -128,11 +126,8 @@ public class RemoveAction implements NotificationsWsAction {
     return checkFound(dbClient.userDao().selectByLogin(dbSession, login), "User '%s' not found", login);
   }
 
-  private Optional<ComponentDto> searchProject(DbSession dbSession, RemoveRequest request) {
-    Optional<ComponentDto> project = request.getProject() == null ? empty() : Optional.of(componentFinder.getByKey(dbSession, request.getProject()));
-    project.ifPresent(p -> checkRequest(Qualifiers.PROJECT.equals(p.qualifier()) && Scopes.PROJECT.equals(p.scope()),
-      "Component '%s' must be a project", request.getProject()));
-    return project;
+  private Optional<ProjectDto> searchProject(DbSession dbSession, RemoveRequest request) {
+    return request.getProject() == null ? empty() : Optional.of(componentFinder.getProjectByKey(dbSession, request.getProject()));
   }
 
   private void checkPermissions(RemoveRequest request) {
index 963e6734aebf6378af1253d3dbdbfc5ba552f96b..c3fb9db0d67a868e98d26728d62b3f437690d8c4 100644 (file)
@@ -81,7 +81,7 @@ public class PermissionWsSupport {
     return Optional.empty();
   }
 
-  public ComponentDto getRootComponentOrModule(DbSession dbSession, ProjectWsRef projectRef) {
+  public ComponentDto getRootComponent(DbSession dbSession, ProjectWsRef projectRef) {
     return componentFinder.getRootComponentByUuidOrKey(dbSession, projectRef.uuid(), projectRef.key());
   }
 
index 284ce6cbbeb30a0359423191bf140de38eeacfb7..bf30894174b5684e0c4c029b130434aeb4158a43 100644 (file)
@@ -92,7 +92,7 @@ public class ApplyTemplateAction implements PermissionsWsAction {
       PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(
         request.getTemplateId(), request.getTemplateName()));
 
-      ComponentDto project = wsSupport.getRootComponentOrModule(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey()));
+      ComponentDto project = wsSupport.getRootComponent(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey()));
       checkGlobalAdmin(userSession);
 
       permissionTemplateService.applyAndCommit(dbSession, template, Collections.singletonList(project));
index e8a180e417d9afce019db4618efd6bbb0a9a9d2c..bf9f67a1aaf4682034871e69cd10dc203f9a967e 100644 (file)
@@ -23,7 +23,7 @@ import org.sonar.api.server.ServerSide;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
 
@@ -42,8 +42,8 @@ public class ProjectDumpWsSupport {
 
   public void verifyAdminOfProjectByKey(String projectKey) {
     try (DbSession dbSession = dbClient.openSession(false)) {
-      ComponentDto project = componentFinder.getByKey(dbSession, projectKey);
-      userSession.checkComponentPermission(UserRole.ADMIN, project);
+      ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
+      userSession.checkEntityPermission(UserRole.ADMIN, project);
     }
   }
 }
index b704ddeb92c6f9fad543430fbd1cbb2a78b3c670..505e468c0aae5fb5a402e753f35453bb65ffafd0 100644 (file)
@@ -32,7 +32,9 @@ import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbInputStream;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.scannercache.ScannerCache;
@@ -85,10 +87,11 @@ public class GetAction implements AnalysisCacheWsAction {
     String branchKey = request.param(BRANCH);
 
     try (DbSession dbSession = dbClient.openSession(false)) {
-      ComponentDto component = componentFinder.getByKeyAndOptionalBranchOrPullRequest(dbSession, projectKey, branchKey, null);
-      checkPermission(component);
+      ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
+      checkPermission(project);
+      BranchDto branchDto = componentFinder.getBranchOrPullRequest(dbSession, project, branchKey, null);
 
-      try (DbInputStream dbInputStream = cache.get(component.uuid())) {
+      try (DbInputStream dbInputStream = cache.get(branchDto.getUuid())) {
         if (dbInputStream == null) {
           throw new NotFoundException("No cache for given branch or pull request");
         }
@@ -117,9 +120,9 @@ public class GetAction implements AnalysisCacheWsAction {
       .orElse(false);
   }
 
-  private void checkPermission(ComponentDto project) {
-    if (userSession.hasComponentPermission(UserRole.SCAN, project) ||
-      userSession.hasComponentPermission(UserRole.ADMIN, project) ||
+  private void checkPermission(ProjectDto project) {
+    if (userSession.hasEntityPermission(UserRole.SCAN, project) ||
+      userSession.hasEntityPermission(UserRole.ADMIN, project) ||
       userSession.hasPermission(SCAN)) {
       return;
     }
index f1f5e18f09018dd88f60be174f699a1572e18c31..bf30d61bd81dcd0dc775f49e664ffd28013d38ee 100644 (file)
@@ -88,14 +88,14 @@ public class SettingValidations {
   }
 
   private static boolean checkComponentQualifier(SettingData data, @Nullable PropertyDefinition definition) {
-    EntityDto component = data.entity;
-    if (component == null) {
+    EntityDto entity = data.entity;
+    if (entity == null) {
       return true;
     }
     if (definition == null) {
-      return SUPPORTED_QUALIFIERS.contains(component.getQualifier());
+      return SUPPORTED_QUALIFIERS.contains(entity.getQualifier());
     }
-    return definition.qualifiers().contains(component.getQualifier());
+    return definition.qualifiers().contains(entity.getQualifier());
   }
 
   public Consumer<SettingData> valueType() {
index 320e0bf411004c4c1067d9c06d940f6a6200ad75..90c6735b840d2e25eccff693334cad9e8856fb5e 100644 (file)
@@ -144,15 +144,15 @@ public class ValuesAction implements SettingsWsAction {
       return Optional.empty();
     }
 
-    EntityDto component = dbClient.entityDao().selectByKey(dbSession, componentKey)
+    EntityDto entity = dbClient.entityDao().selectByKey(dbSession, componentKey)
       .orElseThrow(() -> new NotFoundException(format("Component key '%s' not found", componentKey)));
 
-    if (!userSession.hasEntityPermission(USER, component) &&
-      !userSession.hasEntityPermission(UserRole.SCAN, component) &&
+    if (!userSession.hasEntityPermission(USER, entity) &&
+      !userSession.hasEntityPermission(UserRole.SCAN, entity) &&
       !userSession.hasPermission(GlobalPermission.SCAN)) {
       throw insufficientPrivilegesException();
     }
-    return Optional.of(component);
+    return Optional.of(entity);
   }
 
   private List<Setting> loadSettings(DbSession dbSession, Optional<EntityDto> component, Set<String> keys) {