Skip to content

Commit

Permalink
473: Handle Bitbucket API changes (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverlockwood authored Aug 27, 2021
1 parent 3817449 commit 9ee5f97
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
} else {
// Navigate the repositories of the repoOwner as a user
listener.getLogger().format("Looking up repositories of user %s%n", repoOwner);
request.withRepositories(bitbucket.getRepositories(UserRoleInRepository.OWNER));
request.withRepositories(bitbucket.getRepositories(UserRoleInRepository.ADMIN));
}
for (BitbucketRepository repo : request.repositories()) {
if (request.process(repo.getRepositoryName(), sourceFactory, null, witness)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.cloudbees.jenkins.plugins.bitbucket.api;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.List;
import java.util.Map;

public class BitbucketCloudWorkspace implements BitbucketWorkspace {
@JsonProperty("uuid")
private String uuid;

@JsonProperty("name")
private String name;

@JsonProperty("slug")
private String slug;

@JsonProperty("is_private")
private boolean isPrivate;

@JsonProperty("links")
@JsonDeserialize(keyAs = String.class, contentUsing = BitbucketHref.Deserializer.class)
private Map<String, List<BitbucketHref>> links;

@Override
public String getUuid() {
return uuid;
}

@Override
public String getName() {
return name;
}

@Override
public String getDisplayName() {
return name;
}

@Override
public String getSlug() {
return slug;
}

@Override
public boolean isPrivate() {
return isPrivate;
}

@Override
public Map<String, List<BitbucketHref>> getLinks() {
return links;
}

@Override
public String getLink(String name) {
if (links == null) {
return null;
}
List<BitbucketHref> hrefs = links.get(name);
if (hrefs == null || hrefs.isEmpty()) {
return null;
}
BitbucketHref href = hrefs.get(0);
return href == null ? null : href.getHref();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cloudbees.jenkins.plugins.bitbucket.api;

public interface BitbucketWorkspace extends BitbucketTeam {
String getUuid();

String getSlug();

boolean isPrivate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketBuildStatus;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCloudWorkspace;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketException;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
Expand All @@ -46,7 +47,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequestValue;
import com.cloudbees.jenkins.plugins.bitbucket.client.pullrequest.BitbucketPullRequests;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudRepository;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketCloudTeam;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositoryHook;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositoryHooks;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.BitbucketRepositorySource;
Expand Down Expand Up @@ -128,7 +128,7 @@ public class BitbucketCloudApiClient implements BitbucketApi {
private static final Logger LOGGER = Logger.getLogger(BitbucketCloudApiClient.class.getName());
private static final HttpHost API_HOST = HttpHost.create("https://api.bitbucket.org");
private static final String V2_API_BASE_URL = "https://api.bitbucket.org/2.0/repositories";
private static final String V2_TEAMS_API_BASE_URL = "https://api.bitbucket.org/2.0/teams";
private static final String V2_WORKSPACES_API_BASE_URL = "https://api.bitbucket.org/2.0/workspaces";
private static final String REPO_URL_TEMPLATE = V2_API_BASE_URL + "{/owner,repo}";
private static final int API_RATE_LIMIT_CODE = 429;
// Limit images to 16k
Expand Down Expand Up @@ -675,14 +675,14 @@ private BitbucketRepositoryHooks parsePaginatedRepositoryHooks(String response)
@Override
@CheckForNull
public BitbucketTeam getTeam() throws IOException, InterruptedException {
final String url = UriTemplate.fromTemplate(V2_TEAMS_API_BASE_URL + "{/owner}")
final String url = UriTemplate.fromTemplate(V2_WORKSPACES_API_BASE_URL + "{/owner}")
.set("owner", owner)
.expand();

Callable<BitbucketTeam> request = () -> {
try {
String response = getRequest(url);
return JsonParser.toJava(response, BitbucketCloudTeam.class);
return JsonParser.toJava(response, BitbucketCloudWorkspace.class);
} catch (FileNotFoundException e) {
return null;
} catch (IOException e) {
Expand Down

0 comments on commit 9ee5f97

Please sign in to comment.