Skip to content

Commit

Permalink
fix: Fix url resolving in the bitbucket api client (#346)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vinokurig authored Aug 30, 2022
1 parent c4d272b commit 86799d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -131,7 +131,7 @@ public BitbucketUser getUser(Subject cheUser)
@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(
Expand Down Expand Up @@ -163,7 +163,7 @@ public BitbucketUser getUser(String slug, @Nullable String token)
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);
}
Expand All @@ -173,7 +173,7 @@ public List<BitbucketUser> getUsers()
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);
}
Expand All @@ -182,7 +182,7 @@ public List<BitbucketUser> getUsers(String filter)
@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()
Expand Down Expand Up @@ -217,7 +217,7 @@ public void deletePersonalAccessTokens(String userSlug, Long tokenId)
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 =
Expand Down Expand Up @@ -256,7 +256,7 @@ public List<BitbucketPersonalAccessToken> getPersonalAccessTokens(String userSlu
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);
}
Expand All @@ -265,7 +265,7 @@ public List<BitbucketPersonalAccessToken> getPersonalAccessTokens(String userSlu
@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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -57,15 +57,15 @@ public void shouldNormalizeURLsBeforeCreateBitbucketServerApi() {
// 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")
Expand Down

0 comments on commit 86799d5

Please sign in to comment.