Skip to content

Commit 3cc0ea0

Browse files
Lirickmarthast
andauthored
Add list repositories for an org (spotify#21)
Co-authored-by: marthast <marthast@spotify.com>
1 parent 26f84d3 commit 3cc0ea0

4 files changed

Lines changed: 142 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.spotify.github.v3.repos.Branch;
3535
import com.spotify.github.v3.repos.CommitItem;
3636
import com.spotify.github.v3.repos.FolderContent;
37+
import com.spotify.github.v3.repos.Repository;
3738
import com.spotify.github.v3.repos.Status;
3839
import java.io.File;
3940
import java.io.IOException;
@@ -69,6 +70,8 @@ public class GitHubClient {
6970
static final Consumer<Response> IGNORE_RESPONSE_CONSUMER = (ignore) -> {};
7071
static final TypeReference<List<Comment>> LIST_COMMENT_TYPE_REFERENCE =
7172
new TypeReference<>() {};
73+
static final TypeReference<List<Repository>> LIST_REPOSITORY =
74+
new TypeReference<>() {};
7275
static final TypeReference<List<CommitItem>> LIST_COMMIT_TYPE_REFERENCE =
7376
new TypeReference<>() {};
7477
static final TypeReference<List<Review>> LIST_REVIEW_TYPE_REFERENCE = new TypeReference<>() {};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_STATUS_TYPE_REFERENCE;
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
28+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
2829

2930
import com.google.common.collect.ImmutableMap;
3031
import com.spotify.github.async.AsyncPage;
@@ -76,6 +77,7 @@ public class RepositoryClient {
7677
private static final String LANGUAGES_TEMPLATE = "/repos/%s/%s/languages";
7778
private static final String MERGE_TEMPLATE = "/repos/%s/%s/merges";
7879
private static final String FORK_TEMPLATE = "/repos/%s/%s/forks";
80+
private static final String LIST_REPOSITORY_TEMPLATE = "/orgs/%s/repos";
7981

8082
private final String owner;
8183
private final String repo;
@@ -140,6 +142,16 @@ public CompletableFuture<Repository> getRepository() {
140142
return github.request(path, Repository.class);
141143
}
142144

145+
/**
146+
* List all repositories in this organization.
147+
*
148+
* @return list of all repositories under organization
149+
*/
150+
public CompletableFuture<List<Repository>> listOrganizationRepositories() {
151+
final String path = String.format(LIST_REPOSITORY_TEMPLATE, owner);
152+
return github.request(path, LIST_REPOSITORY);
153+
}
154+
143155
/**
144156
* Create a webhook.
145157
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
2727
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2828
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
29+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REPOSITORY;
2930
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
3031
import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE;
3132
import static java.lang.String.format;
@@ -102,6 +103,16 @@ public void getRepository() throws Exception {
102103
assertThat(repository.fork(), is(false));
103104
}
104105

106+
@Test
107+
public void listOrganizationRepositories() throws Exception {
108+
final CompletableFuture<List<Repository>> fixture =
109+
completedFuture(json.fromJson(getFixture("list_of_repos_for_org.json"), LIST_REPOSITORY));
110+
when(github.request("/orgs/someowner/repos", LIST_REPOSITORY)).thenReturn(fixture);
111+
final List<Repository> repositories = repoClient.listOrganizationRepositories().get();
112+
assertThat(repositories.get(0).id(), is(1296269));
113+
assertThat(repositories.size(), is(1));
114+
}
115+
105116
@Test
106117
public void listCommits() throws Exception {
107118
final CompletableFuture<List<CommitItem>> fixture =
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
[
2+
{
3+
"id": 1296269,
4+
"node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5",
5+
"name": "Hello-World",
6+
"full_name": "octocat/Hello-World",
7+
"owner": {
8+
"login": "octocat",
9+
"id": 1,
10+
"node_id": "MDQ6VXNlcjE=",
11+
"avatar_url": "https://github.com/images/error/octocat_happy.gif",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/octocat",
14+
"html_url": "https://github.com/octocat",
15+
"followers_url": "https://api.github.com/users/octocat/followers",
16+
"following_url": "https://api.github.com/users/octocat/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
20+
"organizations_url": "https://api.github.com/users/octocat/orgs",
21+
"repos_url": "https://api.github.com/users/octocat/repos",
22+
"events_url": "https://api.github.com/users/octocat/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/octocat/received_events",
24+
"type": "User",
25+
"site_admin": false
26+
},
27+
"private": false,
28+
"html_url": "https://github.com/octocat/Hello-World",
29+
"description": "This your first repo!",
30+
"fork": false,
31+
"url": "https://api.github.com/repos/octocat/Hello-World",
32+
"archive_url": "http://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}",
33+
"assignees_url": "http://api.github.com/repos/octocat/Hello-World/assignees{/user}",
34+
"blobs_url": "http://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}",
35+
"branches_url": "http://api.github.com/repos/octocat/Hello-World/branches{/branch}",
36+
"collaborators_url": "http://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}",
37+
"comments_url": "http://api.github.com/repos/octocat/Hello-World/comments{/number}",
38+
"commits_url": "http://api.github.com/repos/octocat/Hello-World/commits{/sha}",
39+
"compare_url": "http://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}",
40+
"contents_url": "http://api.github.com/repos/octocat/Hello-World/contents/{+path}",
41+
"contributors_url": "http://api.github.com/repos/octocat/Hello-World/contributors",
42+
"deployments_url": "http://api.github.com/repos/octocat/Hello-World/deployments",
43+
"downloads_url": "http://api.github.com/repos/octocat/Hello-World/downloads",
44+
"events_url": "http://api.github.com/repos/octocat/Hello-World/events",
45+
"forks_url": "http://api.github.com/repos/octocat/Hello-World/forks",
46+
"git_commits_url": "http://api.github.com/repos/octocat/Hello-World/git/commits{/sha}",
47+
"git_refs_url": "http://api.github.com/repos/octocat/Hello-World/git/refs{/sha}",
48+
"git_tags_url": "http://api.github.com/repos/octocat/Hello-World/git/tags{/sha}",
49+
"git_url": "git:github.com/octocat/Hello-World.git",
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+
"notifications_url": "http://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}",
59+
"pulls_url": "http://api.github.com/repos/octocat/Hello-World/pulls{/number}",
60+
"releases_url": "http://api.github.com/repos/octocat/Hello-World/releases{/id}",
61+
"ssh_url": "git@github.com:octocat/Hello-World.git",
62+
"stargazers_url": "http://api.github.com/repos/octocat/Hello-World/stargazers",
63+
"statuses_url": "http://api.github.com/repos/octocat/Hello-World/statuses/{sha}",
64+
"subscribers_url": "http://api.github.com/repos/octocat/Hello-World/subscribers",
65+
"subscription_url": "http://api.github.com/repos/octocat/Hello-World/subscription",
66+
"tags_url": "http://api.github.com/repos/octocat/Hello-World/tags",
67+
"teams_url": "http://api.github.com/repos/octocat/Hello-World/teams",
68+
"trees_url": "http://api.github.com/repos/octocat/Hello-World/git/trees{/sha}",
69+
"clone_url": "https://github.com/octocat/Hello-World.git",
70+
"mirror_url": "git:git.example.com/octocat/Hello-World",
71+
"hooks_url": "http://api.github.com/repos/octocat/Hello-World/hooks",
72+
"svn_url": "https://svn.github.com/octocat/Hello-World",
73+
"homepage": "https://github.com",
74+
"language": null,
75+
"forks_count": 9,
76+
"stargazers_count": 80,
77+
"watchers_count": 80,
78+
"size": 108,
79+
"default_branch": "master",
80+
"open_issues_count": 0,
81+
"is_template": true,
82+
"topics": [
83+
"octocat",
84+
"atom",
85+
"electron",
86+
"api"
87+
],
88+
"has_issues": true,
89+
"has_projects": true,
90+
"has_wiki": true,
91+
"has_pages": false,
92+
"has_downloads": true,
93+
"archived": false,
94+
"disabled": false,
95+
"visibility": "public",
96+
"pushed_at": "2011-01-26T19:06:43Z",
97+
"created_at": "2011-01-26T19:01:12Z",
98+
"updated_at": "2011-01-26T19:14:43Z",
99+
"permissions": {
100+
"admin": false,
101+
"push": false,
102+
"pull": true
103+
},
104+
"template_repository": null,
105+
"temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O",
106+
"subscribers_count": 42,
107+
"network_count": 0,
108+
"license": {
109+
"key": "mit",
110+
"name": "MIT License",
111+
"spdx_id": "MIT",
112+
"url": "https://api.github.com/licenses/mit",
113+
"node_id": "MDc6TGljZW5zZW1pdA=="
114+
}
115+
}
116+
]

0 commit comments

Comments
 (0)