Search in sources :

Example 1 with GithubRepository

use of org.sonarqube.ws.AlmIntegrations.GithubRepository in project sonarqube by SonarSource.

the class ListGithubRepositoriesActionTest method return_repositories_using_existing_personal_access_token.

@Test
public void return_repositories_using_existing_personal_access_token() {
    AlmSettingDto githubAlmSettings = setupAlm();
    AlmPatDto pat = db.almPats().insert(p -> p.setAlmSettingUuid(githubAlmSettings.getUuid()).setUserUuid(userSession.getUuid()));
    when(appClient.listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1), eq(100))).thenReturn(new GithubApplicationClient.Repositories().setTotal(2).setRepositories(Stream.of("HelloWorld", "HelloUniverse").map(name -> new GithubApplicationClient.Repository(name.length(), name, false, "github/" + name, "https://github-enterprise.sonarqube.com/api/v3/github/HelloWorld", "main")).collect(Collectors.toList())));
    ProjectDto project = db.components().insertPrivateProjectDto(componentDto -> componentDto.setDbKey("github_HelloWorld"));
    db.almSettings().insertGitHubProjectAlmSetting(githubAlmSettings, project, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("github/HelloWorld"));
    ProjectDto project2 = db.components().insertPrivateProjectDto(componentDto -> componentDto.setDbKey("github_HelloWorld2"));
    db.almSettings().insertGitHubProjectAlmSetting(githubAlmSettings, project2, projectAlmSettingDto -> projectAlmSettingDto.setAlmRepo("github/HelloWorld"));
    ListGithubRepositoriesWsResponse response = ws.newRequest().setParam(ListGithubRepositoriesAction.PARAM_ALM_SETTING, githubAlmSettings.getKey()).setParam(ListGithubRepositoriesAction.PARAM_ORGANIZATION, "github").executeProtobuf(ListGithubRepositoriesWsResponse.class);
    assertThat(response.getPaging()).extracting(Common.Paging::getPageIndex, Common.Paging::getPageSize, Common.Paging::getTotal).containsOnly(1, 100, 2);
    assertThat(response.getRepositoriesCount()).isEqualTo(2);
    assertThat(response.getRepositoriesList()).extracting(GithubRepository::getKey, GithubRepository::getName, GithubRepository::getSqProjectKey).containsOnly(tuple("github/HelloWorld", "HelloWorld", project.getKey()), tuple("github/HelloUniverse", "HelloUniverse", ""));
    verify(appClient).listRepositories(eq(githubAlmSettings.getUrl()), argThat(token -> token.getValue().equals(pat.getPersonalAccessToken())), eq("github"), isNull(), eq(1), eq(100));
}
Also used : AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) UserDto(org.sonar.db.user.UserDto) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) GlobalPermission(org.sonar.db.permission.GlobalPermission) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) GithubApplicationClientImpl(org.sonar.alm.client.github.GithubApplicationClientImpl) GithubRepository(org.sonarqube.ws.AlmIntegrations.GithubRepository) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) UserSessionRule.standalone(org.sonar.server.tester.UserSessionRule.standalone) ListGithubRepositoriesWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubRepositoriesWsResponse) ProjectAlmSettingDao(org.sonar.db.alm.setting.ProjectAlmSettingDao) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) UserSessionRule(org.sonar.server.tester.UserSessionRule) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) DbTester(org.sonar.db.DbTester) System2(org.sonar.api.utils.System2) Assertions.tuple(org.assertj.core.api.Assertions.tuple) TestRequest(org.sonar.server.ws.TestRequest) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) WsActionTester(org.sonar.server.ws.WsActionTester) Collectors(java.util.stream.Collectors) NotFoundException(org.sonar.server.exceptions.NotFoundException) Common(org.sonarqube.ws.Common) Mockito.verify(org.mockito.Mockito.verify) Stream(java.util.stream.Stream) Rule(org.junit.Rule) UnauthorizedException(org.sonar.server.exceptions.UnauthorizedException) ProjectDto(org.sonar.db.project.ProjectDto) Mockito.mock(org.mockito.Mockito.mock) ProjectDto(org.sonar.db.project.ProjectDto) GithubRepository(org.sonarqube.ws.AlmIntegrations.GithubRepository) GithubApplicationClient(org.sonar.alm.client.github.GithubApplicationClient) AlmPatDto(org.sonar.db.alm.pat.AlmPatDto) ListGithubRepositoriesWsResponse(org.sonarqube.ws.AlmIntegrations.ListGithubRepositoriesWsResponse) AlmSettingDto(org.sonar.db.alm.setting.AlmSettingDto) Common(org.sonarqube.ws.Common) Test(org.junit.Test)

Aggregations

Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)1 Assertions.tuple (org.assertj.core.api.Assertions.tuple)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)1 ArgumentMatchers.isNull (org.mockito.ArgumentMatchers.isNull)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1 GithubApplicationClient (org.sonar.alm.client.github.GithubApplicationClient)1 GithubApplicationClientImpl (org.sonar.alm.client.github.GithubApplicationClientImpl)1 System2 (org.sonar.api.utils.System2)1 DbTester (org.sonar.db.DbTester)1 AlmPatDto (org.sonar.db.alm.pat.AlmPatDto)1 AlmSettingDto (org.sonar.db.alm.setting.AlmSettingDto)1 ProjectAlmSettingDao (org.sonar.db.alm.setting.ProjectAlmSettingDao)1