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