diff --git a/backend/routes/git.go b/backend/routes/git.go index 23685b05..aa889be2 100644 --- a/backend/routes/git.go +++ b/backend/routes/git.go @@ -10,7 +10,9 @@ import ( func GetGitRemoteBranches(c *gin.Context) { url := c.Query("url") - branches, err := services.GetGitRemoteBranchesPlain(url) + username := c.Query("username") + password := c.Query("password") + branches, err := services.GetGitRemoteBranchesPlain(url, username, password) if err != nil { HandleError(http.StatusInternalServerError, c, err) return diff --git a/backend/services/git.go b/backend/services/git.go index e629a5ff..e0a57a03 100644 --- a/backend/services/git.go +++ b/backend/services/git.go @@ -137,13 +137,38 @@ func SaveSpiderGitSyncError(s model.Spider, errMsg string) { } // 获得Git分支 -func GetGitRemoteBranchesPlain(url string) (branches []string, err error) { +func GetGitRemoteBranchesPlain(gitUrl string, username string, password string) (branches []string, err error) { storage := memory.NewStorage() + u, err := url.Parse(gitUrl) + if err != nil { + return branches, err + } + var listOptions git.ListOptions + if strings.HasPrefix(gitUrl, "http") { + gitUrl = fmt.Sprintf( + "%s://%s:%s@%s%s", + u.Scheme, + username, + password, + u.Hostname(), + u.Path, + ) + } else { + auth, err := ssh.NewPublicKeysFromFile(username, path.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), "") + if err != nil { + log.Error(err.Error()) + debug.PrintStack() + return branches, err + } + listOptions = git.ListOptions{ + Auth: auth, + } + } remote := git.NewRemote(storage, &config.RemoteConfig{ URLs: []string{ - url, + gitUrl, }}) - rfs, err := remote.List(&git.ListOptions{}) + rfs, err := remote.List(&listOptions) if err != nil { return } diff --git a/frontend/src/components/Settings/GitSettings.vue b/frontend/src/components/Settings/GitSettings.vue index 02d06658..4f092bf4 100644 --- a/frontend/src/components/Settings/GitSettings.vue +++ b/frontend/src/components/Settings/GitSettings.vue @@ -40,6 +40,7 @@