Skip to content

Commit 95f83d1

Browse files
authored
Merge pull request #774 from martinvanzijl/issue_518_allow_getref_with_prefix
Allow "refs/" prefix in parameter to GHRepository.getRef()
2 parents 081e485 + e4c3802 commit 95f83d1

10 files changed

+383
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,11 @@ public PagedIterable<GHRef> listRefs(String refType) throws IOException {
15681568
* on failure communicating with GitHub, potentially due to an invalid ref type being requested
15691569
*/
15701570
public GHRef getRef(String refName) throws IOException {
1571+
// Also accept e.g. "refs/heads/branch" for consistency with createRef().
1572+
if (refName.startsWith("refs/")) {
1573+
refName = refName.replaceFirst("refs/", "");
1574+
}
1575+
15711576
return root.createRequest()
15721577
.withUrlPath(getApiTailUrl(String.format("git/refs/%s", refName)))
15731578
.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();
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"id": "21815889-8806-4388-8794-8d1c45a4331c",
3+
"name": "repos_github-api-test-org_temp-getrefwithprefix",
4+
"request": {
5+
"url": "/repos/github-api-test-org/temp-getRefWithPrefix",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_github-api-test-org_temp-getrefwithprefix-2.json",
16+
"headers": {
17+
"Date": "Sun, 05 Apr 2020 16:39:23 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4807",
23+
"X-RateLimit-Reset": "1586107929",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding, Accept, X-Requested-With"
28+
],
29+
"ETag": "W/\"627ee081300bc1b81768415662402fb4\"",
30+
"Last-Modified": "Sun, 05 Apr 2020 16:39:21 GMT",
31+
"X-OAuth-Scopes": "admin:org, admin:repo_hook, delete_repo, gist, repo",
32+
"X-Accepted-OAuth-Scopes": "repo",
33+
"X-GitHub-Media-Type": "unknown, github.v3",
34+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
35+
"X-Frame-Options": "deny",
36+
"X-Content-Type-Options": "nosniff",
37+
"X-XSS-Protection": "1; mode=block",
38+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
39+
"Content-Security-Policy": "default-src 'none'",
40+
"X-GitHub-Request-Id": "B856:3D6D:513335:5C5706:5E8A09B4"
41+
}
42+
},
43+
"uuid": "21815889-8806-4388-8794-8d1c45a4331c",
44+
"persistent": true,
45+
"insertionIndex": 2
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"id": "376d4598-61d9-4379-bd13-16a70c8b49ac",
3+
"name": "repos_github-api-test-org_temp-getrefwithprefix_git_refs_heads_master",
4+
"request": {
5+
"url": "/repos/github-api-test-org/temp-getRefWithPrefix/git/refs/heads/master",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_github-api-test-org_temp-getrefwithprefix_git_refs_heads_master-3.json",
16+
"headers": {
17+
"Date": "Sun, 05 Apr 2020 16:39:23 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4806",
23+
"X-RateLimit-Reset": "1586107929",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding, Accept, X-Requested-With"
28+
],
29+
"ETag": "W/\"94a0e0fde791301d3625ecb33739e457\"",
30+
"Last-Modified": "Sun, 05 Apr 2020 16:39:21 GMT",
31+
"X-Poll-Interval": "300",
32+
"X-OAuth-Scopes": "admin:org, admin:repo_hook, delete_repo, gist, repo",
33+
"X-Accepted-OAuth-Scopes": "repo",
34+
"X-GitHub-Media-Type": "unknown, github.v3",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "1; mode=block",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "B856:3D6D:513345:5C5868:5E8A09BB"
42+
}
43+
},
44+
"uuid": "376d4598-61d9-4379-bd13-16a70c8b49ac",
45+
"persistent": true,
46+
"scenarioName": "scenario-1-repos-github-api-test-org-temp-getRefWithPrefix-git-refs-heads-master",
47+
"requiredScenarioState": "Started",
48+
"newScenarioState": "scenario-1-repos-github-api-test-org-temp-getRefWithPrefix-git-refs-heads-master-2",
49+
"insertionIndex": 3
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"id": "10ebc76b-e6fe-4b0c-b544-55730bee8809",
3+
"name": "repos_github-api-test-org_temp-getrefwithprefix_git_refs_heads_master",
4+
"request": {
5+
"url": "/repos/github-api-test-org/temp-getRefWithPrefix/git/refs/heads/master",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_github-api-test-org_temp-getrefwithprefix_git_refs_heads_master-4.json",
16+
"headers": {
17+
"Date": "Sun, 05 Apr 2020 16:39:24 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"X-RateLimit-Limit": "5000",
22+
"X-RateLimit-Remaining": "4805",
23+
"X-RateLimit-Reset": "1586107930",
24+
"Cache-Control": "private, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept, Authorization, Cookie, X-GitHub-OTP",
27+
"Accept-Encoding, Accept, X-Requested-With"
28+
],
29+
"ETag": "W/\"94a0e0fde791301d3625ecb33739e457\"",
30+
"Last-Modified": "Sun, 05 Apr 2020 16:39:21 GMT",
31+
"X-Poll-Interval": "300",
32+
"X-OAuth-Scopes": "admin:org, admin:repo_hook, delete_repo, gist, repo",
33+
"X-Accepted-OAuth-Scopes": "repo",
34+
"X-GitHub-Media-Type": "unknown, github.v3",
35+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
36+
"X-Frame-Options": "deny",
37+
"X-Content-Type-Options": "nosniff",
38+
"X-XSS-Protection": "1; mode=block",
39+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
40+
"Content-Security-Policy": "default-src 'none'",
41+
"X-GitHub-Request-Id": "B856:3D6D:513353:5C5876:5E8A09BB"
42+
}
43+
},
44+
"uuid": "10ebc76b-e6fe-4b0c-b544-55730bee8809",
45+
"persistent": true,
46+
"scenarioName": "scenario-1-repos-github-api-test-org-temp-getRefWithPrefix-git-refs-heads-master",
47+
"requiredScenarioState": "scenario-1-repos-github-api-test-org-temp-getRefWithPrefix-git-refs-heads-master-2",
48+
"insertionIndex": 4
49+
}

0 commit comments

Comments
 (0)