fixed git integration error

This commit is contained in:
marvzhang
2020-07-01 15:13:51 +08:00
parent 1cada25601
commit 468497ddc5
3 changed files with 33 additions and 4 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -40,6 +40,7 @@
<el-input
v-model="spiderForm.git_username"
:placeholder="$t('Git Username')"
@blur="onGitUrlChange"
/>
</el-form-item>
<el-form-item
@@ -51,6 +52,7 @@
v-model="spiderForm.git_password"
:placeholder="$t('Git Password')"
type="password"
@blur="onGitUrlChange"
/>
</el-form-item>
<el-form-item