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