support Bitbucket repos (#5)

This commit is contained in:
Ross Light 2017-07-06 10:18:53 -07:00 committed by Jaana B. Dogan
parent 713c74bb24
commit b514f2bb0d
2 changed files with 56 additions and 6 deletions

View File

@ -29,6 +29,7 @@ type handler struct {
m map[string]*struct {
Repo string `yaml:"repo,omitempty"`
Display string `yaml:"display,omitempty"`
VCS string `yaml:"vcs,omitempty"`
}
}
@ -37,12 +38,25 @@ func newHandler(config []byte) (*handler, error) {
if err := yaml.Unmarshal(config, &h.m); err != nil {
return nil, err
}
for _, e := range h.m {
if e.Display != "" {
continue
}
if strings.Contains(e.Repo, "github.com") {
for path, e := range h.m {
switch {
case e.Display != "":
// Already filled in.
case strings.HasPrefix(e.Repo, "https://github.com/"):
e.Display = fmt.Sprintf("%v %v/tree/master{/dir} %v/blob/master{/dir}/{file}#L{line}", e.Repo, e.Repo, e.Repo)
case strings.HasPrefix(e.Repo, "https://bitbucket.org"):
e.Display = fmt.Sprintf("%v %v/src/default{/dir} %v/src/default{/dir}/{file}#{file}-{line}", e.Repo, e.Repo, e.Repo)
}
switch {
case e.VCS != "":
// Already filled in.
if e.VCS != "bzr" && e.VCS != "git" && e.VCS != "hg" && e.VCS != "svn" {
return nil, fmt.Errorf("configuration for %v: unknown VCS %s", path, e.VCS)
}
case strings.HasPrefix(e.Repo, "https://github.com/"):
e.VCS = "git"
default:
return nil, fmt.Errorf("configuration for %v: cannot infer VCS from %s", path, e.Repo)
}
}
return h, nil
@ -64,10 +78,12 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Import string
Repo string
Display string
VCS string
}{
Import: host + current,
Repo: p.Repo,
Display: p.Display,
VCS: p.VCS,
}); err != nil {
http.Error(w, "cannot render the page", http.StatusInternalServerError)
}
@ -77,7 +93,7 @@ var vanityTmpl = template.Must(template.New("vanity").Parse(`<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="go-import" content="{{.Import}} git {{.Repo}}">
<meta name="go-import" content="{{.Import}} {{.VCS}} {{.Repo}}">
<meta name="go-source" content="{{.Import}} {{.Display}}">
<meta http-equiv="refresh" content="0; url=https://godoc.org/{{.Import}}">
</head>

View File

@ -48,6 +48,24 @@ func TestHandler(t *testing.T) {
goImport: "example.com/portmidi git https://github.com/rakyll/portmidi",
goSource: "example.com/portmidi https://github.com/rakyll/portmidi https://github.com/rakyll/portmidi/tree/master{/dir} https://github.com/rakyll/portmidi/blob/master{/dir}/{file}#L{line}",
},
{
name: "Bitbucket Mercurial",
config: "/gopdf:\n" +
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
" vcs: hg\n",
path: "/gopdf",
goImport: "example.com/gopdf hg https://bitbucket.org/zombiezen/gopdf",
goSource: "example.com/gopdf https://bitbucket.org/zombiezen/gopdf https://bitbucket.org/zombiezen/gopdf/src/default{/dir} https://bitbucket.org/zombiezen/gopdf/src/default{/dir}/{file}#{file}-{line}",
},
{
name: "Bitbucket Git",
config: "/mygit:\n" +
" repo: https://bitbucket.org/zombiezen/mygit\n" +
" vcs: git\n",
path: "/mygit",
goImport: "example.com/mygit git https://bitbucket.org/zombiezen/mygit",
goSource: "example.com/mygit https://bitbucket.org/zombiezen/mygit https://bitbucket.org/zombiezen/mygit/src/default{/dir} https://bitbucket.org/zombiezen/mygit/src/default{/dir}/{file}#{file}-{line}",
},
}
for _, test := range tests {
h, err := newHandler([]byte(test.config))
@ -82,6 +100,22 @@ func TestHandler(t *testing.T) {
}
}
func TestBadConfigs(t *testing.T) {
badConfigs := []string{
"/missingvcs:\n" +
" repo: https://bitbucket.org/zombiezen/gopdf\n",
"/unknownvcs:\n" +
" repo: https://bitbucket.org/zombiezen/gopdf\n" +
" vcs: xyzzy\n",
}
for _, config := range badConfigs {
_, err := newHandler([]byte(config))
if err == nil {
t.Errorf("expected config to produce an error, but did not:\n%s", config)
}
}
}
func findMeta(data []byte, name string) string {
var sep []byte
sep = append(sep, `<meta name="`...)