Skip to content

Commit 56d7772

Browse files
committed
[JENKINS-75119] Build report UnknownHostException in console log for Bitbucket Server when the server instance is under a context root (#969)
Fix issue in the bitbucket server client when is built the Apache HttpHost from the serverURL. If the Bitbucket Server is installed under a context root different than root ("/"), than the HttpHost built parsing the entire given serverURL throw an UnknownHostException failure in the client. The fix removes the file part of the serverURL.
1 parent 737f853 commit 56d7772

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -954,12 +954,11 @@ protected HttpHost getHost() {
954954
try {
955955
// it's really needed?
956956
URL tmp = new URL(baseURL);
957-
if (tmp.getProtocol() == null) {
958-
url = new URL("http", tmp.getHost(), tmp.getPort(), tmp.getFile()).toString();
959-
}
957+
String schema = tmp.getProtocol() == null ? "http" : tmp.getProtocol();
958+
return new HttpHost(tmp.getHost(), tmp.getPort(), schema);
960959
} catch (MalformedURLException e) {
960+
return HttpHost.create(url);
961961
}
962-
return HttpHost.create(url);
963962
}
964963

965964
@Override

src/test/java/com/cloudbees/jenkins/plugins/bitbucket/server/client/BitbucketServerAPIClientTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
import java.nio.charset.StandardCharsets;
1414
import java.util.List;
1515
import org.apache.commons.io.IOUtils;
16+
import org.apache.http.HttpHost;
1617
import org.apache.http.HttpRequest;
1718
import org.apache.http.client.methods.HttpGet;
1819
import org.apache.http.client.methods.HttpHead;
1920
import org.apache.http.client.methods.HttpPost;
2021
import org.apache.http.client.methods.HttpRequestBase;
22+
import org.apache.http.client.protocol.HttpClientContext;
2123
import org.apache.http.impl.client.HttpClientBuilder;
2224
import org.junit.jupiter.api.BeforeAll;
2325
import org.junit.jupiter.api.Test;
@@ -34,6 +36,7 @@
3436
import static org.hamcrest.Matchers.is;
3537
import static org.hamcrest.Matchers.not;
3638
import static org.mockito.ArgumentMatchers.any;
39+
import static org.mockito.ArgumentMatchers.eq;
3740
import static org.mockito.Mockito.RETURNS_SELF;
3841
import static org.mockito.Mockito.mock;
3942
import static org.mockito.Mockito.mockStatic;
@@ -184,4 +187,17 @@ void verify_getTag_request_URL() throws Exception {
184187
.hasPath("/rest/api/1.0/projects/amuniz/repos/test-repos/tags"));
185188
}
186189

190+
@Issue("JENKINS-75119")
191+
@Test
192+
void verify_HttpHost_built_when_server_has_context_root() throws Exception {
193+
String serverURL = "https://acme.bitbucket.org/bitbucket";
194+
BitbucketServerAPIClient client = (BitbucketServerAPIClient) BitbucketIntegrationClientFactory.getClient(serverURL, "amuniz", "test-repos");
195+
196+
BitbucketAuthenticator authenticator = extractAuthenticator(client);
197+
client.getRepository();
198+
199+
HttpHost expectedHost = HttpHost.create("https://acme.bitbucket.org");
200+
verify(authenticator).configureContext(any(HttpClientContext.class), eq(expectedHost));
201+
}
202+
187203
}

0 commit comments

Comments
 (0)