use of org.sonar.alm.client.bitbucket.bitbucketcloud.Repository in project sonarqube by SonarSource.
the class SearchBitbucketCloudReposAction method doHandle.
private SearchBitbucketcloudReposWsResponse doHandle(Request request) {
userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
String repoName = request.param(PARAM_REPO_NAME);
int page = request.mandatoryParamAsInt(PAGE);
int pageSize = request.mandatoryParamAsInt(PAGE_SIZE);
try (DbSession dbSession = dbClient.openSession(false)) {
AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey).orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
String workspace = ofNullable(almSettingDto.getAppId()).orElseThrow(() -> new IllegalArgumentException(String.format("workspace for alm setting %s is missing", almSettingDto.getKey())));
String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
RepositoryList repositoryList = bitbucketCloudRestClient.searchRepos(pat, workspace, repoName, page, pageSize);
Map<String, String> sqProjectKeyByRepoSlug = getSqProjectKeyByRepoSlug(dbSession, almSettingDto, repositoryList.getValues());
List<BBCRepo> bbcRepos = repositoryList.getValues().stream().map(repository -> toBBCRepo(repository, workspace, sqProjectKeyByRepoSlug)).collect(toList());
SearchBitbucketcloudReposWsResponse.Builder builder = SearchBitbucketcloudReposWsResponse.newBuilder().setIsLastPage(repositoryList.getNext() == null).setPaging(Paging.newBuilder().setPageIndex(page).setPageSize(pageSize).build()).addAllRepositories(bbcRepos);
return builder.build();
}
}
use of org.sonar.alm.client.bitbucket.bitbucketcloud.Repository in project sonarqube by SonarSource.
the class ImportBitbucketCloudRepoActionTest method getGsonBBCRepo.
private Repository getGsonBBCRepo() {
Project project1 = new Project("PROJECT-UUID-ONE", "projectKey1", "projectName1");
MainBranch mainBranch = new MainBranch("branch", "develop");
return new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1, mainBranch);
}
use of org.sonar.alm.client.bitbucket.bitbucketcloud.Repository in project sonarqube by SonarSource.
the class ImportBitbucketCloudRepoActionTest method import_project.
@Test
public void import_project() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
AlmSettingDto almSetting = db.almSettings().insertBitbucketCloudAlmSetting();
db.almPats().insert(dto -> {
dto.setAlmSettingUuid(almSetting.getUuid());
dto.setUserUuid(user.getUuid());
});
Repository repo = getGsonBBCRepo();
when(bitbucketCloudRestClient.getRepo(any(), any(), any())).thenReturn(repo);
Projects.CreateWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("repositorySlug", "repo-slug-1").executeProtobuf(Projects.CreateWsResponse.class);
Projects.CreateWsResponse.Project result = response.getProject();
assertThat(result.getKey()).isEqualTo(almSetting.getAppId() + "_" + repo.getSlug());
assertThat(result.getName()).isEqualTo(repo.getName());
Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
assertThat(projectDto).isPresent();
Optional<ProjectAlmSettingDto> projectAlmSettingDto = db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get());
assertThat(projectAlmSettingDto).isPresent();
assertThat(projectAlmSettingDto.get().getAlmRepo()).isEqualTo("repo-slug-1");
Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), projectDto.get().getUuid(), "develop");
assertThat(branchDto).isPresent();
assertThat(branchDto.get().isMain()).isTrue();
}
use of org.sonar.alm.client.bitbucket.bitbucketcloud.Repository in project sonarqube by SonarSource.
the class ImportBitbucketCloudRepoActionTest method fail_project_already_exist.
@Test
public void fail_project_already_exist() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
db.almPats().insert(dto -> {
dto.setAlmSettingUuid(almSetting.getUuid());
dto.setUserUuid(user.getUuid());
});
Repository repo = getGsonBBCRepo();
String projectKey = almSetting.getAppId() + "_" + repo.getSlug();
db.components().insertPublicProject(p -> p.setDbKey(projectKey));
when(bitbucketCloudRestClient.getRepo(any(), any(), any())).thenReturn(repo);
TestRequest request = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("repositorySlug", "repo-slug-1");
assertThatThrownBy(request::execute).isInstanceOf(BadRequestException.class).hasMessageContaining("Could not create null, key already exists: " + projectKey);
}
use of org.sonar.alm.client.bitbucket.bitbucketcloud.Repository in project sonarqube by SonarSource.
the class ImportBitbucketCloudRepoAction method doHandle.
private Projects.CreateWsResponse doHandle(Request request) {
importHelper.checkProvisionProjectPermission();
String userUuid = importHelper.getUserUuid();
String repoSlug = request.mandatoryParam(PARAM_REPO_SLUG);
AlmSettingDto almSettingDto = importHelper.getAlmSetting(request);
String workspace = ofNullable(almSettingDto.getAppId()).orElseThrow(() -> new IllegalArgumentException(String.format("workspace for alm setting %s is missing", almSettingDto.getKey())));
try (DbSession dbSession = dbClient.openSession(false)) {
String pat = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto).map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException(String.format("Username and App Password for '%s' is missing", almSettingDto.getKey())));
Repository repo = bitbucketCloudRestClient.getRepo(pat, workspace, repoSlug);
ComponentDto componentDto = createProject(dbSession, workspace, repo, repo.getMainBranch().getName());
populatePRSetting(dbSession, repo, componentDto, almSettingDto);
componentUpdater.commitAndIndex(dbSession, componentDto);
return toCreateResponse(componentDto);
}
}
Aggregations