handle gogs custom gravatars. fixes #1263

This commit is contained in:
Brad Rydzewski 2015-10-27 10:32:14 -07:00
parent 56ba42d9a9
commit b5a90e0a88
2 changed files with 52 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/url"
"strings" "strings"
"time" "time"
@ -15,11 +16,15 @@ import (
// to a Drone repository. // to a Drone repository.
func toRepoLite(from *gogs.Repository) *model.RepoLite { func toRepoLite(from *gogs.Repository) *model.RepoLite {
name := strings.Split(from.FullName, "/")[1] name := strings.Split(from.FullName, "/")[1]
avatar := expandAvatar(
from.HtmlUrl,
from.Owner.AvatarUrl,
)
return &model.RepoLite{ return &model.RepoLite{
Name: name, Name: name,
Owner: from.Owner.UserName, Owner: from.Owner.UserName,
FullName: from.FullName, FullName: from.FullName,
Avatar: from.Owner.AvatarUrl, Avatar: avatar,
} }
} }
@ -27,11 +32,15 @@ func toRepoLite(from *gogs.Repository) *model.RepoLite {
// to a Drone repository. // to a Drone repository.
func toRepo(from *gogs.Repository) *model.Repo { func toRepo(from *gogs.Repository) *model.Repo {
name := strings.Split(from.FullName, "/")[1] name := strings.Split(from.FullName, "/")[1]
avatar := expandAvatar(
from.HtmlUrl,
from.Owner.AvatarUrl,
)
return &model.Repo{ return &model.Repo{
Name: name, Name: name,
Owner: from.Owner.UserName, Owner: from.Owner.UserName,
FullName: from.FullName, FullName: from.FullName,
Avatar: from.Owner.AvatarUrl, Avatar: avatar,
Link: from.HtmlUrl, Link: from.HtmlUrl,
IsPrivate: from.Private, IsPrivate: from.Private,
Clone: from.CloneUrl, Clone: from.CloneUrl,
@ -52,6 +61,10 @@ func toPerm(from gogs.Permission) *model.Perm {
// helper function that extracts the Build data // helper function that extracts the Build data
// from a Gogs push hook // from a Gogs push hook
func buildFromPush(hook *PushHook) *model.Build { func buildFromPush(hook *PushHook) *model.Build {
avatar := expandAvatar(
hook.Repo.Url,
fixMalformedAvatar(hook.Sender.Avatar),
)
return &model.Build{ return &model.Build{
Event: model.EventPush, Event: model.EventPush,
Commit: hook.After, Commit: hook.After,
@ -59,7 +72,7 @@ func buildFromPush(hook *PushHook) *model.Build {
Link: hook.Compare, Link: hook.Compare,
Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"), Branch: strings.TrimPrefix(hook.Ref, "refs/heads/"),
Message: hook.Commits[0].Message, Message: hook.Commits[0].Message,
Avatar: fixMalformedAvatar(hook.Sender.Avatar), Avatar: avatar,
Author: hook.Sender.Login, Author: hook.Sender.Login,
Timestamp: time.Now().UTC().Unix(), Timestamp: time.Now().UTC().Unix(),
} }
@ -98,3 +111,17 @@ func fixMalformedAvatar(url string) string {
} }
return url return url
} }
// expandAvatar is a helper function that converts
// a relative avatar URL to the abosolute url.
func expandAvatar(repo, rawurl string) string {
if !strings.HasPrefix(rawurl, "/avatars/") {
return rawurl
}
url_, err := url.Parse(repo)
if err != nil {
return rawurl
}
url_.Path = rawurl
return url_.String()
}

View file

@ -123,5 +123,27 @@ func Test_parse(t *testing.T) {
g.Assert(url).Equal("//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87") g.Assert(url).Equal("//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87")
} }
}) })
g.It("Should expand the avatar url", func() {
var urls = []struct {
Before string
After string
}{
{
"/avatars/1",
"http://gogs.io/avatars/1",
},
{
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
"//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87",
},
}
var repo = "http://gogs.io/foo/bar"
for _, url := range urls {
got := expandAvatar(repo, url.Before)
g.Assert(got).Equal(url.After)
}
})
}) })
} }