Skip to content

Commit 7daa356

Browse files
yardenshohamGiteaBot
authored andcommitted
Fix topics deleted via API not being deleted in org page (go-gitea#24825)
The topics are saved in the `repo_topic` table. They are also saved directly in the `repository` table. Before this PR, only `AddTopic` and `SaveTopics` made sure the `topics` field in the `repository` table 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. Fixes go-gitea#24820
1 parent b369ed5 commit 7daa356

File tree

1 file changed

+25
-20
lines changed

1 file changed

+25
-20
lines changed

models/repo/topic.go

+25-20
Original file line numberDiff line numberDiff line change
@@ -253,16 +253,7 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
253253
return nil, err
254254
}
255255

256-
topicNames := make([]string, 0, 25)
257-
if err := sess.Select("name").Table("topic").
258-
Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
259-
Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil {
260-
return nil, err
261-
}
262-
263-
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
264-
Topics: topicNames,
265-
}); err != nil {
256+
if err = syncTopicsInRepository(sess, repoID); err != nil {
266257
return nil, err
267258
}
268259

@@ -281,6 +272,11 @@ func DeleteTopic(repoID int64, topicName string) (*Topic, error) {
281272
}
282273

283274
err = removeTopicFromRepo(db.DefaultContext, repoID, topic)
275+
if err != nil {
276+
return nil, err
277+
}
278+
279+
err = syncTopicsInRepository(db.GetEngine(db.DefaultContext), repoID)
284280

285281
return topic, err
286282
}
@@ -347,16 +343,7 @@ func SaveTopics(repoID int64, topicNames ...string) error {
347343
}
348344
}
349345

350-
topicNames = make([]string, 0, 25)
351-
if err := sess.Table("topic").Cols("name").
352-
Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
353-
Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil {
354-
return err
355-
}
356-
357-
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
358-
Topics: topicNames,
359-
}); err != nil {
346+
if err := syncTopicsInRepository(sess, repoID); err != nil {
360347
return err
361348
}
362349

@@ -370,5 +357,23 @@ func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository)
370357
return err
371358
}
372359
}
360+
361+
return syncTopicsInRepository(db.GetEngine(ctx), generateRepo.ID)
362+
}
363+
364+
// syncTopicsInRepository makes sure topics in the topics table are copied into the topics field of the repository
365+
func syncTopicsInRepository(sess db.Engine, repoID int64) error {
366+
topicNames := make([]string, 0, 25)
367+
if err := sess.Table("topic").Cols("name").
368+
Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
369+
Where("repo_topic.repo_id = ?", repoID).Desc("topic.repo_count").Find(&topicNames); err != nil {
370+
return err
371+
}
372+
373+
if _, err := sess.ID(repoID).Cols("topics").Update(&Repository{
374+
Topics: topicNames,
375+
}); err != nil {
376+
return err
377+
}
373378
return nil
374379
}

0 commit comments

Comments
 (0)