aboutsummaryrefslogtreecommitdiff
path: root/git-codereview/api.go
diff options
context:
space:
mode:
authorDaniel Theophanes <kardianos@gmail.com>2016-03-10 16:40:13 -0800
committerAndrew Gerrand <adg@golang.org>2016-05-30 05:05:07 +0000
commit510dcfe65574591e23e3e3e8488deee0642b2f2e (patch)
tree0b3aab9eaa4519ce49d345bef8fe95d070a3a261 /git-codereview/api.go
parent13ec236026ca7434479884eab6a8e4ea7047aaa5 (diff)
downloadgo-x-review-510dcfe65574591e23e3e3e8488deee0642b2f2e.tar.xz
review: handle hosting Gerrit in a sub-path
The git-review module is useful for other gerrit sites. Some host gerrit under a sub-path. Add tests for both googlesource.com repos and non-googlesource.com repos. Change-Id: If2848128d6957db61ac50cbab7e6927da431002b Reviewed-on: https://go-review.googlesource.com/20553 Reviewed-by: Andrew Gerrand <adg@golang.org> Run-TryBot: Andrew Gerrand <adg@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'git-codereview/api.go')
-rw-r--r--git-codereview/api.go85
1 files changed, 64 insertions, 21 deletions
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 6fbdd48..786ac60 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -11,6 +11,7 @@ import (
"io"
"io/ioutil"
"net/http"
+ "net/url"
"os"
"sort"
"strings"
@@ -43,38 +44,80 @@ func loadGerritOrigin() {
// Gerrit must be set, either explicitly via the code review config or
// implicitly as Git's origin remote.
origin := config()["gerrit"]
- if origin == "" {
- origin = trim(cmdOutput("git", "config", "remote.origin.url"))
+ originUrl := trim(cmdOutput("git", "config", "remote.origin.url"))
+
+ err := loadGerritOriginInternal(origin, originUrl)
+ if err != nil {
+ dief("failed to load Gerrit origin: %v", err)
}
+}
+// loadGerritOriginInternal does the work of loadGerritOrigin, just extracted out
+// for easier testing.
+func loadGerritOriginInternal(origin, remoteOrigin string) error {
+ originUrl, err := url.Parse(remoteOrigin)
+ if err != nil {
+ return fmt.Errorf("failed to parse git's remote.origin.url %q as a URL: %v", remoteOrigin, err)
+ } else {
+ originUrl.User = nil
+ remoteOrigin = originUrl.String()
+ }
+ hasGerritConfig := true
+ if origin == "" {
+ hasGerritConfig = false
+ origin = remoteOrigin
+ }
if strings.Contains(origin, "github.com") {
- dief("git origin must be a Gerrit host, not GitHub: %s", origin)
+ return fmt.Errorf("git origin must be a Gerrit host, not GitHub: %s", origin)
}
if !strings.HasPrefix(origin, "https://") {
- dief("git origin must be an https:// URL: %s", origin)
- }
- // https:// prefix and then one slash between host and top-level name
- if strings.Count(origin, "/") != 3 {
- dief("git origin is malformed: %s", origin)
+ return fmt.Errorf("git origin must be an https:// URL: %s", origin)
}
- host := origin[len("https://"):strings.LastIndex(origin, "/")]
- // In the case of Google's Gerrit, host is go.googlesource.com
- // and apiURL uses go-review.googlesource.com, but the Gerrit
- // setup instructions do not write down a cookie explicitly for
- // go-review.googlesource.com, so we look for the non-review
- // host name instead.
- url := origin
- if i := strings.Index(url, ".googlesource.com"); i >= 0 {
+ if googlesourceIndex := strings.Index(origin, ".googlesource.com"); googlesourceIndex >= 0 {
+ // https:// prefix and then one slash between host and top-level name
+ if strings.Count(origin, "/") != 3 {
+ return fmt.Errorf("git origin is malformed: %s", origin)
+ }
+ host := origin[len("https://"):strings.LastIndex(origin, "/")]
+
+ // In the case of Google's Gerrit, host is go.googlesource.com
+ // and apiURL uses go-review.googlesource.com, but the Gerrit
+ // setup instructions do not write down a cookie explicitly for
+ // go-review.googlesource.com, so we look for the non-review
+ // host name instead.
+ url := origin
+ i := googlesourceIndex
url = url[:i] + "-review" + url[i:]
+
+ i = strings.LastIndex(url, "/")
+ url, project := url[:i], url[i+1:]
+
+ auth.host = host
+ auth.url = url
+ auth.project = project
+ return nil
+ }
+
+ // Origin is not *.googlesource.com.
+ //
+ // If the Gerrit origin is set from the codereview.cfg file than we handle it
+ // differently to allow for sub-path hosted Gerrit.
+ auth.host = originUrl.Host
+ if hasGerritConfig {
+ if !strings.HasPrefix(remoteOrigin, origin) {
+ return fmt.Errorf("Gerrit origin %q from %q different then git origin url %q", origin, configRef, originUrl)
+ }
+
+ auth.project = strings.Trim(strings.TrimPrefix(remoteOrigin, origin), "/")
+ auth.url = origin
+ } else {
+ auth.project = strings.Trim(originUrl.Path, "/")
+ auth.url = strings.TrimSuffix(remoteOrigin, originUrl.Path)
}
- i := strings.LastIndex(url, "/")
- url, project := url[:i], url[i+1:]
- auth.host = host
- auth.url = url
- auth.project = project
+ return nil
}
// loadAuth loads the authentication tokens for making API calls to