diff --git a/models/topic.go b/models/topic.go index 0b19bc1f0..ea5698f4c 100644 --- a/models/topic.go +++ b/models/topic.go @@ -9,6 +9,7 @@ import ( "regexp" "strings" "unicode/utf8" + "xorm.io/xorm" "code.gitea.io/gitea/modules/timeutil" @@ -337,3 +338,16 @@ func GetOrgTopics(orgId int64) ([]Topic, error) { return result, nil } + +func UpdateRepoTopics(repoID int64, topicNames []string, sess ...*xorm.Engine) error { + e := x + if len(sess) > 0 { + e = sess[0] + } + if _, err := e.ID(repoID).Cols("topics").Update(&Repository{ + Topics: topicNames, + }); err != nil { + return err + } + return nil +} diff --git a/routers/api/v1/repo/topic.go b/routers/api/v1/repo/topic.go index f4ff7a329..d2522c9ce 100644 --- a/routers/api/v1/repo/topic.go +++ b/routers/api/v1/repo/topic.go @@ -177,13 +177,25 @@ func AddTopic(ctx *context.APIContext) { return } - _, err = models.AddTopic(ctx.Repo.Repository.ID, topicName) + topic, err := models.AddTopic(ctx.Repo.Repository.ID, topicName) if err != nil { log.Error("AddTopic failed: %v", err) ctx.InternalServerError(err) return } - + found := false + topicNames := make([]string, len(topics)) + for i, t := range topics { + topicNames[i] = t.Name + if strings.EqualFold(topic.Name, t.Name) { + found = true + break + } + } + if !found && topic.Name != "" { + topicNames = append(topicNames, topic.Name) + } + models.UpdateRepoTopics(ctx.Repo.Repository.ID, topicNames) ctx.Status(http.StatusNoContent) }