From cacfd0c44414c61b7d39d41508f85b401caa155e Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Wed, 6 Jul 2016 15:58:31 +0200 Subject: [PATCH] Fix resolution of relative avatar urls for Gogs published in subpath Closes #1701 Includes a new test, and updates expected results for 3 of them (adding the schema part) --- remote/gogs/helper.go | 22 +++++++++++++++------- remote/gogs/helper_test.go | 12 ++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/remote/gogs/helper.go b/remote/gogs/helper.go index 32b1fb52..dd6f91ed 100644 --- a/remote/gogs/helper.go +++ b/remote/gogs/helper.go @@ -120,15 +120,23 @@ func fixMalformedAvatar(url string) string { } // expandAvatar is a helper function that converts a relative avatar URL to the -// abosolute url. +// absolute url. func expandAvatar(repo, rawurl string) string { - if !strings.HasPrefix(rawurl, "/avatars/") { - return rawurl - } - url, err := url.Parse(repo) + aurl, err := url.Parse(rawurl) if err != nil { return rawurl } - url.Path = rawurl - return url.String() + if aurl.IsAbs() { + // Url is already absolute + return aurl.String() + } + + // Resolve to base + burl, err := url.Parse(repo) + if err != nil { + return rawurl + } + aurl = burl.ResolveReference(aurl) + + return aurl.String() } diff --git a/remote/gogs/helper_test.go b/remote/gogs/helper_test.go index c203a856..e6e63a02 100644 --- a/remote/gogs/helper_test.go +++ b/remote/gogs/helper_test.go @@ -47,7 +47,7 @@ func Test_parse(t *testing.T) { g.Assert(build.Link).Equal(hook.Compare) g.Assert(build.Branch).Equal("master") g.Assert(build.Message).Equal(hook.Commits[0].Message) - g.Assert(build.Avatar).Equal("//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87") + g.Assert(build.Avatar).Equal("http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87") g.Assert(build.Author).Equal(hook.Sender.Login) }) @@ -92,7 +92,7 @@ func Test_parse(t *testing.T) { FullName: "gophers/hello-world", Owner: gogs.User{ UserName: "gordon", - AvatarUrl: "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + AvatarUrl: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", }, CloneUrl: "http://gogs.golang.org/gophers/hello-world.git", HtmlUrl: "http://gogs.golang.org/gophers/hello-world", @@ -114,7 +114,7 @@ func Test_parse(t *testing.T) { FullName: "gophers/hello-world", Owner: gogs.User{ UserName: "gordon", - AvatarUrl: "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + AvatarUrl: "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", }, } repo := toRepoLite(&from) @@ -165,7 +165,11 @@ func Test_parse(t *testing.T) { }, { "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", - "//1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + "http://1.gravatar.com/avatar/8c58a0be77ee441bb8f8595b7f1b4e87", + }, + { + "/gogs/avatars/2", + "http://gogs.io/gogs/avatars/2", }, }