Skip to content

Commit

Permalink
zoekt-mirror-gerrit: handle http authentication (#770)
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier-calland authored Apr 30, 2024
1 parent 570757e commit 68d0465
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/zoekt-mirror-gerrit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func main() {
for _, s := range []string{"http", "anonymous http"} {
if schemeInfo, ok := info.Download.Schemes[s]; ok {
projectURL = schemeInfo.URL
if s == "http" && schemeInfo.IsAuthRequired {
projectURL = addPassword(projectURL, rootURL.User)
// remove "/a/" prefix needed for API call with basic auth but not with git command → cleaner repo name
projectURL = strings.Replace(projectURL, "/a/${project}", "/${project}", 1)
}
break
}
}
Expand Down Expand Up @@ -187,7 +192,7 @@ func main() {
config := map[string]string{
"zoekt.name": zoektName,
"zoekt.gerrit-project": k,
"zoekt.gerrit-host": rootURL.String(),
"zoekt.gerrit-host": anonymousURL(rootURL),
"zoekt.archived": marshalBool(v.State == "READ_ONLY"),
"zoekt.public": marshalBool(v.State != "HIDDEN"),
}
Expand Down Expand Up @@ -246,3 +251,15 @@ func marshalBool(b bool) string {
}
return "0"
}

func anonymousURL(u *url.URL) string {
anon := *u
anon.User = nil
return anon.String()
}

func addPassword(u string, user *url.Userinfo) string {
password, _ := user.Password()
username := user.Username()
return strings.Replace(u, fmt.Sprintf("://%s@", username), fmt.Sprintf("://%s:%s@", username, password), 1)
}

0 comments on commit 68d0465

Please sign in to comment.