Skip to content

Commit

Permalink
Issue hub4j#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 authored and bitwiseman committed Sep 25, 2019
1 parent 50fb9c1 commit a50a011
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 @@ -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

0 comments on commit a50a011

Please sign in to comment.