Skip to content

Commit

Permalink
Issue #381 - Escape branch URL.
Browse files Browse the repository at this point in the history
This is using the "old" HTTP.UTF_8 Charset, since the new
StandardCharsets doesn't compile.
  • Loading branch information
martinvanzijl committed Nov 11, 2018
1 parent fad203a commit 87d7314
Showing 1 changed file with 22 additions and 1 deletion.
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 @@ -1350,8 +1354,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

0 comments on commit 87d7314

Please sign in to comment.