Skip to content

Commit b76889e

Browse files
authored
Merge pull request #845 from bitwiseman/task/redo-822
Modify getRef() changes to be compatible with older GHE versions
2 parents 612800b + e6a7b64 commit b76889e

File tree

31 files changed

+179
-137
lines changed

31 files changed

+179
-137
lines changed

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.fasterxml.jackson.annotation.JsonProperty;
2727
import com.fasterxml.jackson.core.JsonParseException;
28+
import com.fasterxml.jackson.databind.JsonMappingException;
2829
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
2930
import edu.umd.cs.findbugs.annotations.CheckForNull;
3031
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -1589,10 +1590,29 @@ public GHRef getRef(String refName) throws IOException {
15891590
refName = refName.replaceFirst("refs/", "");
15901591
}
15911592

1592-
return root.createRequest()
1593-
.withUrlPath(getApiTailUrl(String.format("git/ref/%s", refName)))
1594-
.fetch(GHRef.class)
1595-
.wrap(root);
1593+
// We would expect this to use `git/ref/%s` but some versions of GHE seem to not support it
1594+
// Instead use `git/refs/%s` and check the result actually matches the ref
1595+
GHRef result = null;
1596+
try {
1597+
result = root.createRequest()
1598+
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
1599+
.fetch(GHRef.class)
1600+
.wrap(root);
1601+
} catch (IOException e) {
1602+
// If the parse exception is due to the above returning an array instead of a single ref
1603+
// that means the individual ref did not exist
1604+
if (!(e.getCause() instanceof JsonMappingException)) {
1605+
throw e;
1606+
}
1607+
}
1608+
1609+
// Verify that the ref returned is the one requested
1610+
if (result == null || !result.getRef().equals("refs/" + refName)) {
1611+
throw new GHFileNotFoundException(String.format("git/refs/%s", refName)
1612+
+ " {\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}");
1613+
}
1614+
return result;
1615+
15961616
}
15971617

15981618
/**

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ public void getRefsEmptyTags() throws Exception {
429429
assertThat(e.getMessage(),
430430
containsString(
431431
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
432-
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
433432
}
434433
}
435434

@@ -474,7 +473,6 @@ public void listRefs() throws Exception {
474473
assertThat(e.getMessage(),
475474
containsString(
476475
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
477-
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
478476
}
479477
}
480478

@@ -505,8 +503,7 @@ public void getRef() throws Exception {
505503
assertThat(e, instanceOf(GHFileNotFoundException.class));
506504
assertThat(e.getMessage(),
507505
containsString(
508-
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-single-reference\"}"));
509-
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
506+
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
510507
}
511508

512509
// git/refs/headz
@@ -517,8 +514,7 @@ public void getRef() throws Exception {
517514
assertThat(e, instanceOf(GHFileNotFoundException.class));
518515
assertThat(e.getMessage(),
519516
containsString(
520-
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-single-reference\"}"));
521-
assertThat(e.getCause(), instanceOf(FileNotFoundException.class));
517+
"{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3/git/refs/#get-a-reference\"}"));
522518
}
523519
}
524520

src/test/resources/org/kohsuke/github/AppTest/wiremock/testRef/mappings/repos_jenkinsci_jenkins_git_refs_heads_master-3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "257aa346-b2fa-4808-a587-ce26a6859bf7",
33
"name": "repos_jenkinsci_jenkins_git_refs_heads_master",
44
"request": {
5-
"url": "/repos/jenkinsci/jenkins/git/ref/heads/master",
5+
"url": "/repos/jenkinsci/jenkins/git/refs/heads/master",
66
"method": "GET",
77
"headers": {
88
"Accept": {

src/test/resources/org/kohsuke/github/GHEventPayloadTest/wiremock/pushToFork/mappings/repos_hub4j_github-api_git_refs_heads_master-3.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "79569c1e-168b-4816-930b-1ac370e164b9",
33
"name": "repos_hub4j_github-api_git_refs_heads_master",
44
"request": {
5-
"url": "/repos/hub4j/github-api/git/ref/heads/master",
5+
"url": "/repos/hub4j/github-api/git/refs/heads/master",
66
"method": "GET",
77
"headers": {
88
"Accept": {

src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/squashMerge/mappings/repos_hub4j-test-org_github-api_git_refs_heads_master-4.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "7bfa9315-c036-44af-bc3c-1e639325a8fe",
33
"name": "repos_hub4j-test-org_github-api_git_refs_heads_master",
44
"request": {
5-
"url": "/repos/hub4j-test-org/github-api/git/ref/heads/master",
5+
"url": "/repos/hub4j-test-org/github-api/git/refs/heads/master",
66
"method": "GET",
77
"headers": {
88
"Accept": {

src/test/resources/org/kohsuke/github/GHPullRequestTest/wiremock/updateContentSquashMerge/mappings/repos_hub4j-test-org_github-api_git_refs_heads_master-4.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "fa77be60-4286-4610-bff6-3ab0bac74b1f",
33
"name": "repos_hub4j-test-org_github-api_git_refs_heads_master",
44
"request": {
5-
"url": "/repos/hub4j-test-org/github-api/git/ref/heads/master",
5+
"url": "/repos/hub4j-test-org/github-api/git/refs/heads/master",
66
"method": "GET",
77
"headers": {
88
"Accept": {

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getRef/__files/orgs_hub4j-test-org-2.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,23 @@
1010
"members_url": "https://api.github.com/orgs/hub4j-test-org/members{/member}",
1111
"public_members_url": "https://api.github.com/orgs/hub4j-test-org/public_members{/member}",
1212
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
13-
"description": null,
13+
"description": "Hub4j Test Org Description (this could be null or blank too)",
14+
"name": "Hub4j Test Org Name (this could be null or blank too)",
15+
"company": null,
16+
"blog": "https://hub4j.url.io/could/be/null",
17+
"location": "Hub4j Test Org Location (this could be null or blank too)",
18+
"email": "[email protected]",
19+
"twitter_username": null,
1420
"is_verified": false,
1521
"has_organization_projects": true,
1622
"has_repository_projects": true,
17-
"public_repos": 15,
23+
"public_repos": 12,
1824
"public_gists": 0,
1925
"followers": 0,
2026
"following": 0,
2127
"html_url": "https://github.com/hub4j-test-org",
2228
"created_at": "2014-05-10T19:39:11Z",
23-
"updated_at": "2020-05-15T15:14:14Z",
29+
"updated_at": "2020-06-04T05:56:10Z",
2430
"type": "Organization",
2531
"total_private_repos": 0,
2632
"owned_private_repos": 0,
@@ -35,7 +41,7 @@
3541
"name": "free",
3642
"space": 976562499,
3743
"private_repos": 10000,
38-
"filled_seats": 17,
44+
"filled_seats": 18,
3945
"seats": 3
4046
}
4147
}

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/getRef/__files/repos_hub4j-test-org_github-api-3.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"site_admin": false
2626
},
2727
"html_url": "https://github.com/hub4j-test-org/github-api",
28-
"description": "Tricky",
28+
"description": "Resetting",
2929
"fork": true,
3030
"url": "https://api.github.com/repos/hub4j-test-org/github-api",
3131
"forks_url": "https://api.github.com/repos/hub4j-test-org/github-api/forks",
@@ -65,7 +65,7 @@
6565
"releases_url": "https://api.github.com/repos/hub4j-test-org/github-api/releases{/id}",
6666
"deployments_url": "https://api.github.com/repos/hub4j-test-org/github-api/deployments",
6767
"created_at": "2019-09-06T23:26:04Z",
68-
"updated_at": "2020-01-16T21:22:56Z",
68+
"updated_at": "2020-06-10T23:27:59Z",
6969
"pushed_at": "2020-05-20T16:22:43Z",
7070
"git_url": "git://github.com/hub4j-test-org/github-api.git",
7171
"ssh_url": "[email protected]:hub4j-test-org/github-api.git",
@@ -194,37 +194,37 @@
194194
"releases_url": "https://api.github.com/repos/hub4j/github-api/releases{/id}",
195195
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
196196
"created_at": "2010-04-19T04:13:03Z",
197-
"updated_at": "2020-05-20T22:54:46Z",
198-
"pushed_at": "2020-05-20T20:24:04Z",
197+
"updated_at": "2020-06-11T00:43:49Z",
198+
"pushed_at": "2020-06-11T00:30:13Z",
199199
"git_url": "git://github.com/hub4j/github-api.git",
200200
"ssh_url": "[email protected]:hub4j/github-api.git",
201201
"clone_url": "https://github.com/hub4j/github-api.git",
202202
"svn_url": "https://github.com/hub4j/github-api",
203203
"homepage": "https://github-api.kohsuke.org/",
204-
"size": 23100,
205-
"stargazers_count": 656,
206-
"watchers_count": 656,
204+
"size": 24124,
205+
"stargazers_count": 669,
206+
"watchers_count": 669,
207207
"language": "Java",
208208
"has_issues": true,
209209
"has_projects": true,
210210
"has_downloads": true,
211211
"has_wiki": true,
212212
"has_pages": true,
213-
"forks_count": 478,
213+
"forks_count": 481,
214214
"mirror_url": null,
215215
"archived": false,
216216
"disabled": false,
217-
"open_issues_count": 67,
217+
"open_issues_count": 64,
218218
"license": {
219219
"key": "mit",
220220
"name": "MIT License",
221221
"spdx_id": "MIT",
222222
"url": "https://api.github.com/licenses/mit",
223223
"node_id": "MDc6TGljZW5zZTEz"
224224
},
225-
"forks": 478,
226-
"open_issues": 67,
227-
"watchers": 656,
225+
"forks": 481,
226+
"open_issues": 64,
227+
"watchers": 669,
228228
"default_branch": "master"
229229
},
230230
"source": {
@@ -294,39 +294,39 @@
294294
"releases_url": "https://api.github.com/repos/hub4j/github-api/releases{/id}",
295295
"deployments_url": "https://api.github.com/repos/hub4j/github-api/deployments",
296296
"created_at": "2010-04-19T04:13:03Z",
297-
"updated_at": "2020-05-20T22:54:46Z",
298-
"pushed_at": "2020-05-20T20:24:04Z",
297+
"updated_at": "2020-06-11T00:43:49Z",
298+
"pushed_at": "2020-06-11T00:30:13Z",
299299
"git_url": "git://github.com/hub4j/github-api.git",
300300
"ssh_url": "[email protected]:hub4j/github-api.git",
301301
"clone_url": "https://github.com/hub4j/github-api.git",
302302
"svn_url": "https://github.com/hub4j/github-api",
303303
"homepage": "https://github-api.kohsuke.org/",
304-
"size": 23100,
305-
"stargazers_count": 656,
306-
"watchers_count": 656,
304+
"size": 24124,
305+
"stargazers_count": 669,
306+
"watchers_count": 669,
307307
"language": "Java",
308308
"has_issues": true,
309309
"has_projects": true,
310310
"has_downloads": true,
311311
"has_wiki": true,
312312
"has_pages": true,
313-
"forks_count": 478,
313+
"forks_count": 481,
314314
"mirror_url": null,
315315
"archived": false,
316316
"disabled": false,
317-
"open_issues_count": 67,
317+
"open_issues_count": 64,
318318
"license": {
319319
"key": "mit",
320320
"name": "MIT License",
321321
"spdx_id": "MIT",
322322
"url": "https://api.github.com/licenses/mit",
323323
"node_id": "MDc6TGljZW5zZTEz"
324324
},
325-
"forks": 478,
326-
"open_issues": 67,
327-
"watchers": 656,
325+
"forks": 481,
326+
"open_issues": 64,
327+
"watchers": 669,
328328
"default_branch": "master"
329329
},
330-
"network_count": 478,
330+
"network_count": 481,
331331
"subscribers_count": 0
332332
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"ref": "refs/heads/gh-pages",
4+
"node_id": "MDM6UmVmMjA2ODg4MjAxOnJlZnMvaGVhZHMvZ2gtcGFnZXM=",
5+
"url": "https://api.github.com/repos/hub4j-test-org/github-api/git/refs/heads/gh-pages",
6+
"object": {
7+
"sha": "4e64a0f9c3d561ab8587d2f7b03074b8745b5943",
8+
"type": "commit",
9+
"url": "https://api.github.com/repos/hub4j-test-org/github-api/git/commits/4e64a0f9c3d561ab8587d2f7b03074b8745b5943"
10+
}
11+
}
12+
]

0 commit comments

Comments
 (0)