Search in sources :

Example 1 with Repository

use of org.sonar.alm.client.github.GithubApplicationClient.Repository in project sonarqube by SonarSource.

the class ImportGithubProjectAction method doHandle.

private Projects.CreateWsResponse doHandle(Request request) {
    importHelper.checkProvisionProjectPermission();
    AlmSettingDto almSettingDto = importHelper.getAlmSetting(request);
    String userUuid = importHelper.getUserUuid();
    try (DbSession dbSession = dbClient.openSession(false)) {
        AccessToken accessToken = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto).map(AlmPatDto::getPersonalAccessToken).map(UserAccessToken::new).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String githubOrganization = request.mandatoryParam(PARAM_ORGANIZATION);
        String repositoryKey = request.mandatoryParam(PARAM_REPOSITORY_KEY);
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        Repository repository = githubApplicationClient.getRepository(url, accessToken, githubOrganization, repositoryKey).orElseThrow(() -> new NotFoundException(String.format("GitHub repository '%s' not found", repositoryKey)));
        ComponentDto componentDto = createProject(dbSession, repository, repository.getDefaultBranch());
        populatePRSetting(dbSession, repository, componentDto, almSettingDto);
        componentUpdater.commitAndIndex(dbSession, componentDto);
        return toCreateResponse(componentDto);
    }
}
Also used : 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) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) ComponentDto(org.sonar.db.component.ComponentDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 2 with Repository

use of org.sonar.alm.client.github.GithubApplicationClient.Repository 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)

Aggregations

Repository (org.sonar.alm.client.github.GithubApplicationClient.Repository)2 AccessToken (org.sonar.alm.client.github.security.AccessToken)2 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)2 DbSession (org.sonar.db.DbSession)2 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)2 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)2 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)2 NotFoundException (org.sonar.server.exceptions.NotFoundException)2 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)1 ComponentDto (org.sonar.db.component.ComponentDto)1 ProjectDto (org.sonar.db.project.ProjectDto)1