Skip to content

Commit 385a88c

Browse files
Lirickmarthast
andauthored
Add functionality to list matching references (#28)
Co-authored-by: marthast <marthast@spotify.com>
1 parent 5c33cf4 commit 385a88c

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222

2323
import static com.google.common.collect.ImmutableMap.of;
2424
import static com.spotify.github.v3.clients.GitHubClient.IGNORE_RESPONSE_CONSUMER;
25+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REFERENCES;
2526
import static java.lang.String.format;
2627

2728
import com.google.common.collect.ImmutableMap;
2829
import com.spotify.github.v3.git.Reference;
2930
import com.spotify.github.v3.git.Tag;
3031
import java.time.Instant;
32+
import java.util.List;
3133
import java.util.concurrent.CompletableFuture;
3234

3335
/** Reference Api client */
@@ -38,6 +40,7 @@ public class GitDataClient {
3840
private static final String TAG_URI = "/repos/%s/%s/git/tags/%s";
3941
private static final String CREATE_REFERENCE_URI = "/repos/%s/%s/git/refs";
4042
private static final String CREATE_REFERENCE_TAG = "/repos/%s/%s/git/tags";
43+
private static final String LIST_MATCHING_REFERENCES_URI = "/repos/%s/%s/git/matching-refs/%s";
4144
private final GitHubClient github;
4245
private final String owner;
4346
private final String repo;
@@ -93,6 +96,16 @@ public CompletableFuture<Tag> getTag(final String tag) {
9396
return github.request(path, Tag.class);
9497
}
9598

99+
/**
100+
* List matching references.
101+
*
102+
* @param ref reference name
103+
*/
104+
public CompletableFuture<List<Reference>> listMatchingReferences(final String ref) {
105+
final String path = format(LIST_MATCHING_REFERENCES_URI, owner, repo, ref);
106+
return github.request(path, LIST_REFERENCES);
107+
}
108+
96109
/**
97110
* Create a git branch reference. It must not include the refs/heads.
98111
*

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.spotify.github.v3.comment.Comment;
3131
import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
3232
import com.spotify.github.v3.exceptions.RequestNotOkException;
33+
import com.spotify.github.v3.git.Reference;
3334
import com.spotify.github.v3.prs.PullRequestItem;
3435
import com.spotify.github.v3.repos.Branch;
3536
import com.spotify.github.v3.repos.CommitItem;
@@ -83,6 +84,8 @@ public class GitHubClient {
8384
new TypeReference<>() {};
8485
static final TypeReference<List<Branch>> LIST_BRANCHES =
8586
new TypeReference<>() {};
87+
static final TypeReference<List<Reference>> LIST_REFERENCES =
88+
new TypeReference<>() {};
8689

8790
private static final String GET_ACCESS_TOKEN_URL = "app/installations/%s/access_tokens";
8891

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
import static com.google.common.collect.ImmutableMap.of;
2424
import static com.google.common.io.Resources.getResource;
25+
import static com.spotify.github.v3.clients.GitHubClient.LIST_REFERENCES;
2526
import static java.nio.charset.Charset.defaultCharset;
2627
import static java.util.concurrent.CompletableFuture.completedFuture;
2728
import static org.hamcrest.MatcherAssert.assertThat;
2829
import static org.hamcrest.core.Is.is;
30+
import static org.hamcrest.core.StringContains.containsString;
2931
import static org.mockito.ArgumentMatchers.any;
3032
import static org.mockito.ArgumentMatchers.eq;
3133
import static org.mockito.Mockito.mock;
@@ -38,6 +40,7 @@
3840
import com.spotify.github.v3.git.Tag;
3941
import java.io.IOException;
4042
import java.time.Instant;
43+
import java.util.List;
4144
import java.util.concurrent.CompletableFuture;
4245
import org.junit.Before;
4346
import org.junit.Test;
@@ -83,6 +86,22 @@ public void getTag() throws Exception {
8386
assertThat(tag.object().type(), is("commit"));
8487
}
8588

89+
@Test
90+
public void listMatchingReferences() throws Exception {
91+
final CompletableFuture<List<Reference>> fixture =
92+
completedFuture(json.fromJson(getFixture("reference_list.json"), LIST_REFERENCES));
93+
when(github.request(
94+
"/repos/someowner/somerepo/git/matching-refs/heads/feature",
95+
LIST_REFERENCES))
96+
.thenReturn(fixture);
97+
final List<Reference> matchingReferences = gitDataClient.listMatchingReferences("heads/feature").get();
98+
assertThat(matchingReferences.size(), is(2));
99+
for (Reference ref : matchingReferences) {
100+
assertThat(ref.ref(), containsString("heads/feature"));
101+
}
102+
}
103+
104+
86105
@Test
87106
public void createBranchReference() throws Exception {
88107
final CompletableFuture<Reference> fixture =
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[
2+
{
3+
"ref": "refs/heads/feature-a",
4+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWE=",
5+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-a",
6+
"object": {
7+
"type": "commit",
8+
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
9+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
10+
}
11+
},
12+
{
13+
"ref": "refs/heads/feature-b",
14+
"node_id": "MDM6UmVmcmVmcy9oZWFkcy9mZWF0dXJlLWI=",
15+
"url": "https://api.github.com/repos/octocat/Hello-World/git/refs/heads/feature-b",
16+
"object": {
17+
"type": "commit",
18+
"sha": "612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac",
19+
"url": "https://api.github.com/repos/octocat/Hello-World/git/commits/612077ae6dffb4d2fbd8ce0cccaa58893b07b5ac"
20+
}
21+
}
22+
]

0 commit comments

Comments
 (0)