@@ -167,6 +167,7 @@ func init() { | |||||
new(Badge), | new(Badge), | ||||
new(BadgeUser), | new(BadgeUser), | ||||
new(BadgeUserLog), | new(BadgeUserLog), | ||||
new(RepoPaper), | |||||
) | ) | ||||
tablesStatistic = append(tablesStatistic, | tablesStatistic = append(tablesStatistic, | ||||
@@ -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 | |||||
} |
@@ -74,6 +74,7 @@ type MigrateRepoForm struct { | |||||
Issues bool `json:"issues"` | Issues bool `json:"issues"` | ||||
PullRequests bool `json:"pull_requests"` | PullRequests bool `json:"pull_requests"` | ||||
Releases bool `json:"releases"` | Releases bool `json:"releases"` | ||||
PaperUrl string `json:"paper_url"` | |||||
} | } | ||||
// Validate validates the fields | // Validate validates the fields | ||||
@@ -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 | |||||
} |
@@ -690,6 +690,11 @@ var ( | |||||
IncubationSourceOrgName string | IncubationSourceOrgName string | ||||
PaperRepoTopicName string | PaperRepoTopicName string | ||||
//repo-migrate config | |||||
RepoMigrateHost string | |||||
IsRepoMigrateWhiteListEffect bool | |||||
RepoMigrateWhiteList []string | |||||
//nginx proxy | //nginx proxy | ||||
PROXYURL string | PROXYURL string | ||||
RadarMap = struct { | RadarMap = struct { | ||||
@@ -1614,6 +1619,11 @@ func NewContext() { | |||||
IncubationSourceOrgName = sec.Key("INCUBATION_ORG_NAME").MustString("OpenI") | IncubationSourceOrgName = sec.Key("INCUBATION_ORG_NAME").MustString("OpenI") | ||||
PaperRepoTopicName = sec.Key("PAPER_REPO_TOPIC_NAME").MustString("openi-paper") | 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") | sec = Cfg.Section("point") | ||||
CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false) | CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false) | ||||
CloudBrainPayDelay = sec.Key("CLOUDBRAIN_PAY_DELAY").MustDuration(30 * time.Minute) | CloudBrainPayDelay = sec.Key("CLOUDBRAIN_PAY_DELAY").MustDuration(30 * time.Minute) | ||||
@@ -238,4 +238,5 @@ type MigrateRepoOption struct { | |||||
Comments bool | Comments bool | ||||
PullRequests bool | PullRequests bool | ||||
MigrateToRepoID int64 | MigrateToRepoID int64 | ||||
PaperUrl string | |||||
} | } |
@@ -107,5 +107,6 @@ func CreateMigrateTask(doer, u *models.User, opts base.MigrateOptions) (*models. | |||||
return nil, err | return nil, err | ||||
} | } | ||||
models.InsertRepoPaper(&models.RepoPaper{RepoID: repo.ID, PaperUrl: opts.PaperUrl}) | |||||
return &task, nil | return &task, nil | ||||
} | } |
@@ -757,6 +757,8 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) | m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) | ||||
m.Post("/migrate/submit", reqToken(), bind(auth.MigrateRepoForm{}), repo.MigrateSubmit) | 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.Group("/specification", func() { | ||||
m.Get("", repo.GetResourceSpec) | m.Get("", repo.GetResourceSpec) | ||||
@@ -6,6 +6,7 @@ package repo | |||||
import ( | import ( | ||||
"bytes" | "bytes" | ||||
repo_migrate "code.gitea.io/gitea/modules/repo-migrate" | |||||
"code.gitea.io/gitea/modules/task" | "code.gitea.io/gitea/modules/task" | ||||
"code.gitea.io/gitea/routers/response" | "code.gitea.io/gitea/routers/response" | ||||
"errors" | "errors" | ||||
@@ -271,6 +272,7 @@ func MigrateSubmit(ctx *context.APIContext, form auth.MigrateRepoForm) { | |||||
Comments: true, | Comments: true, | ||||
PullRequests: form.PullRequests, | PullRequests: form.PullRequests, | ||||
Releases: form.Releases, | Releases: form.Releases, | ||||
PaperUrl: form.PaperUrl, | |||||
} | } | ||||
if opts.Mirror { | if opts.Mirror { | ||||
opts.Issues = false | opts.Issues = false | ||||
@@ -299,6 +301,46 @@ func MigrateSubmit(ctx *context.APIContext, form auth.MigrateRepoForm) { | |||||
handleMigrateError4Api(ctx, ctxUser, remoteAddr, err) | 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) { | func checkContextUser(ctx *context.APIContext, uid int64) (*models.User, *response.BizError) { | ||||
if uid == ctx.User.ID || uid == 0 { | if uid == ctx.User.ID || uid == 0 { | ||||
return ctx.User, nil | return ctx.User, nil | ||||