Populate URL field of API commits (#3546)

* Populate URL field of API commits

* fix orgmode_test
This commit is contained in:
Ethan Koenig
2018-02-20 04:50:42 -08:00
committed by Bo-Yi Wu
parent 7b297808ce
commit 7b104f0cd0
11 changed files with 126 additions and 98 deletions

View File

@ -4,6 +4,13 @@
package util
import (
"net/url"
"path"
"code.gitea.io/gitea/modules/log"
)
// OptionalBool a boolean that can be "null"
type OptionalBool byte
@ -47,6 +54,20 @@ func Max(a, b int) int {
return a
}
// URLJoin joins url components, like path.Join, but preserving contents
func URLJoin(base string, elems ...string) string {
u, err := url.Parse(base)
if err != nil {
log.Error(4, "URLJoin: Invalid base URL %s", base)
return ""
}
joinArgs := make([]string, 0, len(elems)+1)
joinArgs = append(joinArgs, u.Path)
joinArgs = append(joinArgs, elems...)
u.Path = path.Join(joinArgs...)
return u.String()
}
// Min min of two ints
func Min(a, b int) int {
if a > b {

36
modules/util/util_test.go Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2018 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package util
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestURLJoin(t *testing.T) {
type test struct {
Expected string
Base string
Elements []string
}
newTest := func(expected, base string, elements ...string) test {
return test{Expected: expected, Base: base, Elements: elements}
}
for _, test := range []test{
newTest("https://try.gitea.io/a/b/c",
"https://try.gitea.io", "a/b", "c"),
newTest("https://try.gitea.io/a/b/c",
"https://try.gitea.io/", "/a/b/", "/c/"),
newTest("https://try.gitea.io/a/c",
"https://try.gitea.io/", "/a/./b/", "../c/"),
newTest("a/b/c",
"a", "b/c/"),
newTest("a/b/d",
"a/", "b/c/", "/../d/"),
} {
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
}
}