From a50a011eb3aeb6ea7726505e68c45b7c709c67f7 Mon Sep 17 00:00:00 2001 From: Martin van Zijl Date: Mon, 12 Nov 2018 11:43:13 +1300 Subject: [PATCH] Issue #381 - Escape branch URL. This is using the "old" HTTP.UTF_8 Charset, since the new StandardCharsets doesn't compile. --- .../java/org/kohsuke/github/GHRepository.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 296a3a490a..f66902c263 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -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; @@ -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.*; /** @@ -1393,8 +1397,25 @@ public Map 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); } /**