Search in sources :

Example 16 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class ImportBitbucketServerProjectAction 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)) {
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException(String.format("personal access token for '%s' is missing", almSettingDto.getKey())));
        String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY);
        String repoSlug = request.mandatoryParam(PARAM_REPO_SLUG);
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        Repository repo = bitbucketServerRestClient.getRepo(url, pat, projectKey, repoSlug);
        String defaultBranchName = getDefaultBranchName(pat, projectKey, repoSlug, url);
        ComponentDto componentDto = createProject(dbSession, repo, defaultBranchName);
        populatePRSetting(dbSession, repo, componentDto, almSettingDto);
        componentUpdater.commitAndIndex(dbSession, componentDto);
        return toCreateResponse(componentDto);
    }
}
Also used : DbSession(org.sonar.db.DbSession) Repository(org.sonar.alm.client.bitbucketserver.Repository) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) ComponentDto(org.sonar.db.component.ComponentDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 17 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class ListBitbucketServerProjectsAction method doHandle.

private ListBitbucketserverProjectsWsResponse doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
        AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "URL cannot be null");
        ProjectList projectList = bitbucketServerRestClient.getProjects(url, pat);
        List<AlmProject> values = projectList.getValues().stream().map(ListBitbucketServerProjectsAction::toAlmProject).collect(Collectors.toList());
        ListBitbucketserverProjectsWsResponse.Builder builder = ListBitbucketserverProjectsWsResponse.newBuilder().addAllProjects(values);
        return builder.build();
    }
}
Also used : DbSession(org.sonar.db.DbSession) ProjectList(org.sonar.alm.client.bitbucketserver.ProjectList) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) AlmProject(org.sonarqube.ws.AlmIntegrations.AlmProject) ListBitbucketserverProjectsWsResponse(org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Example 18 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto 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 19 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto 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 20 with AlmPatDto

use of org.sonar.db.alm.pat.AlmPatDto in project sonarqube by SonarSource.

the class ImportGitLabProjectAction method doHandle.

private CreateWsResponse doHandle(Request request) {
    importHelper.checkProvisionProjectPermission();
    AlmSettingDto almSettingDto = importHelper.getAlmSetting(request);
    String userUuid = importHelper.getUserUuid();
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException(String.format("personal access token for '%s' is missing", almSettingDto.getKey())));
        long gitlabProjectId = request.mandatoryParamAsLong(PARAM_GITLAB_PROJECT_ID);
        String gitlabUrl = requireNonNull(almSettingDto.getUrl(), "ALM gitlabUrl cannot be null");
        Project gitlabProject = gitlabHttpClient.getProject(gitlabUrl, pat, gitlabProjectId);
        Optional<String> almMainBranchName = getAlmDefaultBranch(pat, gitlabProjectId, gitlabUrl);
        ComponentDto componentDto = createProject(dbSession, gitlabProject, almMainBranchName.orElse(null));
        populateMRSetting(dbSession, gitlabProjectId, componentDto, almSettingDto);
        componentUpdater.commitAndIndex(dbSession, componentDto);
        return ImportHelper.toCreateResponse(componentDto);
    }
}
Also used : DbSession(org.sonar.db.DbSession) Project(org.sonar.alm.client.gitlab.Project) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) ComponentDto(org.sonar.db.component.ComponentDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Aggregations

AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)34 NotFoundException (org.sonar.server.exceptions.NotFoundException)24 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)23 Test (org.junit.Test)19 UserDto (org.sonar.db.user.UserDto)19 DbSession (org.sonar.db.DbSession)14 TestRequest (org.sonar.server.ws.TestRequest)11 AlmPatsTesting.newAlmPatDto (org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto)10 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)7 Collectors (java.util.stream.Collectors)5 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)5 UserAccessToken (org.sonar.alm.client.github.security.UserAccessToken)5 List (java.util.List)4 Map (java.util.Map)4 Objects.requireNonNull (java.util.Objects.requireNonNull)4 Optional (java.util.Optional)4 Set (java.util.Set)4 BinaryOperator (java.util.function.BinaryOperator)4 Collectors.toList (java.util.stream.Collectors.toList)4 Collectors.toSet (java.util.stream.Collectors.toSet)4