From 5c65ed4da15422aaa23786fe4f9a0d69675c6715 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Tue, 18 Feb 2020 13:42:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9ssh=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/services/git.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/backend/services/git.go b/backend/services/git.go index 14fcb52d..27b5420c 100644 --- a/backend/services/git.go +++ b/backend/services/git.go @@ -140,8 +140,23 @@ func SyncSpiderGit(s model.Spider) (err error) { var auth ssh.AuthMethod if !strings.HasPrefix(s.GitUrl, "http") { // 为 SSH - u, _ := url.Parse(s.GitUrl) - auth, _ = ssh.NewPublicKeysFromFile(u.User.String(), path.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), "") + regex := regexp.MustCompile("^(?:ssh://?)?([0-9a-zA-Z_]+)@") + res := regex.FindStringSubmatch(s.GitUrl) + username := s.GitUsername + if username == "" { + if len(res) > 1 { + username = res[1] + } else { + username = "git" + } + } + auth, err = ssh.NewPublicKeysFromFile(username, path.Join(os.Getenv("HOME"), ".ssh", "id_rsa"), "") + if err != nil { + log.Error(err.Error()) + debug.PrintStack() + SaveSpiderGitSyncError(s, err.Error()) + return err + } } // 获取 repo @@ -166,6 +181,9 @@ func SyncSpiderGit(s model.Spider) (err error) { Auth: auth, }); err != nil { if err.Error() == "already up-to-date" { + // 检查是否为 Scrapy + sync := spider_handler.SpiderSync{Spider: s} + sync.CheckIsScrapy() // 如果没有错误,则保存空字符串 SaveSpiderGitSyncError(s, "") return nil