Skip to content

Commit

Permalink
Fix missing primary clone links in BitbucketSCMSource when retrieve b…
Browse files Browse the repository at this point in the history
…ranches and pull requests using REST APIs (#912)
  • Loading branch information
nfalco79 authored Nov 14, 2024
1 parent 200b56c commit 5b9b36e
Showing 1 changed file with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,25 @@ public BitbucketApi buildBitbucketClient(String repoOwner, String repository) {
return BitbucketApiFactory.newInstance(getServerUrl(), authenticator(), repoOwner, null, repository);
}

@Override
public void afterSave() {
try {
gatherPrimaryCloneLinks(buildBitbucketClient());
} catch (InterruptedException | IOException e) {
LOGGER.log(Level.SEVERE,
"Could not determine clone links of " + getRepoOwner() + "/" + getRepository() +
" on " + getServerUrl() + " for " + getOwner() + " falling back to generated links", e);

Check warning on line 542 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 539-542 are not covered by tests
}
}

private void gatherPrimaryCloneLinks(@NonNull BitbucketApi apiClient) throws IOException, InterruptedException {
BitbucketRepository r = apiClient.getRepository();
Map<String, List<BitbucketHref>> links = r.getLinks();
if (links != null && links.containsKey("clone")) {

Check warning on line 549 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 549 is only partially covered, one branch is missing
setPrimaryCloneLinks(links.get("clone"));
}
}

@Override
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
Expand All @@ -546,6 +565,8 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
listener.getLogger().format("Connecting to %s using %s%n", getServerUrl(),
CredentialsNameProvider.name(scanCredentials));
}
BitbucketApi apiClient = buildBitbucketClient();
gatherPrimaryCloneLinks(apiClient);

// populate the request with its data sources
if (request.isFetchPRs()) {
Expand All @@ -558,7 +579,7 @@ protected Iterable<BitbucketPullRequest> create() {
return getBitbucketPullRequestsFromEvent(hasPrEvent, listener);
}

return (Iterable<BitbucketPullRequest>) buildBitbucketClient().getPullRequests();
return (Iterable<BitbucketPullRequest>) apiClient.getPullRequests();
} catch (IOException | InterruptedException e) {
throw new BitbucketSCMSource.WrappedException(e);
}
Expand All @@ -570,7 +591,7 @@ protected Iterable<BitbucketPullRequest> create() {
@Override
protected Iterable<BitbucketBranch> create() {
try {
return (Iterable<BitbucketBranch>) buildBitbucketClient().getBranches();
return (Iterable<BitbucketBranch>) apiClient.getBranches();
} catch (IOException | InterruptedException e) {
throw new BitbucketSCMSource.WrappedException(e);
}
Expand All @@ -582,7 +603,7 @@ protected Iterable<BitbucketBranch> create() {
@Override
protected Iterable<BitbucketBranch> create() {
try {
return (Iterable<BitbucketBranch>) buildBitbucketClient().getTags();
return (Iterable<BitbucketBranch>) apiClient.getTags();
} catch (IOException | InterruptedException e) {
throw new BitbucketSCMSource.WrappedException(e);
}
Expand Down Expand Up @@ -1032,11 +1053,8 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
// TODO when we have support for trusted events, use the details from event if event was from trusted source

Check warning on line 1053 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

TODO

NORMAL: when we have support for trusted events, use the details from event if event was from trusted source
List<Action> result = new ArrayList<>();
final BitbucketApi bitbucket = buildBitbucketClient();
gatherPrimaryCloneLinks(bitbucket);
BitbucketRepository r = bitbucket.getRepository();
Map<String, List<BitbucketHref>> links = r.getLinks();
if (links != null && links.containsKey("clone")) {
setPrimaryCloneLinks(links.get("clone"));
}
result.add(new BitbucketRepoMetadataAction(r));
String defaultBranch = bitbucket.getDefaultBranch();
if (StringUtils.isNotBlank(defaultBranch)) {
Expand Down

0 comments on commit 5b9b36e

Please sign in to comment.