Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape special characters in branch URLs #480

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -51,6 +53,8 @@
import java.util.WeakHashMap;

import static java.util.Arrays.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.kohsuke.github.Previews.*;

/**
Expand Down Expand Up @@ -1393,8 +1397,25 @@ public Map<String,GHBranch> getBranches() throws IOException {
return r;
}

/**
* Replace special characters (e.g. #) with standard values (e.g. %23) so
* GitHub understands what is being requested.
* @param The string to be encoded.
* @return The encoded string.
*/
private String UrlEncode(String value) {
try {
return URLEncoder.encode(value, org.apache.commons.codec.CharEncoding.UTF_8);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(GHRepository.class.getName()).log(Level.SEVERE, null, ex);
}

// Something went wrong - just return original value as is.
return value;
}

public GHBranch getBranch(String name) throws IOException {
return root.retrieve().to(getApiTailUrl("branches/"+name),GHBranch.class).wrap(this);
return root.retrieve().to(getApiTailUrl("branches/"+UrlEncode(name)),GHBranch.class).wrap(this);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/kohsuke/github/GHRepositoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ public void archive() throws Exception {
assertThat(getRepository().isArchived(), is(true));
}

@Test
public void getBranch_URLEncoded() throws Exception {
GHRepository repo = getRepository();
GHBranch branch = repo.getBranch("test/#UrlEncode");
assertThat(branch.getName(), is("test/#UrlEncode"));
}



protected GHRepository getRepository() throws IOException {
return getRepository(gitHub);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"login":"github-api-test-org","id":7544739,"node_id":"MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=","url":"https://api.github.com/orgs/github-api-test-org","repos_url":"https://api.github.com/orgs/github-api-test-org/repos","events_url":"https://api.github.com/orgs/github-api-test-org/events","hooks_url":"https://api.github.com/orgs/github-api-test-org/hooks","issues_url":"https://api.github.com/orgs/github-api-test-org/issues","members_url":"https://api.github.com/orgs/github-api-test-org/members{/member}","public_members_url":"https://api.github.com/orgs/github-api-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":9,"public_gists":0,"followers":0,"following":0,"html_url":"https://github.com/github-api-test-org","created_at":"2014-05-10T19:39:11Z","updated_at":"2015-04-20T00:42:30Z","type":"Organization","total_private_repos":0,"owned_private_repos":0,"private_gists":0,"disk_usage":132,"collaborators":0,"billing_email":"[email protected]","default_repository_permission":"none","members_can_create_repositories":false,"two_factor_requirement_enabled":false,"members_allowed_repository_creation_type":"none","plan":{"name":"free","space":976562499,"private_repos":0,"filled_seats":3,"seats":0}}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"test/#UrlEncode","commit":{"sha":"14fa3698221f91613b9e1d809434326e5ed546af","node_id":"MDY6Q29tbWl0MjA2ODg4MjAxOjE0ZmEzNjk4MjIxZjkxNjEzYjllMWQ4MDk0MzQzMjZlNWVkNTQ2YWY=","commit":{"author":{"name":"Liam Newman","email":"[email protected]","date":"2019-09-06T23:38:04Z"},"committer":{"name":"Liam Newman","email":"[email protected]","date":"2019-09-07T00:09:09Z"},"message":"Update README","tree":{"sha":"f791b2c2752a83ddd7604a800800b18e7c1d0196","url":"https://api.github.com/repos/github-api-test-org/github-api/git/trees/f791b2c2752a83ddd7604a800800b18e7c1d0196"},"url":"https://api.github.com/repos/github-api-test-org/github-api/git/commits/14fa3698221f91613b9e1d809434326e5ed546af","comment_count":0,"verification":{"verified":false,"reason":"unsigned","signature":null,"payload":null}},"url":"https://api.github.com/repos/github-api-test-org/github-api/commits/14fa3698221f91613b9e1d809434326e5ed546af","html_url":"https://github.com/github-api-test-org/github-api/commit/14fa3698221f91613b9e1d809434326e5ed546af","comments_url":"https://api.github.com/repos/github-api-test-org/github-api/commits/14fa3698221f91613b9e1d809434326e5ed546af/comments","author":{"login":"bitwiseman","id":1958953,"node_id":"MDQ6VXNlcjE5NTg5NTM=","avatar_url":"https://avatars3.githubusercontent.com/u/1958953?v=4","gravatar_id":"","url":"https://api.github.com/users/bitwiseman","html_url":"https://github.com/bitwiseman","followers_url":"https://api.github.com/users/bitwiseman/followers","following_url":"https://api.github.com/users/bitwiseman/following{/other_user}","gists_url":"https://api.github.com/users/bitwiseman/gists{/gist_id}","starred_url":"https://api.github.com/users/bitwiseman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitwiseman/subscriptions","organizations_url":"https://api.github.com/users/bitwiseman/orgs","repos_url":"https://api.github.com/users/bitwiseman/repos","events_url":"https://api.github.com/users/bitwiseman/events{/privacy}","received_events_url":"https://api.github.com/users/bitwiseman/received_events","type":"User","site_admin":false},"committer":{"login":"bitwiseman","id":1958953,"node_id":"MDQ6VXNlcjE5NTg5NTM=","avatar_url":"https://avatars3.githubusercontent.com/u/1958953?v=4","gravatar_id":"","url":"https://api.github.com/users/bitwiseman","html_url":"https://github.com/bitwiseman","followers_url":"https://api.github.com/users/bitwiseman/followers","following_url":"https://api.github.com/users/bitwiseman/following{/other_user}","gists_url":"https://api.github.com/users/bitwiseman/gists{/gist_id}","starred_url":"https://api.github.com/users/bitwiseman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitwiseman/subscriptions","organizations_url":"https://api.github.com/users/bitwiseman/orgs","repos_url":"https://api.github.com/users/bitwiseman/repos","events_url":"https://api.github.com/users/bitwiseman/events{/privacy}","received_events_url":"https://api.github.com/users/bitwiseman/received_events","type":"User","site_admin":false},"parents":[{"sha":"3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353","url":"https://api.github.com/repos/github-api-test-org/github-api/commits/3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353","html_url":"https://github.com/github-api-test-org/github-api/commit/3a09d2de4a9a1322a0ba2c3e2f54a919ca8fe353"}]},"_links":{"self":"https://api.github.com/repos/github-api-test-org/github-api/branches/test/#UrlEncode","html":"https://github.com/github-api-test-org/github-api/tree/test/#UrlEncode"},"protected":false,"protection":{"enabled":false,"required_status_checks":{"enforcement_level":"off","contexts":[]}},"protection_url":"https://api.github.com/repos/github-api-test-org/github-api/branches/test/#UrlEncode/protection"}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"login":"bitwiseman","id":1958953,"node_id":"MDQ6VXNlcjE5NTg5NTM=","avatar_url":"https://avatars3.githubusercontent.com/u/1958953?v=4","gravatar_id":"","url":"https://api.github.com/users/bitwiseman","html_url":"https://github.com/bitwiseman","followers_url":"https://api.github.com/users/bitwiseman/followers","following_url":"https://api.github.com/users/bitwiseman/following{/other_user}","gists_url":"https://api.github.com/users/bitwiseman/gists{/gist_id}","starred_url":"https://api.github.com/users/bitwiseman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bitwiseman/subscriptions","organizations_url":"https://api.github.com/users/bitwiseman/orgs","repos_url":"https://api.github.com/users/bitwiseman/repos","events_url":"https://api.github.com/users/bitwiseman/events{/privacy}","received_events_url":"https://api.github.com/users/bitwiseman/received_events","type":"User","site_admin":false,"name":"Liam Newman","company":"Cloudbees, Inc.","blog":"","location":"Seattle, WA, USA","email":"[email protected]","hireable":null,"bio":"https://twitter.com/bitwiseman","public_repos":166,"public_gists":4,"followers":135,"following":9,"created_at":"2012-07-11T20:38:33Z","updated_at":"2019-09-24T19:32:29Z"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id" : "5e8ae8ed-0988-4df2-89d3-eda4c6bdc60d",
"name" : "orgs_github-api-test-org",
"request" : {
"url" : "/orgs/github-api-test-org",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "orgs_github-api-test-org-5e8ae8ed-0988-4df2-89d3-eda4c6bdc60d.json",
"headers" : {
"Date" : "Thu, 26 Sep 2019 00:11:04 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Server" : "GitHub.com",
"Status" : "200 OK",
"X-RateLimit-Limit" : "5000",
"X-RateLimit-Remaining" : "4992",
"X-RateLimit-Reset" : "1569460192",
"Cache-Control" : "private, max-age=60, s-maxage=60",
"Vary" : [ "Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding" ],
"ETag" : "W/\"b7989d48e6539c9c76038995b902421b\"",
"Last-Modified" : "Mon, 20 Apr 2015 00:42:30 GMT",
"X-OAuth-Scopes" : "gist, notifications, read:org, read:public_key, read:repo_hook, repo",
"X-Accepted-OAuth-Scopes" : "admin:org, read:org, repo, user, write:org",
"X-GitHub-Media-Type" : "unknown, github.v3",
"Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin" : "*",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options" : "deny",
"X-Content-Type-Options" : "nosniff",
"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" : "FD24:3764:2322DB:2A314B:5D8C0218"
}
},
"uuid" : "5e8ae8ed-0988-4df2-89d3-eda4c6bdc60d",
"persistent" : true,
"insertionIndex" : 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id" : "30364e1b-193c-4929-9dd0-5aab7b47dbb9",
"name" : "repos_github-api-test-org_github-api",
"request" : {
"url" : "/repos/github-api-test-org/github-api",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "repos_github-api-test-org_github-api-30364e1b-193c-4929-9dd0-5aab7b47dbb9.json",
"headers" : {
"Date" : "Thu, 26 Sep 2019 00:11:05 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Server" : "GitHub.com",
"Status" : "200 OK",
"X-RateLimit-Limit" : "5000",
"X-RateLimit-Remaining" : "4991",
"X-RateLimit-Reset" : "1569460192",
"Cache-Control" : "private, max-age=60, s-maxage=60",
"Vary" : [ "Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding" ],
"ETag" : "W/\"afa5bdcd11463905460dc32b4cbac3ba\"",
"Last-Modified" : "Wed, 25 Sep 2019 23:37:07 GMT",
"X-OAuth-Scopes" : "gist, notifications, read:org, read:public_key, read:repo_hook, repo",
"X-Accepted-OAuth-Scopes" : "repo",
"X-GitHub-Media-Type" : "unknown, github.v3",
"Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin" : "*",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options" : "deny",
"X-Content-Type-Options" : "nosniff",
"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" : "FD24:3764:2322DE:2A315A:5D8C0218"
}
},
"uuid" : "30364e1b-193c-4929-9dd0-5aab7b47dbb9",
"persistent" : true,
"insertionIndex" : 3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"id" : "ed8bf5ba-65e0-47d8-bb4d-614063828c87",
"name" : "repos_github-api-test-org_github-api_branches_test_urlencode",
"request" : {
"url" : "/repos/github-api-test-org/github-api/branches/test%2F%23UrlEncode",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "repos_github-api-test-org_github-api_branches_test_urlencode-ed8bf5ba-65e0-47d8-bb4d-614063828c87.json",
"headers" : {
"Date" : "Thu, 26 Sep 2019 00:11:05 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Server" : "GitHub.com",
"Status" : "200 OK",
"X-RateLimit-Limit" : "5000",
"X-RateLimit-Remaining" : "4990",
"X-RateLimit-Reset" : "1569460192",
"Cache-Control" : "private, max-age=60, s-maxage=60",
"Vary" : [ "Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding" ],
"ETag" : "W/\"70021000b4de67768f66b57bbeead27c\"",
"X-OAuth-Scopes" : "gist, notifications, read:org, read:public_key, read:repo_hook, repo",
"X-Accepted-OAuth-Scopes" : "",
"X-GitHub-Media-Type" : "unknown, github.v3",
"Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin" : "*",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options" : "deny",
"X-Content-Type-Options" : "nosniff",
"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" : "FD24:3764:2322E4:2A3160:5D8C0219"
}
},
"uuid" : "ed8bf5ba-65e0-47d8-bb4d-614063828c87",
"persistent" : true,
"insertionIndex" : 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id" : "c92cdb3f-7cae-43ba-bee2-e5aa6863dcbc",
"name" : "user",
"request" : {
"url" : "/user",
"method" : "GET"
},
"response" : {
"status" : 200,
"bodyFileName" : "user-c92cdb3f-7cae-43ba-bee2-e5aa6863dcbc.json",
"headers" : {
"Date" : "Thu, 26 Sep 2019 00:11:04 GMT",
"Content-Type" : "application/json; charset=utf-8",
"Server" : "GitHub.com",
"Status" : "200 OK",
"X-RateLimit-Limit" : "5000",
"X-RateLimit-Remaining" : "4994",
"X-RateLimit-Reset" : "1569460192",
"Cache-Control" : "private, max-age=60, s-maxage=60",
"Vary" : [ "Accept, Authorization, Cookie, X-GitHub-OTP", "Accept-Encoding" ],
"ETag" : "W/\"14ffd29009ddc2209c450bb29a5a8330\"",
"Last-Modified" : "Tue, 24 Sep 2019 19:32:29 GMT",
"X-OAuth-Scopes" : "gist, notifications, read:org, read:public_key, read:repo_hook, repo",
"X-Accepted-OAuth-Scopes" : "",
"X-GitHub-Media-Type" : "unknown, github.v3",
"Access-Control-Expose-Headers" : "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type",
"Access-Control-Allow-Origin" : "*",
"Strict-Transport-Security" : "max-age=31536000; includeSubdomains; preload",
"X-Frame-Options" : "deny",
"X-Content-Type-Options" : "nosniff",
"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" : "FD24:3764:2322CA:2A3143:5D8C0218"
}
},
"uuid" : "c92cdb3f-7cae-43ba-bee2-e5aa6863dcbc",
"persistent" : true,
"insertionIndex" : 1
}