Skip to content

Commit bd28abd

Browse files
committed
Allow "refs/" prefix in parameter to GHRepository.getRef().
Fixes #518.
1 parent 410bac2 commit bd28abd

14 files changed

+383
-443
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,6 +1559,11 @@ public PagedIterable<GHRef> listRefs(String refType) throws IOException {
15591559
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
15601560
*/
15611561
public GHRef getRef(String refName) throws IOException {
1562+
// Also accept e.g. "refs/heads/branch" for consistency with createRef().
1563+
if (refName.startsWith("refs/")) {
1564+
refName = refName.replaceFirst("refs/", "");
1565+
}
1566+
15621567
return root.createRequest()
15631568
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
15641569
.fetch(GHRef.class)

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ public void listRefs() throws Exception {
424424
assertThat(refs.get(0).getRef(), equalTo("refs/heads/master"));
425425
}
426426

427+
@Test
428+
public void getRefWithPrefix() throws Exception {
429+
GHRepository repo = getTempRepository();
430+
GHRef refWithoutPrefix = repo.getRef("heads/master");
431+
GHRef refWithPrefix = repo.getRef("refs/heads/master");
432+
assertThat(refWithoutPrefix.getRef(), equalTo(refWithPrefix.getRef()));
433+
}
434+
427435
@Test
428436
public void listRefsHeads() throws Exception {
429437
GHRepository repo = getTempRepository();

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/__files/orgs_github-api-test-org-a3d2b552-58b8-4028-b731-d00420496ca8.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/__files/repos_github-api-test-org_github-api-95ce4098-4e40-49f0-8661-0870614d5d78.json

Lines changed: 0 additions & 328 deletions
This file was deleted.

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/__files/user-f20ea960-c1f4-440c-bdc1-a605956a351a.json

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/test/resources/org/kohsuke/github/GHRepositoryTest/wiremock/addCollaborators/__files/users_jimmysombrero-6685376c-451b-486d-88cd-502af9a7c5d1.json

Lines changed: 0 additions & 45 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"id": 253282149,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkyNTMyODIxNDk=",
4+
"name": "temp-getRefWithPrefix",
5+
"full_name": "github-api-test-org/temp-getRefWithPrefix",
6+
"private": false,
7+
"owner": {
8+
"login": "github-api-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/github-api-test-org",
14+
"html_url": "https://github.com/github-api-test-org",
15+
"followers_url": "https://api.github.com/users/github-api-test-org/followers",
16+
"following_url": "https://api.github.com/users/github-api-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/github-api-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/github-api-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/github-api-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/github-api-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/github-api-test-org/repos",
22+
"events_url": "https://api.github.com/users/github-api-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/github-api-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/github-api-test-org/temp-getRefWithPrefix",
28+
"description": "A test repository for testing the github-api project: temp-getRefWithPrefix",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix",
31+
"forks_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/forks",
32+
"keys_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/teams",
35+
"hooks_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/hooks",
36+
"issue_events_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/events",
38+
"assignees_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/tags",
41+
"blobs_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/languages",
47+
"stargazers_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/stargazers",
48+
"contributors_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/contributors",
49+
"subscribers_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/subscribers",
50+
"subscription_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/subscription",
51+
"commits_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/merges",
58+
"archive_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/downloads",
60+
"issues_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/deployments",
67+
"created_at": "2020-04-05T16:39:17Z",
68+
"updated_at": "2020-04-05T16:39:21Z",
69+
"pushed_at": "2020-04-05T16:39:19Z",
70+
"git_url": "git://github.com/github-api-test-org/temp-getRefWithPrefix.git",
71+
"ssh_url": "[email protected]:github-api-test-org/temp-getRefWithPrefix.git",
72+
"clone_url": "https://github.com/github-api-test-org/temp-getRefWithPrefix.git",
73+
"svn_url": "https://github.com/github-api-test-org/temp-getRefWithPrefix",
74+
"homepage": "http://github-api.kohsuke.org/",
75+
"size": 0,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "master",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"temp_clone_token": "",
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"organization": {
105+
"login": "github-api-test-org",
106+
"id": 7544739,
107+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
108+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
109+
"gravatar_id": "",
110+
"url": "https://api.github.com/users/github-api-test-org",
111+
"html_url": "https://github.com/github-api-test-org",
112+
"followers_url": "https://api.github.com/users/github-api-test-org/followers",
113+
"following_url": "https://api.github.com/users/github-api-test-org/following{/other_user}",
114+
"gists_url": "https://api.github.com/users/github-api-test-org/gists{/gist_id}",
115+
"starred_url": "https://api.github.com/users/github-api-test-org/starred{/owner}{/repo}",
116+
"subscriptions_url": "https://api.github.com/users/github-api-test-org/subscriptions",
117+
"organizations_url": "https://api.github.com/users/github-api-test-org/orgs",
118+
"repos_url": "https://api.github.com/users/github-api-test-org/repos",
119+
"events_url": "https://api.github.com/users/github-api-test-org/events{/privacy}",
120+
"received_events_url": "https://api.github.com/users/github-api-test-org/received_events",
121+
"type": "Organization",
122+
"site_admin": false
123+
},
124+
"network_count": 0,
125+
"subscribers_count": 7
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ref": "refs/heads/master",
3+
"node_id": "MDM6UmVmMjUzMjgyMTQ5Om1hc3Rlcg==",
4+
"url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/refs/heads/master",
5+
"object": {
6+
"sha": "6d0ef2a84df4eb5222d9d1ff14203713f1833771",
7+
"type": "commit",
8+
"url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/commits/6d0ef2a84df4eb5222d9d1ff14203713f1833771"
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ref": "refs/heads/master",
3+
"node_id": "MDM6UmVmMjUzMjgyMTQ5Om1hc3Rlcg==",
4+
"url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/refs/heads/master",
5+
"object": {
6+
"sha": "6d0ef2a84df4eb5222d9d1ff14203713f1833771",
7+
"type": "commit",
8+
"url": "https://api.github.com/repos/github-api-test-org/temp-getRefWithPrefix/git/commits/6d0ef2a84df4eb5222d9d1ff14203713f1833771"
9+
}
10+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"login": "martinvanzijl",
3+
"id": 24422213,
4+
"node_id": "MDQ6VXNlcjI0NDIyMjEz",
5+
"avatar_url": "https://avatars0.githubusercontent.com/u/24422213?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/martinvanzijl",
8+
"html_url": "https://github.com/martinvanzijl",
9+
"followers_url": "https://api.github.com/users/martinvanzijl/followers",
10+
"following_url": "https://api.github.com/users/martinvanzijl/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/martinvanzijl/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/martinvanzijl/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/martinvanzijl/subscriptions",
14+
"organizations_url": "https://api.github.com/users/martinvanzijl/orgs",
15+
"repos_url": "https://api.github.com/users/martinvanzijl/repos",
16+
"events_url": "https://api.github.com/users/martinvanzijl/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/martinvanzijl/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Martin van Zijl",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": true,
26+
"bio": "My strongest language is C++, but I also know C, Java, Python and SQL.",
27+
"public_repos": 15,
28+
"public_gists": 0,
29+
"followers": 3,
30+
"following": 0,
31+
"created_at": "2016-12-07T00:18:58Z",
32+
"updated_at": "2020-04-05T03:19:50Z"
33+
}

0 commit comments

Comments
 (0)