Skip to content

Commit fa142d0

Browse files
authored
Added method for listing repositories for the authenticated user (spotify#46)
Co-authored-by: Martin Kero <[email protected]>
1 parent 8c8ed1e commit fa142d0

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

src/main/java/com/spotify/github/v3/clients/RepositoryClient.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2828
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
2929

30+
import com.google.common.base.Strings;
3031
import com.google.common.collect.ImmutableMap;
3132
import com.spotify.github.async.AsyncPage;
3233
import com.spotify.github.v3.comment.Comment;
@@ -44,6 +45,7 @@
4445
import com.spotify.github.v3.repos.Repository;
4546
import com.spotify.github.v3.repos.Status;
4647
import com.spotify.github.v3.repos.requests.RepositoryCreateStatus;
48+
import com.spotify.github.v3.repos.requests.AuthenticatedUserRepositoriesFilter;
4749
import java.lang.invoke.MethodHandles;
4850
import java.util.Iterator;
4951
import java.util.List;
@@ -78,6 +80,7 @@ public class RepositoryClient {
7880
private static final String MERGE_TEMPLATE = "/repos/%s/%s/merges";
7981
private static final String FORK_TEMPLATE = "/repos/%s/%s/forks";
8082
private static final String LIST_REPOSITORY_TEMPLATE = "/orgs/%s/repos";
83+
private static final String LIST_REPOSITORIES_FOR_AUTHENTICATED_USER = "/user/repos";
8184

8285
private final String owner;
8386
private final String repo;
@@ -152,6 +155,18 @@ public CompletableFuture<List<Repository>> listOrganizationRepositories() {
152155
return github.request(path, LIST_REPOSITORY);
153156
}
154157

158+
/**
159+
* List repositories for the authenticated user.
160+
*
161+
* @param filter filter parameters
162+
* @return list of repositories for the authenticated user
163+
*/
164+
public Iterator<AsyncPage<Repository>> listAuthenticatedUserRepositories(final AuthenticatedUserRepositoriesFilter filter) {
165+
final String serial = filter.serialize();
166+
final String path = LIST_REPOSITORIES_FOR_AUTHENTICATED_USER + (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
167+
return new GithubPageIterator<>(new GithubPage<>(github, path, LIST_REPOSITORY));
168+
}
169+
155170
/**
156171
* Create a webhook.
157172
*
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.repos.requests;
22+
23+
import javax.annotation.Nullable;
24+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
26+
import com.spotify.github.GithubStyle;
27+
import com.spotify.github.Parameters;
28+
import org.immutables.value.Value;
29+
30+
/**
31+
* Filter parameters for listing authenticated user's repositories. To be
32+
* serialized as key=value.
33+
*/
34+
@Value.Immutable
35+
@GithubStyle
36+
@JsonSerialize(as = ImmutableAuthenticatedUserRepositoriesFilter.class)
37+
@JsonDeserialize(as = ImmutableAuthenticatedUserRepositoriesFilter.class)
38+
public interface AuthenticatedUserRepositoriesFilter extends Parameters {
39+
40+
/**
41+
* Can be one of all, public, or private. Default: all
42+
*/
43+
@Nullable String visibility();
44+
45+
/**
46+
* Comma-separated list of values. Can include:
47+
* * owner: Repositories that are owned by the authenticated user.
48+
* * collaborator: Repositories that the user has been added to as a
49+
* collaborator.
50+
* * organization_member: Repositories that the user has access to through
51+
* being a member of an organization. This includes every repository on
52+
* every team that the user is on.
53+
*
54+
* Default: owner,collaborator,organization_member
55+
*/
56+
@Nullable String affiliation();
57+
58+
/**
59+
* Can be one of all, owner, public, private, member. Default: all
60+
* Will cause a 422 error if used in the same request as visibility or
61+
* affiliation.
62+
*/
63+
@Nullable String type();
64+
65+
/**
66+
* Can be one of created, updated, pushed, full_name. Default: full_name
67+
*/
68+
@Nullable String sort();
69+
70+
/**
71+
* Can be one of asc or desc. Default: asc when using full_name, otherwise
72+
* desc
73+
* */
74+
@Nullable String direction();
75+
}

src/test/java/com/spotify/github/v3/clients/RepositoryClientTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
import static org.mockito.ArgumentMatchers.any;
3838
import static org.mockito.Mockito.mock;
3939
import static org.mockito.Mockito.when;
40+
import static java.util.stream.StreamSupport.stream;
4041

4142
import com.google.common.collect.ImmutableMap;
4243
import com.google.common.collect.Lists;
4344
import com.google.common.io.Resources;
45+
import com.spotify.github.async.AsyncPage;
4446
import com.spotify.github.jackson.Json;
4547
import com.spotify.github.v3.comment.Comment;
4648
import com.spotify.github.v3.repos.Branch;
@@ -53,10 +55,12 @@
5355
import com.spotify.github.v3.repos.Repository;
5456
import com.spotify.github.v3.repos.RepositoryTest;
5557
import com.spotify.github.v3.repos.Status;
58+
import com.spotify.github.v3.repos.requests.ImmutableAuthenticatedUserRepositoriesFilter;
5659
import java.io.IOException;
5760
import java.util.List;
5861
import java.util.Optional;
5962
import java.util.concurrent.CompletableFuture;
63+
import java.util.stream.Collectors;
6064
import okhttp3.Headers;
6165
import okhttp3.MediaType;
6266
import okhttp3.Protocol;
@@ -113,6 +117,24 @@ public void listOrganizationRepositories() throws Exception {
113117
assertThat(repositories.size(), is(1));
114118
}
115119

120+
@Test
121+
public void listAuthenticatedUserRepositories() throws Exception {
122+
final String pageLink = "<https://github.com/api/v3/user/repos>; rel=\"first\"";
123+
final String pageBody = getFixture("list_of_repos_for_authenticated_user.json");
124+
final Response pageResponse = createMockResponse(pageLink, pageBody);
125+
126+
when(github.request("/user/repos")).thenReturn(completedFuture(pageResponse));
127+
128+
final Iterable<AsyncPage<Repository>> pageIterator = () -> repoClient.listAuthenticatedUserRepositories(ImmutableAuthenticatedUserRepositoriesFilter.builder().build());
129+
final List<Repository> repositories =
130+
stream(pageIterator.spliterator(), false)
131+
.flatMap(page -> stream(page.spliterator(), false))
132+
.collect(Collectors.toList());
133+
134+
assertThat(repositories.get(0).id(), is(1296269));
135+
assertThat(repositories.size(), is(1));
136+
}
137+
116138
@Test
117139
public void listCommits() throws Exception {
118140
final CompletableFuture<List<CommitItem>> fixture =
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
[
2+
{
3+
"id": 1296269,
4+
"owner": {
5+
"login": "octocat",
6+
"id": 1,
7+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
8+
"gravatar_id": "",
9+
"url": "https://api.github.com/users/octocat",
10+
"html_url": "https://github.com/octocat",
11+
"followers_url": "https://api.github.com/users/octocat/followers",
12+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
13+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
14+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
15+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
16+
"organizations_url": "https://api.github.com/users/octocat/orgs",
17+
"repos_url": "https://api.github.com/users/octocat/repos",
18+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
19+
"received_events_url": "https://api.github.com/users/octocat/received_events",
20+
"type": "User",
21+
"site_admin": false
22+
},
23+
"name": "Hello-World",
24+
"full_name": "octocat/Hello-World",
25+
"description": "This your first repo!",
26+
"private": false,
27+
"fork": true,
28+
"url": "https://api.github.com/repos/octocat/Hello-World",
29+
"html_url": "https://github.com/octocat/Hello-World",
30+
"archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}",
31+
"assignees_url": "http://api.github.com/repos/octocat/Hello-World/assignees{/user}",
32+
"blobs_url": "http://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}",
33+
"branches_url": "http://api.github.com/repos/octocat/Hello-World/branches{/branch}",
34+
"clone_url": "https://github.com/octocat/Hello-World.git",
35+
"collaborators_url": "http://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}",
36+
"comments_url": "http://api.github.com/repos/octocat/Hello-World/comments{/number}",
37+
"commits_url": "http://api.github.com/repos/octocat/Hello-World/commits{/sha}",
38+
"compare_url": "http://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}",
39+
"contents_url": "http://api.github.com/repos/octocat/Hello-World/contents/{+path}",
40+
"contributors_url": "http://api.github.com/repos/octocat/Hello-World/contributors",
41+
"deployments_url": "http://api.github.com/repos/octocat/Hello-World/deployments",
42+
"downloads_url": "http://api.github.com/repos/octocat/Hello-World/downloads",
43+
"events_url": "http://api.github.com/repos/octocat/Hello-World/events",
44+
"forks_url": "http://api.github.com/repos/octocat/Hello-World/forks",
45+
"git_commits_url": "http://api.github.com/repos/octocat/Hello-World/git/commits{/sha}",
46+
"git_refs_url": "http://api.github.com/repos/octocat/Hello-World/git/refs{/sha}",
47+
"git_tags_url": "http://api.github.com/repos/octocat/Hello-World/git/tags{/sha}",
48+
"git_url": "git:github.com/octocat/Hello-World.git",
49+
"hooks_url": "http://api.github.com/repos/octocat/Hello-World/hooks",
50+
"issue_comment_url": "http://api.github.com/repos/octocat/Hello-World/issues/comments{/number}",
51+
"issue_events_url": "http://api.github.com/repos/octocat/Hello-World/issues/events{/number}",
52+
"issues_url": "http://api.github.com/repos/octocat/Hello-World/issues{/number}",
53+
"keys_url": "http://api.github.com/repos/octocat/Hello-World/keys{/key_id}",
54+
"labels_url": "http://api.github.com/repos/octocat/Hello-World/labels{/name}",
55+
"languages_url": "http://api.github.com/repos/octocat/Hello-World/languages",
56+
"merges_url": "http://api.github.com/repos/octocat/Hello-World/merges",
57+
"milestones_url": "http://api.github.com/repos/octocat/Hello-World/milestones{/number}",
58+
"mirror_url": "git:git.example.com/octocat/Hello-World",
59+
"notifications_url": "http://api.github.com/repos/octocat/Hello-World/notifications{?since, all, participating}",
60+
"pulls_url": "http://api.github.com/repos/octocat/Hello-World/pulls{/number}",
61+
"releases_url": "http://api.github.com/repos/octocat/Hello-World/releases{/id}",
62+
"ssh_url": "[email protected]:octocat/Hello-World.git",
63+
"stargazers_url": "http://api.github.com/repos/octocat/Hello-World/stargazers",
64+
"statuses_url": "http://api.github.com/repos/octocat/Hello-World/statuses/{sha}",
65+
"subscribers_url": "http://api.github.com/repos/octocat/Hello-World/subscribers",
66+
"subscription_url": "http://api.github.com/repos/octocat/Hello-World/subscription",
67+
"svn_url": "https://svn.github.com/octocat/Hello-World",
68+
"tags_url": "http://api.github.com/repos/octocat/Hello-World/tags",
69+
"teams_url": "http://api.github.com/repos/octocat/Hello-World/teams",
70+
"trees_url": "http://api.github.com/repos/octocat/Hello-World/git/trees{/sha}",
71+
"homepage": "https://github.com",
72+
"language": null,
73+
"forks_count": 9,
74+
"stargazers_count": 80,
75+
"watchers_count": 80,
76+
"size": 108,
77+
"default_branch": "master",
78+
"open_issues_count": 0,
79+
"has_issues": true,
80+
"has_wiki": true,
81+
"forks": 2,
82+
"has_pages": false,
83+
"has_downloads": true,
84+
"pushed_at": "2011-01-26T19:06:43Z",
85+
"created_at": "2011-01-26T19:01:12Z",
86+
"updated_at": "2011-01-26T19:14:43Z",
87+
"permissions": {
88+
"admin": false,
89+
"push": false,
90+
"pull": true
91+
}
92+
}
93+
]

0 commit comments

Comments
 (0)