Skip to content

Commit

Permalink
Merge pull request #845 from bitwiseman/task/redo-822
Browse files Browse the repository at this point in the history
Modify getRef() changes to be compatible with older GHE versions
  • Loading branch information
bitwiseman committed Jun 11, 2020
2 parents 612800b + e6a7b64 commit b76889e
Show file tree
Hide file tree
Showing 31 changed files with 179 additions and 137 deletions.
28 changes: 24 additions & 4 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -1589,10 +1590,29 @@ public GHRef getRef(String refName) throws IOException {
refName = refName.replaceFirst("refs/", "");
}

return root.createRequest()
.withUrlPath(getApiTailUrl(String.format("git/ref/%s", refName)))
.fetch(GHRef.class)
.wrap(root);
// We would expect this to use `git/ref/%s` but some versions of GHE seem to not support it
// Instead use `git/refs/%s` and check the result actually matches the ref
GHRef result = null;
try {
result = root.createRequest()
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
.fetch(GHRef.class)
.wrap(root);
} catch (IOException e) {
// If the parse exception is due to the above returning an array instead of a single ref
// that means the individual ref did not exist
if (!(e.getCause() instanceof JsonMappingException)) {
throw e;
}
}

// Verify that the ref returned is the one requested
if (result == null || !result.getRef().equals("refs/" + refName)) {
throw new GHFileNotFoundException(String.format("git/refs/%s", refName)
+ " {\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}");
}
return result;

}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ public void getRefsEmptyTags() throws Exception {
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));
}
}

Expand Down Expand Up @@ -474,7 +473,6 @@ public void listRefs() throws Exception {
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));
}
}

Expand Down Expand Up @@ -505,8 +503,7 @@ public void getRef() throws Exception {
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));
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}

// git/refs/headz
Expand All @@ -517,8 +514,7 @@ public void getRef() throws Exception {
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));
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
}
}

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/ref/heads/master",
"url": "/repos/jenkinsci/jenkins/git/refs/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": "79569c1e-168b-4816-930b-1ac370e164b9",
"name": "repos_hub4j_github-api_git_refs_heads_master",
"request": {
"url": "/repos/hub4j/github-api/git/ref/heads/master",
"url": "/repos/hub4j/github-api/git/refs/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/ref/heads/master",
"url": "/repos/hub4j-test-org/github-api/git/refs/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/ref/heads/master",
"url": "/repos/hub4j-test-org/github-api/git/refs/heads/master",
"method": "GET",
"headers": {
"Accept": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@
"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,
"description": "Hub4j Test Org Description (this could be null or blank too)",
"name": "Hub4j Test Org Name (this could be null or blank too)",
"company": null,
"blog": "https://hub4j.url.io/could/be/null",
"location": "Hub4j Test Org Location (this could be null or blank too)",
"email": "hub4jtestorgemail@could.be.null.com",
"twitter_username": null,
"is_verified": false,
"has_organization_projects": true,
"has_repository_projects": true,
"public_repos": 15,
"public_repos": 12,
"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",
"updated_at": "2020-06-04T05:56:10Z",
"type": "Organization",
"total_private_repos": 0,
"owned_private_repos": 0,
Expand All @@ -35,7 +41,7 @@
"name": "free",
"space": 976562499,
"private_repos": 10000,
"filled_seats": 17,
"filled_seats": 18,
"seats": 3
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"site_admin": false
},
"html_url": "https://github.com/hub4j-test-org/github-api",
"description": "Tricky",
"description": "Resetting",
"fork": true,
"url": "https://api.github.com/repos/hub4j-test-org/github-api",
"forks_url": "https://api.github.com/repos/hub4j-test-org/github-api/forks",
Expand Down Expand Up @@ -65,7 +65,7 @@
"releases_url": "https://api.github.com/repos/hub4j-test-org/github-api/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments",
"created_at": "2019-09-06T23:26:04Z",
"updated_at": "2020-01-16T21:22:56Z",
"updated_at": "2020-06-10T23:27:59Z",
"pushed_at": "2020-05-20T16:22:43Z",
"git_url": "git://github.com/hub4j-test-org/github-api.git",
"ssh_url": "git@github.com:hub4j-test-org/github-api.git",
Expand Down Expand Up @@ -194,37 +194,37 @@
"releases_url": "https://api.github.com/repos/hub4j/github-api/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
"created_at": "2010-04-19T04:13:03Z",
"updated_at": "2020-05-20T22:54:46Z",
"pushed_at": "2020-05-20T20:24:04Z",
"updated_at": "2020-06-11T00:43:49Z",
"pushed_at": "2020-06-11T00:30:13Z",
"git_url": "git://github.com/hub4j/github-api.git",
"ssh_url": "git@github.com:hub4j/github-api.git",
"clone_url": "https://github.com/hub4j/github-api.git",
"svn_url": "https://github.com/hub4j/github-api",
"homepage": "https://github-api.kohsuke.org/",
"size": 23100,
"stargazers_count": 656,
"watchers_count": 656,
"size": 24124,
"stargazers_count": 669,
"watchers_count": 669,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 478,
"forks_count": 481,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 67,
"open_issues_count": 64,
"license": {
"key": "mit",
"name": "MIT License",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 478,
"open_issues": 67,
"watchers": 656,
"forks": 481,
"open_issues": 64,
"watchers": 669,
"default_branch": "master"
},
"source": {
Expand Down Expand Up @@ -294,39 +294,39 @@
"releases_url": "https://api.github.com/repos/hub4j/github-api/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
"created_at": "2010-04-19T04:13:03Z",
"updated_at": "2020-05-20T22:54:46Z",
"pushed_at": "2020-05-20T20:24:04Z",
"updated_at": "2020-06-11T00:43:49Z",
"pushed_at": "2020-06-11T00:30:13Z",
"git_url": "git://github.com/hub4j/github-api.git",
"ssh_url": "git@github.com:hub4j/github-api.git",
"clone_url": "https://github.com/hub4j/github-api.git",
"svn_url": "https://github.com/hub4j/github-api",
"homepage": "https://github-api.kohsuke.org/",
"size": 23100,
"stargazers_count": 656,
"watchers_count": 656,
"size": 24124,
"stargazers_count": 669,
"watchers_count": 669,
"language": "Java",
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": true,
"forks_count": 478,
"forks_count": 481,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 67,
"open_issues_count": 64,
"license": {
"key": "mit",
"name": "MIT License",
"spdx_id": "MIT",
"url": "https://api.github.com/licenses/mit",
"node_id": "MDc6TGljZW5zZTEz"
},
"forks": 478,
"open_issues": 67,
"watchers": 656,
"forks": 481,
"open_issues": 64,
"watchers": 669,
"default_branch": "master"
},
"network_count": 478,
"network_count": 481,
"subscribers_count": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"ref": "refs/heads/gh-pages",
"node_id": "MDM6UmVmMjA2ODg4MjAxOnJlZnMvaGVhZHMvZ2gtcGFnZXM=",
"url": "https://api.github.com/repos/hub4j-test-org/github-api/git/refs/heads/gh-pages",
"object": {
"sha": "4e64a0f9c3d561ab8587d2f7b03074b8745b5943",
"type": "commit",
"url": "https://api.github.com/repos/hub4j-test-org/github-api/git/commits/4e64a0f9c3d561ab8587d2f7b03074b8745b5943"
}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@
"email": "bitwiseman@gmail.com",
"hireable": null,
"bio": "https://twitter.com/bitwiseman",
"public_repos": 183,
"twitter_username": null,
"public_repos": 187,
"public_gists": 7,
"followers": 159,
"followers": 161,
"following": 9,
"created_at": "2012-07-11T20:38:33Z",
"updated_at": "2020-05-20T16:02:42Z",
"updated_at": "2020-05-29T18:24:44Z",
"private_gists": 19,
"total_private_repos": 12,
"owned_private_repos": 0,
"disk_usage": 33697,
"disk_usage": 33700,
"collaborators": 0,
"two_factor_authentication": true,
"plan": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"id": "865b9946-8a74-4402-971b-ecd97db742e3",
"id": "93493987-1167-42ca-9ce1-668c3697700e",
"name": "orgs_hub4j-test-org",
"request": {
"url": "/orgs/hub4j-test-org",
Expand All @@ -14,21 +14,21 @@
"status": 200,
"bodyFileName": "orgs_hub4j-test-org-2.json",
"headers": {
"Date": "Wed, 20 May 2020 23:48:56 GMT",
"Date": "Thu, 11 Jun 2020 02:20:47 GMT",
"Content-Type": "application/json; charset=utf-8",
"Server": "GitHub.com",
"Status": "200 OK",
"X-RateLimit-Limit": "5000",
"X-RateLimit-Remaining": "4952",
"X-RateLimit-Reset": "1590020148",
"X-RateLimit-Remaining": "4917",
"X-RateLimit-Reset": "1591843209",
"Cache-Control": "private, max-age=60, s-maxage=60",
"Vary": [
"Accept, Authorization, Cookie, X-GitHub-OTP",
"Accept-Encoding, Accept, X-Requested-With",
"Accept-Encoding"
],
"ETag": "W/\"de5da0827fbd925dd0dde9d2d6a85f45\"",
"Last-Modified": "Fri, 15 May 2020 15:14:14 GMT",
"ETag": "W/\"6bd323dd4ab2a01dae2464621246c3cd\"",
"Last-Modified": "Thu, 04 Jun 2020 05:56:10 GMT",
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
"X-Accepted-OAuth-Scopes": "admin:org, read:org, repo, user, write:org",
"X-GitHub-Media-Type": "unknown, github.v3",
Expand All @@ -38,10 +38,10 @@
"X-XSS-Protection": "1; mode=block",
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
"Content-Security-Policy": "default-src 'none'",
"X-GitHub-Request-Id": "EFC6:5429:F7742:126AA2:5EC5C1E7"
"X-GitHub-Request-Id": "E5B0:5D6A:3A9A6:47E29:5EE194FE"
}
},
"uuid": "865b9946-8a74-4402-971b-ecd97db742e3",
"uuid": "93493987-1167-42ca-9ce1-668c3697700e",
"persistent": true,
"insertionIndex": 2
}
Loading

0 comments on commit b76889e

Please sign in to comment.