fix: Fix url resolving in the bitbucket api client (#346)

Fix a bug where url is not resolved correctly if a server url has path segments. URI.create(https://server-url/path-segment).resolve(/second-path) returns https://server-url/second-path, so the path-segment is ignored. See for more details.

Add a trailing / to the server url and make the rest api segments reletive to fix the problem.
pull/347/head
Igor Vinokur 2022-08-30 14:53:15 +03:00 committed by GitHub
parent c4d272b140
commit 86799d5fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 12 deletions

View File

@ -70,7 +70,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
private final HttpClient httpClient;
public HttpBitbucketServerApiClient(String serverUrl, OAuthAuthenticator authenticator) {
this.serverUri = URI.create(serverUrl);
this.serverUri = URI.create(serverUrl.endsWith("/") ? serverUrl : serverUrl + "/");
this.authenticator = authenticator;
this.httpClient =
HttpClient.newBuilder()
@ -131,7 +131,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
@Override
public BitbucketUser getUser(String slug, @Nullable String token)
throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
URI uri = serverUri.resolve("/rest/api/1.0/users/" + slug);
URI uri = serverUri.resolve("./rest/api/1.0/users/" + slug);
HttpRequest request =
HttpRequest.newBuilder(uri)
.headers(
@ -163,7 +163,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
public List<BitbucketUser> getUsers()
throws ScmBadRequestException, ScmUnauthorizedException, ScmCommunicationException {
try {
return doGetItems(BitbucketUser.class, "/rest/api/1.0/users", null);
return doGetItems(BitbucketUser.class, "./rest/api/1.0/users", null);
} catch (ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
@ -173,7 +173,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
public List<BitbucketUser> getUsers(String filter)
throws ScmBadRequestException, ScmUnauthorizedException, ScmCommunicationException {
try {
return doGetItems(BitbucketUser.class, "/rest/api/1.0/users", filter);
return doGetItems(BitbucketUser.class, "./rest/api/1.0/users", filter);
} catch (ScmItemNotFoundException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
@ -182,7 +182,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
@Override
public void deletePersonalAccessTokens(String userSlug, Long tokenId)
throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug + "/" + tokenId);
URI uri = serverUri.resolve("./rest/access-tokens/1.0/users/" + userSlug + "/" + tokenId);
HttpRequest request =
HttpRequest.newBuilder(uri)
.DELETE()
@ -217,7 +217,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
public BitbucketPersonalAccessToken createPersonalAccessTokens(
String userSlug, String tokenName, Set<String> permissions)
throws ScmBadRequestException, ScmUnauthorizedException, ScmCommunicationException {
URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug);
URI uri = serverUri.resolve("./rest/access-tokens/1.0/users/" + userSlug);
try {
HttpRequest request =
@ -256,7 +256,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
try {
return doGetItems(
BitbucketPersonalAccessToken.class, "/rest/access-tokens/1.0/users/" + userSlug, null);
BitbucketPersonalAccessToken.class, "./rest/access-tokens/1.0/users/" + userSlug, null);
} catch (ScmBadRequestException e) {
throw new ScmCommunicationException(e.getMessage(), e);
}
@ -265,7 +265,7 @@ public class HttpBitbucketServerApiClient implements BitbucketServerApiClient {
@Override
public BitbucketPersonalAccessToken getPersonalAccessToken(String userSlug, Long tokenId)
throws ScmItemNotFoundException, ScmUnauthorizedException, ScmCommunicationException {
URI uri = serverUri.resolve("/rest/access-tokens/1.0/users/" + userSlug + "/" + tokenId);
URI uri = serverUri.resolve("./rest/access-tokens/1.0/users/" + userSlug + "/" + tokenId);
HttpRequest request =
HttpRequest.newBuilder(uri)
.headers(

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2021 Red Hat, Inc.
* Copyright (c) 2012-2022 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@ -57,15 +57,15 @@ public class BitbucketServerApiClientProviderTest {
// given
BitbucketServerApiProvider bitbucketServerApiProvider =
new BitbucketServerApiProvider(
"https://bitbucket.server.com/, https://bitbucket2.server.com/",
"https://bitbucket.server.com/",
"https://bitbucket.server.com, https://bitbucket2.server.com",
"https://bitbucket.server.com",
ImmutableSet.of(oAuthAuthenticator));
// when
BitbucketServerApiClient actual = bitbucketServerApiProvider.get();
// then
assertNotNull(actual);
// internal representation always w/out slashes
assertTrue(actual.isConnected("https://bitbucket.server.com"));
assertTrue(actual.isConnected("https://bitbucket.server.com/"));
}
@Test(dataProvider = "noopConfig")