@Test
public void return_existing_uuids() {
setAnalysisMetadataHolder();
- ComponentDto project = dbTester.components().insertPrivateProject("ABCD", p -> p.setKey(REPORT_PROJECT_KEY)).getMainBranchComponent();
- ComponentDto directory = newDirectory(project, "CDEF", REPORT_DIR_PATH_1);
+ ComponentDto mainBranch = dbTester.components().insertPrivateProject("ABCD", p -> p.setKey(REPORT_PROJECT_KEY)).getMainBranchComponent();
+ ComponentDto directory = newDirectory(mainBranch, "CDEF", REPORT_DIR_PATH_1);
insertComponent(directory.setKey(REPORT_PROJECT_KEY + ":" + REPORT_DIR_PATH_1));
- insertComponent(newFileDto(project, directory, "DEFG")
+ insertComponent(newFileDto(mainBranch, directory, "DEFG")
.setKey(REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1)
.setPath(REPORT_FILE_PATH_1));
underTest.execute(new TestComputationStepContext());
- verifyComponentByRef(ROOT_REF, REPORT_PROJECT_KEY, analysisMetadataHolder.getProject().getName(), "ABCD");
+ verifyComponentByRef(ROOT_REF, REPORT_PROJECT_KEY, analysisMetadataHolder.getProject().getName(), mainBranch.uuid());
verifyComponentByKey(REPORT_PROJECT_KEY + ":" + REPORT_DIR_PATH_1, REPORT_DIR_PATH_1, "CDEF");
verifyComponentByRef(FILE_1_REF, REPORT_PROJECT_KEY + ":" + REPORT_FILE_PATH_1, REPORT_FILE_NAME_1, "DEFG");
}
@Override
public void execute(ComputationStep.Context context) {
- executeForProject(treeRootHolder.getRoot());
+ executeForBranch(treeRootHolder.getRoot());
}
- private void executeForProject(Component projectComponent) {
- Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
+ private void executeForBranch(Component branchComponent) {
+ Optional<Measure> baseMeasure = measureRepository.getBaseMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
if (!baseMeasure.isPresent()) {
// first analysis -> do not generate events
return;
}
// Load profiles used in current analysis for which at least one file of the corresponding language exists
- Optional<Measure> rawMeasure = measureRepository.getRawMeasure(projectComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
+ Optional<Measure> rawMeasure = measureRepository.getRawMeasure(branchComponent, metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY));
if (!rawMeasure.isPresent()) {
// No qualify profile computed on the project
return;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
import static java.util.Objects.requireNonNull;
/**
* Build a {@link ProjectDescriptor} without checking qualifier of ComponentDto.
*/
- public static ProjectDescriptor of(ComponentDto project) {
- return new ProjectDescriptor(project.uuid(), project.getKey(), project.name());
+ public static ProjectDescriptor of(ProjectDto project) {
+ return new ProjectDescriptor(project.getUuid(), project.getKey(), project.getName());
}
public final String getUuid() {
/**
* Returned finished analysis from a list of projects and dates.
* "Finished" analysis means that the status in the CE_ACTIVITY table is SUCCESS => the goal is to be sure that the CE task is completely finished.
- *
* Note that branches analysis of projects are also returned.
*/
public List<SnapshotDto> selectFinishedByProjectUuidsAndFromDates(DbSession dbSession, List<String> projectUuids, List<Long> fromDates) {
checkArgument(projectUuids.size() == fromDates.size(), "The number of components (%s) and from dates (%s) must be the same.",
String.valueOf(projectUuids.size()),
String.valueOf(fromDates.size()));
- List<ComponentUuidFromDatePair> componentUuidFromDatePairs = IntStream.range(0, projectUuids.size())
- .mapToObj(i -> new ComponentUuidFromDatePair(projectUuids.get(i), fromDates.get(i)))
+ List<ProjectUuidFromDatePair> projectUuidFromDatePairs = IntStream.range(0, projectUuids.size())
+ .mapToObj(i -> new ProjectUuidFromDatePair(projectUuids.get(i), fromDates.get(i)))
.collect(MoreCollectors.toList(projectUuids.size()));
- return executeLargeInputs(componentUuidFromDatePairs, partition -> mapper(dbSession).selectFinishedByProjectUuidsAndFromDates(partition), i -> i / 2);
+ return executeLargeInputs(projectUuidFromDatePairs, partition -> mapper(dbSession).selectFinishedByProjectUuidsAndFromDates(partition), i -> i / 2);
}
public void switchIsLastFlagAndSetProcessedStatus(DbSession dbSession, String componentUuid, String analysisUuid) {
return session.getMapper(SnapshotMapper.class);
}
- static class ComponentUuidFromDatePair implements Comparable<ComponentUuidFromDatePair> {
- private final String componentUuid;
+ static class ProjectUuidFromDatePair implements Comparable<ProjectUuidFromDatePair> {
+ private final String projectUuid;
private final long from;
- ComponentUuidFromDatePair(String componentUuid, long from) {
- this.componentUuid = requireNonNull(componentUuid);
+ ProjectUuidFromDatePair(String projectUuid, long from) {
+ this.projectUuid = requireNonNull(projectUuid);
this.from = from;
}
@Override
- public int compareTo(ComponentUuidFromDatePair other) {
+ public int compareTo(ProjectUuidFromDatePair other) {
if (this == other) {
return 0;
}
- int c = componentUuid.compareTo(other.componentUuid);
+ int c = projectUuid.compareTo(other.projectUuid);
if (c == 0) {
c = Long.compare(from, other.from);
}
return false;
}
- ComponentUuidFromDatePair other = (ComponentUuidFromDatePair) o;
- return componentUuid.equals(other.componentUuid)
- && from == other.from;
+ ProjectUuidFromDatePair other = (ProjectUuidFromDatePair) o;
+ return projectUuid.equals(other.projectUuid) && from == other.from;
}
@Override
public int hashCode() {
- return Objects.hash(componentUuid, from);
+ return Objects.hash(projectUuid, from);
}
}
}
import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.RowBounds;
-import org.sonar.db.component.SnapshotDao.ComponentUuidFromDatePair;
+import org.sonar.db.component.SnapshotDao.ProjectUuidFromDatePair;
public interface SnapshotMapper {
void update(SnapshotDto analysis);
- List<SnapshotDto> selectFinishedByProjectUuidsAndFromDates(@Param("projectUuidFromDatePairs") List<ComponentUuidFromDatePair> pairs);
+ List<SnapshotDto> selectFinishedByProjectUuidsAndFromDates(@Param("projectUuidFromDatePairs") List<ProjectUuidFromDatePair> pairs);
@CheckForNull
Long selectLastAnalysisDateByProject(String projectUuid);
inner join project_branches pb on pb.uuid=p.uuid
where
<foreach collection="projectUuidFromDatePairs" open="(" close=")" item="projectUuidFromDatePair" separator=" or ">
- (pb.project_uuid=#{projectUuidFromDatePair.componentUuid, jdbcType=VARCHAR} and s.created_at >= #{projectUuidFromDatePair.from, jdbcType=BIGINT})
+ (pb.project_uuid=#{projectUuidFromDatePair.projectUuid, jdbcType=VARCHAR} and s.created_at >= #{projectUuidFromDatePair.from, jdbcType=BIGINT})
</foreach>
and s.status = 'P'
order by
}
public final ProjectData insertPrivateProject(String uuid, Consumer<ComponentDto> dtoPopulator) {
- return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true, defaults(), dtoPopulator);
+ if (useDifferentUuids) {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(), true, defaults(), dtoPopulator, p -> p.setUuid(uuid));
+ } else {
+ return insertComponentAndBranchAndProject(ComponentTesting.newPrivateProjectDto(uuid), true, defaults(), dtoPopulator);
+ }
}
public final ProjectData insertPrivateProjectWithCustomBranch(String branchKey) {
public class ComponentTesting {
- public static ComponentDto newFileDto(ComponentDto project) {
- return newFileDto(project, (ComponentDto) null);
+ public static ComponentDto newFileDto(ComponentDto branch) {
+ return newFileDto(branch, (ComponentDto) null);
}
- public static ComponentDto newFileDto(ComponentDto project, @Nullable ComponentDto directory) {
- return newFileDto(project, directory, Uuids.createFast());
+ public static ComponentDto newFileDto(ComponentDto branch, @Nullable ComponentDto directory) {
+ return newFileDto(branch, directory, Uuids.createFast());
}
public static ComponentDto newFileDto(ComponentDto branch, String mainBranchUuid) {
.setLanguage("xoo");
}
- public static ComponentDto newDirectory(ComponentDto project, String path) {
- return newDirectory(project, Uuids.createFast(), path);
+ public static ComponentDto newDirectory(ComponentDto branch, String path) {
+ return newDirectory(branch, Uuids.createFast(), path);
}
- public static ComponentDto newDirectoryOnBranch(ComponentDto project, String path, String mainBranchUuid) {
- return newDirectory(project, Uuids.createFast(), path, mainBranchUuid);
+ public static ComponentDto newDirectoryOnBranch(ComponentDto branch, String path, String mainBranchUuid) {
+ return newDirectory(branch, Uuids.createFast(), path, mainBranchUuid);
}
- private static ComponentDto newDirectory(ComponentDto project, String uuid, String path, String mainBranchUuid) {
- String key = !path.equals("/") ? project.getKey() + ":" + path : project.getKey() + ":/";
- return newChildComponent(uuid, project, project)
+ private static ComponentDto newDirectory(ComponentDto branch, String uuid, String path, String mainBranchUuid) {
+ String key = !path.equals("/") ? branch.getKey() + ":" + path : branch.getKey() + ":/";
+ return newChildComponent(uuid, branch, branch)
.setKey(key)
.setName(path)
.setLongName(path)
- .setBranchUuid(project.branchUuid())
+ .setBranchUuid(branch.branchUuid())
.setPath(path)
.setScope(Scopes.DIRECTORY)
.setQualifier(Qualifiers.DIRECTORY);
}
- public static ComponentDto newDirectory(ComponentDto project, String uuid, String path) {
- return newDirectory(project, uuid, path, null);
+ public static ComponentDto newDirectory(ComponentDto branch, String uuid, String path) {
+ return newDirectory(branch, uuid, path, null);
}
public static ComponentDto newSubPortfolio(ComponentDto portfolioOrSubPortfolio, String uuid, String key) {
* @throws AssertionError if rule is a Security Hotspot
*/
@SafeVarargs
- public final IssueDto insertIssue(RuleDto rule, ComponentDto project, ComponentDto file, Consumer<IssueDto>... populators) {
+ public final IssueDto insertIssue(RuleDto rule, ComponentDto branch, ComponentDto file, Consumer<IssueDto>... populators) {
assertThat(rule.getType())
.describedAs("rule must not be a Security Hotspot type")
.isNotEqualTo(SECURITY_HOTSPOT.getDbConstant());
- IssueDto issue = newIssue(rule, project, file)
+ IssueDto issue = newIssue(rule, branch, file)
.setType(RULE_TYPES_EXCEPT_HOTSPOTS[new Random().nextInt(RULE_TYPES_EXCEPT_HOTSPOTS.length)]);
stream(populators).forEach(p -> p.accept(issue));
return insertIssue(issue);
import org.sonar.api.web.UserRole;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.project.ProjectDto;
import org.sonar.db.user.TokenType;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.tester.UserSessionRule;
@Test
public void should_generate_token() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectDto project = db.components().insertPrivateProject().getProjectDto();
userSession.logIn().addProjectPermission(UserRole.USER, project);
when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token");
@Test
public void handle_whenApplicationKeyPassed_shouldReturnToken() {
- ComponentDto application = db.components().insertPrivateApplication().getMainBranchComponent();
+ ProjectDto application = db.components().insertPrivateApplication().getProjectDto();
userSession.logIn().addProjectPermission(UserRole.USER, application);
when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token");
response.assertJson("{\"token\":\"generated_token\"}");
}
-
@Test
public void should_reuse_generated_token() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectDto project = db.components().insertPrivateProject().getProjectDto();
userSession.logIn().addProjectPermission(UserRole.USER, project);
when(tokenGenerator.generate(TokenType.PROJECT_BADGE_TOKEN)).thenReturn("generated_token");
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.ce.CeTaskTypes;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
+import org.sonar.db.project.ProjectDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.platform.NodeInformation;
import org.sonar.server.tester.UserSessionRule;
@Test
public void cancel_pending_task_when_project_administer() {
- ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
- userSession.addProjectPermission(UserRole.ADMIN, project);
- CeQueueDto queue = createTaskSubmit(project);
+ ProjectData project = db.components().insertPrivateProject();
+ ProjectDto projectDto = project.getProjectDto();
+ ComponentDto mainBranchComponent = project.getMainBranchComponent();
+ userSession.addProjectPermission(UserRole.ADMIN, projectDto);
+ userSession.addProjectBranchMapping(projectDto.getUuid(), mainBranchComponent);
+ CeQueueDto queue = createTaskSubmit(mainBranchComponent);
tester.newRequest()
.setParam("id", queue.getUuid())
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
+import org.sonar.db.component.BranchDto;
import org.sonar.db.component.ComponentDbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ProjectData;
import org.sonar.db.component.ResourceTypesRule;
import org.sonar.db.issue.IssueDbTester;
import org.sonar.db.issue.IssueDto;
+import org.sonar.db.project.ProjectDto;
import org.sonar.db.protobuf.DbCommons;
import org.sonar.db.protobuf.DbIssues;
import org.sonar.db.rule.RuleDto;
pullActionProtobufObjectGenerator);
private final WsActionTester tester = new WsActionTester(underTest);
+ private ProjectDto project;
private ComponentDto correctProject, incorrectProject;
private ComponentDto correctFile, incorrectFile;
@Before
public void before() {
when(system2.now()).thenReturn(NOW);
- correctProject = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectData projectData = db.components().insertPrivateProject();
+ correctProject = projectData.getMainBranchComponent();
+ project = projectData.getProjectDto();
+
correctFile = db.components().insertComponent(newFileDto(correctProject));
- incorrectProject = db.components().insertPrivateProject().getMainBranchComponent();
+ ProjectData incorrectProjectData = db.components().insertPrivateProject();
+ incorrectProject = incorrectProjectData.getMainBranchComponent();
incorrectFile = db.components().insertComponent(newFileDto(incorrectProject));
}
@Test
public void wsExecution_whenHotspotOnAnotherBranchThanMain_shouldReturnOneIssue() throws IOException {
- ComponentDto developBranch = componentDbTester.insertPrivateProjectWithCustomBranch("develop").getMainBranchComponent();
+ ProjectData projectData = componentDbTester.insertPrivateProjectWithCustomBranch("develop");
+ ProjectDto project = projectData.getProjectDto();
+ ComponentDto developBranch = projectData.getMainBranchComponent();
ComponentDto developFile = db.components().insertComponent(newFileDto(developBranch));
List<String> hotspotKeys = generateHotspots(developBranch, developFile, 1);
- loginWithBrowsePermission(developBranch.uuid(), developFile.uuid());
+ loginWithBrowsePermission(project, developFile.uuid());
TestRequest request = tester.newRequest()
.setParam("projectKey", developBranch.getKey())
@Test
public void wsExecution_whenDifferentHotspotsInTheTable_shouldReturnOnlyThatBelongToSelectedProject() throws IOException {
- loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid());
+ loginWithBrowsePermission(project, correctFile.uuid());
List<String> correctIssueKeys = generateHotspots(correctProject, correctFile, 10);
List<String> incorrectIssueKeys = generateHotspots(incorrectProject, incorrectFile, 5);
@Test
public void wsExecution_whenNoIssuesBelongToTheProject_shouldReturnZeroIssues() throws IOException {
- loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid());
+ loginWithBrowsePermission(project, correctFile.uuid());
generateHotspots(incorrectProject, incorrectFile, 5);
TestRequest request = tester.newRequest()
@Test
public void wsExecution_whenLanguagesParam_shouldReturnOneIssue() throws IOException {
- loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid());
+ loginWithBrowsePermission(project, correctFile.uuid());
RuleDto javaRule = db.rules().insert(r -> r.setLanguage("java"));
IssueDto javaIssue = issueDbTester.insertHotspot(p -> p.setSeverity("MINOR")
@Test
public void wsExecution_whenChangedSinceParam_shouldReturnMatchingIssue() throws IOException {
- loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid());
+ loginWithBrowsePermission(project, correctFile.uuid());
RuleDto javaRule = db.rules().insert(r -> r.setLanguage("java"));
IssueDto issueBefore = issueDbTester.insertHotspot(p -> p.setSeverity("MINOR")
@Test
public void wsExecution_whenWrongLanguageSet_shouldReturnZeroIssues() throws IOException {
- loginWithBrowsePermission(correctProject.uuid(), correctFile.uuid());
+ loginWithBrowsePermission(project, correctFile.uuid());
RuleDto javascriptRule = db.rules().insert(r -> r.setLanguage("javascript"));
issueDbTester.insertHotspot(p -> p.setSeverity("MINOR")
}
private void loginWithBrowsePermission(IssueDto issueDto) {
- loginWithBrowsePermission(issueDto.getProjectUuid(), issueDto.getComponentUuid());
+ BranchDto branchDto = db.getDbClient().branchDao().selectByUuid(db.getSession(), issueDto.getProjectUuid()).get();
+ ProjectDto projectDto = db.getDbClient().projectDao().selectByUuid(db.getSession(), branchDto.getProjectUuid()).get();
+ loginWithBrowsePermission(projectDto, issueDto.getComponentUuid());
}
- private void loginWithBrowsePermission(String projectUuid, String componentUuid) {
+ private void loginWithBrowsePermission(ProjectDto project, String componentUuid) {
UserDto user = db.users().insertUser("john");
- userSession.logIn(user).addProjectPermission(USER, getComponentOrFail(projectUuid, "project not found"), getComponentOrFail(componentUuid, "component not found"));
+ userSession.logIn(user)
+ .addProjectPermission(USER, project)
+ .addProjectPermission(USER, getComponentOrFail(componentUuid, "component not found"));
}
private ComponentDto getComponentOrFail(String componentUuid, String failMessage) {
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.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.tester.UserSessionRule;
}
private void setNcloc(double ncloc) {
- ComponentDto project = db.components().insertPublicProject().getMainBranchComponent();
- db.getDbClient().projectDao().updateNcloc(db.getSession(), project.uuid(), (long) ncloc);
+ ProjectDto project = db.components().insertPublicProject().getProjectDto();
+ db.getDbClient().projectDao().updateNcloc(db.getSession(), project.getUuid(), (long) ncloc);
db.commit();
}
}
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex;
import static org.sonar.db.component.BranchType.BRANCH;
import static org.sonar.db.component.BranchType.PULL_REQUEST;
-import static org.sonar.server.developers.ws.UuidFromPairs.componentUuids;
import static org.sonar.server.developers.ws.UuidFromPairs.fromDates;
+import static org.sonar.server.developers.ws.UuidFromPairs.projectUuids;
import static org.sonar.server.exceptions.BadRequestException.checkRequest;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
List<ProjectDto> authorizedProjects = searchProjects(dbSession, projectKeys);
Map<String, ProjectDto> projectsByUuid = authorizedProjects.stream().collect(uniqueIndex(ProjectDto::getUuid));
List<UuidFromPair> uuidFromPairs = buildUuidFromPairs(fromDates, projectKeys, authorizedProjects);
- List<SnapshotDto> analyses = dbClient.snapshotDao().selectFinishedByProjectUuidsAndFromDates(dbSession, componentUuids(uuidFromPairs), fromDates(uuidFromPairs));
+ List<SnapshotDto> analyses = dbClient.snapshotDao().selectFinishedByProjectUuidsAndFromDates(dbSession, projectUuids(uuidFromPairs), fromDates(uuidFromPairs));
if (analyses.isEmpty()) {
return Stream.empty();
private Stream<Event> computeNewIssuesEvents(Map<String, ProjectDto> projectsByUuid, Map<String, BranchDto> branchesByUuids,
List<UuidFromPair> uuidFromPairs) {
Map<String, Long> fromsByProjectUuid = uuidFromPairs.stream().collect(Collectors.toMap(
- UuidFromPair::getComponentUuid,
+ UuidFromPair::getProjectUuid,
UuidFromPair::getFrom));
- List<ProjectStatistics> projectStatistics = issueIndex.searchProjectStatistics(componentUuids(uuidFromPairs), fromDates(uuidFromPairs), userSession.getUuid());
+ List<ProjectStatistics> projectStatistics = issueIndex.searchProjectStatistics(projectUuids(uuidFromPairs), fromDates(uuidFromPairs), userSession.getUuid());
return projectStatistics
.stream()
.map(e -> {
package org.sonar.server.developers.ws;
class UuidFromPair {
- private final String componentUuid;
+ private final String projectUuid;
private final long from;
- public UuidFromPair(String componentUuid, long from) {
- this.componentUuid = componentUuid;
+ public UuidFromPair(String projectUuid, long from) {
+ this.projectUuid = projectUuid;
this.from = from;
}
- public String getComponentUuid() {
- return componentUuid;
+ public String getProjectUuid() {
+ return projectUuid;
}
public long getFrom() {
// prevent instantiation
}
- public static List<String> componentUuids(List<UuidFromPair> pairs) {
- return pairs.stream().map(UuidFromPair::getComponentUuid).collect(MoreCollectors.toList(pairs.size()));
+ public static List<String> projectUuids(List<UuidFromPair> pairs) {
+ return pairs.stream().map(UuidFromPair::getProjectUuid).collect(MoreCollectors.toList(pairs.size()));
}
public static List<Long> fromDates(List<UuidFromPair> pairs) {