gitblit-org / gitblit

pure java git solution
http://gitblit.com
Apache License 2.0
2.28k stars 670 forks source link

On-Premise Avatar Hosting for Gitblit #816

Open gitblit opened 9 years ago

gitblit commented 9 years ago

Originally reported on Google Code with ID 520

Hello Guys,

i recently discovered Gitblit and the only thing i'd really wish for is an alternative
to gravatar. Best would be an Option for LDAP-Sync to use the jpegPhoto Attribute from
Active Directory.

A nice alternative would be an user-configurable URL for the Gravatar-Service, so I
could use an libravatar-Service (see http://wiki.libravatar.org/api/ )

Friendly regards,

a satisfied Gitblit-User

Reported by friesenschrauber on 2014-10-29 17:11:47

acoberlin commented 8 years ago

We also would love to integrate Gitblit with Jira/Confluence Avatars. Atlassian provides Plugins for Jira/Confluence to act as avatar server. It should be easy to change the URL for requesting Avatars in utils/ActivityUtils.java URL for Jira https://your-jira/rest/avatar/1.0/server/

We are a non-profit company in Berlin with 250+ users and highly satisfied with GitBlit.

Kind Regads, aco

gitblit commented 8 years ago

Hi @acoberlin. Gitblit 1.7+ does support a pluggable avatar source. You will have to create an implementation of AvatarGenerator.

The current implementation looks like this:

import com.gitblit.utils.ActivityUtils;
import com.google.inject.Singleton;

@Singleton
public class GravatarGenerator implements AvatarGenerator {

    @Override
    public String getURL(String username, String emailaddress, boolean identicon, int width) {
        String email = emailaddress == null ? username : emailaddress;
        if (identicon) {
            return ActivityUtils.getGravatarIdenticonUrl(email, width);
        } else {
            return ActivityUtils.getGravatarThumbnailUrl(email, width);
        }
    }
}

And then in your gitblit.properties file you will have to instruct Gitblit to use your avatar source:

web.avatarClass = com.gitblit.GravatarGenerator

If you'd like to contribute a JIRA-based implementation in a pull request then @paulsputer and I would be happy to review.

acoberlin commented 8 years ago

I think I have found a quite simple solution:

public static String getGravatarIdenticonUrl(String email, int width) {
    if (width <= 0) {
        width = 50;
    }   
    IStoredSettings settings = GitBlitWebApp.get().settings();
    String keyurl = settings.getString(Keys.web.avatarUrl, "https://www.gravatar.com/avatar");
    String emailHash = StringUtils.getMD5(email.toLowerCase());
    String url = MessageFormat.format(
            keyurl + "/{0}?s={1,number,0}&d=identicon", emailHash, width);
    return url;
}

and getGravatarThubnailUrl likewise

defaults.properties contains now a new Key # Define Custom Avatar Server URL # empty Url sets the use of the Gravatar Server # Example for Jira https://yourjiraserver/rest/avatar/1.0/server # SINCE 1.7.2 web.avatarUrl =

The only unfortunate thing now is, that the Junit Test fails because GitBlitWebApp.get().settings() is only available during runtime. I have not yet figured out a way to access the settings more elegantly.

Hope this helps :)