diff options
| author | Taras Madan <tarasmadan@google.com> | 2025-02-13 14:01:15 +0100 |
|---|---|---|
| committer | Taras Madan <tarasmadan@google.com> | 2025-02-13 14:54:04 +0000 |
| commit | eb791944826a22c0fe8f1377831aed7681858b83 (patch) | |
| tree | b9f8d542f816190e70c6926a174a880016e4db05 | |
| parent | a98a8417f2c0dfdab1e8d3d49e8a7605340ada89 (diff) | |
dashboard/app: support gerrit servers as a webgit source
Gerrit servers return base64 encoded file content.
To get the raw data, &format=TEXT request param is needed.
| -rw-r--r-- | dashboard/app/config.go | 1 | ||||
| -rw-r--r-- | dashboard/app/coverage.go | 5 | ||||
| -rw-r--r-- | pkg/covermerger/provider_web.go | 30 |
3 files changed, 29 insertions, 7 deletions
diff --git a/dashboard/app/config.go b/dashboard/app/config.go index c71d09d8e..836344954 100644 --- a/dashboard/app/config.go +++ b/dashboard/app/config.go @@ -165,6 +165,7 @@ type CoverageConfig struct { DashboardClientName string // WebGitURI specifies where can we get the kernel file source code directly from AppEngine. + // It may be the Git or Gerrit compatible repo. WebGitURI string } diff --git a/dashboard/app/coverage.go b/dashboard/app/coverage.go index 3535dff9e..4c83f299f 100644 --- a/dashboard/app/coverage.go +++ b/dashboard/app/coverage.go @@ -146,7 +146,10 @@ func handleHeatmap(c context.Context, w http.ResponseWriter, r *http.Request, f func makeProxyURIProvider(url string) covermerger.FuncProxyURI { return func(filePath, commit string) string { - return fmt.Sprintf("%s/%s/%s", url, commit, filePath) + // Parameter format=TEXT is ignored by git servers but is processed by gerrit servers. + // Gerrit returns base64 encoded data. + // Git return the plain text data. + return fmt.Sprintf("%s/%s/%s?format=TEXT", url, commit, filePath) } } diff --git a/pkg/covermerger/provider_web.go b/pkg/covermerger/provider_web.go index e172d47e9..1c0442cae 100644 --- a/pkg/covermerger/provider_web.go +++ b/pkg/covermerger/provider_web.go @@ -4,11 +4,13 @@ package covermerger import ( + "encoding/base64" "errors" "fmt" "io" "net/http" "net/url" + "strings" ) type FuncProxyURI func(filePath, commit string) string @@ -52,25 +54,41 @@ func (mr *webGit) loadFile(filePath, repo, commit string) ([]byte, error) { } u.Scheme = "https" uri = u.String() - res, err := http.Get(uri) + resp, err := http.Get(uri) if err != nil { return nil, fmt.Errorf("failed to http.Get: %w", err) } - defer res.Body.Close() + defer resp.Body.Close() - if res.StatusCode == 404 { + if resp.StatusCode == 404 { return nil, errFileNotFound } - if res.StatusCode != 200 { - return nil, fmt.Errorf("error: status %d getting '%s'", res.StatusCode, uri) + if resp.StatusCode != 200 { + return nil, fmt.Errorf("error: status %d getting '%s'", resp.StatusCode, uri) } - body, err := io.ReadAll(res.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to io.ReadAll from body: %w", err) } + if isGerritServer(resp) { + if body, err = base64.StdEncoding.DecodeString(string(body)); err != nil { + return nil, fmt.Errorf("base64.StdEncoding.DecodeString: %w", err) + } + } return body, nil } +func isGerritServer(resp *http.Response) bool { + for _, headerVals := range resp.Header { + for _, header := range headerVals { + if strings.Contains(header, "gerrit") { + return true + } + } + } + return false +} + func MakeWebGit(funcProxy FuncProxyURI) FileVersProvider { return &webGit{ funcProxy: funcProxy, |
