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);
}
}
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();
}
}
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();
}
}
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();
}
}
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;
}
Aggregations