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