use of org.sonar.alm.client.github.security.AccessToken in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method listRepositories_returns_search_results.
@Test
public void listRepositories_returns_search_results() throws IOException {
String appUrl = "https://github.sonarsource.com";
AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
String responseJson = "{\n" + " \"total_count\": 2,\n" + " \"incomplete_results\": false,\n" + " \"items\": [\n" + " {\n" + " \"id\": 3081286,\n" + " \"node_id\": \"MDEwOlJlcG9zaXRvcnkzMDgxMjg2\",\n" + " \"name\": \"HelloWorld\",\n" + " \"full_name\": \"github/HelloWorld\",\n" + " \"owner\": {\n" + " \"login\": \"github\",\n" + " \"id\": 872147,\n" + " \"node_id\": \"MDQ6VXNlcjg3MjE0Nw==\",\n" + " \"avatar_url\": \"https://github.sonarsource.com/images/error/octocat_happy.gif\",\n" + " \"gravatar_id\": \"\",\n" + " \"url\": \"https://github.sonarsource.com/api/v3/users/github\",\n" + " \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/github/received_events\",\n" + " \"type\": \"User\"\n" + " },\n" + " \"private\": false,\n" + " \"html_url\": \"https://github.com/github/HelloWorld\",\n" + " \"description\": \"A C implementation of HelloWorld\",\n" + " \"fork\": false,\n" + " \"url\": \"https://github.sonarsource.com/api/v3/repos/github/HelloWorld\",\n" + " \"created_at\": \"2012-01-01T00:31:50Z\",\n" + " \"updated_at\": \"2013-01-05T17:58:47Z\",\n" + " \"pushed_at\": \"2012-01-01T00:37:02Z\",\n" + " \"homepage\": \"\",\n" + " \"size\": 524,\n" + " \"stargazers_count\": 1,\n" + " \"watchers_count\": 1,\n" + " \"language\": \"Assembly\",\n" + " \"forks_count\": 0,\n" + " \"open_issues_count\": 0,\n" + " \"master_branch\": \"master\",\n" + " \"default_branch\": \"master\",\n" + " \"score\": 1.0\n" + " }\n" + " ]\n" + "}";
when(httpClient.get(appUrl, accessToken, String.format("/search/repositories?q=%s&page=%s&per_page=%s", "world+fork:true+org:github", 1, 100))).thenReturn(new GithubApplicationHttpClient.GetResponse() {
@Override
public Optional<String> getNextEndPoint() {
return Optional.empty();
}
@Override
public int getCode() {
return 200;
}
@Override
public Optional<String> getContent() {
return Optional.of(responseJson);
}
});
GithubApplicationClient.Repositories repositories = underTest.listRepositories(appUrl, accessToken, "github", "world", 1, 100);
assertThat(repositories.getTotal()).isEqualTo(2);
assertThat(repositories.getRepositories()).extracting(GithubApplicationClient.Repository::getName, GithubApplicationClient.Repository::getFullName).containsOnly(tuple("HelloWorld", "github/HelloWorld"));
}
use of org.sonar.alm.client.github.security.AccessToken in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method listRepositories_fail_on_failure.
@Test
public void listRepositories_fail_on_failure() throws IOException {
String appUrl = "https://github.sonarsource.com";
AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
when(httpClient.get(appUrl, accessToken, String.format("/search/repositories?q=%s&page=%s&per_page=%s", "org:test", 1, 100))).thenThrow(new IOException("OOPS"));
assertThatThrownBy(() -> underTest.listRepositories(appUrl, accessToken, "test", null, 1, 100)).isInstanceOf(IllegalStateException.class).hasMessage("Failed to list all repositories of 'test' accessible by user access token on 'https://github.sonarsource.com' using query 'fork:true+org:test'");
}
use of org.sonar.alm.client.github.security.AccessToken in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method listRepositories_returns_pages_results.
@Test
public void listRepositories_returns_pages_results() throws IOException {
String appUrl = "https://github.sonarsource.com";
AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
String responseJson = "{\n" + " \"total_count\": 2,\n" + " \"incomplete_results\": false,\n" + " \"items\": [\n" + " {\n" + " \"id\": 3081286,\n" + " \"node_id\": \"MDEwOlJlcG9zaXRvcnkzMDgxMjg2\",\n" + " \"name\": \"HelloWorld\",\n" + " \"full_name\": \"github/HelloWorld\",\n" + " \"owner\": {\n" + " \"login\": \"github\",\n" + " \"id\": 872147,\n" + " \"node_id\": \"MDQ6VXNlcjg3MjE0Nw==\",\n" + " \"avatar_url\": \"https://github.sonarsource.com/images/error/octocat_happy.gif\",\n" + " \"gravatar_id\": \"\",\n" + " \"url\": \"https://github.sonarsource.com/api/v3/users/github\",\n" + " \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/github/received_events\",\n" + " \"type\": \"User\"\n" + " },\n" + " \"private\": false,\n" + " \"html_url\": \"https://github.com/github/HelloWorld\",\n" + " \"description\": \"A C implementation of HelloWorld\",\n" + " \"fork\": false,\n" + " \"url\": \"https://github.sonarsource.com/api/v3/repos/github/HelloWorld\",\n" + " \"created_at\": \"2012-01-01T00:31:50Z\",\n" + " \"updated_at\": \"2013-01-05T17:58:47Z\",\n" + " \"pushed_at\": \"2012-01-01T00:37:02Z\",\n" + " \"homepage\": \"\",\n" + " \"size\": 524,\n" + " \"stargazers_count\": 1,\n" + " \"watchers_count\": 1,\n" + " \"language\": \"Assembly\",\n" + " \"forks_count\": 0,\n" + " \"open_issues_count\": 0,\n" + " \"master_branch\": \"master\",\n" + " \"default_branch\": \"master\",\n" + " \"score\": 1.0\n" + " },\n" + " {\n" + " \"id\": 3081286,\n" + " \"node_id\": \"MDEwOlJlcG9zaXRvcnkzMDgxMjg2\",\n" + " \"name\": \"HelloUniverse\",\n" + " \"full_name\": \"github/HelloUniverse\",\n" + " \"owner\": {\n" + " \"login\": \"github\",\n" + " \"id\": 872147,\n" + " \"node_id\": \"MDQ6VXNlcjg3MjE0Nw==\",\n" + " \"avatar_url\": \"https://github.sonarsource.com/images/error/octocat_happy.gif\",\n" + " \"gravatar_id\": \"\",\n" + " \"url\": \"https://github.sonarsource.com/api/v3/users/github\",\n" + " \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/github/received_events\",\n" + " \"type\": \"User\"\n" + " },\n" + " \"private\": false,\n" + " \"html_url\": \"https://github.com/github/HelloUniverse\",\n" + " \"description\": \"A C implementation of HelloUniverse\",\n" + " \"fork\": false,\n" + " \"url\": \"https://github.sonarsource.com/api/v3/repos/github/HelloUniverse\",\n" + " \"created_at\": \"2012-01-01T00:31:50Z\",\n" + " \"updated_at\": \"2013-01-05T17:58:47Z\",\n" + " \"pushed_at\": \"2012-01-01T00:37:02Z\",\n" + " \"homepage\": \"\",\n" + " \"size\": 524,\n" + " \"stargazers_count\": 1,\n" + " \"watchers_count\": 1,\n" + " \"language\": \"Assembly\",\n" + " \"forks_count\": 0,\n" + " \"open_issues_count\": 0,\n" + " \"master_branch\": \"master\",\n" + " \"default_branch\": \"master\",\n" + " \"score\": 1.0\n" + " }\n" + " ]\n" + "}";
when(httpClient.get(appUrl, accessToken, String.format("/search/repositories?q=%s&page=%s&per_page=%s", "fork:true+org:github", 1, 100))).thenReturn(new OkGetResponse(responseJson));
GithubApplicationClient.Repositories repositories = underTest.listRepositories(appUrl, accessToken, "github", null, 1, 100);
assertThat(repositories.getTotal()).isEqualTo(2);
assertThat(repositories.getRepositories()).extracting(GithubApplicationClient.Repository::getName, GithubApplicationClient.Repository::getFullName).containsOnly(tuple("HelloWorld", "github/HelloWorld"), tuple("HelloUniverse", "github/HelloUniverse"));
}
use of org.sonar.alm.client.github.security.AccessToken in project sonarqube by SonarSource.
the class GithubApplicationClientImplTest method listOrganizations_returns_pages_results.
@Test
public void listOrganizations_returns_pages_results() throws IOException {
String appUrl = "https://github.sonarsource.com";
AccessToken accessToken = new UserAccessToken(randomAlphanumeric(10));
String responseJson = "{\n" + " \"total_count\": 2,\n" + " \"installations\": [\n" + " {\n" + " \"id\": 1,\n" + " \"account\": {\n" + " \"login\": \"github\",\n" + " \"id\": 1,\n" + " \"node_id\": \"MDEyOk9yZ2FuaXphdGlvbjE=\",\n" + " \"url\": \"https://github.sonarsource.com/api/v3/orgs/github\",\n" + " \"repos_url\": \"https://github.sonarsource.com/api/v3/orgs/github/repos\",\n" + " \"events_url\": \"https://github.sonarsource.com/api/v3/orgs/github/events\",\n" + " \"hooks_url\": \"https://github.sonarsource.com/api/v3/orgs/github/hooks\",\n" + " \"issues_url\": \"https://github.sonarsource.com/api/v3/orgs/github/issues\",\n" + " \"members_url\": \"https://github.sonarsource.com/api/v3/orgs/github/members{/member}\",\n" + " \"public_members_url\": \"https://github.sonarsource.com/api/v3/orgs/github/public_members{/member}\",\n" + " \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n" + " \"description\": \"A great organization\"\n" + " },\n" + " \"access_tokens_url\": \"https://github.sonarsource.com/api/v3/app/installations/1/access_tokens\",\n" + " \"repositories_url\": \"https://github.sonarsource.com/api/v3/installation/repositories\",\n" + " \"html_url\": \"https://github.com/organizations/github/settings/installations/1\",\n" + " \"app_id\": 1,\n" + " \"target_id\": 1,\n" + " \"target_type\": \"Organization\",\n" + " \"permissions\": {\n" + " \"checks\": \"write\",\n" + " \"metadata\": \"read\",\n" + " \"contents\": \"read\"\n" + " },\n" + " \"events\": [\n" + " \"push\",\n" + " \"pull_request\"\n" + " ],\n" + " \"single_file_name\": \"config.yml\"\n" + " },\n" + " {\n" + " \"id\": 3,\n" + " \"account\": {\n" + " \"login\": \"octocat\",\n" + " \"id\": 2,\n" + " \"node_id\": \"MDQ6VXNlcjE=\",\n" + " \"avatar_url\": \"https://github.com/images/error/octocat_happy.gif\",\n" + " \"gravatar_id\": \"\",\n" + " \"url\": \"https://github.sonarsource.com/api/v3/users/octocat\",\n" + " \"html_url\": \"https://github.com/octocat\",\n" + " \"followers_url\": \"https://github.sonarsource.com/api/v3/users/octocat/followers\",\n" + " \"following_url\": \"https://github.sonarsource.com/api/v3/users/octocat/following{/other_user}\",\n" + " \"gists_url\": \"https://github.sonarsource.com/api/v3/users/octocat/gists{/gist_id}\",\n" + " \"starred_url\": \"https://github.sonarsource.com/api/v3/users/octocat/starred{/owner}{/repo}\",\n" + " \"subscriptions_url\": \"https://github.sonarsource.com/api/v3/users/octocat/subscriptions\",\n" + " \"organizations_url\": \"https://github.sonarsource.com/api/v3/users/octocat/orgs\",\n" + " \"repos_url\": \"https://github.sonarsource.com/api/v3/users/octocat/repos\",\n" + " \"events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/events{/privacy}\",\n" + " \"received_events_url\": \"https://github.sonarsource.com/api/v3/users/octocat/received_events\",\n" + " \"type\": \"User\",\n" + " \"site_admin\": false\n" + " },\n" + " \"access_tokens_url\": \"https://github.sonarsource.com/api/v3/app/installations/1/access_tokens\",\n" + " \"repositories_url\": \"https://github.sonarsource.com/api/v3/installation/repositories\",\n" + " \"html_url\": \"https://github.com/organizations/github/settings/installations/1\",\n" + " \"app_id\": 1,\n" + " \"target_id\": 1,\n" + " \"target_type\": \"Organization\",\n" + " \"permissions\": {\n" + " \"checks\": \"write\",\n" + " \"metadata\": \"read\",\n" + " \"contents\": \"read\"\n" + " },\n" + " \"events\": [\n" + " \"push\",\n" + " \"pull_request\"\n" + " ],\n" + " \"single_file_name\": \"config.yml\"\n" + " }\n" + " ]\n" + "} ";
when(httpClient.get(appUrl, accessToken, String.format("/user/installations?page=%s&per_page=%s", 1, 100))).thenReturn(new OkGetResponse(responseJson));
GithubApplicationClient.Organizations organizations = underTest.listOrganizations(appUrl, accessToken, 1, 100);
assertThat(organizations.getTotal()).isEqualTo(2);
assertThat(organizations.getOrganizations()).extracting(GithubApplicationClient.Organization::getLogin).containsOnly("github", "octocat");
}
use of org.sonar.alm.client.github.security.AccessToken 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);
}
}
Aggregations