From fbf36458f128dee22c6573fedd3c4f329a6ac470 Mon Sep 17 00:00:00 2001 From: Kirilll Zaycev Date: Mon, 1 Feb 2016 02:49:52 +0300 Subject: [PATCH] Committer avatar --- remote/gitlab/gitlab.go | 7 +++++++ remote/gitlab/helper.go | 18 ++++++++++++++++++ template/amber/repo.amber | 5 ++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/remote/gitlab/gitlab.go b/remote/gitlab/gitlab.go index 79471911..01b86e48 100644 --- a/remote/gitlab/gitlab.go +++ b/remote/gitlab/gitlab.go @@ -372,6 +372,10 @@ func mergeRequest(parsed *client.HookPayload, req *http.Request) (*model.Repo, * build.Author = parsed.ObjectAttributes.LastCommit.Author.Name build.Email = parsed.ObjectAttributes.LastCommit.Author.Email + if len(build.Email) != 0 { + build.Avatar = GetUserAvatar(build.Email) + } + build.Title = parsed.ObjectAttributes.Title build.Link = parsed.ObjectAttributes.Url @@ -417,6 +421,9 @@ func push(parsed *client.HookPayload, req *http.Request) (*model.Repo, *model.Bu case head.Author != nil: build.Email = head.Author.Email build.Author = parsed.UserName + if len(build.Email) != 0 { + build.Avatar = GetUserAvatar(build.Email) + } case head.Author == nil: build.Author = parsed.UserName } diff --git a/remote/gitlab/helper.go b/remote/gitlab/helper.go index 62edc019..616a6a04 100644 --- a/remote/gitlab/helper.go +++ b/remote/gitlab/helper.go @@ -1,6 +1,8 @@ package gitlab import ( + "crypto/md5" + "encoding/hex" "fmt" "net/url" "strconv" @@ -8,6 +10,10 @@ import ( "github.com/drone/drone/remote/gitlab/client" ) +const ( + gravatarBase = "https://www.gravatar.com/avatar" +) + // NewClient is a helper function that returns a new GitHub // client using the provided OAuth token. func NewClient(url, accessToken string, skipVerify bool) *client.Client { @@ -79,6 +85,18 @@ func ns(owner, name string) string { return fmt.Sprintf("%s%%2F%s", owner, name) } +func GetUserAvatar(email string) string { + hasher := md5.New() + hasher.Write([]byte(email)) + + return fmt.Sprintf( + "%s/%v.jpg?s=%s", + gravatarBase, + hex.EncodeToString(hasher.Sum(nil)), + "128", + ) +} + func GetUserEmail(c *client.Client, defaultURL string) (*client.Client, error) { return c, nil } diff --git a/template/amber/repo.amber b/template/amber/repo.amber index 79e7d9dc..d7358c69 100644 --- a/template/amber/repo.amber +++ b/template/amber/repo.amber @@ -32,7 +32,10 @@ block content each $build in $group.Builds a.card[href=$repo.Name+"/"+$build.Number][data-build=$build.Number] div.card-header - img[src=$build.Avatar] + if $build.Avatar != "" + img[src=$build.Avatar] + else + img[src="/static/images/dummy.png"] div.card-block div div.status[class=$build.Status] #{$build.Status}