Skip to content

Commit

Permalink
Replace the avatar cache beta implementation with default one provide…
Browse files Browse the repository at this point in the history
…d by the scm-api plugin.

There are two action that store the AvatarImage, BitbucketTeamAvatarMetadataAction and BitbucketRepoAvatarMetadataAction.
The BitbucketTeamAvatarMetadataAction holds the metadata to retrieve the repository avatar of single multi branch repository.
The BitbucketRepoAvatarMetadataAction holds the metadata to retrieve the team/project avatar of an organization folder.
The SVG images generated by bitbucket when an avatar has not been changed are not supported, for those images it will rely on Jenkins random images.
  • Loading branch information
nfalco79 committed Jan 27, 2025
1 parent 1876923 commit d02e956
Show file tree
Hide file tree
Showing 24 changed files with 360 additions and 1,217 deletions.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<!-- Jenkins.MANAGE is still in Beta -->
<useBeta>true</useBeta>
<tagNameFormat>@{project.version}</tagNameFormat>
<!-- becuase eclipse generate JUnit5 run configuration with unreplaced variables! -->
<surefire.forkNumber>1</surefire.forkNumber>
</properties>

<developers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,49 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
import jenkins.scm.api.metadata.AvatarMetadataAction;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

/**
* Invisible property that retains information about Bitbucket repository.
*/
public class BitbucketRepoMetadataAction extends AvatarMetadataAction{
public class BitbucketRepoAvatarMetadataAction extends AvatarMetadataAction {

private final String scm;
private String avatarURL;

public BitbucketRepoMetadataAction(@NonNull BitbucketRepository repo) {
public BitbucketRepoAvatarMetadataAction(@NonNull BitbucketRepository repo) {
this(repo.getScm());
this.avatarURL = repo.getAvatar();
}

public BitbucketRepoMetadataAction(String scm) {
@DataBoundConstructor
public BitbucketRepoAvatarMetadataAction(String scm) {
this.scm = scm;
}

public String getScm() {
return scm;
}


public String getAvatarURL() {
return avatarURL;
}

@DataBoundSetter
public void setAvatarURL(String avatarURL) {
this.avatarURL = avatarURL;
}

/**
* {@inheritDoc}
*/
@Override
public String getAvatarIconClassName() {
if (avatarURL != null) {
return avatarURL;

Check warning on line 71 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketRepoAvatarMetadataAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 57-71 are not covered by tests
}
if ("git".equals(scm)) {
return "icon-bitbucket-repo-git";
}
Expand All @@ -69,9 +87,6 @@ public String getAvatarDescription() {
return Messages.BitbucketRepoMetadataAction_IconDescription();
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -81,17 +96,14 @@ public boolean equals(Object o) {
return false;
}

BitbucketRepoMetadataAction that = (BitbucketRepoMetadataAction) o;

return Objects.equals(scm, that.scm);
BitbucketRepoAvatarMetadataAction that = (BitbucketRepoAvatarMetadataAction) o;
return Objects.equals(scm, that.scm)

Check warning on line 100 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketRepoAvatarMetadataAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 100 is only partially covered, one branch is missing
&& Objects.equals(avatarURL, that.avatarURL);

Check warning on line 101 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketRepoAvatarMetadataAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 101 is only partially covered, one branch is missing
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hashCode(scm);
return Objects.hash(scm, avatarURL);

Check warning on line 106 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketRepoAvatarMetadataAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 106 is not covered by tests
}

/**
Expand All @@ -103,4 +115,5 @@ public String toString() {
"scm='" + scm + '\'' +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCloudWorkspace;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketRepository;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketTeam;
import com.cloudbees.jenkins.plugins.bitbucket.client.repository.UserRoleInRepository;
Expand All @@ -36,7 +37,6 @@
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentials;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.MirrorListSupplier;
import com.cloudbees.jenkins.plugins.bitbucket.server.BitbucketServerWebhookImplementation;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerProject;
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand Down Expand Up @@ -569,37 +569,33 @@ public List<Action> retrieveActions(@NonNull SCMNavigatorOwner owner,

BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials);

BitbucketApi bitbucket = BitbucketApiFactory.newInstance(serverUrl, authenticator, repoOwner, null, null);
BitbucketTeam team = bitbucket.getTeam();
if (team != null) {
String defaultTeamUrl;
if (team instanceof BitbucketServerProject) {
defaultTeamUrl = serverUrl + "/projects/" + team.getName();
try (BitbucketApi bitbucket = BitbucketApiFactory.newInstance(serverUrl, authenticator, repoOwner, getProjectKey(), null)) {
BitbucketTeam team = bitbucket.getTeam();
String avatarURL = null;
String teamURL;
String teamDisplayName;
if (team != null) {
avatarURL = team.getAvatar();
teamURL = team.getLink("html");
teamDisplayName = StringUtils.defaultIfBlank(team.getDisplayName(), team.getName());
if (StringUtils.isBlank(teamURL)) {

Check warning on line 581 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 581 is only partially covered, one branch is missing
if (team instanceof BitbucketCloudWorkspace wks) {

Check warning on line 582 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 582 is only partially covered, one branch is missing
teamURL = serverUrl + "/" + wks.getSlug();
} else {
teamURL = serverUrl + "/projects/" + team.getName();

Check warning on line 585 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 585 is not covered by tests
}
}
listener.getLogger().printf("Team: %s%n", HyperlinkNote.encodeTo(teamURL, teamDisplayName));
} else {
defaultTeamUrl = serverUrl + "/" + team.getName();
teamURL = serverUrl + "/" + repoOwner;
teamDisplayName = repoOwner;

Check warning on line 591 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 590-591 are not covered by tests
listener.getLogger().println("Could not resolve team details");
}
String teamUrl = StringUtils.defaultIfBlank(team.getLink("html"), defaultTeamUrl);
String teamDisplayName = StringUtils.defaultIfBlank(team.getDisplayName(), team.getName());
result.add(new ObjectMetadataAction(
teamDisplayName,
null,
teamUrl
));
result.add(new BitbucketTeamMetadataAction(serverUrl, credentials, team.getName()));
result.add(new BitbucketLink("icon-bitbucket-logo", teamUrl));
listener.getLogger().printf("Team: %s%n", HyperlinkNote.encodeTo(teamUrl, teamDisplayName));
} else {
String teamUrl = serverUrl + "/" + repoOwner;
result.add(new ObjectMetadataAction(
repoOwner,
null,
teamUrl
));
result.add(new BitbucketTeamMetadataAction(null, null, null));
result.add(new BitbucketLink("icon-bitbucket-logo", teamUrl));
listener.getLogger().println("Could not resolve team details");
}
return result;
result.add(new ObjectMetadataAction(teamDisplayName, null, teamURL));
result.add(new BitbucketTeamAvatarMetadataAction(avatarURL/*, credentialsId*/)); // FIXME bitbucket server required authentication!

Check warning on line 595 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMNavigator.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

FIXME

HIGH: bitbucket server required authentication!
result.add(new BitbucketLink("icon-bitbucket-logo", teamURL));
return result;
}
}

@Symbol("bitbucket")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@
import hudson.RestrictedSince;
import hudson.Util;
import hudson.console.HyperlinkNote;
import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.model.Action;
import hudson.model.Actionable;
import hudson.model.Item;
import hudson.model.Items;
import hudson.model.TaskListener;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
Expand All @@ -97,7 +94,6 @@
import java.util.logging.Logger;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.model.Jenkins;
import jenkins.plugins.git.MergeWithGitSCMExtension;
import jenkins.plugins.git.traits.GitBrowserSCMSourceTrait;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMHeadCategory;
Expand Down Expand Up @@ -152,14 +148,6 @@ public class BitbucketSCMSource extends SCMSource {
private static final String CLOUD_REPO_TEMPLATE = "{/owner,repo}";
private static final String SERVER_REPO_TEMPLATE = "/projects{/owner}/repos{/repo}";

/**
* Mapping classes after refactoring for backward compatibility.
*/
@Initializer(before = InitMilestone.PLUGINS_STARTED)
public static void aliases() {
Items.XSTREAM2.addCompatibilityAlias("com.cloudbees.jenkins.plugins.bitbucket.MergeWithGitSCMExtension", MergeWithGitSCMExtension.class);
}

/** How long to delay events received from Bitbucket in order to allow the API caches to sync. */
private static /*mostly final*/ int eventDelaySeconds =
Math.min(
Expand Down Expand Up @@ -1107,26 +1095,27 @@ protected List<Action> retrieveActions(@CheckForNull SCMSourceEvent event,
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 1096 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();
result.add(new BitbucketRepoMetadataAction(r));
String defaultBranch = bitbucket.getDefaultBranch();
if (StringUtils.isNotBlank(defaultBranch)) {
result.add(new BitbucketDefaultBranch(repoOwner, repository, defaultBranch));
try (BitbucketApi bitbucket = buildBitbucketClient()) {
gatherPrimaryCloneLinks(bitbucket);
BitbucketRepository r = bitbucket.getRepository();
result.add(new BitbucketRepoAvatarMetadataAction(r));
String defaultBranch = bitbucket.getDefaultBranch();
if (StringUtils.isNotBlank(defaultBranch)) {
result.add(new BitbucketDefaultBranch(repoOwner, repository, defaultBranch));
}
UriTemplate template;
if (BitbucketApiUtils.isCloud(getServerUrl())) {
template = UriTemplate.fromTemplate(getServerUrl() + CLOUD_REPO_TEMPLATE);
} else {
template = UriTemplate.fromTemplate(getServerUrl() + SERVER_REPO_TEMPLATE);
}
String url = template
.set("owner", repoOwner)
.set("repo", repository)
.expand();
result.add(new BitbucketLink("icon-bitbucket-repo", url));
result.add(new ObjectMetadataAction(r.getRepositoryName(), null, url));
}
UriTemplate template;
if (BitbucketApiUtils.isCloud(getServerUrl())) {
template = UriTemplate.fromTemplate(getServerUrl() + CLOUD_REPO_TEMPLATE);
} else {
template = UriTemplate.fromTemplate(getServerUrl() + SERVER_REPO_TEMPLATE);
}
String url = template
.set("owner", repoOwner)
.set("repo", repository)
.expand();
result.add(new BitbucketLink("icon-bitbucket-repo", url));
result.add(new ObjectMetadataAction(r.getRepositoryName(), null, url));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.jenkins.plugins.bitbucket;

import java.util.Objects;
import jenkins.scm.api.metadata.AvatarMetadataAction;
import org.kohsuke.stapler.DataBoundConstructor;

/**
* Invisible property that retains information about the Bitbucket team avatar.
*/
public class BitbucketTeamAvatarMetadataAction extends AvatarMetadataAction {
private static final long serialVersionUID = -7472619697440514373L;

private final String avatarURL;

@DataBoundConstructor
public BitbucketTeamAvatarMetadataAction(String avatarURL) {
this.avatarURL = avatarURL;
}

/**
* {@inheritDoc}
*/
@Override
public String getAvatarImageOf(String size) {
// fall back to the generic bitbucket.org icon if no avatar provided
return avatarURL == null
? super.getAvatarImageOf(size)
: cachedResizedImageOf(avatarURL, size);
}

/**
* {@inheritDoc}
*/
@Override
public String getAvatarIconClassName() {
return avatarURL == null ? "icon-bitbucket-logo" : null;
}

/**
* {@inheritDoc}
*/
@Override
public String getAvatarDescription() {
return Messages.BitbucketTeamMetadataAction_IconDescription();
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
BitbucketTeamAvatarMetadataAction other = (BitbucketTeamAvatarMetadataAction) obj;
return Objects.equals(avatarURL, other.avatarURL);
}

@Override
public int hashCode() {
return Objects.hash(avatarURL);

Check warning on line 87 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketTeamAvatarMetadataAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 49-87 are not covered by tests
}
}
Loading

0 comments on commit d02e956

Please sign in to comment.