Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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