From 5b9b36e0379ee01d394d5745140cf7a81b1154ea Mon Sep 17 00:00:00 2001 From: Nikolas Falco Date: Thu, 14 Nov 2024 13:37:24 +0100 Subject: [PATCH] Fix missing primary clone links in BitbucketSCMSource when retrieve branches and pull requests using REST APIs (#912) --- .../plugins/bitbucket/BitbucketSCMSource.java | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java index 61088d547..a9c9e3929 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java @@ -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); + } + } + + private void gatherPrimaryCloneLinks(@NonNull BitbucketApi apiClient) throws IOException, InterruptedException { + BitbucketRepository r = apiClient.getRepository(); + Map> links = r.getLinks(); + if (links != null && links.containsKey("clone")) { + setPrimaryCloneLinks(links.get("clone")); + } + } + @Override protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer, @CheckForNull SCMHeadEvent event, @NonNull TaskListener listener) @@ -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()) { @@ -558,7 +579,7 @@ protected Iterable create() { return getBitbucketPullRequestsFromEvent(hasPrEvent, listener); } - return (Iterable) buildBitbucketClient().getPullRequests(); + return (Iterable) apiClient.getPullRequests(); } catch (IOException | InterruptedException e) { throw new BitbucketSCMSource.WrappedException(e); } @@ -570,7 +591,7 @@ protected Iterable create() { @Override protected Iterable create() { try { - return (Iterable) buildBitbucketClient().getBranches(); + return (Iterable) apiClient.getBranches(); } catch (IOException | InterruptedException e) { throw new BitbucketSCMSource.WrappedException(e); } @@ -582,7 +603,7 @@ protected Iterable create() { @Override protected Iterable create() { try { - return (Iterable) buildBitbucketClient().getTags(); + return (Iterable) apiClient.getTags(); } catch (IOException | InterruptedException e) { throw new BitbucketSCMSource.WrappedException(e); } @@ -1032,11 +1053,8 @@ protected List retrieveActions(@CheckForNull SCMSourceEvent event, // TODO when we have support for trusted events, use the details from event if event was from trusted source List result = new ArrayList<>(); final BitbucketApi bitbucket = buildBitbucketClient(); + gatherPrimaryCloneLinks(bitbucket); BitbucketRepository r = bitbucket.getRepository(); - Map> 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)) {