}
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) {
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);
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;
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();
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();
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();
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
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());
}
}
}
@Override
- public final boolean hasChildProjectsPermission(String permission, ProjectDto application) {
+ public final boolean hasChildProjectsPermission(String permission, EntityDto application) {
return hasChildProjectsPermission(permission, application.getUuid());
}
}
@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;
}
}
@Override
- public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
+ public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) {
get().checkChildProjectsPermission(projectPermission, application);
return this;
}
}
@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
boolean hasChildProjectsPermission(String permission, ComponentDto component);
- boolean hasChildProjectsPermission(String permission, ProjectDto component);
+ boolean hasChildProjectsPermission(String permission, EntityDto application);
boolean hasPortfolioChildProjectsPermission(String permission, ComponentDto component);
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);
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},
return this;
}
+ public UserSessionRule registerPortfolios(PortfolioDto... portfolioDtos) {
+ ensureAbstractMockUserSession().registerPortfolios(portfolioDtos);
+ return this;
+ }
+
public UserSessionRule registerProjects(ProjectDto... projectDtos) {
ensureAbstractMockUserSession().registerProjects(projectDtos);
return this;
}
@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
}
@Override
- public UserSession checkChildProjectsPermission(String projectPermission, ProjectDto application) {
+ public UserSession checkChildProjectsPermission(String projectPermission, EntityDto application) {
currentUserSession.checkChildProjectsPermission(projectPermission, application);
return this;
}
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;
@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));
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));
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));
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);
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);
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);
}
@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));
.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();
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
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;
@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();
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
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();
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);
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);
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;
@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();
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();
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();
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
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();
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;
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()) {
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()));
}
}
@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);
}
}
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()));
}
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)
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;
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);
}
}
- 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();
}
*/
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;
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) {
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;
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;
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)));
.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) {
.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();
}
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();
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();
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);
}
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");
}
}
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");
}
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;
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) {
.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) {
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) {
.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);
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;
return pullRequest;
}
- public ComponentDto getProject() {
- return project;
+ public ComponentDto getBranchComponent() {
+ return branchComponent;
}
public ComponentDto getComponent() {
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;
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;
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)
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();
}
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;
}
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;
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();
.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))
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());
};
}
- 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) {
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;
/**
* 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");
.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);
}
}
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;
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;
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)
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();
}
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) {
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());
}
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));
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;
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);
}
}
}
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;
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");
}
.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;
}
}
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() {
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) {