use of org.sonar.alm.client.azure.GsonAzureRepo in project sonarqube by SonarSource.
the class SearchAzureReposAction method toAzureRepo.
private static AzureRepo toAzureRepo(GsonAzureRepo azureRepo, Map<ProjectKeyName, ProjectDto> sqProjectsKeyByAzureKey) {
AzureRepo.Builder builder = AzureRepo.newBuilder().setName(azureRepo.getName()).setProjectName(azureRepo.getProject().getName());
ProjectDto projectDto = sqProjectsKeyByAzureKey.get(ProjectKeyName.from(azureRepo));
if (projectDto != null) {
builder.setSqProjectName(projectDto.getName());
builder.setSqProjectKey(projectDto.getKey());
}
return builder.build();
}
use of org.sonar.alm.client.azure.GsonAzureRepo in project sonarqube by SonarSource.
the class ImportAzureProjectActionTest method import_project.
@Test
public void import_project() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
db.almPats().insert(dto -> {
dto.setAlmSettingUuid(almSetting.getUuid());
dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
dto.setUserUuid(user.getUuid());
});
GsonAzureRepo repo = getGsonAzureRepo();
when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption), "project-name", "repo-name")).thenReturn(repo);
Projects.CreateWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("projectName", "project-name").setParam("repositoryName", "repo-name").executeProtobuf(Projects.CreateWsResponse.class);
Projects.CreateWsResponse.Project result = response.getProject();
assertThat(result.getKey()).isEqualTo(repo.getProject().getName() + "_" + repo.getName());
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.get().getAlmRepo()).isEqualTo("repo-name");
assertThat(projectAlmSettingDto.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
assertThat(projectAlmSettingDto.get().getAlmSlug()).isEqualTo("project-name");
Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()).stream().filter(BranchDto::isMain).findFirst();
assertThat(mainBranch).isPresent();
assertThat(mainBranch.get().getKey()).hasToString("repo-default-branch");
}
use of org.sonar.alm.client.azure.GsonAzureRepo in project sonarqube by SonarSource.
the class ImportAzureProjectActionTest method fail_project_already_exists.
@Test
public void fail_project_already_exists() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
db.almPats().insert(dto -> {
dto.setAlmSettingUuid(almSetting.getUuid());
dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
dto.setUserUuid(user.getUuid());
});
GsonAzureRepo repo = getGsonAzureRepo();
String projectKey = repo.getProject().getName() + "_" + repo.getName();
db.components().insertPublicProject(p -> p.setDbKey(projectKey));
when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption), "project-name", "repo-name")).thenReturn(repo);
TestRequest request = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("projectName", "project-name").setParam("repositoryName", "repo-name");
assertThatThrownBy(request::execute).isInstanceOf(BadRequestException.class).hasMessage("Could not create null, key already exists: " + projectKey);
}
use of org.sonar.alm.client.azure.GsonAzureRepo 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.alm.client.azure.GsonAzureRepo in project sonarqube by SonarSource.
the class ImportAzureProjectActionTest method import_project_from_empty_repo.
@Test
public void import_project_from_empty_repo() {
UserDto user = db.users().insertUser();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
AlmSettingDto almSetting = db.almSettings().insertAzureAlmSetting();
db.almPats().insert(dto -> {
dto.setAlmSettingUuid(almSetting.getUuid());
dto.setPersonalAccessToken(almSetting.getDecryptedPersonalAccessToken(encryption));
dto.setUserUuid(user.getUuid());
});
GsonAzureRepo repo = getEmptyGsonAzureRepo();
when(azureDevOpsHttpClient.getRepo(almSetting.getUrl(), almSetting.getDecryptedPersonalAccessToken(encryption), "project-name", "repo-name")).thenReturn(repo);
Projects.CreateWsResponse response = ws.newRequest().setParam("almSetting", almSetting.getKey()).setParam("projectName", "project-name").setParam("repositoryName", "repo-name").executeProtobuf(Projects.CreateWsResponse.class);
Projects.CreateWsResponse.Project result = response.getProject();
Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
Optional<BranchDto> mainBranch = db.getDbClient().branchDao().selectByProject(db.getSession(), projectDto.get()).stream().filter(BranchDto::isMain).findFirst();
assertThat(mainBranch).isPresent();
assertThat(mainBranch.get().getKey()).hasToString("master");
}
Aggregations