-
Notifications
You must be signed in to change notification settings - Fork 354
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
Avatar metadata for BitBucket organization folder is reworked #700
Draft
duemir
wants to merge
5
commits into
jenkinsci:master
Choose a base branch
from
duemir:credentialless-avatar-actions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−151
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9969022
Avatar metadata for BitBucket organization folder is reworked
duemir e07941a
Merge branch 'master' into credentialless-avatar-actions
lifeofguenter b37d3fe
Merge branch 'master' into credentialless-avatar-actions
lifeofguenter 660c049
Merge remote-tracking branch 'upstream/master' into credentialless-av…
duemir 7e48060
Merge remote-tracking branch 'upstream/master' into credentialless-av…
duemir File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
...ain/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketCloudWorkspaceAvatarAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.cloudbees.jenkins.plugins.bitbucket; | ||
|
||
import edu.umd.cs.findbugs.annotations.CheckForNull; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import java.util.Objects; | ||
import jenkins.scm.api.metadata.AvatarMetadataAction; | ||
|
||
/** | ||
* Avatar link returned by <a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-workspaces/#api-workspaces-workspace-get">Get Workspace</a> Bitbucket REST API is public at the moment of writing. Hence, reusing using SCM API plugin provided APIs. | ||
*/ | ||
public class BitbucketCloudWorkspaceAvatarAction extends AvatarMetadataAction { | ||
@CheckForNull | ||
private String avatar; | ||
|
||
public BitbucketCloudWorkspaceAvatarAction(@CheckForNull String avatar) { | ||
this.avatar = avatar; | ||
} | ||
|
||
@Override | ||
public String getAvatarIconClassName() { | ||
if (avatar == null) { | ||
return "icon-bitbucket-scm-navigator"; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getAvatarDescription() { | ||
return Messages.BitbucketCloudWorkspaceAvatarMetadataAction_IconDescription(); | ||
} | ||
|
||
@Override | ||
public String getAvatarImageOf(@NonNull String size) { | ||
if (avatar != null) { | ||
return cachedResizedImageOf(avatar, size); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
BitbucketCloudWorkspaceAvatarAction that = (BitbucketCloudWorkspaceAvatarAction) o; | ||
|
||
return Objects.equals(avatar, that.avatar); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(avatar); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "BitbucketCloudWorkspaceAvatarAction{" + | ||
"avatar='" + avatar + '\'' + | ||
'}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketServerProjectAvatarAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.cloudbees.jenkins.plugins.bitbucket; | ||
|
||
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.plugins.credentials.common.StandardCredentials; | ||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Util; | ||
import java.io.IOException; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
import jenkins.authentication.tokens.api.AuthenticationTokens; | ||
import jenkins.model.Jenkins; | ||
import jenkins.scm.api.SCMNavigatorOwner; | ||
import jenkins.scm.api.metadata.AvatarMetadataAction; | ||
import jenkins.scm.impl.avatars.AvatarCache; | ||
import jenkins.scm.impl.avatars.AvatarImage; | ||
import jenkins.scm.impl.avatars.AvatarImageSource; | ||
|
||
public class BitbucketServerProjectAvatarAction extends AvatarMetadataAction implements AvatarImageSource { | ||
|
||
// This can change when SCMNavigatorOwner is moved but this Action is only persisted | ||
// when BitbucketSCMNavigator.retrieveActions which, at the moment of writing, happens on webhook events and indexing. | ||
// Hence, implementing ItemListener to monitor of location changes seems impractical. | ||
private String ownerFullName; | ||
private String serverUrl; | ||
private String credentialsId; | ||
private String projectKey; | ||
// owner can be moved around or credential can get blocked or revoked. | ||
private transient boolean canFetch = true; | ||
|
||
public BitbucketServerProjectAvatarAction(SCMNavigatorOwner owner, BitbucketSCMNavigator navigator) { | ||
this(owner.getFullName(), navigator.getServerUrl(), navigator.getCredentialsId(), navigator.getRepoOwner()); | ||
} | ||
|
||
public BitbucketServerProjectAvatarAction(String ownerFullName, String serverUrl, String credentialsId, String projectKey) { | ||
this.ownerFullName = Util.fixEmpty(ownerFullName); | ||
this.serverUrl = Util.fixEmpty(serverUrl); | ||
this.credentialsId = Util.fixEmpty(credentialsId); | ||
this.projectKey = Util.fixEmpty(projectKey); | ||
} | ||
|
||
@Override | ||
public String getAvatarIconClassName() { | ||
if (!canFetch()) { | ||
return "icon-bitbucket-scm-navigator"; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getAvatarDescription() { | ||
return Messages.BitbucketServerProjectAvatarMetadataAction_IconDescription(); | ||
} | ||
|
||
@Override | ||
public String getAvatarImageOf(@NonNull String size) { | ||
if (canFetch()) { | ||
return AvatarCache.buildUrl(this, size); | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
BitbucketServerProjectAvatarAction that = (BitbucketServerProjectAvatarAction) o; | ||
return Objects.equals(serverUrl, that.serverUrl) && Objects.equals(credentialsId, that.credentialsId) && Objects.equals(projectKey, that.projectKey) && Objects.equals(ownerFullName, that.ownerFullName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(serverUrl, credentialsId, projectKey, ownerFullName); | ||
} | ||
|
||
@Override | ||
public AvatarImage fetch() { | ||
if (canFetch()) { | ||
return doFetch(); | ||
} | ||
return null; | ||
} | ||
|
||
private AvatarImage doFetch() { | ||
SCMNavigatorOwner owner = Jenkins.get().getItemByFullName(ownerFullName, SCMNavigatorOwner.class); | ||
if (owner != null) { | ||
StandardCredentials credentials = BitbucketCredentials.lookupCredentials( | ||
serverUrl, | ||
owner, | ||
credentialsId, | ||
StandardCredentials.class | ||
); | ||
|
||
BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials); | ||
|
||
BitbucketApi bitbucket = BitbucketApiFactory.newInstance(serverUrl, authenticator, projectKey, null, null); | ||
try { | ||
return bitbucket.getTeamAvatar(); | ||
} catch (IOException e) { | ||
canFetch = false; | ||
throw new RuntimeException(e); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} else { | ||
// Owner was probably relocated | ||
canFetch = false; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return new StringJoiner("::") | ||
.add(serverUrl) | ||
.add(credentialsId) | ||
.add(projectKey) | ||
.add(ownerFullName) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public boolean canFetch() { | ||
return canFetch && ownerFullName != null && serverUrl != null && projectKey != null; | ||
Check warning on line 125 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketServerProjectAvatarAction.java ci.jenkins.io / Code CoverageNot covered lines
Check warning on line 125 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketServerProjectAvatarAction.java ci.jenkins.io / SpotBugsSE_NO_SERIALVERSIONID
Raw output
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Class has same name as super class Note