Search in sources :

Example 21 with AlmPatDto

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

the class ImportAzureProjectAction 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())));
        String projectName = request.mandatoryParam(PARAM_PROJECT_NAME);
        String repositoryName = request.mandatoryParam(PARAM_REPOSITORY_NAME);
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        GsonAzureRepo repo = azureDevOpsHttpClient.getRepo(url, pat, projectName, repositoryName);
        ComponentDto componentDto = createProject(dbSession, repo);
        populatePRSetting(dbSession, repo, componentDto, almSettingDto);
        componentUpdater.commitAndIndex(dbSession, componentDto);
        return toCreateResponse(componentDto);
    }
}
Also used : DbSession(org.sonar.db.DbSession) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) 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 22 with AlmPatDto

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

the class SearchAzureReposAction method doHandle.

private SearchAzureReposWsResponse 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 cannot be 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 projectKey = request.param(PARAM_PROJECT_NAME);
        String searchQuery = request.param(PARAM_SEARCH_QUERY);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        GsonAzureRepoList gsonAzureRepoList = azureDevOpsHttpClient.getRepos(url, pat, projectKey);
        Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey = getSqProjectsKeyByCustomKey(dbSession, almSettingDto, gsonAzureRepoList);
        List<AzureRepo> repositories = gsonAzureRepoList.getValues().stream().filter(r -> isSearchOnlyByProjectName(searchQuery) || doesSearchCriteriaMatchProjectOrRepo(r, searchQuery)).map(repo -> toAzureRepo(repo, sqProjectsKeyByAzureKey)).sorted(comparing(AzureRepo::getName, String::compareToIgnoreCase)).collect(toList());
        LOG.debug(repositories.toString());
        return SearchAzureReposWsResponse.newBuilder().addAllRepositories(repositories).build();
    }
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) StringUtils.containsIgnoreCase(org.apache.commons.lang.StringUtils.containsIgnoreCase) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) Request(org.sonar.api.server.ws.Request) Loggers(org.sonar.api.utils.log.Loggers) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) Comparator.comparing(java.util.Comparator.comparing) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) Collectors.toSet(java.util.stream.Collectors.toSet) Set(java.util.Set) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) NotFoundException(org.sonar.server.exceptions.NotFoundException) Objects(java.util.Objects) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) SearchAzureReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchAzureReposWsResponse) AzureDevOpsHttpClient(org.sonar.alm.client.azure.AzureDevOpsHttpClient) AzureRepo(org.sonarqube.ws.AlmIntegrations.AzureRepo) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AzureRepo(org.sonarqube.ws.AlmIntegrations.AzureRepo) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) DbSession(org.sonar.db.DbSession) GsonAzureRepoList(org.sonar.alm.client.azure.GsonAzureRepoList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 23 with AlmPatDto

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

the class SearchBitbucketServerReposAction method doHandle.

private SearchBitbucketserverReposWsResponse 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 cannot be 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 projectKey = request.param(PARAM_PROJECT_NAME);
        String repoName = request.param(PARAM_REPO_NAME);
        String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
        String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
        RepositoryList gsonBBSRepoList = bitbucketServerRestClient.getRepos(url, pat, projectKey, repoName);
        Map<String, String> sqProjectsKeyByBBSKey = getSqProjectsKeyByBBSKey(dbSession, almSettingDto, gsonBBSRepoList);
        List<BBSRepo> bbsRepos = gsonBBSRepoList.getValues().stream().map(gsonBBSRepo -> toBBSRepo(gsonBBSRepo, sqProjectsKeyByBBSKey)).collect(toList());
        SearchBitbucketserverReposWsResponse.Builder builder = SearchBitbucketserverReposWsResponse.newBuilder().setIsLastPage(gsonBBSRepoList.isLastPage()).addAllRepositories(bbsRepos);
        return builder.build();
    }
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Arrays(java.util.Arrays) SearchBitbucketserverReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse) PROVISION_PROJECTS(org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS) Function(java.util.function.Function) DbSession(org.sonar.db.DbSession) TreeSet(java.util.TreeSet) Request(org.sonar.api.server.ws.Request) WebService(org.sonar.api.server.ws.WebService) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Response(org.sonar.api.server.ws.Response) ProjectAlmSettingDao(org.sonar.db.alm.setting.ProjectAlmSettingDao) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) Collectors.toSet(java.util.stream.Collectors.toSet) ProjectDao(org.sonar.db.project.ProjectDao) Set(java.util.Set) BinaryOperator(java.util.function.BinaryOperator) Repository(org.sonar.alm.client.bitbucketserver.Repository) NotFoundException(org.sonar.server.exceptions.NotFoundException) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) DbClient(org.sonar.db.DbClient) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) BBSRepo(org.sonarqube.ws.AlmIntegrations.BBSRepo) ProjectDto(org.sonar.db.project.ProjectDto) Optional(java.util.Optional) AlmIntegrationsWsAction(org.sonar.server.almintegration.ws.AlmIntegrationsWsAction) UserSession(org.sonar.server.user.UserSession) BitbucketServerRestClient(org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient) WsUtils.writeProtobuf(org.sonar.server.ws.WsUtils.writeProtobuf) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) SearchBitbucketserverReposWsResponse(org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse) DbSession(org.sonar.db.DbSession) BBSRepo(org.sonarqube.ws.AlmIntegrations.BBSRepo) RepositoryList(org.sonar.alm.client.bitbucketserver.RepositoryList) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 24 with AlmPatDto

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

the class SetPatAction method doHandle.

private void doHandle(Request request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
        String pat = request.mandatoryParam(PARAM_PAT);
        String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
        String username = request.param(PARAM_USERNAME);
        String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
        AlmSettingDto almSetting = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(format("ALM Setting '%s' not found", almSettingKey)));
        Preconditions.checkArgument(Arrays.asList(AZURE_DEVOPS, BITBUCKET, GITLAB, BITBUCKET_CLOUD).contains(almSetting.getAlm()), "Only Azure DevOps, Bitbucket Server, GitLab ALM and Bitbucket Cloud Settings are supported.");
        if (almSetting.getAlm().equals(BITBUCKET_CLOUD)) {
            Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "Username cannot be null for Bitbucket Cloud");
        }
        String resultingPat = CredentialsEncoderHelper.encodeCredentials(almSetting.getAlm(), pat, username);
        Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSetting);
        if (almPatDto.isPresent()) {
            AlmPatDto almPat = almPatDto.get();
            almPat.setPersonalAccessToken(resultingPat);
            dbClient.almPatDao().update(dbSession, almPat, userSession.getLogin(), almSetting.getKey());
        } else {
            AlmPatDto almPat = new AlmPatDto().setPersonalAccessToken(resultingPat).setAlmSettingUuid(almSetting.getUuid()).setUserUuid(userUuid);
            dbClient.almPatDao().insert(dbSession, almPat, userSession.getLogin(), almSetting.getKey());
        }
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) NotFoundException(org.sonar.server.exceptions.NotFoundException) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto)

Example 25 with AlmPatDto

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

the class AlmPatsTesting method newAlmPatDto.

public static AlmPatDto newAlmPatDto() {
    AlmPatDto almPatDto = new AlmPatDto();
    almPatDto.setAlmSettingUuid(randomAlphanumeric(40));
    almPatDto.setPersonalAccessToken(randomAlphanumeric(2000));
    almPatDto.setUserUuid(randomAlphanumeric(40));
    return almPatDto;
}
Also used : AlmPatDto(org.sonar.db.alm.pat.AlmPatDto)

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