From 29a77cdcf86991774c483c119a00b8b1d59902f9 Mon Sep 17 00:00:00 2001 From: Gitea Date: Thu, 10 Feb 2022 11:12:00 +0800 Subject: [PATCH] fix bug:error happens when create repo by api --- models/repo.go | 3 +++ modules/structs/repo.go | 4 ++++ modules/validation/binding.go | 3 +++ routers/api/v1/repo/repo.go | 1 + 4 files changed, 11 insertions(+) diff --git a/models/repo.go b/models/repo.go index 5f90f97fc..6b3df9fe0 100755 --- a/models/repo.go +++ b/models/repo.go @@ -1133,6 +1133,9 @@ func IsUsableRepoAlias(name string) error { // CreateRepository creates a repository for the user/organization. func CreateRepository(ctx DBContext, doer, u *User, repo *Repository, opts ...CreateRepoOptions) (err error) { + if repo.Alias == "" { + repo.Alias = repo.Name + } repo.LowerAlias = strings.ToLower(repo.Alias) if err = IsUsableRepoName(repo.Name); err != nil { return err diff --git a/modules/structs/repo.go b/modules/structs/repo.go index e290488a4..6e9ece4b0 100755 --- a/modules/structs/repo.go +++ b/modules/structs/repo.go @@ -100,6 +100,10 @@ type CreateRepoOption struct { // required: true // unique: true Name string `json:"name" binding:"Required;AlphaDashDot;MaxSize(100)"` + // Alias of the repository to create + // required: false + // unique: true + Alias string `json:"alias" binding:"AlphaDashDotChinese;MaxSize(100)"` // Description of the repository to create Description string `json:"description" binding:"MaxSize(255)"` // Whether the repository is private diff --git a/modules/validation/binding.go b/modules/validation/binding.go index b608cdea2..d52919475 100644 --- a/modules/validation/binding.go +++ b/modules/validation/binding.go @@ -128,6 +128,9 @@ func addAlphaDashDotChineseRule() { return strings.HasPrefix(rule, "AlphaDashDotChinese") }, IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) { + if val == "" { + return true, errs + } if !ValidAlphaDashDotChinese(fmt.Sprintf("%v", val)) { errs.Add([]string{name}, ErrAlphaDashDotChinese, "ErrAlphaDashDotChinese") return false, errs diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index a724ebcc3..a85f88cb8 100755 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -232,6 +232,7 @@ func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateR } repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{ Name: opt.Name, + Alias: opt.Alias, Description: opt.Description, IssueLabels: opt.IssueLabels, Gitignores: opt.Gitignores,