Skip to content

Commit 3ecd46e

Browse files
feat: Added matching-ref API (Issue #2155) (#2195)
* feat: Added matching-ref API. [2155](#2155) * feat: Added matching-ref API. [2155](#2155) * feat: Added matching-ref API. [2155](#2155) * feat: Added matching-ref API. [2155](#2155) --------- Co-authored-by: rball11 <ravikumar.balla3@aexp.com>
1 parent b888639 commit 3ecd46e

File tree

15 files changed

+834
-0
lines changed

15 files changed

+834
-0
lines changed

src/main/java/org/kohsuke/github/GHRef.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ static PagedIterable<GHRef> readMatching(GHRepository repository, String refType
126126
return repository.root().createRequest().withUrlPath(url).toIterable(GHRef[].class, item -> repository.root());
127127
}
128128

129+
/**
130+
* Retrieves all refs that match the given prefix using the matching-refs endpoint.
131+
*
132+
* @param repository
133+
* the repository to read from
134+
* @param refPrefix
135+
* the ref prefix to search for e.g. <code>heads/main</code> or <code>tags/v1</code>
136+
* @return paged iterable of all refs matching the specified prefix
137+
*/
138+
static PagedIterable<GHRef> readMatchingRefs(GHRepository repository, String refPrefix) {
139+
if (refPrefix.startsWith("refs/")) {
140+
refPrefix = refPrefix.replaceFirst("refs/", "");
141+
}
142+
143+
String url = repository.getApiTailUrl(String.format("git/matching-refs/%s", refPrefix));
144+
return repository.root().createRequest().withUrlPath(url).toIterable(GHRef[].class, item -> repository.root());
145+
}
146+
129147
private GHObject object;
130148

131149
private String ref, url;

src/main/java/org/kohsuke/github/GHRepository.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,18 @@ public Map<String, Long> listLanguages() throws IOException {
28142814
return result;
28152815
}
28162816

2817+
/**
2818+
* Retrieves all refs that match the given prefix using the matching-refs endpoint. This is useful to avoid fetching
2819+
* all available refs.
2820+
*
2821+
* @param refPrefix
2822+
* the ref prefix to match e.g. <code>heads/main</code> or <code>tags/v1</code>
2823+
* @return paged iterable of all refs matching the specified prefix
2824+
*/
2825+
public PagedIterable<GHRef> listMatchingRefs(String refPrefix) {
2826+
return GHRef.readMatchingRefs(this, refPrefix);
2827+
}
2828+
28172829
/**
28182830
* Lists up all the milestones in this repository.
28192831
*

src/test/java/org/kohsuke/github/GHRepositoryTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,35 @@ public void listLanguages() throws IOException {
10091009
assertThat(languages.get("Java"), greaterThan(100000L));
10101010
}
10111011

1012+
/**
1013+
* List matching refs.
1014+
*
1015+
* @throws Exception
1016+
* the exception
1017+
*/
1018+
@Test
1019+
public void listMatchingRefs() throws Exception {
1020+
GHRepository repo = getRepository();
1021+
List<GHRef> refs;
1022+
1023+
// Test listing refs matching a prefix
1024+
refs = repo.listMatchingRefs("heads").toList();
1025+
assertThat(refs, notNullValue());
1026+
assertThat(refs.size(), greaterThan(3));
1027+
assertThat(refs.get(0).getRef(), equalTo("refs/heads/changes"));
1028+
1029+
// Test with refs/ prefix
1030+
List<GHRef> refsWithPrefix = repo.listMatchingRefs("refs/heads").toList();
1031+
assertThat(refsWithPrefix.size(), equalTo(refs.size()));
1032+
assertThat(refsWithPrefix.get(0).getRef(), equalTo(refs.get(0).getRef()));
1033+
1034+
// Test with a more specific prefix
1035+
refs = repo.listMatchingRefs("heads/gh").toList();
1036+
assertThat(refs, notNullValue());
1037+
assertThat(refs.size(), equalTo(1));
1038+
assertThat(refs.get(0).getRef(), equalTo("refs/heads/gh-pages"));
1039+
}
1040+
10121041
/**
10131042
* List refs.
10141043
*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/bitwiseman",
8+
"html_url": "https://github.com/bitwiseman",
9+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
10+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
14+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
15+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
16+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Liam Newman",
21+
"company": "Cloudbees, Inc.",
22+
"blog": "",
23+
"location": "Seattle, WA, USA",
24+
"email": "bitwiseman@gmail.com",
25+
"hireable": null,
26+
"bio": "https://twitter.com/bitwiseman",
27+
"public_repos": 183,
28+
"public_gists": 7,
29+
"followers": 159,
30+
"following": 9,
31+
"created_at": "2012-07-11T20:38:33Z",
32+
"updated_at": "2020-05-20T16:02:42Z",
33+
"private_gists": 19,
34+
"total_private_repos": 12,
35+
"owned_private_repos": 0,
36+
"disk_usage": 33697,
37+
"collaborators": 0,
38+
"two_factor_authentication": true,
39+
"plan": {
40+
"name": "free",
41+
"space": 976562499,
42+
"collaborators": 0,
43+
"private_repos": 10000
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"login": "hub4j-test-org",
3+
"id": 7544739,
4+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
5+
"url": "https://api.github.com/orgs/hub4j-test-org",
6+
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
7+
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
8+
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
9+
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
10+
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
11+
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
12+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13+
"description": null,
14+
"is_verified": false,
15+
"has_organization_projects": true,
16+
"has_repository_projects": true,
17+
"public_repos": 15,
18+
"public_gists": 0,
19+
"followers": 0,
20+
"following": 0,
21+
"html_url": "https://github.com/hub4j-test-org",
22+
"created_at": "2014-05-10T19:39:11Z",
23+
"updated_at": "2020-05-15T15:14:14Z",
24+
"type": "Organization",
25+
"total_private_repos": 0,
26+
"owned_private_repos": 0,
27+
"private_gists": 0,
28+
"disk_usage": 148,
29+
"collaborators": 0,
30+
"billing_email": "kk@kohsuke.org",
31+
"default_repository_permission": "none",
32+
"members_can_create_repositories": false,
33+
"two_factor_requirement_enabled": false,
34+
"plan": {
35+
"name": "free",
36+
"space": 976562499,
37+
"private_repos": 10000,
38+
"filled_seats": 17,
39+
"seats": 3
40+
}
41+
}

0 commit comments

Comments
 (0)