Skip to content

Commit

Permalink
Fixed getRef() to use git/ref endpoint instead of git/refs
Browse files Browse the repository at this point in the history
Fixes hub4j#794
  • Loading branch information
bitwiseman committed May 21, 2020
1 parent ba12efe commit 2badb61
Show file tree
Hide file tree
Showing 51 changed files with 1,698 additions and 473 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,9 @@ public GHRef[] getRefs(String refType) throws IOException {
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
*/
public PagedIterable<GHRef> listRefs(String refType) throws IOException {
if (refType.startsWith("refs/")) {
refType = refType.replaceFirst("refs/", "");
}
final String url = String.format("/repos/%s/%s/git/refs/%s", getOwnerName(), name, refType);
return root.createRequest().withUrlPath(url).toIterable(GHRef[].class, item -> item.wrap(root));
}
Expand All @@ -1585,7 +1588,7 @@ public GHRef getRef(String refName) throws IOException {
}

return root.createRequest()
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
.withUrlPath(getApiTailUrl(String.format("git/ref/%s", refName)))
.fetch(GHRef.class)
.wrap(root);
}
Expand Down
94 changes: 84 additions & 10 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kohsuke.github;

import com.fasterxml.jackson.databind.JsonMappingException;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

Expand Down Expand Up @@ -434,19 +435,92 @@ public void getRefsEmptyTags() throws Exception {

@Test
public void listRefs() throws Exception {
GHRepository repo = getTempRepository();
List<GHRef> refs = repo.listRefs().toList();
assertThat(refs, notNullValue());
assertThat(refs.size(), equalTo(1));
assertThat(refs.get(0).getRef(), equalTo("refs/heads/master"));
GHRepository repo = getRepository();

List<GHRef> ghRefs;

// handle refs/*
ghRefs = repo.listRefs("heads").toList();
List<GHRef> ghRefsWithPrefix = repo.listRefs("refs/heads").toList();

assertThat(ghRefs, notNullValue());
assertThat(ghRefs.size(), greaterThan(3));
assertThat(ghRefs.get(0).getRef(), equalTo("refs/heads/changes"));
assertThat(ghRefsWithPrefix.size(), equalTo(ghRefs.size()));
assertThat(ghRefsWithPrefix.get(0).getRef(), equalTo(ghRefs.get(0).getRef()));

// git/refs/heads/gh-pages
// passing a specific ref to listRefs will fail to parse due to returning a single item not an array
try {
ghRefs = repo.listRefs("heads/gh-pages").toList();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(HttpException.class));
assertThat(e.getCause(), instanceOf(IOException.class));
assertThat(e.getCause().getCause(), instanceOf(JsonMappingException.class));
}

// git/refs/heads/gh
ghRefs = repo.listRefs("heads/gh").toList();
assertThat(ghRefs, notNullValue());
assertThat(ghRefs.size(), equalTo(1));
assertThat(ghRefs.get(0).getRef(), equalTo("refs/heads/gh-pages"));

// git/refs/headz
try {
ghRefs = repo.listRefs("headz").toList();
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
}
}

@Test
public void getRefWithPrefix() throws Exception {
GHRepository repo = getTempRepository();
GHRef refWithoutPrefix = repo.getRef("heads/master");
GHRef refWithPrefix = repo.getRef("refs/heads/master");
assertThat(refWithoutPrefix.getRef(), equalTo(refWithPrefix.getRef()));
public void getRef() throws Exception {
GHRepository repo = getRepository();

GHRef ghRef;

// handle refs/*
ghRef = repo.getRef("heads/gh-pages");
GHRef ghRefWithPrefix = repo.getRef("refs/heads/gh-pages");

assertThat(ghRef, notNullValue());
assertThat(ghRef.getRef(), equalTo("refs/heads/gh-pages"));
assertThat(ghRefWithPrefix.getRef(), equalTo(ghRef.getRef()));

// git/refs/heads/gh-pages
ghRef = repo.getRef("heads/gh-pages");
assertThat(ghRef, notNullValue());
assertThat(ghRef.getRef(), equalTo("refs/heads/gh-pages"));

// git/refs/heads/gh
try {
ghRef = repo.getRef("heads/gh");
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-single-reference\"}"));
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
}

// git/refs/headz
try {
ghRef = repo.getRef("headz");
fail();
} catch (Exception e) {
assertThat(e, instanceOf(GHFileNotFoundException.class));
assertThat(e.getMessage(),
containsString(
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-single-reference\"}"));
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "257aa346-b2fa-4808-a587-ce26a6859bf7",
"name": "repos_jenkinsci_jenkins_git_refs_heads_master",
"request": {
"url": "/repos/jenkinsci/jenkins/git/refs/heads/master",
"url": "/repos/jenkinsci/jenkins/git/ref/heads/master",
"method": "GET",
"headers": {
"Accept": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "7bfa9315-c036-44af-bc3c-1e639325a8fe",
"name": "repos_hub4j-test-org_github-api_git_refs_heads_master",
"request": {
"url": "/repos/hub4j-test-org/github-api/git/refs/heads/master",
"url": "/repos/hub4j-test-org/github-api/git/ref/heads/master",
"method": "GET",
"headers": {
"Accept": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "fa77be60-4286-4610-bff6-3ab0bac74b1f",
"name": "repos_hub4j-test-org_github-api_git_refs_heads_master",
"request": {
"url": "/repos/hub4j-test-org/github-api/git/refs/heads/master",
"url": "/repos/hub4j-test-org/github-api/git/ref/heads/master",
"method": "GET",
"headers": {
"Accept": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"url": "https://api.github.com/orgs/hub4j-test-org",
"repos_url": "https://api.github.com/orgs/hub4j-test-org/repos",
"events_url": "https://api.github.com/orgs/hub4j-test-org/events",
"hooks_url": "https://api.github.com/orgs/hub4j-test-org/hooks",
"issues_url": "https://api.github.com/orgs/hub4j-test-org/issues",
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
"description": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 15,
"public_gists": 0,
"followers": 0,
"following": 0,
"html_url": "https://github.com/hub4j-test-org",
"created_at": "2014-05-10T19:39:11Z",
"updated_at": "2020-05-15T15:14:14Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
"private_gists": 0,
"disk_usage": 148,
"collaborators": 0,
"billing_email": "kk@kohsuke.org",
"default_repository_permission": "none",
"members_can_create_repositories": false,
"two_factor_requirement_enabled": false,
"plan": {
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 17,
"seats": 3
}
}
Loading

0 comments on commit 2badb61

Please sign in to comment.