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 missing primary clone links in BitbucketSCMSource when retrieve branches and pull requests using REST APIs #912

Merged
merged 1 commit into from
Nov 14, 2024
Merged
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 @@ -532,6 +532,25 @@
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 @@
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 @@
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 @@
@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 @@
@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 @@ -1029,14 +1050,11 @@
protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
@NonNull TaskListener listener)
throws IOException, InterruptedException {
// 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