From 6d8f53b773e5be4125a09c19120ad48b4ddf313b Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 20 May 2023 18:17:58 +0000 Subject: [PATCH 1/2] Fix topics deleted via API not being deleted in org page The topics are saved in the repo_topic table with a repoID key. They are also saved directly in the repository table. Before this PR, only `AddTopic` and `SaveTopics` made sure the `topics` field in the repository field was synced with the repo_topic table. This PR makes sure `GenerateTopics` and `DeleteTopic` also sync the `topics` in the repository table. `RemoveTopicsFromRepo` doesn't need to sync the data as it is only used to delete a repository. --- models/repo/topic.go | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/models/repo/topic.go b/models/repo/topic.go index 88fe532be9a7..a56e5bd514dd 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -253,16 +253,7 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) { return nil, err } - topicNames := make([]string, 0, 25) - if err := sess.Select("name").Table("topic"). - Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id"). - Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil { - return nil, err - } - - if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ - Topics: topicNames, - }); err != nil { + if err = syncTopicsInRepository(sess, repoID); err != nil { return nil, err } @@ -281,6 +272,11 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) { } err = removeTopicFromRepo(db.DefaultContext, repoID, topic) + if err != nil { + return nil, err + } + + err = syncTopicsInRepository(db.GetEngine(db.DefaultContext), repoID) return topic, err } @@ -347,16 +343,7 @@ func SaveTopics(repoID int64, topicNames ...string) error { } } - topicNames = make([]string, 0, 25) - if err := sess.Table("topic").Cols("name"). - Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id"). - Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil { - return err - } - - if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ - Topics: topicNames, - }); err != nil { + if err := syncTopicsInRepository(sess, repoID); err != nil { return err } @@ -370,5 +357,30 @@ func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository) return err } } + ctx, committer, err := db.TxContext(ctx) + if err != nil { + return err + } + defer committer.Close() + if err := syncTopicsInRepository(db.GetEngine(ctx), generateRepo.ID); err != nil { + return err + } + return committer.Commit() +} + +// syncTopicsInRepository makes sure topics in the topics table are copied into the topics field of the repository +func syncTopicsInRepository(sess db.Engine, repoID int64) error { + topicNames := make([]string, 0, 25) + if err := sess.Table("topic").Cols("name"). + Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id"). + Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil { + return err + } + + if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{ + Topics: topicNames, + }); err != nil { + return err + } return nil } From 405a42edf773c5ad8bd182ef772f0a5e26a7cb5d Mon Sep 17 00:00:00 2001 From: Yarden Shoham Date: Sat, 20 May 2023 20:14:29 +0000 Subject: [PATCH 2/2] Simplify --- models/repo/topic.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/models/repo/topic.go b/models/repo/topic.go index a56e5bd514dd..ec3de869d5be 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -357,15 +357,8 @@ func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository) return err } } - ctx, committer, err := db.TxContext(ctx) - if err != nil { - return err - } - defer committer.Close() - if err := syncTopicsInRepository(db.GetEngine(ctx), generateRepo.ID); err != nil { - return err - } - return committer.Commit() + + return syncTopicsInRepository(db.GetEngine(ctx), generateRepo.ID) } // syncTopicsInRepository makes sure topics in the topics table are copied into the topics field of the repository