From cda216c19b1c6265056349c996bf43caf49d5254 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Mon, 16 Jan 2023 17:12:37 +0800 Subject: [PATCH] #3338 add repo paper --- models/models.go | 1 + models/repo_paper.go | 23 ++++++++++ modules/auth/repo_form.go | 1 + modules/repo-migrate/migrate.go | 93 +++++++++++++++++++++++++++++++++++++++++ modules/setting/setting.go | 10 +++++ modules/structs/repo.go | 1 + modules/task/task.go | 1 + routers/api/v1/api.go | 2 + routers/api/v1/repo/migrate.go | 42 +++++++++++++++++++ 9 files changed, 174 insertions(+) create mode 100644 models/repo_paper.go create mode 100644 modules/repo-migrate/migrate.go diff --git a/models/models.go b/models/models.go index 6427c576c..af7064b83 100755 --- a/models/models.go +++ b/models/models.go @@ -167,6 +167,7 @@ func init() { new(Badge), new(BadgeUser), new(BadgeUserLog), + new(RepoPaper), ) tablesStatistic = append(tablesStatistic, diff --git a/models/repo_paper.go b/models/repo_paper.go new file mode 100644 index 000000000..0d1588c32 --- /dev/null +++ b/models/repo_paper.go @@ -0,0 +1,23 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "code.gitea.io/gitea/modules/timeutil" +) + +// RepoPaper represents related paper information of a repository. +type RepoPaper struct { + ID int64 `xorm:"pk autoincr"` + RepoID int64 `xorm:"INDEX"` + PaperUrl string `xorm:"INDEX"` + CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"` +} + +func InsertRepoPaper(p *RepoPaper) error { + _, err := x.Insert(p) + return err +} diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 9ea5da879..45c031c92 100755 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -74,6 +74,7 @@ type MigrateRepoForm struct { Issues bool `json:"issues"` PullRequests bool `json:"pull_requests"` Releases bool `json:"releases"` + PaperUrl string `json:"paper_url"` } // Validate validates the fields diff --git a/modules/repo-migrate/migrate.go b/modules/repo-migrate/migrate.go new file mode 100644 index 000000000..e7db6098b --- /dev/null +++ b/modules/repo-migrate/migrate.go @@ -0,0 +1,93 @@ +package repo_migrate + +import ( + "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/routers/response" + "crypto/tls" + "errors" + "fmt" + "github.com/go-resty/resty/v2" + "strings" +) + +var ( + restyClient *resty.Client +) + +func getRestyClient() *resty.Client { + if restyClient == nil { + restyClient = resty.New() + restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) + } + return restyClient +} + +func CommitFile(fileUrl string) (interface{}, error) { + client := getRestyClient() + var result response.AiforgeResponse + + _, err := client.R(). + SetResult(&result). + Post(fmt.Sprintf("%s/api/repo/migrate/commit?file=%s", setting.RepoMigrateHost, fileUrl)) + + if err != nil { + return "", errors.New("service not available") + } + + if result.Code > 0 { + log.Error("commitFile failed(%d): %s", result.Code, result.Msg) + return "", errors.New(result.Msg) + + } + return result.Data, nil +} + +func QueryFileStatus(md5 string) (interface{}, error) { + client := getRestyClient() + var result response.AiforgeResponse + + _, err := client.R(). + SetResult(&result). + Get(fmt.Sprintf("%s/api/repo/task/query?md5=%s", setting.RepoMigrateHost, md5)) + + if err != nil { + return "", errors.New("service not available") + } + + if result.Code > 0 { + log.Error("QueryFileStatus failed(%d): %s", result.Code, result.Msg) + return "", errors.New(result.Msg) + + } + return result.Data, nil +} + +func Auth(userName string) bool { + if isWhiteListEffect() && isInWhiteList(userName) { + return true + } + + if !isWhiteListEffect() { + return true + } + + return false +} + +func isWhiteListEffect() bool { + return setting.IsRepoMigrateWhiteListEffect +} + +func isInWhiteList(userName string) bool { + for _, name := range getWhiteList() { + if strings.ToUpper(name) == strings.ToUpper(userName) { + return true + } + } + return false +} + +func getWhiteList() []string { + return setting.RepoMigrateWhiteList +} diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 09e7259f2..154df1998 100755 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -690,6 +690,11 @@ var ( IncubationSourceOrgName string PaperRepoTopicName string + //repo-migrate config + RepoMigrateHost string + IsRepoMigrateWhiteListEffect bool + RepoMigrateWhiteList []string + //nginx proxy PROXYURL string RadarMap = struct { @@ -1614,6 +1619,11 @@ func NewContext() { IncubationSourceOrgName = sec.Key("INCUBATION_ORG_NAME").MustString("OpenI") PaperRepoTopicName = sec.Key("PAPER_REPO_TOPIC_NAME").MustString("openi-paper") + sec = Cfg.Section("repo-migrate") + RepoMigrateHost = sec.Key("HOST").MustString("") + IsRepoMigrateWhiteListEffect = sec.Key("IS_WHITE_LIST_EFFECT").MustBool(false) + RepoMigrateWhiteList = strings.Split(sec.Key("WHITE_LIST").MustString(""), ",") + sec = Cfg.Section("point") CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false) CloudBrainPayDelay = sec.Key("CLOUDBRAIN_PAY_DELAY").MustDuration(30 * time.Minute) diff --git a/modules/structs/repo.go b/modules/structs/repo.go index 03741d03b..3edcfa884 100755 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -238,4 +238,5 @@ type MigrateRepoOption struct { Comments bool PullRequests bool MigrateToRepoID int64 + PaperUrl string } diff --git a/modules/task/task.go b/modules/task/task.go index 722e39bec..17db526ce 100644 --- a/modules/task/task.go +++ b/modules/task/task.go @@ -107,5 +107,6 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models. return nil, err } + models.InsertRepoPaper(&models.RepoPaper{RepoID: repo.ID, PaperUrl: opts.PaperUrl}) return &task, nil } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index ba7346481..0e431d4e1 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -757,6 +757,8 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) m.Post("/migrate/submit", reqToken(), bind(auth.MigrateRepoForm{}), repo.MigrateSubmit) + m.Post("/migrate/file/commit", reqToken(), repo.CommitMigrateFile) + m.Get("/migrate/file/query", reqToken(), repo.QueryMigrateFile) m.Group("/specification", func() { m.Get("", repo.GetResourceSpec) diff --git a/routers/api/v1/repo/migrate.go b/routers/api/v1/repo/migrate.go index 2f28b0bd3..100dc5e0f 100644 --- a/routers/api/v1/repo/migrate.go +++ b/routers/api/v1/repo/migrate.go @@ -6,6 +6,7 @@ package repo import ( "bytes" + repo_migrate "code.gitea.io/gitea/modules/repo-migrate" "code.gitea.io/gitea/modules/task" "code.gitea.io/gitea/routers/response" "errors" @@ -271,6 +272,7 @@ func MigrateSubmit(ctx *context.APIContext, form auth.MigrateRepoForm) { Comments: true, PullRequests: form.PullRequests, Releases: form.Releases, + PaperUrl: form.PaperUrl, } if opts.Mirror { opts.Issues = false @@ -299,6 +301,46 @@ func MigrateSubmit(ctx *context.APIContext, form auth.MigrateRepoForm) { handleMigrateError4Api(ctx, ctxUser, remoteAddr, err) } +func CommitMigrateFile(ctx *context.APIContext) { + log.Info("receive MigrateSubmit request") + fileUrl := ctx.Query("file") + if fileUrl == "" { + ctx.JSON(http.StatusOK, response.PARAM_ERROR) + return + } + if !repo_migrate.Auth(ctx.User.Name) { + ctx.JSON(http.StatusOK, response.INSUFFICIENT_PERMISSION) + return + } + //调用repo-migrate + r, err := repo_migrate.CommitFile(fileUrl) + if err != nil { + ctx.JSON(http.StatusOK, response.ResponseError(err)) + return + } + ctx.JSON(http.StatusOK, response.SuccessWithData(r)) +} + +func QueryMigrateFile(ctx *context.APIContext) { + log.Info("receive QueryMigrateFile request") + md5 := ctx.Query("md5") + if md5 == "" { + ctx.JSON(http.StatusOK, response.PARAM_ERROR) + return + } + if !repo_migrate.Auth(ctx.User.Name) { + ctx.JSON(http.StatusOK, response.INSUFFICIENT_PERMISSION) + return + } + //调用repo-migrate + r, err := repo_migrate.QueryFileStatus(md5) + if err != nil { + ctx.JSON(http.StatusOK, response.ResponseError(err)) + return + } + ctx.JSON(http.StatusOK, response.SuccessWithData(r)) +} + func checkContextUser(ctx *context.APIContext, uid int64) (*models.User, *response.BizError) { if uid == ctx.User.ID || uid == 0 { return ctx.User, nil