Skip to content

Commit 5b9b36e

Browse files
authored
Fix missing primary clone links in BitbucketSCMSource when retrieve branches and pull requests using REST APIs (#912)
1 parent 200b56c commit 5b9b36e

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,25 @@ public BitbucketApi buildBitbucketClient(String repoOwner, String repository) {
532532
return BitbucketApiFactory.newInstance(getServerUrl(), authenticator(), repoOwner, null, repository);
533533
}
534534

535+
@Override
536+
public void afterSave() {
537+
try {
538+
gatherPrimaryCloneLinks(buildBitbucketClient());
539+
} catch (InterruptedException | IOException e) {
540+
LOGGER.log(Level.SEVERE,
541+
"Could not determine clone links of " + getRepoOwner() + "/" + getRepository() +
542+
" on " + getServerUrl() + " for " + getOwner() + " falling back to generated links", e);
543+
}
544+
}
545+
546+
private void gatherPrimaryCloneLinks(@NonNull BitbucketApi apiClient) throws IOException, InterruptedException {
547+
BitbucketRepository r = apiClient.getRepository();
548+
Map<String, List<BitbucketHref>> links = r.getLinks();
549+
if (links != null && links.containsKey("clone")) {
550+
setPrimaryCloneLinks(links.get("clone"));
551+
}
552+
}
553+
535554
@Override
536555
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
537556
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
@@ -546,6 +565,8 @@ protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHe
546565
listener.getLogger().format("Connecting to %s using %s%n", getServerUrl(),
547566
CredentialsNameProvider.name(scanCredentials));
548567
}
568+
BitbucketApi apiClient = buildBitbucketClient();
569+
gatherPrimaryCloneLinks(apiClient);
549570

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

561-
return (Iterable<BitbucketPullRequest>) buildBitbucketClient().getPullRequests();
582+
return (Iterable<BitbucketPullRequest>) apiClient.getPullRequests();
562583
} catch (IOException | InterruptedException e) {
563584
throw new BitbucketSCMSource.WrappedException(e);
564585
}
@@ -570,7 +591,7 @@ protected Iterable<BitbucketPullRequest> create() {
570591
@Override
571592
protected Iterable<BitbucketBranch> create() {
572593
try {
573-
return (Iterable<BitbucketBranch>) buildBitbucketClient().getBranches();
594+
return (Iterable<BitbucketBranch>) apiClient.getBranches();
574595
} catch (IOException | InterruptedException e) {
575596
throw new BitbucketSCMSource.WrappedException(e);
576597
}
@@ -582,7 +603,7 @@ protected Iterable<BitbucketBranch> create() {
582603
@Override
583604
protected Iterable<BitbucketBranch> create() {
584605
try {
585-
return (Iterable<BitbucketBranch>) buildBitbucketClient().getTags();
606+
return (Iterable<BitbucketBranch>) apiClient.getTags();
586607
} catch (IOException | InterruptedException e) {
587608
throw new BitbucketSCMSource.WrappedException(e);
588609
}
@@ -1032,11 +1053,8 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
10321053
// TODO when we have support for trusted events, use the details from event if event was from trusted source
10331054
List<Action> result = new ArrayList<>();
10341055
final BitbucketApi bitbucket = buildBitbucketClient();
1056+
gatherPrimaryCloneLinks(bitbucket);
10351057
BitbucketRepository r = bitbucket.getRepository();
1036-
Map<String, List<BitbucketHref>> links = r.getLinks();
1037-
if (links != null && links.containsKey("clone")) {
1038-
setPrimaryCloneLinks(links.get("clone"));
1039-
}
10401058
result.add(new BitbucketRepoMetadataAction(r));
10411059
String defaultBranch = bitbucket.getDefaultBranch();
10421060
if (StringUtils.isNotBlank(defaultBranch)) {

0 commit comments

Comments
 (0)