mirror of
https://github.com/go-gitea/gitea
synced 2024-12-24 17:44:27 +00:00
Partial Backport #13285 Fix #12426 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
parent
5178aa2130
commit
3d272b899d
@ -197,10 +197,13 @@ func FindTopics(opts *FindTopicOptions) (topics []*Topic, err error) {
|
|||||||
|
|
||||||
// GetRepoTopicByName retrives topic from name for a repo if it exist
|
// GetRepoTopicByName retrives topic from name for a repo if it exist
|
||||||
func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
|
func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
|
||||||
|
return getRepoTopicByName(x, repoID, topicName)
|
||||||
|
}
|
||||||
|
func getRepoTopicByName(e Engine, repoID int64, topicName string) (*Topic, error) {
|
||||||
var cond = builder.NewCond()
|
var cond = builder.NewCond()
|
||||||
var topic Topic
|
var topic Topic
|
||||||
cond = cond.And(builder.Eq{"repo_topic.repo_id": repoID}).And(builder.Eq{"topic.name": topicName})
|
cond = cond.And(builder.Eq{"repo_topic.repo_id": repoID}).And(builder.Eq{"topic.name": topicName})
|
||||||
sess := x.Table("topic").Where(cond)
|
sess := e.Table("topic").Where(cond)
|
||||||
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
|
sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
|
||||||
has, err := sess.Get(&topic)
|
has, err := sess.Get(&topic)
|
||||||
if has {
|
if has {
|
||||||
@ -211,7 +214,13 @@ func GetRepoTopicByName(repoID int64, topicName string) (*Topic, error) {
|
|||||||
|
|
||||||
// AddTopic adds a topic name to a repository (if it does not already have it)
|
// AddTopic adds a topic name to a repository (if it does not already have it)
|
||||||
func AddTopic(repoID int64, topicName string) (*Topic, error) {
|
func AddTopic(repoID int64, topicName string) (*Topic, error) {
|
||||||
topic, err := GetRepoTopicByName(repoID, topicName)
|
sess := x.NewSession()
|
||||||
|
defer sess.Close()
|
||||||
|
if err := sess.Begin(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
topic, err := getRepoTopicByName(sess, repoID, topicName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -220,7 +229,25 @@ func AddTopic(repoID int64, topicName string) (*Topic, error) {
|
|||||||
return topic, nil
|
return topic, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return addTopicByNameToRepo(x, repoID, topicName)
|
topic, err = addTopicByNameToRepo(sess, repoID, topicName)
|
||||||
|
if err != nil {
|
||||||
|
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 {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return topic, sess.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteTopic removes a topic name from a repository (if it has it)
|
// DeleteTopic removes a topic name from a repository (if it has it)
|
||||||
|
Loading…
Reference in New Issue
Block a user