From 3c125805db70d412fa6213a8d1a4e0fa1ce83cc9 Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Wed, 1 Feb 2023 16:37:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=9D=9E=E5=90=AF=E6=99=BA?= =?UTF-8?q?=E9=A1=B9=E7=9B=AE=E7=9A=84=E6=A0=87=E7=AD=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/structs/tech.go | 16 ++++++++-------- routers/api/v1/tech/repo.go | 24 +++++++++++++++++++++++- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/modules/structs/tech.go b/modules/structs/tech.go index 7d3e6d760..2f66b5bbe 100644 --- a/modules/structs/tech.go +++ b/modules/structs/tech.go @@ -1,14 +1,14 @@ package structs type NotOpenITechRepo struct { - Url string `json:"url" binding:"Required"` - TechNo string `json:"no"` - Institution string `json:"institution"` - UID int64 `json:"uid"` //启智项目uid - RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"` - Alias string `json:"alias" binding:"Required;AlphaDashDotChinese;MaxSize(100)"` - Topics string `json:"topics"` //关键词 - Description string `json:"description" binding:"MaxSize(255)"` + Url string `json:"url" binding:"Required"` + TechNo string `json:"no"` + Institution string `json:"institution"` + UID int64 `json:"uid"` //启智项目uid + RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"` + Alias string `json:"alias" binding:"Required;AlphaDashDotChinese;MaxSize(100)"` + Topics []string `json:"topics"` //关键词 + Description string `json:"description" binding:"MaxSize(255)"` } type OpenITechRepo struct { diff --git a/routers/api/v1/tech/repo.go b/routers/api/v1/tech/repo.go index 91be289da..3ad9da64a 100644 --- a/routers/api/v1/tech/repo.go +++ b/routers/api/v1/tech/repo.go @@ -8,6 +8,7 @@ import ( "code.gitea.io/gitea/routers/response" "code.gitea.io/gitea/services/repository" techService "code.gitea.io/gitea/services/tech" + "errors" "fmt" "net/http" ) @@ -86,7 +87,7 @@ func CommitNotOpenIRepo(ctx *context.APIContext, form api.NotOpenITechRepo) { RepoName: form.RepoName, Alias: form.Alias, Description: form.Description, - Labels: true, + Labels: false, Mirror: true, }) if bizErr != nil { @@ -115,6 +116,9 @@ func CommitNotOpenIRepo(ctx *context.APIContext, form api.NotOpenITechRepo) { return } + //给仓库加标签 + updateTopics(repo.ID, form.Topics) + //写入数据库 rci := &models.RepoConvergeInfo{ RepoID: repo.ID, @@ -131,3 +135,21 @@ func CommitNotOpenIRepo(ctx *context.APIContext, form api.NotOpenITechRepo) { } ctx.JSON(http.StatusOK, response.OuterSuccess()) } + +func updateTopics(repoId int64, topicNames []string) error { + validTopics, invalidTopics := models.SanitizeAndValidateTopics(topicNames) + + if len(validTopics) > 25 { + return errors.New("Exceeding maximum number of topics per repo") + } + + if len(invalidTopics) > 0 { + return errors.New("Topic names are invalid") + } + + err := models.SaveTopics(repoId, validTopics...) + if err != nil { + return err + } + return nil +}