diff options
| author | Tyler Sullivan <sullivtr@gmail.com> | 2022-01-03 16:36:17 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-03 16:36:17 -0800 |
| commit | 8a0c579592136da8105d54081c3f69060c4dcf71 (patch) | |
| tree | e9a54b6e9ca5fc1f6d31af3e3bd23abafce43218 | |
| parent | dd62ebd406a23184ec072d38894a5a3aac141b0d (diff) | |
| download | sally-8a0c579592136da8105d54081c3f69060c4dcf71.tar.gz | |
Support package-level overrides for URLs (#52)
Sally accepts the base vanity URL (e.g. `go.uber.org`) in a top-level
`url` key. This applies to all packages listed in the configuration.
Add support for overriding the `url` on a per-package basis.
This will provide for an easier transition period when migrating Go
packages of an organization between hosts, for example from BitBucket
to GitHub.
With this, source code can be modified across the various
repositories over time to use the new vanity URL. For example, some
packages will use a URL of bitbucketurl.org and some will use
mycoolgoimportvanity.org in their source code imports.
Other than the use-case outlined above, this feature adds flexibility
to the vanity server to support more than one vanity URL when used
behind an ingress controller.
| -rw-r--r-- | config.go | 1 | ||||
| -rw-r--r-- | config_test.go | 20 | ||||
| -rw-r--r-- | handler.go | 6 | ||||
| -rw-r--r-- | handler_test.go | 20 | ||||
| -rw-r--r-- | templates/index.html | 3 |
5 files changed, 49 insertions, 1 deletions
@@ -25,6 +25,7 @@ type Config struct { type Package struct { Repo string `yaml:"repo"` Branch string `yaml:"branch"` + URL string `yaml:"url"` } // ensureAlphabetical checks that the packages are listed alphabetically in the configuration. diff --git a/config_test.go b/config_test.go index e87954b..eb6c463 100644 --- a/config_test.go +++ b/config_test.go @@ -51,6 +51,26 @@ packages: assert.Equal(t, pkg, Package{Repo: "github.com/grpc/grpc-go", Branch: "master"}) } +func TestParsePackageLevelURL(t *testing.T) { + path, clean := TempFile(t, ` + +url: google.golang.org +packages: + grpc: + repo: github.com/grpc/grpc-go + url: go.uber.org + +`) + defer clean() + + config, err := Parse(path) + assert.NoError(t, err) + + pkg, ok := config.Packages["grpc"] + assert.True(t, ok) + assert.Equal(t, pkg.URL, "go.uber.org") +} + func TestParseGodocServer(t *testing.T) { tests := []struct { give string @@ -53,7 +53,11 @@ type packageHandler struct { } func (h packageHandler) Handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - canonicalURL := fmt.Sprintf("%s/%s", h.config.URL, h.pkgName) + baseURL := h.config.URL + if h.pkg.URL != "" { + baseURL = h.pkg.URL + } + canonicalURL := fmt.Sprintf("%s/%s", baseURL, h.pkgName) data := struct { Repo string Branch string diff --git a/handler_test.go b/handler_test.go index 978f62d..96a10dd 100644 --- a/handler_test.go +++ b/handler_test.go @@ -14,6 +14,9 @@ packages: repo: github.com/thriftrw/thriftrw-go yarpc: repo: github.com/yarpc/yarpc-go + zap: + url: go.uberalt.org + repo: github.com/uber-go/zap ` @@ -98,3 +101,20 @@ func TestDeepImports(t *testing.T) { </html> `) } + +func TestPackageLevelURL(t *testing.T) { + rr := CallAndRecord(t, config, "/zap") + AssertResponse(t, rr, 200, ` +<!DOCTYPE html> +<html> + <head> + <meta name="go-import" content="go.uberalt.org/zap git https://github.com/uber-go/zap"> + <meta name="go-source" content="go.uberalt.org/zap https://github.com/uber-go/zap https://github.com/uber-go/zap/tree/master{/dir} https://github.com/uber-go/zap/tree/master{/dir}/{file}#L{line}"> + <meta http-equiv="refresh" content="0; url=https://pkg.go.dev/go.uberalt.org/zap"> + </head> + <body> + Nothing to see here. Please <a href="https://pkg.go.dev/go.uberalt.org/zap">move along</a>. + </body> +</html> +`) +} diff --git a/templates/index.html b/templates/index.html index 50b6004..2599bf0 100644 --- a/templates/index.html +++ b/templates/index.html @@ -17,6 +17,9 @@ <tbody> {{ range $key, $value := .Packages }} {{ $importPath := printf "%v/%v" $.URL $key }} + {{ if ne $value.URL "" }} + {{ $importPath = printf "%v/%v" $value.URL $key }} + {{ end }} <tr> <td>{{ $importPath }}</td> <td> |