Optimize topics creation and updates

This commit is contained in:
Chongyi Zheng 2022-07-11 04:17:45 -04:00
parent 176009ed60
commit 698d6be490
No known key found for this signature in database
GPG Key ID: CC2953E050C19686
2 changed files with 40 additions and 6 deletions

View File

@ -262,6 +262,34 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
return topic, committer.Commit()
}
func AddTopics(repoID int64, topicNames ...string) error {
ctx, committer, err := db.TxContext()
if err != nil {
return err
}
defer committer.Close()
sess := db.GetEngine(ctx)
for _, topicName := range topicNames {
if strings.TrimSpace(topicName) == "" {
continue
}
_, err := addTopicByNameToRepo(ctx, repoID, topicName)
if err != nil {
return err
}
}
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
Topics: topicNames,
}); err != nil {
return err
}
return committer.Commit()
}
// DeleteTopic removes a topic name from a repository (if it has it)
func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
topic, err := GetRepoTopicByName(db.DefaultContext, repoID, topicName)
@ -278,7 +306,7 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
return topic, err
}
// SaveTopics save topics to a repository
// SaveTopics save topics to a repository (add and delete respective topics)
func SaveTopics(repoID int64, topicNames ...string) error {
topics, _, err := FindTopics(&FindTopicOptions{
RepoID: repoID,

View File

@ -150,9 +150,8 @@ func (g *GiteaLocalUploader) Close() {
}
}
// CreateTopics creates topics
func (g *GiteaLocalUploader) CreateTopics(topics ...string) error {
// ignore topics to long for the db
func filterTopicsForDB(topics []string) []string {
// filter out topics to long for the db
c := 0
for i := range topics {
if len(topics[i]) <= 50 {
@ -161,7 +160,13 @@ func (g *GiteaLocalUploader) CreateTopics(topics ...string) error {
}
}
topics = topics[:c]
return repo_model.SaveTopics(g.repo.ID, topics...)
return topics
}
// CreateTopics creates topics
func (g *GiteaLocalUploader) CreateTopics(topics ...string) error {
topics = filterTopicsForDB(topics)
return repo_model.AddTopics(g.repo.ID, topics...)
}
// CreateMilestones creates milestones
@ -804,7 +809,8 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
// UpdateTopics updates topics
func (g *GiteaLocalUploader) UpdateTopics(topics ...string) error {
return g.CreateTopics(topics...)
topics = filterTopicsForDB(topics)
return repo_model.SaveTopics(g.repo.ID, topics...)
}
func (g *GiteaLocalUploader) UpdateMilestones(milestones ...*base.Milestone) error {