Search in sources :

Example 1 with GsonAzureRepo

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();
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AzureRepo(org.sonarqube.ws.AlmIntegrations.AzureRepo)

Example 2 with GsonAzureRepo

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");
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) UserDto(org.sonar.db.user.UserDto) Projects(org.sonarqube.ws.Projects) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Test(org.junit.Test)

Example 3 with GsonAzureRepo

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);
}
Also used : GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) UserDto(org.sonar.db.user.UserDto) BadRequestException(org.sonar.server.exceptions.BadRequestException) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test)

Example 4 with GsonAzureRepo

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);
    }
}
Also used : DbSession(org.sonar.db.DbSession) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) ComponentDto(org.sonar.db.component.ComponentDto) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto)

Example 5 with GsonAzureRepo

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");
}
Also used : ProjectDto(org.sonar.db.project.ProjectDto) BranchDto(org.sonar.db.component.BranchDto) GsonAzureRepo(org.sonar.alm.client.azure.GsonAzureRepo) UserDto(org.sonar.db.user.UserDto) Projects(org.sonarqube.ws.Projects) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) ProjectAlmSettingDto(org.sonar.db.alm.setting.ProjectAlmSettingDto) Test(org.junit.Test)

Aggregations

GsonAzureRepo (org.sonar.alm.client.azure.GsonAzureRepo)5 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)4 ProjectAlmSettingDto (org.sonar.db.alm.setting.ProjectAlmSettingDto)4 Test (org.junit.Test)3 ProjectDto (org.sonar.db.project.ProjectDto)3 UserDto (org.sonar.db.user.UserDto)3 BranchDto (org.sonar.db.component.BranchDto)2 Projects (org.sonarqube.ws.Projects)2 DbSession (org.sonar.db.DbSession)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 ComponentDto (org.sonar.db.component.ComponentDto)1 BadRequestException (org.sonar.server.exceptions.BadRequestException)1 TestRequest (org.sonar.server.ws.TestRequest)1 AzureRepo (org.sonarqube.ws.AlmIntegrations.AzureRepo)1