1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-30 08:06:52 +02:00

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