@@ -589,7 +589,15 @@ static URL getApiURL(String apiUrl, String tailApiUrl) {
589589 // backward compatibility
590590 apiUrl = GitHubClient .GITHUB_URL ;
591591 }
592- return new URI (apiUrl + tailApiUrl ).toURL ();
592+
593+ String fullApiUrl = apiUrl + tailApiUrl ;
594+ try {
595+ return new URI (fullApiUrl ).toURL ();
596+ } catch (URISyntaxException e ) {
597+ // Some API URL fields include unescaped square brackets in path segments.
598+ // Keep existing escaping as-is while making the URL URI-safe for connectors.
599+ return new URI (encodeSquareBrackets (fullApiUrl )).toURL ();
600+ }
593601 } catch (Exception e ) {
594602 // The data going into constructing this URL should be controlled by the GitHub API framework,
595603 // so a malformed URL here is a framework runtime error.
@@ -598,6 +606,35 @@ static URL getApiURL(String apiUrl, String tailApiUrl) {
598606 throw new GHException ("Unable to build GitHub API URL" , e );
599607 }
600608 }
609+
610+ @ Nonnull
611+ private static String encodeSquareBrackets (@ Nonnull String url ) {
612+ URL parsedUrl ;
613+ try {
614+ parsedUrl = new URL (url );
615+ } catch (MalformedURLException e ) {
616+ // Preserve the original input when URL parsing fails so existing error behavior is unchanged.
617+ return url ;
618+ }
619+
620+ String path = parsedUrl .getPath ();
621+ String query = parsedUrl .getQuery ();
622+ String ref = parsedUrl .getRef ();
623+
624+ StringBuilder encodedUrl = new StringBuilder ();
625+ encodedUrl .append (parsedUrl .getProtocol ()).append ("://" ).append (parsedUrl .getAuthority ());
626+ if (path != null ) {
627+ encodedUrl .append (path .replace ("[" , "%5B" ).replace ("]" , "%5D" ));
628+ }
629+ if (query != null ) {
630+ encodedUrl .append ('?' ).append (query .replace ("[" , "%5B" ).replace ("]" , "%5D" ));
631+ }
632+ if (ref != null ) {
633+ encodedUrl .append ('#' ).append (ref .replace ("[" , "%5B" ).replace ("]" , "%5D" ));
634+ }
635+
636+ return encodedUrl .toString ();
637+ }
601638 /**
602639 * Create a new {@link Builder}.
603640 *
0 commit comments