Skip to content

Commit 26f84d3

Browse files
Lirickmarthast
andauthored
Add functionality to list branches of repository (spotify#20)
Co-authored-by: marthast <marthast@spotify.com>
1 parent 87c71db commit 26f84d3

4 files changed

Lines changed: 49 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
@@ -31,6 +31,7 @@
3131
import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
3232
import com.spotify.github.v3.exceptions.RequestNotOkException;
3333
import com.spotify.github.v3.prs.PullRequestItem;
34+
import com.spotify.github.v3.repos.Branch;
3435
import com.spotify.github.v3.repos.CommitItem;
3536
import com.spotify.github.v3.repos.FolderContent;
3637
import com.spotify.github.v3.repos.Status;
@@ -77,6 +78,8 @@ public class GitHubClient {
7778
new TypeReference<>() {};
7879
static final TypeReference<List<PullRequestItem>> LIST_PR_TYPE_REFERENCE =
7980
new TypeReference<>() {};
81+
static final TypeReference<List<Branch>> LIST_BRANCHES =
82+
new TypeReference<>() {};
8083

8184
private static final String GET_ACCESS_TOKEN_URL = "app/installations/%s/access_tokens";
8285

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
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;
27+
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2728

2829
import com.google.common.collect.ImmutableMap;
2930
import com.spotify.github.async.AsyncPage;
@@ -69,6 +70,7 @@ public class RepositoryClient {
6970
private static final String TREE_SHA_URI_TEMPLATE = "/repos/%s/%s/git/trees/%s";
7071
private static final String COMPARE_COMMIT_TEMPLATE = "/repos/%s/%s/compare/%s...%s";
7172
private static final String BRANCH_TEMPLATE = "/repos/%s/%s/branches/%s";
73+
private static final String LIST_BRANCHES_TEMPLATE = "/repos/%s/%s/branches";
7274
private static final String CREATE_COMMENT_TEMPLATE = "/repos/%s/%s/commits/%s/comments";
7375
private static final String COMMENT_TEMPLATE = "/repos/%s/%s/comments/%s";
7476
private static final String LANGUAGES_TEMPLATE = "/repos/%s/%s/languages";
@@ -342,6 +344,16 @@ public CompletableFuture<Branch> getBranch(final String branch) {
342344
return github.request(path, Branch.class);
343345
}
344346

347+
/**
348+
* Get a specific branch.
349+
*
350+
* @return list of all branches in repository
351+
*/
352+
public CompletableFuture<List<Branch>> listBranches() {
353+
final String path = String.format(LIST_BRANCHES_TEMPLATE, owner, repo);
354+
return github.request(path, LIST_BRANCHES);
355+
}
356+
345357
/**
346358
* Delete a comment for a given id.
347359
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.spotify.github.FixtureHelper.loadFixture;
2525
import static com.spotify.github.v3.UserTest.assertUser;
2626
import static com.spotify.github.v3.clients.GitHubClient.LIST_COMMIT_TYPE_REFERENCE;
27+
import static com.spotify.github.v3.clients.GitHubClient.LIST_BRANCHES;
2728
import static com.spotify.github.v3.clients.GitHubClient.LIST_FOLDERCONTENT_TYPE_REFERENCE;
2829
import static com.spotify.github.v3.clients.MockHelper.createMockResponse;
2930
import static com.spotify.github.v3.clients.RepositoryClient.STATUS_URI_TEMPLATE;
@@ -198,6 +199,18 @@ public void getBranch() throws Exception {
198199
assertThat(branch.commit().sha(), is("6dcb09b5b57875f334f61aebed695e2e4193db5e"));
199200
}
200201

202+
@Test
203+
public void listBranches() throws Exception {
204+
final CompletableFuture<List<Branch>> fixture =
205+
completedFuture(json.fromJson(getFixture("list_branches.json"), LIST_BRANCHES));
206+
when(github.request("/repos/someowner/somerepo/branches", LIST_BRANCHES))
207+
.thenReturn(fixture);
208+
final List<Branch> branches = repoClient.listBranches().get();
209+
assertThat(branches.get(0).commit().sha(), is("c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"));
210+
assertThat(branches.size(), is(1));
211+
}
212+
213+
201214
@Test
202215
public void testCommentCreated() throws IOException {
203216
final String expectedRequestBody = json.toJsonUnchecked(ImmutableMap.of("body", "Me too"));
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[
2+
{
3+
"name": "master",
4+
"commit": {
5+
"sha": "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc",
6+
"url": "https://api.github.com/repos/octocat/Hello-World/commits/c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc"
7+
},
8+
"protected": true,
9+
"protection": {
10+
"enabled": true,
11+
"required_status_checks": {
12+
"enforcement_level": "non_admins",
13+
"contexts": [
14+
"ci-test",
15+
"linter"
16+
]
17+
}
18+
},
19+
"protection_url": "https://api.github.com/repos/octocat/hello-world/branches/master/protection"
20+
}
21+
]

0 commit comments

Comments
 (0)