defaults(), defaults(), defaults());
}
+ public BranchDto getBranchDto(ComponentDto branch) {
+ return db.getDbClient().branchDao().selectByUuid(dbSession, branch.uuid())
+ .orElseThrow(() -> new IllegalStateException("Project has invalid configuration"));
+ }
+
public ProjectDto getProjectDto(ComponentDto project) {
return db.getDbClient().projectDao().selectByUuid(dbSession, project.uuid())
.orElseThrow(() -> new IllegalStateException("Project has invalid configuration"));
return insertComponentAndBranchAndProject(ComponentTesting.newPublicProjectDto(uuid), false, defaults(), dtoPopulator);
}
-
public ComponentDto insertPublicProject(ComponentDto componentDto) {
return insertComponentAndBranchAndProject(componentDto, false);
}
db.commit();
}
+ public void addProjectBranchToApplicationBranch(ComponentDto applicationBranchComponent, ComponentDto... projectBranchesComponent) {
+ BranchDto applicationBranch = getBranchDto(applicationBranchComponent);
+ BranchDto[] componentDtos = Arrays.stream(projectBranchesComponent).map(this::getBranchDto).toArray(BranchDto[]::new);
+
+ addProjectBranchToApplicationBranch(applicationBranch, componentDtos);
+ }
+
public void addProjectBranchToApplicationBranch(BranchDto applicationBranch, BranchDto... projectBranches) {
for (BranchDto projectBranch : projectBranches) {
dbClient.applicationProjectsDao().addProjectBranchToAppBranch(dbSession, applicationBranch, projectBranch);
return insertHotspot(issue);
}
+ public final IssueDto insertHotspot(ProjectDto project, ComponentDto file, Consumer<IssueDto>... populators) {
+ RuleDto rule = db.rules().insertHotspotRule();
+ IssueDto issue = newIssue(rule, project, file)
+ .setType(SECURITY_HOTSPOT)
+ .setStatus(Issue.STATUS_TO_REVIEW)
+ .setResolution(null);
+ stream(populators).forEach(p -> p.accept(issue));
+ return insertHotspot(issue);
+ }
+
/**
* Inserts a Security Hotspot.
*
package org.sonar.db.measure;
import org.apache.commons.lang.math.RandomUtils;
+import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.project.ProjectDto;
import static com.google.common.base.Preconditions.checkNotNull;
.setData(String.valueOf(cursor++))
.setValue((double) cursor++);
}
+
+ public static LiveMeasureDto newLiveMeasure(BranchDto branchDto, MetricDto metric) {
+ return new LiveMeasureDto()
+ .setMetricUuid(metric.getUuid())
+ .setComponentUuid(branchDto.getUuid())
+ .setProjectUuid(branchDto.getProjectUuid())
+ .setData(String.valueOf(cursor++))
+ .setValue((double) cursor++);
+ }
}
protected abstract boolean hasPermissionImpl(GlobalPermission permission);
@Override
- public final boolean hasComponentPermission(String permission, ComponentDto component) {
- String projectUuid = defaultString(component.getMainBranchProjectUuid(), component.branchUuid());
- return hasProjectUuidPermission(permission, projectUuid);
+ public boolean hasComponentPermission(String permission, ComponentDto component) {
+
+ Optional<String> projectUuid1 = componentUuidToProjectUuid(component.uuid());
+
+ return projectUuid1
+ .map(projectUuid -> hasProjectUuidPermission(permission, projectUuid))
+ .orElse(false);
}
@Override
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
+import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.user.GroupDto;
return true;
}
+ @Override
+ public boolean hasComponentPermission(String permission, ComponentDto component) {
+ return true;
+ }
+
@Override
protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
// always root so unused
import org.sonar.core.util.stream.MoreCollectors;
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.component.ComponentTreeQuery;
import org.sonar.db.component.ComponentTreeQuery.Strategy;
import static java.util.Optional.of;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toSet;
-import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
+import static org.sonar.api.resources.Qualifiers.SUBVIEW;
+import static org.sonar.api.resources.Qualifiers.VIEW;
import static org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS;
/**
* Implementation of {@link UserSession} used in web server
*/
public class ServerUserSession extends AbstractUserSession {
+
+ private static final Set<String> QUALIFIERS = Set.of(VIEW, SUBVIEW);
+
@CheckForNull
private final UserDto userDto;
private final DbClient dbClient;
}
// if component is part of a branch, then permissions must be
// checked on the project (represented by its main branch)
- projectUuid = defaultIfEmpty(component.get().getMainBranchProjectUuid(), component.get().branchUuid());
+ projectUuid = getMainProjectUuid(dbSession, component.get());
projectUuidByComponentUuid.put(componentUuid, projectUuid);
return of(projectUuid);
}
.collect(toSet());
}
- private Set<String> findProjectUuids(Set<String> branchesComponents) {
+ private Set<String> findProjectUuids(Set<String> branchesComponentsUuid) {
try (DbSession dbSession = dbClient.openSession(false)) {
- return dbClient.componentDao().selectByUuids(dbSession, branchesComponents).stream()
- .map(ServerUserSession::getProjectId)
- .collect(toSet());
+ List<ComponentDto> componentDtos = dbClient.componentDao().selectByUuids(dbSession, branchesComponentsUuid);
+ return getMainProjectUuids(dbSession, componentDtos);
}
}
- private static String getProjectId(ComponentDto branchComponent) {
- return Optional.ofNullable(branchComponent.getMainBranchProjectUuid()).orElse(branchComponent.uuid());
+ private String getMainProjectUuid(DbSession dbSession, ComponentDto componentDto) {
+ // Portfolio & subPortfolio don't have branch, so branchUuid represents the portfolio uuid.
+ // technical project store root portfolio uuid in branchUuid
+ if (isPortfolioOrSubPortfolio(componentDto) || isTechnicalProject(componentDto)) {
+ return componentDto.branchUuid();
+ }
+ Optional<BranchDto> branchDto = dbClient.branchDao().selectByUuid(dbSession, componentDto.branchUuid());
+ return branchDto.map(BranchDto::getProjectUuid).orElseThrow(() -> new IllegalStateException("No branch found for component : " + componentDto));
+ }
+
+ private Set<String> getMainProjectUuids(DbSession dbSession, Collection<ComponentDto> components) {
+ Set<String> mainProjectUuids = new HashSet<>();
+
+ // the result of following stream could be project or application
+ Collection<String> componentsWithBranch = components.stream()
+ .filter(c -> !(isTechnicalProject(c) || isPortfolioOrSubPortfolio(c)))
+ .map(ComponentDto::branchUuid)
+ .toList();
+
+ dbClient.branchDao().selectByUuids(dbSession, componentsWithBranch).stream()
+ .map(BranchDto::getProjectUuid).forEach(mainProjectUuids::add);
+
+ components.stream()
+ .filter(c -> isTechnicalProject(c) || isPortfolioOrSubPortfolio(c))
+ .map(ComponentDto::branchUuid)
+ .forEach(mainProjectUuids::add);
+
+ return mainProjectUuids;
+ }
+
+ private static boolean isTechnicalProject(ComponentDto componentDto) {
+ return Qualifiers.PROJECT.equals(componentDto.qualifier()) && Scopes.FILE.equals(componentDto.scope());
+ }
+
+ private static boolean isPortfolioOrSubPortfolio(ComponentDto componentDto) {
+ return !Objects.isNull(componentDto.qualifier()) && QUALIFIERS.contains(componentDto.qualifier());
}
private boolean hasPermission(String permission, String projectUuid) {
@Override
protected List<ComponentDto> doKeepAuthorizedComponents(String permission, Collection<ComponentDto> components) {
try (DbSession dbSession = dbClient.openSession(false)) {
- Set<String> projectUuids = components.stream()
- .map(c -> defaultIfEmpty(c.getMainBranchProjectUuid(), c.branchUuid()))
- .collect(MoreCollectors.toSet(components.size()));
+ Set<String> projectUuids = getMainProjectUuids(dbSession, components);
Map<String, ComponentDto> originalComponents = findComponentsByCopyComponentUuid(components,
- dbSession);
+ dbSession);
- Set<String> originalComponentsProjectUuids = originalComponents.values().stream()
- .map(c -> defaultIfEmpty(c.getMainBranchProjectUuid(), c.branchUuid()))
- .collect(MoreCollectors.toSet(components.size()));
+ Set<String> originalComponentsProjectUuids = getMainProjectUuids(dbSession, originalComponents.values());
Set<String> allProjectUuids = new HashSet<>(projectUuids);
allProjectUuids.addAll(originalComponentsProjectUuids);
.filter(c -> {
if (c.getCopyComponentUuid() != null) {
var componentDto = originalComponents.get(c.getCopyComponentUuid());
- return componentDto != null && authorizedProjectUuids.contains(defaultIfEmpty(componentDto.getMainBranchProjectUuid(), componentDto.branchUuid()));
+ return componentDto != null && authorizedProjectUuids.contains(getMainProjectUuid(dbSession, componentDto));
}
return authorizedProjectUuids.contains(c.branchUuid()) || authorizedProjectUuids.contains(
- c.getMainBranchProjectUuid());
+ getMainProjectUuid(dbSession, c));
})
.collect(MoreCollectors.toList(components.size()));
}
private Map<String, ComponentDto> findComponentsByCopyComponentUuid(Collection<ComponentDto> components, DbSession dbSession) {
Set<String> copyComponentsUuid = components.stream()
- .map(ComponentDto::getCopyComponentUuid)
- .filter(Objects::nonNull)
- .collect(MoreCollectors.toSet(components.size()));
+ .map(ComponentDto::getCopyComponentUuid)
+ .filter(Objects::nonNull)
+ .collect(MoreCollectors.toSet(components.size()));
return dbClient.componentDao().selectByUuids(dbSession, copyComponentsUuid).stream()
- .collect(Collectors.toMap(ComponentDto::uuid, componentDto -> componentDto));
+ .collect(Collectors.toMap(ComponentDto::uuid, componentDto -> componentDto));
}
@Override
ComponentDto project = db.components().insertPrivateProject();
ComponentDto application = db.components().insertPrivateApplication();
db.components().addApplicationProject(application, project);
- //add computed project
+ // add computed project
db.components().insertComponent(newProjectCopy(project, application));
UserSession underTest = newUserSession(user);
assertThat(underTest.keepAuthorizedComponents(UserRole.USER, Arrays.asList(app, subPortfolio, app2))).containsExactly(app, subPortfolio);
assertThat(underTest.keepAuthorizedComponents(UserRole.ADMIN, Arrays.asList(project1, project2, project3, project4, project5, project6))).isEmpty();
- assertThat(underTest.keepAuthorizedComponents(UserRole.USER, Arrays.asList(project1, project2, project3, project4, project5, project6))).containsExactly(project1, project2, project4, project5);
-
+ assertThat(underTest.keepAuthorizedComponents(UserRole.USER, Arrays.asList(project1, project2, project3, project4, project5, project6))).containsExactly(project1, project2,
+ project4, project5);
assertThat(underTest.keepAuthorizedComponents(UserRole.USER, Arrays.asList(copyProject1, copyProject2, copyProject3, copyProject4, copyProject5, copyProject6)))
- .containsExactly(copyProject1, copyProject2, copyProject4, copyProject5);
+ .containsExactly(copyProject1, copyProject2, copyProject4, copyProject5);
+ }
+
+ @Test
+ public void keepAuthorizedComponents_filters__files_with_granted_permissions_for_logged_in_user() {
+ ComponentDto project1 = db.components().insertPrivateProject();
+ ComponentDto project2 = db.components().insertPrivateProject();
+
+ UserDto user = db.users().insertUser();
+ UserSession underTest = newUserSession(user);
+
+ db.users().insertProjectPermissionOnUser(user, UserRole.USER, project1);
+ ComponentDto file1Project1 = db.components().insertFile(db.components().getProjectDto(project1));
+ ComponentDto file2Project1 = db.components().insertFile(db.components().getProjectDto(project1));
+
+ ComponentDto file1Project2 = db.components().insertFile(db.components().getProjectDto(project2));
+
+ assertThat(underTest.keepAuthorizedComponents(UserRole.USER, List.of(file1Project1, file2Project1, file1Project2))).containsExactly(file1Project1, file2Project1);
}
@Test
}
@Test
- public void keepAuthorizedProjects_shouldAcceptsPublicProjects_whenCalledWithPublicPermissionAndNoUser(){
- ComponentDto publicProject = db.components().insertPublicProject();
+ public void keepAuthorizedProjects_shouldAcceptsPublicProjects_whenCalledWithPublicPermissionAndNoUser() {
+ ComponentDto publicProject = db.components().insertPublicProject();
ComponentDto privateProject = db.components().insertPrivateProject();
Set<ProjectDto> projectDto = Set.of(getProjectDto(publicProject), getProjectDto(privateProject));
}
@Test
- public void keepAuthorizedProjects_shouldAcceptsPublicProjects_whenCalledWithPublicPermissionAndAnUser(){
+ public void keepAuthorizedProjects_shouldAcceptsPublicProjects_whenCalledWithPublicPermissionAndAnUser() {
UserDto userDto = db.users().insertUser();
- ComponentDto publicProject = db.components().insertPublicProject();
+ ComponentDto publicProject = db.components().insertPublicProject();
ComponentDto privateProject = db.components().insertPrivateProject();
Set<ProjectDto> projectDto = Set.of(getProjectDto(publicProject), getProjectDto(privateProject));
}
@Test
- public void keepAuthorizedProjects_shouldAcceptsOnlyPrivateProject_whenCalledWithGoodPermissionAndAnUser(){
+ public void keepAuthorizedProjects_shouldAcceptsOnlyPrivateProject_whenCalledWithGoodPermissionAndAnUser() {
String permission = "aNewPermission";
UserDto userDto = db.users().insertUser();
- ComponentDto publicProject = db.components().insertPublicProject();
+ ComponentDto publicProject = db.components().insertPublicProject();
ComponentDto privateProject = db.components().insertPrivateProject();
- db.users().insertProjectPermissionOnUser(userDto, permission,privateProject);
+ db.users().insertProjectPermissionOnUser(userDto, permission, privateProject);
ComponentDto privateProjectWithoutPermission = db.components().insertPrivateProject();
Set<ProjectDto> projectDto = Set.of(getProjectDto(publicProject), getProjectDto(privateProject), getProjectDto(privateProjectWithoutPermission));
}
@Test
- public void keepAuthorizedProjects_shouldRejectPrivateAndPublicProject_whenCalledWithWrongPermissionAndNoUser(){
+ public void keepAuthorizedProjects_shouldRejectPrivateAndPublicProject_whenCalledWithWrongPermissionAndNoUser() {
String permission = "aNewPermission";
UserDto userDto = db.users().insertUser();
- ComponentDto publicProject = db.components().insertPublicProject();
+ ComponentDto publicProject = db.components().insertPublicProject();
ComponentDto privateProject = db.components().insertPrivateProject();
- db.users().insertProjectPermissionOnUser(userDto, permission,privateProject);
+ db.users().insertProjectPermissionOnUser(userDto, permission, privateProject);
ComponentDto privateProjectWithoutPermission = db.components().insertPrivateProject();
Set<ProjectDto> projectDto = Set.of(getProjectDto(publicProject), getProjectDto(privateProject), getProjectDto(privateProjectWithoutPermission));
return db.components().getProjectDto(publicProject);
}
-
private ServerUserSession newUserSession(@Nullable UserDto userDto) {
return new ServerUserSession(dbClient, userDto);
}
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.sonar.api.web.UserRole;
+import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.portfolio.PortfolioDto;
import static com.google.common.base.Preconditions.checkArgument;
public abstract class AbstractMockUserSession<T extends AbstractMockUserSession> extends AbstractUserSession {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(AbstractMockUserSession.class);
private static final Set<String> PUBLIC_PERMISSIONS = ImmutableSet.of(UserRole.USER, UserRole.CODEVIEWER); // FIXME to check with Simon
private final Class<T> clazz;
private final HashMultimap<String, String> projectUuidByPermission = HashMultimap.create();
private final Set<GlobalPermission> permissions = new HashSet<>();
private final Map<String, String> projectUuidByComponentUuid = new HashMap<>();
+ private final Map<String, String> projectUuidByBranchUuid = new HashMap<>();
private final Map<String, Set<String>> applicationProjects = new HashMap<>();
private final Map<String, Set<String>> portfolioProjects = new HashMap<>();
private final Set<String> projectPermissions = new HashSet<>();
return clazz.cast(this);
}
- public T registerPortfolioProjects(PortfolioDto portfolio, ProjectDto... portfolioProjects) {
- registerPortfolios(portfolio);
- registerProjects(portfolioProjects);
-
- Set<String> portfolioProjectsUuid = Arrays.stream(portfolioProjects)
- .map(ProjectDto::getUuid)
- .collect(Collectors.toSet());
-
- this.portfolioProjects.put(portfolio.getUuid(), portfolioProjectsUuid);
+ public T registerBranches(BranchDto ...branchDtos){
+ Arrays.stream(branchDtos)
+ .forEach(branch -> projectUuidByBranchUuid.put(branch.getUuid(), branch.getProjectUuid()));
+ return clazz.cast(this);
+ }
+ /**
+ * Branches need to be registered in order to save the mapping between branch and project.
+ */
+ public T addProjectBranchMapping(String projectUuid, ComponentDto... componentDtos) {
+ Arrays.stream(componentDtos)
+ .forEach(componentDto -> projectUuidByBranchUuid.put(componentDto.uuid(), projectUuid));
return clazz.cast(this);
}
@Override
protected Optional<String> componentUuidToProjectUuid(String componentUuid) {
- return Optional.ofNullable(projectUuidByComponentUuid.get(componentUuid));
+ return Optional.ofNullable(Optional.ofNullable(projectUuidByBranchUuid.get(componentUuid))
+ .orElse(projectUuidByComponentUuid.get(componentUuid)));
+ }
+
+ @Override
+ public boolean hasComponentPermission(String permission, ComponentDto component) {
+ return componentUuidToProjectUuid(component.uuid())
+ .or(() -> componentUuidToProjectUuid(component.branchUuid()))
+ .map(projectUuid -> hasProjectUuidPermission(permission, projectUuid)).orElseGet(() -> {
+ LOGGER.warn("No project uuid for branchUuid : {}", component.branchUuid());
+ return false;
+ });
}
@Override
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
+import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.permission.GlobalPermission;
import org.sonar.db.portfolio.PortfolioDto;
return this;
}
+ public UserSessionRule addProjectBranchMapping(String projectUuid, ComponentDto... branchComponents) {
+ ensureAbstractMockUserSession().addProjectBranchMapping(projectUuid, branchComponents);
+ return this;
+ }
+
+ public UserSession registerBranches(BranchDto ...branchDtos){
+ ensureAbstractMockUserSession().registerBranches(branchDtos);
+ return this;
+ }
+
public UserSessionRule addProjectPermission(String projectPermission, ProjectDto... projectDto) {
ensureAbstractMockUserSession().addProjectPermission(projectPermission, projectDto);
return this;
ComponentDto project = db.components().insertPublicProject();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("branch1"));
UserDto user = db.users().insertUser();
- userSession.logIn(user).addProjectPermission(SCAN.getKey(), project);
+ userSession.logIn(user).addProjectPermission(SCAN.getKey(), project).addProjectBranchMapping(project.uuid(), branch);
Map<String, String> randomCharacteristics = randomNonEmptyMap();
BranchSupport.ComponentKey componentKey = createComponentKeyOfBranch(project.getKey(), "branch1");
when(branchSupportDelegate.createComponentKey(project.getKey(), randomCharacteristics)).thenReturn(componentKey);
userSession.logIn(user).addProjectPermission(SCAN.getKey(), existingProject);
Map<String, String> randomCharacteristics = randomNonEmptyMap();
ComponentDto createdBranch = createButDoNotInsertBranch(existingProject);
+ userSession.addProjectBranchMapping(existingProject.uuid(), createdBranch);
BranchSupport.ComponentKey componentKey = createComponentKeyOfBranch(existingProject.getKey(), "branch1");
when(branchSupportDelegate.createComponentKey(existingProject.getKey(), randomCharacteristics)).thenReturn(componentKey);
when(branchSupportDelegate.createBranchComponent(any(DbSession.class), same(componentKey), eq(existingProject), eq(exitingProjectMainBranch))).thenReturn(createdBranch);
import org.junit.Test;
import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.project.ProjectDto;
import org.sonar.server.component.TestComponentFinder;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
userSession.logIn("john").addProjectPermission(USER, project);
String branchName = randomAlphanumeric(248);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto directory = db.components().insertComponent(newDirectory(branch, "src"));
ComponentDto file = db.components().insertComponent(newFileDto(project.uuid(), branch, directory));
MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY));
userSession.logIn("john").addProjectPermission(USER, project);
String branchName = randomAlphanumeric(248);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
String result = ws.newRequest()
userSession.logIn("john").addProjectPermission(USER, project);
String pullRequestKey = RandomStringUtils.randomAlphanumeric(100);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
String result = ws.newRequest()
userSession.addProjectPermission(UserRole.USER, project);
String branchKey = "my_branch";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", project.uuid()));
ComponentDto file = db.components().insertComponent(newFileDto(project.uuid(), branch, directory));
db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
userSession.addProjectPermission(UserRole.USER, project);
String pullRequest = "pr-1234";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(pullRequest).setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", project.uuid()));
ComponentDto file = db.components().insertComponent(newFileDto(project.uuid(), branch, directory));
db.components().insertSnapshot(branch, s -> s.setProjectVersion("1.1"));
.setNeedIssueSync(true));
ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch1, "dir", project1.uuid()));
ComponentDto file = db.components().insertComponent(newFileDto(project1.uuid(), branch1, directory));
+ userSession.addProjectBranchMapping(project1.uuid(), branch1);
ComponentDto project2 = db.components().insertPrivateProject();
String branchName2 = randomAlphanumeric(248);
ComponentDto branch2 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(true).setKey(branchName2));
String branchName3 = randomAlphanumeric(248);
ComponentDto branch3 = db.components().insertProjectBranch(project2, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName3));
+ userSession.addProjectBranchMapping(project2.uuid(), branch2);
+ userSession.addProjectBranchMapping(project2.uuid(), branch3);
ComponentDto project3 = db.components().insertPrivateProject();
String pullRequestKey4 = randomAlphanumeric(100);
ComponentDto fileOfBranch4 = db.components().insertComponent(newFileDto(project3.uuid(), branch4, directoryOfBranch4));
String branchName5 = randomAlphanumeric(248);
ComponentDto branch5 = db.components().insertProjectBranch(project3, b -> b.setBranchType(BRANCH).setNeedIssueSync(false).setKey(branchName5));
+ userSession.addProjectBranchMapping(project3.uuid(), branch4);
+ userSession.addProjectBranchMapping(project3.uuid(), branch5);
userSession.addProjectPermission(UserRole.USER, project1, project2, project3);
userSession.registerComponents(portfolio1, portfolio2, subview, project1, project2, project3);
private void insertJsonExampleComponentsAndSnapshots() {
ComponentDto project = db.components().insertPrivateProject(c -> c.setUuid("AVIF98jgA3Ax6PH2efOW")
- .setBranchUuid("AVIF98jgA3Ax6PH2efOW")
- .setKey("com.sonarsource:java-markdown")
- .setName("Java Markdown")
- .setDescription("Java Markdown Project")
- .setQualifier(Qualifiers.PROJECT),
+ .setBranchUuid("AVIF98jgA3Ax6PH2efOW")
+ .setKey("com.sonarsource:java-markdown")
+ .setName("Java Markdown")
+ .setDescription("Java Markdown Project")
+ .setQualifier(Qualifiers.PROJECT),
p -> p.setTagsString("language, plugin"));
userSession.addProjectPermission(USER, project);
db.components().insertSnapshot(project, snapshot -> snapshot
@Test
public void project_branch_reference_from_application_branch() {
- ComponentDto application = db.components().insertPrivateProject(c -> c.setQualifier(APP).setKey("app-key"));
String appBranchName = "app-branch";
String projectBranchName = "project-branch";
+
+ ComponentDto application = db.components().insertPrivateProject(c -> c.setQualifier(APP).setKey("app-key"));
ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(appBranchName));
+
ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("project-key"));
ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey(projectBranchName));
ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch)
.setKey(applicationBranch.getKey() + project.getKey()).setMainBranchProjectUuid(application.uuid()));
+
logInWithBrowsePermission(application);
+ userSession.addProjectBranchMapping(application.uuid(), applicationBranch);
TreeWsResponse result = ws.newRequest()
.setParam(MeasuresWsParameters.PARAM_COMPONENT, applicationBranch.getKey())
userSession.addProjectPermission(UserRole.USER, project);
String branchKey = "my_branch";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchKey));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", project.uuid()));
ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(directory, project.uuid()));
userSession.addProjectPermission(UserRole.USER, project);
String pullRequestId = "pr-123";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(pullRequestId).setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto directory = db.components().insertComponent(newDirectoryOnBranch(branch, "dir", project.uuid()));
ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(directory, project.uuid()));
private ComponentDto initJsonExampleComponents() throws IOException {
ComponentDto project = db.components().insertPrivateProject(c -> c.setUuid("MY_PROJECT_ID")
- .setDescription("MY_PROJECT_DESCRIPTION")
- .setKey("MY_PROJECT_KEY")
- .setName("Project Name")
- .setBranchUuid("MY_PROJECT_ID"),
+ .setDescription("MY_PROJECT_DESCRIPTION")
+ .setKey("MY_PROJECT_KEY")
+ .setName("Project Name")
+ .setBranchUuid("MY_PROJECT_ID"),
p -> p.setTagsString("abc,def"));
db.components().insertSnapshot(project);
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
String branchName = randomAlphanumeric(248);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSessionRule.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()).setMainBranchProjectUuid(project.uuid()));
db.measures().insertLiveMeasure(file, dataMetric, m -> m.setData(format("<duplications>\n" +
" <g>\n" +
assertJson(result).isSimilarTo(
format("{\n" +
- " \"duplications\": [\n" +
- " {\n" +
- " \"blocks\": [\n" +
- " {\n" +
- " \"from\": 20,\n" +
- " \"size\": 5,\n" +
- " \"_ref\": \"1\"\n" +
- " },\n" +
- " {\n" +
- " \"from\": 31,\n" +
- " \"size\": 5,\n" +
- " \"_ref\": \"1\"\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
- " ],\n" +
- " \"files\": {\n" +
- " \"1\": {\n" +
- " \"key\": \"%s\",\n" +
- " \"name\": \"%s\",\n" +
- " \"uuid\": \"%s\",\n" +
- " \"project\": \"%s\",\n" +
- " \"projectUuid\": \"%s\",\n" +
- " \"projectName\": \"%s\"\n" +
- " \"branch\": \"%s\"\n" +
- " }\n" +
- " }\n" +
- "}",
+ " \"duplications\": [\n" +
+ " {\n" +
+ " \"blocks\": [\n" +
+ " {\n" +
+ " \"from\": 20,\n" +
+ " \"size\": 5,\n" +
+ " \"_ref\": \"1\"\n" +
+ " },\n" +
+ " {\n" +
+ " \"from\": 31,\n" +
+ " \"size\": 5,\n" +
+ " \"_ref\": \"1\"\n" +
+ " }\n" +
+ " ]\n" +
+ " }\n" +
+ " ],\n" +
+ " \"files\": {\n" +
+ " \"1\": {\n" +
+ " \"key\": \"%s\",\n" +
+ " \"name\": \"%s\",\n" +
+ " \"uuid\": \"%s\",\n" +
+ " \"project\": \"%s\",\n" +
+ " \"projectUuid\": \"%s\",\n" +
+ " \"projectName\": \"%s\"\n" +
+ " \"branch\": \"%s\"\n" +
+ " }\n" +
+ " }\n" +
+ "}",
file.getKey(), file.longName(), file.uuid(), branch.getKey(), branch.uuid(), project.longName(), branchName));
}
userSessionRule.addProjectPermission(UserRole.CODEVIEWER, project);
String pullRequestKey = randomAlphanumeric(100);
ComponentDto pullRequest = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey));
+ userSessionRule.addProjectBranchMapping(project.uuid(), pullRequest);
ComponentDto file = db.components().insertComponent(newFileDto(pullRequest, project.uuid()));
db.measures().insertLiveMeasure(file, dataMetric, m -> m.setData(format("<duplications>\n" +
" <g>\n" +
assertJson(result).isSimilarTo(
format("{\n" +
- " \"duplications\": [\n" +
- " {\n" +
- " \"blocks\": [\n" +
- " {\n" +
- " \"from\": 20,\n" +
- " \"size\": 5,\n" +
- " \"_ref\": \"1\"\n" +
- " },\n" +
- " {\n" +
- " \"from\": 31,\n" +
- " \"size\": 5,\n" +
- " \"_ref\": \"1\"\n" +
- " }\n" +
- " ]\n" +
- " }\n" +
- " ],\n" +
- " \"files\": {\n" +
- " \"1\": {\n" +
- " \"key\": \"%s\",\n" +
- " \"name\": \"%s\",\n" +
- " \"uuid\": \"%s\",\n" +
- " \"project\": \"%s\",\n" +
- " \"projectUuid\": \"%s\",\n" +
- " \"projectName\": \"%s\"\n" +
- " \"pullRequest\": \"%s\"\n" +
- " }\n" +
- " }\n" +
- "}",
+ " \"duplications\": [\n" +
+ " {\n" +
+ " \"blocks\": [\n" +
+ " {\n" +
+ " \"from\": 20,\n" +
+ " \"size\": 5,\n" +
+ " \"_ref\": \"1\"\n" +
+ " },\n" +
+ " {\n" +
+ " \"from\": 31,\n" +
+ " \"size\": 5,\n" +
+ " \"_ref\": \"1\"\n" +
+ " }\n" +
+ " ]\n" +
+ " }\n" +
+ " ],\n" +
+ " \"files\": {\n" +
+ " \"1\": {\n" +
+ " \"key\": \"%s\",\n" +
+ " \"name\": \"%s\",\n" +
+ " \"uuid\": \"%s\",\n" +
+ " \"project\": \"%s\",\n" +
+ " \"projectUuid\": \"%s\",\n" +
+ " \"projectName\": \"%s\"\n" +
+ " \"pullRequest\": \"%s\"\n" +
+ " }\n" +
+ " }\n" +
+ "}",
file.getKey(), file.longName(), file.uuid(), pullRequest.getKey(), pullRequest.uuid(), project.longName(), pullRequestKey));
}
IssueDto hotspot = dbTester.issues().insertHotspot(branch, file);
insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), project, UserRole.USER);
+ userSessionRule.addProjectBranchMapping(project.uuid(), branch);
UserDto assignee = insertUserWithProjectUserPermission(randomAlphanumeric(15), project);
when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
IssueDto hotspot = dbTester.issues().insertHotspot(branch, file);
insertAndLoginAsUserWithProjectUserPermission(randomAlphanumeric(10), project, UserRole.USER);
+ userSessionRule.addProjectBranchMapping(project.uuid(), branch);
UserDto assignee = insertUser(randomAlphanumeric(15));
when(issueFieldsSetter.assign(eq(hotspot.toDefaultIssue()), userMatcher(assignee), any(IssueChangeContext.class))).thenReturn(true);
ComponentDto project = dbTester.components().insertPublicProject();
String branchName = randomAlphanumeric(248);
ComponentDto branch = dbTester.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSessionRule.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = dbTester.components().insertComponent(newFileDto(branch, project.uuid()));
userSessionRule.registerComponents(project);
RuleDto rule = newRule(SECURITY_HOTSPOT);
String pullRequestKey = randomAlphanumeric(100);
ComponentDto pullRequest = dbTester.components().insertProjectBranch(project,
t -> t.setBranchType(BranchType.PULL_REQUEST).setKey(pullRequestKey));
+ userSessionRule.addProjectBranchMapping(project.uuid(), pullRequest);
ComponentDto file = dbTester.components().insertComponent(newFileDto(pullRequest));
userSessionRule.registerComponents(project);
RuleDto rule = newRule(SECURITY_HOTSPOT);
userSession.logIn(user);
ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature").setBranchType(BranchType.BRANCH));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto fileOnBranch = db.components().insertComponent(newFileDto(branch, project.uuid()));
addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
RuleDto rule = db.rules().insertIssueRule();
userSession.logIn(user);
ComponentDto project = db.components().insertPrivateProject();
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("feature").setBranchType(branchType));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto fileOnBranch = db.components().insertComponent(newFileDto(branch, project.uuid()));
addUserProjectPermissions(user, project, USER, ISSUE_ADMIN);
RuleDto rule = db.rules().insertIssueRule();
import java.util.Optional;
import java.util.function.Function;
-import java.util.function.Predicate;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.server.ws.WebService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
-import static org.assertj.core.api.InstanceOfAssertFactories.OPTIONAL;
import static org.sonar.api.utils.DateUtils.parseDateTime;
import static org.sonar.api.web.UserRole.USER;
import static org.sonar.db.component.BranchDto.DEFAULT_MAIN_BRANCH_NAME;
userSession.addProjectPermission(USER, project);
String branchName = "my_branch";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto complexity = db.measures().insertMetric(m1 -> m1.setKey("complexity").setValueType("INT"));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setValueType("INT"));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto bugs = db.measures().insertMetric(m1 -> m1.setKey("bugs").setOptimizedBestValue(false).setValueType("INT"));
import static java.lang.Double.parseDouble;
import static java.lang.String.format;
-import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
userSession.addProjectPermission(USER, project);
String branchName = "my-branch";
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
@Test
public void show_branch_on_empty_response_if_not_main_branch() {
ComponentDto mainProjectBranch = db.components().insertPrivateProject();
- ComponentDto branch = db.components().insertProjectBranch(mainProjectBranch, b -> b.setKey("develop"));
userSession.addProjectPermission(USER, mainProjectBranch);
+ ComponentDto branch = db.components().insertProjectBranch(mainProjectBranch, b -> b.setKey("develop"));
+ userSession.addProjectBranchMapping(mainProjectBranch.uuid(), branch);
+
ComponentDto file = db.components().insertComponent(newFileDto(branch, mainProjectBranch.uuid()));
MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()).setMainBranchProjectUuid(project.uuid()));
MetricDto complexity = db.measures().insertMetric(m -> m.setValueType(INT.name()));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(USER, project);
ComponentDto pr = db.components().insertProjectBranch(project, b -> b.setKey("pr").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), pr);
SnapshotDto analysis = db.components().insertSnapshot(pr);
ComponentDto file = db.components().insertComponent(newFileDto(pr, project.uuid()));
MetricDto bug = db.measures().insertMetric(m -> m.setValueType(INT.name()).setKey(CoreMetrics.BUGS_KEY));
userSession.registerApplication(application);
String branchName = "app-branch";
ComponentDto applicationBranch = db.components().insertProjectBranch(application, a -> a.setKey(branchName), a -> a.setUuid("custom-uuid"));
+ userSession.addProjectBranchMapping(application.uuid(), applicationBranch);
ComponentDto project = db.components().insertPrivateProject(p -> p.setKey("project-key"));
+
ComponentDto projectBranch = db.components().insertProjectBranch(project, b -> b.setKey("project-branch"));
+ userSession.addProjectBranchMapping(project.uuid(), projectBranch);
ComponentDto techProjectBranch = db.components().insertComponent(newProjectCopy(projectBranch, applicationBranch)
.setKey(applicationBranch.getKey() + branchName + projectBranch.getKey()));
+
SnapshotDto applicationBranchAnalysis = db.components().insertSnapshot(applicationBranch);
db.measures().insertLiveMeasure(applicationBranch, ncloc, m -> m.setValue(5d));
db.measures().insertLiveMeasure(techProjectBranch, ncloc, m -> m.setValue(1d));
public void return_measures_on_sub_view() {
ComponentDto view = db.components().insertPrivatePortfolio();
ComponentDto subView = db.components().insertComponent(newSubPortfolio(view));
+ userSession.addProjectPermission(UserRole.USER, view);
userSession.addProjectPermission(UserRole.USER, subView);
MetricDto metric = db.measures().insertMetric(m -> m.setValueType(FLOAT.name()));
db.measures().insertLiveMeasure(subView, metric, m -> m.setValue(15.5d));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
- assertThatThrownBy(() -> call(singletonList(project.uuid()), null))
+ assertThatThrownBy(() -> call(singletonList(project.uuid()), null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The 'metricKeys' parameter is missing");
}
userSession.addProjectPermission(UserRole.USER, project);
MetricDto metric = db.measures().insertMetric();
- assertThatThrownBy(() -> call(singletonList(project.getKey()), newArrayList("violations", metric.getKey(), "ncloc")))
+ assertThatThrownBy(() -> call(singletonList(project.getKey()), newArrayList("violations", metric.getKey(), "ncloc")))
.isInstanceOf(BadRequestException.class)
.hasMessage("The following metrics are not found: ncloc, violations");
}
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
SnapshotDto analysis = db.components().insertSnapshot(branch);
MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d));
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("pr-123").setBranchType(PULL_REQUEST));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
SnapshotDto analysis = db.components().insertSnapshot(branch);
MeasureDto measure = db.measures().insertMeasure(file, analysis, nclocMetric, m -> m.setValue(2d));
.setParam(PARAM_COMPONENT, "file-key")
.setParam(PARAM_METRICS, "ncloc")
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Component key 'file-key' not found");
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining("Component key 'file-key' not found");
}
@Test
.setParam(PARAM_BRANCH, "another_branch")
.setParam(PARAM_METRICS, "ncloc")
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
}
@Test
import static org.sonar.api.utils.DateUtils.formatDate;
import static org.sonar.api.utils.DateUtils.formatDateTime;
import static org.sonar.api.utils.DateUtils.parseDateTime;
-import static org.sonar.db.component.BranchType.BRANCH;
import static org.sonar.db.component.ComponentDbTester.toProjectDto;
import static org.sonar.db.component.ComponentTesting.newBranchDto;
import static org.sonar.db.component.ComponentTesting.newFileDto;
ComponentDto project = db.components().insertPrivateProject();
userSession.addProjectPermission(UserRole.USER, project);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey("my_branch"));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
SnapshotDto analysis = db.components().insertSnapshot(newAnalysis(branch));
EventDto event = db.events().insertEvent(newEvent(analysis).setCategory(QUALITY_GATE.getLabel()));
dao.insert(dbTester.getSession(), project1.getUuid(), stringToCompressedInputStream("test data1"));
dao.insert(dbTester.getSession(), branch.getUuid(), stringToCompressedInputStream("test data2"));
- userSession.logIn().addProjectPermission(SCAN, project1);
+ userSession.logIn().addProjectPermission(SCAN, project1)
+ .registerBranches(branch);
TestResponse response = wsTester.newRequest()
.setParam("project", project1.getKey())
.setParam("branch", branch.getKey())
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
-import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.property.PropertyDbTester;
definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
propertyDb.insertProperties(null, branch.name(), null, null, newComponentPropertyDto(branch).setKey("foo").setValue("value"));
userSession.logIn().addProjectPermission(ADMIN, project);
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ws.newRequest()
.setMediaType(MediaTypes.PROTOBUF)
private void assertUserPropertyExists(String key, UserDto user) {
assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
- .setKey(key)
- .setUserUuid(user.getUuid())
- .build(),
+ .setKey(key)
+ .setUserUuid(user.getUuid())
+ .build(),
dbSession)).isNotEmpty();
}
public void show_source() {
ComponentDto privateProject = db.components().insertPrivateProject();
ComponentDto file = insertFileWithData(FileSourceTesting.newFakeData(3).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
TestResponse response = tester.newRequest()
.setParam("uuid", file.uuid())
public void fail_to_show_source_if_no_source_found() {
ComponentDto privateProject = db.components().insertPrivateProject();
ComponentDto file = insertFile(privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
TestRequest request = tester.newRequest()
.setParam("uuid", file.uuid());
public void show_paginated_lines() {
ComponentDto privateProject = db.components().insertPrivateProject();
ComponentDto file = insertFileWithData(FileSourceTesting.newFakeData(3).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
tester
.newRequest()
@Test
public void branch() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
+
String branchName = randomAlphanumeric(248);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
db.commit();
userSession.logIn("login")
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectBranchMapping(project.uuid(), branch)
.addProjectPermission(UserRole.CODEVIEWER, project, file);
tester.newRequest()
@Test
public void pull_request() {
ComponentDto project = db.components().insertPrivateProject();
- userSession.addProjectPermission(UserRole.USER, project);
String pullRequestKey = randomAlphanumeric(100);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(PULL_REQUEST).setKey(pullRequestKey));
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
db.commit();
userSession.logIn("login")
- .addProjectPermission(UserRole.CODEVIEWER, project, file);
+ .addProjectPermission(UserRole.USER, project)
+ .addProjectPermission(UserRole.CODEVIEWER, project, file)
+ .registerComponents(branch);
tester.newRequest()
.setParam("key", file.getKey())
ComponentDto privateProject = db.components().insertPrivateProject();
ComponentDto file = newFileDto(privateProject).setKey("file-key").setEnabled(false);
db.components().insertComponents(file);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
assertThatThrownBy(() -> tester.newRequest().setParam("key", "file-key").execute())
.isInstanceOf(NotFoundException.class)
public void display_deprecated_fields() {
ComponentDto privateProject = db.components().insertPrivateProject();
ComponentDto file = insertFileWithData(FileSourceTesting.newFakeData(1).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
tester.newRequest()
.setParam("uuid", file.uuid())
ComponentDto project = db.components().insertPrivateProject();
insertPeriod(project, 2000L);
ComponentDto file = insertFileWithData(dataBuilder.build(), project);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, project);
tester.newRequest()
.setParam("uuid", file.uuid())
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
tester.newRequest()
.setParam("uuid", file.uuid())
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
tester.newRequest()
.setParam("uuid", file.uuid())
.setDeprecatedItLineHits(1)
.setDeprecatedItConditions(2)
.setDeprecatedItCoveredConditions(3)).build(), privateProject);
- setUserWithValidPermission(file);
+ setUserWithValidPermission(file, privateProject);
tester.newRequest()
.setParam("uuid", file.uuid())
.setParam("key", file.getKey())
.setParam("branch", "another_branch")
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining(String.format("Component '%s' on branch '%s' not found", file.getKey(), "another_branch"));
}
@Test
.setParam("uuid", file.uuid())
.setParam("branch", "another_branch")
.execute())
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessageContaining("Parameter 'uuid' cannot be used at the same time as 'branch' or 'pullRequest'");
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessageContaining("Parameter 'uuid' cannot be used at the same time as 'branch' or 'pullRequest'");
}
@Test
assertThatThrownBy(() -> tester.newRequest()
.setParam("uuid", branch.uuid())
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(format("Component id '%s' not found", branch.uuid()));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining(format("Component id '%s' not found", branch.uuid()));
}
@Test
return file;
}
- private void setUserWithValidPermission(ComponentDto file) {
- ComponentDto privateProject = db.components().insertPrivateProject();
+ private void setUserWithValidPermission(ComponentDto file, ComponentDto privateProject) {
userSession.logIn("login")
.addProjectPermission(UserRole.CODEVIEWER, privateProject, file);
}
userSession.addProjectPermission(UserRole.CODEVIEWER, project);
String branchName = randomAlphanumeric(248);
ComponentDto branch = db.components().insertProjectBranch(project, b -> b.setKey(branchName));
+ userSession.addProjectBranchMapping(project.uuid(), branch);
ComponentDto file = db.components().insertComponent(newFileDto(branch, project.uuid()));
db.fileSources().insertFileSource(file, s -> s.setSourceData(
Data.newBuilder()
assertThatThrownBy(() -> ws.newRequest()
.setParam("key", "unknown")
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining("Component key 'unknown' not found");
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining("Component key 'unknown' not found");
}
@Test
.setParam("key", file.getKey())
.setParam("branch", "unknown")
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(format("Component '%s' on branch 'unknown' not found", file.getKey()));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining(format("Component '%s' on branch 'unknown' not found", file.getKey()));
}
@Test
assertThatThrownBy(() -> ws.newRequest()
.setParam("key", file.getKey())
.execute())
- .isInstanceOf(NotFoundException.class)
- .hasMessageContaining(format("Component key '%s' not found", file.getKey()));
+ .isInstanceOf(NotFoundException.class)
+ .hasMessageContaining(format("Component key '%s' not found", file.getKey()));
}
@Test
assertThatThrownBy(() -> ws.newRequest()
.setParam("key", file.getKey())
.execute())
- .isInstanceOf(ForbiddenException.class);
+ .isInstanceOf(ForbiddenException.class);
}
}
public void return_favourite_for_branch() {
ComponentDto project = insertProject();
String branchName = "feature1";
- componentDbTester.insertProjectBranch(project, b -> b.setKey(branchName).setUuid("xyz"));
+ ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey(branchName).setUuid("xyz"));
UserDto user = db.users().insertUser("obiwan");
propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setComponentUuid(project.uuid()).setUserUuid(user.getUuid()),
project.getKey(), project.name(), project.qualifier(), user.getLogin());
- userSession.logIn(user).addProjectPermission(UserRole.USER, project);
+ userSession.logIn(user).addProjectPermission(UserRole.USER, project)
+ .addProjectBranchMapping(project.uuid(), branch);
init();
String json = ws.newRequest()
propertyDbTester.insertProperty(new PropertyDto().setKey("favourite").setComponentUuid(subportfolio.uuid()).setUserUuid(user.getUuid()),
subportfolio.getKey(), subportfolio.name(), subportfolio.qualifier(), user.getLogin());
- userSession.logIn(user).addProjectPermission(UserRole.USER, subportfolio);
+ userSession.logIn(user).addProjectPermission(UserRole.USER, portfolio)
+ .addProjectPermission(UserRole.USER, subportfolio);
init();
String json = ws.newRequest()
String branchName = "feature1";
ComponentDto branch = componentDbTester.insertProjectBranch(project, b -> b.setKey(branchName));
userSession.addProjectPermission(UserRole.USER, project);
+ userSession.addProjectBranchMapping(project.uuid(), branch);
init();
ComponentDto dirDto = componentDbTester.insertComponent(newDirectory(branch, "src"));
ComponentDto fileDto = componentDbTester.insertComponent(newFileDto(project.uuid(), branch, dirDto)
BranchDto branch = db.components().insertProjectBranch(project, b -> b.setBranchType(BranchType.BRANCH));
QualityGateDto qualityGateDto = db.qualityGates().insertQualityGate(qg -> qg.setName("Sonar way"));
db.qualityGates().associateProjectToQualityGate(project, qualityGateDto);
- userSession.addProjectPermission(UserRole.USER, project);
+ userSession.addProjectPermission(UserRole.USER, project)
+ .addProjectBranchMapping(project.getUuid(), db.components().getComponentDto(branch));
init();
String json = ws.newRequest()