Search in sources :

Example 6 with ProjectAlmSettingDto

use of org.sonar.db.alm.setting.ProjectAlmSettingDto in project sonarqube by SonarSource.

the class ListGithubRepositoriesAction method doHandle.

private AlmIntegrations.ListGithubRepositoriesWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("GitHub ALM Setting '%s' not found", almSettingKey)));
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
        String url = requireNonNull(almSettingDto.getUrl(), String.format("No URL set for GitHub ALM '%s'", almSettingKey));
        AccessToken accessToken = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto).map(AlmPatDto::getPersonalAccessToken).map(UserAccessToken::new).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        int pageIndex = request.hasParam(PAGE) ? request.mandatoryParamAsInt(PAGE) : 1;
        int pageSize = request.hasParam(PAGE_SIZE) ? request.mandatoryParamAsInt(PAGE_SIZE) : 100;
        GithubApplicationClient.Repositories repositories = githubApplicationClient.listRepositories(url, accessToken, request.mandatoryParam(PARAM_ORGANIZATION), request.param(TEXT_QUERY), pageIndex, pageSize);
        AlmIntegrations.ListGithubRepositoriesWsResponse.Builder response = AlmIntegrations.ListGithubRepositoriesWsResponse.newBuilder().setPaging(Common.Paging.newBuilder().setPageIndex(pageIndex).setPageSize(pageSize).setTotal(repositories.getTotal()).build());
        List<Repository> repositoryList = repositories.getRepositories();
        if (repositoryList != null) {
            Set<String> repo = repositoryList.stream().map(Repository::getFullName).collect(Collectors.toSet());
            List<ProjectAlmSettingDto> projectAlmSettingDtos = projectAlmSettingDao.selectByAlmSettingAndRepos(dbSession, almSettingDto, repo);
            Map<String, ProjectDto> projectsDtoByAlmRepo = getProjectDtoByAlmRepo(dbSession, projectAlmSettingDtos);
            for (Repository repository : repositoryList) {
                AlmIntegrations.GithubRepository.Builder builder = AlmIntegrations.GithubRepository.newBuilder().setId(repository.getId()).setKey(repository.getFullName()).setName(repository.getName()).setUrl(repository.getUrl());
                if (projectsDtoByAlmRepo.containsKey(repository.getFullName())) {
                    Optional.ofNullable(projectsDtoByAlmRepo.get(repository.getFullName())).ifPresent(p -> builder.setSqProjectKey(p.getKey()));
                }
                response.addRepositories(builder.build());
            }
        }
        return response.build();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbSession(org.sonar.db.DbSession) Repository(org.sonar.alm.client.github.GithubApplicationClient.Repository) UserAccessToken(org.sonar.alm.client.github.security.UserAccessToken) AccessToken(org.sonar.alm.client.github.security.AccessToken) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 7 with ProjectAlmSettingDto

use of org.sonar.db.alm.setting.ProjectAlmSettingDto in project sonarqube by SonarSource.

the class ImportAzureProjectAction method populatePRSetting.

private void populatePRSetting(DbSession dbSession, GsonAzureRepo repo, ComponentDto componentDto, AlmSettingDto almSettingDto) {
    ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto().setAlmSettingUuid(almSettingDto.getUuid()).setAlmRepo(repo.getName()).setAlmSlug(repo.getProject().getName()).setProjectUuid(componentDto.uuid()).setMonorepo(false);
    dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto, almSettingDto.getKey(), componentDto.name(), componentDto.getKey());
}
Also used : ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 8 with ProjectAlmSettingDto

use of org.sonar.db.alm.setting.ProjectAlmSettingDto in project sonarqube by SonarSource.

the class GetBindingAction method doHandle.

private GetBindingWsResponse doHandle(Request request) {
    String projectKey = request.mandatoryParam(PARAM_PROJECT);
    try (DbSession dbSession = dbClient.openSession(false)) {
        ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
        userSession.checkProjectPermission(ADMIN, project);
        ProjectAlmSettingDto projectAlmSetting = dbClient.projectAlmSettingDao().selectByProject(dbSession, project).orElseThrow(() -> new NotFoundException(format("Project '%s' is not bound to any ALM", project.getKey())));
        AlmSettingDto almSetting = dbClient.almSettingDao().selectByUuid(dbSession, projectAlmSetting.getAlmSettingUuid()).orElseThrow(() -> new IllegalStateException(format("ALM setting with uuid '%s' cannot be found", projectAlmSetting.getAlmSettingUuid())));
        GetBindingWsResponse.Builder builder = GetBindingWsResponse.newBuilder().setAlm(toAlmWs(almSetting.getAlm())).setKey(almSetting.getKey());
        ofNullable(projectAlmSetting.getAlmRepo()).ifPresent(builder::setRepository);
        ofNullable(almSetting.getUrl()).ifPresent(builder::setUrl);
        ofNullable(projectAlmSetting.getAlmSlug()).ifPresent(builder::setSlug);
        ofNullable(projectAlmSetting.getSummaryCommentEnabled()).ifPresent(builder::setSummaryCommentEnabled);
        ofNullable(projectAlmSetting.getMonorepo()).ifPresent(builder::setMonorepo);
        return builder.build();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) DbSession(org.sonar.db.DbSession) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) GetBindingWsResponse(org.sonarqube.ws.AlmSettings.GetBindingWsResponse) NotFoundException(org.sonar.server.exceptions.NotFoundException) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 9 with ProjectAlmSettingDto

use of org.sonar.db.alm.setting.ProjectAlmSettingDto in project sonarqube by SonarSource.

the class GetBindingActionTest method get_azure_project_binding.

@Test
public void get_azure_project_binding() {
    userSession.logIn(user).addProjectPermission(ADMIN, project);
    AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
    ProjectAlmSettingDto projectAlmSettingDto = db.almSettings().insertAzureMonoRepoProjectAlmSetting(almSetting, project);
    GetBindingWsResponse response = ws.newRequest().setParam("project", project.getKey()).executeProtobuf(GetBindingWsResponse.class);
    assertThat(response.getAlm()).isEqualTo(AlmSettings.Alm.azure);
    assertThat(response.getKey()).isEqualTo(almSetting.getKey());
    assertThat(response.getUrl()).isEqualTo(almSetting.getUrl());
    assertThat(response.getRepository()).isEqualTo(projectAlmSettingDto.getAlmRepo());
    assertThat(response.getSlug()).isEqualTo(projectAlmSettingDto.getAlmSlug());
    assertThat(response.hasSummaryCommentEnabled()).isFalse();
    assertThat(response.getMonorepo()).isTrue();
}
Also used : ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) GetBindingWsResponse(org.sonarqube.ws.AlmSettings.GetBindingWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Test(org.junit.Test)

Example 10 with ProjectAlmSettingDto

use of org.sonar.db.alm.setting.ProjectAlmSettingDto in project sonarqube by SonarSource.

the class GetBindingActionTest method get_github_project_binding.

@Test
public void get_github_project_binding() {
    userSession.logIn(user).addProjectPermission(ADMIN, project);
    AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
    ProjectAlmSettingDto githubProjectAlmSetting = db.almSettings().insertGitHubProjectAlmSetting(githubAlmSetting, project);
    GetBindingWsResponse response = ws.newRequest().setParam("project", project.getKey()).executeProtobuf(GetBindingWsResponse.class);
    assertThat(response.getAlm()).isEqualTo(AlmSettings.Alm.github);
    assertThat(response.getKey()).isEqualTo(githubAlmSetting.getKey());
    assertThat(response.getRepository()).isEqualTo(githubProjectAlmSetting.getAlmRepo());
    assertThat(response.getUrl()).isEqualTo(githubAlmSetting.getUrl());
    assertThat(response.getSummaryCommentEnabled()).isTrue();
}
Also used : ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) GetBindingWsResponse(org.sonarqube.ws.AlmSettings.GetBindingWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Test(org.junit.Test)

Aggregations

ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)11 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)7 Test (org.junit.Test)5 ProjectDto (org.sonar.db.project.ProjectDto)4 GetBindingWsResponse (org.sonarqube.ws.AlmSettings.GetBindingWsResponse)4 DbSession (org.sonar.db.DbSession)2 BranchDto (org.sonar.db.component.BranchDto)2 UserDto (org.sonar.db.user.UserDto)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 Projects (org.sonarqube.ws.Projects)2 GsonAzureRepo (org.sonar.alm.client.azure.GsonAzureRepo)1 Repository (org.sonar.alm.client.bitbucket.bitbucketcloud.Repository)1 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)1 Repository (org.sonar.alm.client.github.GithubApplicationClient.Repository)1 AccessToken (org.sonar.alm.client.github.security.AccessToken)1 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1