Search in sources :

Example 96 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class SearchEventsAction method computeNewIssuesEvents.

private Stream<Event> computeNewIssuesEvents(Map<String, ComponentDto> projectsByUuid, Map<String, BranchDto> branchesByUuids, List<UuidFromPair> uuidFromPairs) {
    Map<String, Long> fromsByProjectUuid = uuidFromPairs.stream().collect(Collectors.toMap(UuidFromPair::getComponentUuid, UuidFromPair::getFrom));
    List<ProjectStatistics> projectStatistics = issueIndex.searchProjectStatistics(componentUuids(uuidFromPairs), fromDates(uuidFromPairs), userSession.getUuid());
    return projectStatistics.stream().map(e -> {
        BranchDto branch = branchesByUuids.get(e.getProjectUuid());
        ComponentDto project = projectsByUuid.get(branch.getProjectUuid());
        long issueCount = e.getIssueCount();
        long lastIssueDate = e.getLastIssueDate();
        String branchType = branch.getBranchType().equals(PULL_REQUEST) ? "pull request" : "branch";
        return Event.newBuilder().setCategory("NEW_ISSUES").setMessage(format("You have %s new %s on project '%s'", issueCount, issueCount == 1 ? "issue" : "issues", project.name()) + (branch.isMain() ? "" : format(" on %s '%s'", branchType, branch.getKey()))).setLink(computeIssuesSearchLink(project, branch, fromsByProjectUuid.get(project.uuid()), userSession.getLogin())).setProject(project.getKey()).setDate(formatDateTime(lastIssueDate)).build();
    });
}
Also used : BranchDto(org.sonar.db.component.BranchDto) ComponentDto(org.sonar.db.component.ComponentDto) ProjectStatistics(org.sonar.server.issue.index.ProjectStatistics)

Example 97 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class SearchEventsAction method computeEvents.

private Stream<Event> computeEvents(Request request) {
    List<String> projectKeys = request.mandatoryParamAsStrings(PARAM_PROJECTS);
    List<Long> fromDates = mandatoryParamAsDateTimes(request, PARAM_FROM);
    if (projectKeys.isEmpty()) {
        return Stream.empty();
    }
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<ComponentDto> authorizedProjects = searchProjects(dbSession, projectKeys);
        Map<String, ComponentDto> componentsByUuid = authorizedProjects.stream().collect(uniqueIndex(ComponentDto::uuid));
        List<UuidFromPair> uuidFromPairs = componentUuidFromPairs(fromDates, projectKeys, authorizedProjects);
        List<SnapshotDto> analyses = dbClient.snapshotDao().selectFinishedByComponentUuidsAndFromDates(dbSession, componentUuids(uuidFromPairs), fromDates(uuidFromPairs));
        if (analyses.isEmpty()) {
            return Stream.empty();
        }
        List<String> projectUuids = analyses.stream().map(SnapshotDto::getComponentUuid).collect(toList());
        Map<String, BranchDto> branchesByUuids = dbClient.branchDao().selectByUuids(dbSession, projectUuids).stream().collect(uniqueIndex(BranchDto::getUuid));
        return Stream.concat(computeQualityGateChangeEvents(dbSession, componentsByUuid, branchesByUuids, analyses), computeNewIssuesEvents(componentsByUuid, branchesByUuids, uuidFromPairs));
    }
}
Also used : BranchDto(org.sonar.db.component.BranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) DbSession(org.sonar.db.DbSession)

Example 98 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ProjectStatusAction method getProjectThenSnapshot.

private ProjectAndSnapshot getProjectThenSnapshot(DbSession dbSession, @Nullable String projectUuid, @Nullable String projectKey, @Nullable String branchKey, @Nullable String pullRequestId) {
    ProjectDto projectDto;
    BranchDto branchDto;
    if (projectUuid != null) {
        projectDto = componentFinder.getProjectByUuid(dbSession, projectUuid);
        branchDto = componentFinder.getMainBranch(dbSession, projectDto);
    } else {
        projectDto = componentFinder.getProjectByKey(dbSession, projectKey);
        branchDto = componentFinder.getBranchOrPullRequest(dbSession, projectDto, branchKey, pullRequestId);
    }
    Optional<SnapshotDto> snapshot = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, branchDto.getUuid());
    return new ProjectAndSnapshot(projectDto, branchDto, snapshot.orElse(null));
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) SnapshotDto(org.sonar.db.component.SnapshotDto)

Example 99 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class BranchSupportTest method createBranchComponent_delegates_to_delegate.

@Test
public void createBranchComponent_delegates_to_delegate() {
    DbSession dbSession = mock(DbSession.class);
    ComponentKey componentKey = mock(ComponentKey.class);
    ComponentDto mainComponentDto = new ComponentDto();
    ComponentDto expected = new ComponentDto();
    BranchDto mainComponentBranchDto = new BranchDto();
    when(branchSupportDelegate.createBranchComponent(dbSession, componentKey, mainComponentDto, mainComponentBranchDto)).thenReturn(expected);
    ComponentDto dto = underTestWithBranch.createBranchComponent(dbSession, componentKey, mainComponentDto, mainComponentBranchDto);
    assertThat(dto).isSameAs(expected);
}
Also used : DbSession(org.sonar.db.DbSession) BranchDto(org.sonar.db.component.BranchDto) ComponentKey(org.sonar.server.ce.queue.BranchSupport.ComponentKey) ComponentDto(org.sonar.db.component.ComponentDto) Test(org.junit.Test)

Example 100 with BranchDto

use of org.sonar.db.component.BranchDto in project sonarqube by SonarSource.

the class ImportGithubProjectActionTest method import_project.

@Test
public void import_project() {
    AlmSettingDto githubAlmSetting = setupAlm();
    db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSetting.getUuid()).setUserUuid(userSession.getUuid()));
    GithubApplicationClient.Repository repository = new GithubApplicationClient.Repository(1L, "Hello-World", false, "octocat/Hello-World", "https://github.sonarsource.com/api/v3/repos/octocat/Hello-World", "default-branch");
    when(appClient.getRepository(any(), any(), any(), any())).thenReturn(Optional.of(repository));
    Projects.CreateWsResponse response = ws.newRequest().setParam(PARAM_ALM_SETTING, githubAlmSetting.getKey()).setParam(PARAM_ORGANIZATION, "octocat").setParam(PARAM_REPOSITORY_KEY, "octocat/Hello-World").executeProtobuf(Projects.CreateWsResponse.class);
    Projects.CreateWsResponse.Project result = response.getProject();
    assertThat(result.getKey()).isEqualTo(repository.getFullName().replace("/", "_"));
    assertThat(result.getName()).isEqualTo(repository.getName());
    Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
    assertThat(projectDto).isPresent();
    assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
    Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()).stream().filter(BranchDto::isMain).findAny();
    assertThat(mainBranch).isPresent();
    assertThat(mainBranch.get().getKey()).isEqualTo("default-branch");
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) Projects(org.sonarqube.ws.Projects) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Test(org.junit.Test)

Aggregations

BranchDto (org.sonar.db.component.BranchDto)111 Test (org.junit.Test)62 ComponentDto (org.sonar.db.component.ComponentDto)52 ProjectDto (org.sonar.db.project.ProjectDto)42 DbSession (org.sonar.db.DbSession)31 SnapshotDto (org.sonar.db.component.SnapshotDto)22 List (java.util.List)15 ComponentTesting.newBranchDto (org.sonar.db.component.ComponentTesting.newBranchDto)13 DbClient (org.sonar.db.DbClient)12 MetricDto (org.sonar.db.metric.MetricDto)12 Map (java.util.Map)9 Optional (java.util.Optional)9 Nullable (javax.annotation.Nullable)9 WebService (org.sonar.api.server.ws.WebService)9 System2 (org.sonar.api.utils.System2)9 NotFoundException (org.sonar.server.exceptions.NotFoundException)9 Collection (java.util.Collection)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 Rule (org.junit.Rule)8 Request (org.sonar.api.server.ws.Request)8