Browse Source

#3338

add repo paper
fix-3338
chenyifan01 2 years ago
parent
commit
cda216c19b
9 changed files with 174 additions and 0 deletions
  1. +1
    -0
      models/models.go
  2. +23
    -0
      models/repo_paper.go
  3. +1
    -0
      modules/auth/repo_form.go
  4. +93
    -0
      modules/repo-migrate/migrate.go
  5. +10
    -0
      modules/setting/setting.go
  6. +1
    -0
      modules/structs/repo.go
  7. +1
    -0
      modules/task/task.go
  8. +2
    -0
      routers/api/v1/api.go
  9. +42
    -0
      routers/api/v1/repo/migrate.go

+ 1
- 0
models/models.go View File

@@ -167,6 +167,7 @@ func init() {
new(Badge),
new(BadgeUser),
new(BadgeUserLog),
new(RepoPaper),
)

tablesStatistic = append(tablesStatistic,


+ 23
- 0
models/repo_paper.go View File

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

+ 1
- 0
modules/auth/repo_form.go View File

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


+ 93
- 0
modules/repo-migrate/migrate.go View File

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

+ 10
- 0
modules/setting/setting.go View File

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


+ 1
- 0
modules/structs/repo.go View File

@@ -238,4 +238,5 @@ type MigrateRepoOption struct {
Comments bool
PullRequests bool
MigrateToRepoID int64
PaperUrl string
}

+ 1
- 0
modules/task/task.go View File

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

+ 2
- 0
routers/api/v1/api.go View File

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


+ 42
- 0
routers/api/v1/repo/migrate.go View File

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


Loading…
Cancel
Save