gitea源码

topic.go 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repo
  4. import (
  5. "context"
  6. "fmt"
  7. "regexp"
  8. "strings"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/modules/container"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. "code.gitea.io/gitea/modules/util"
  13. "xorm.io/builder"
  14. )
  15. func init() {
  16. db.RegisterModel(new(Topic))
  17. db.RegisterModel(new(RepoTopic))
  18. }
  19. var topicPattern = regexp.MustCompile(`^[a-z0-9][-.a-z0-9]*$`)
  20. // Topic represents a topic of repositories
  21. type Topic struct {
  22. ID int64 `xorm:"pk autoincr"`
  23. Name string `xorm:"UNIQUE VARCHAR(50)"`
  24. RepoCount int
  25. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  26. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  27. }
  28. // RepoTopic represents associated repositories and topics
  29. type RepoTopic struct { //revive:disable-line:exported
  30. RepoID int64 `xorm:"pk"`
  31. TopicID int64 `xorm:"pk"`
  32. }
  33. // ErrTopicNotExist represents an error that a topic is not exist
  34. type ErrTopicNotExist struct {
  35. Name string
  36. }
  37. // IsErrTopicNotExist checks if an error is an ErrTopicNotExist.
  38. func IsErrTopicNotExist(err error) bool {
  39. _, ok := err.(ErrTopicNotExist)
  40. return ok
  41. }
  42. // Error implements error interface
  43. func (err ErrTopicNotExist) Error() string {
  44. return fmt.Sprintf("topic is not exist [name: %s]", err.Name)
  45. }
  46. func (err ErrTopicNotExist) Unwrap() error {
  47. return util.ErrNotExist
  48. }
  49. // ValidateTopic checks a topic by length and match pattern rules
  50. func ValidateTopic(topic string) bool {
  51. return len(topic) <= 35 && topicPattern.MatchString(topic)
  52. }
  53. // SanitizeAndValidateTopics sanitizes and checks an array or topics
  54. func SanitizeAndValidateTopics(topics []string) (validTopics, invalidTopics []string) {
  55. validTopics = make([]string, 0)
  56. mValidTopics := make(container.Set[string])
  57. invalidTopics = make([]string, 0)
  58. for _, topic := range topics {
  59. topic = strings.TrimSpace(strings.ToLower(topic))
  60. // ignore empty string
  61. if len(topic) == 0 {
  62. continue
  63. }
  64. // ignore same topic twice
  65. if mValidTopics.Contains(topic) {
  66. continue
  67. }
  68. if ValidateTopic(topic) {
  69. validTopics = append(validTopics, topic)
  70. mValidTopics.Add(topic)
  71. } else {
  72. invalidTopics = append(invalidTopics, topic)
  73. }
  74. }
  75. return validTopics, invalidTopics
  76. }
  77. // GetTopicByName retrieves topic by name
  78. func GetTopicByName(ctx context.Context, name string) (*Topic, error) {
  79. var topic Topic
  80. if has, err := db.GetEngine(ctx).Where("name = ?", name).Get(&topic); err != nil {
  81. return nil, err
  82. } else if !has {
  83. return nil, ErrTopicNotExist{name}
  84. }
  85. return &topic, nil
  86. }
  87. // addTopicByNameToRepo adds a topic name to a repo and increments the topic count.
  88. // Returns topic after the addition
  89. func addTopicByNameToRepo(ctx context.Context, repoID int64, topicName string) (*Topic, error) {
  90. var topic Topic
  91. e := db.GetEngine(ctx)
  92. has, err := e.Where("name = ?", topicName).Get(&topic)
  93. if err != nil {
  94. return nil, err
  95. }
  96. if !has {
  97. topic.Name = topicName
  98. topic.RepoCount = 1
  99. if err := db.Insert(ctx, &topic); err != nil {
  100. return nil, err
  101. }
  102. } else {
  103. topic.RepoCount++
  104. if _, err := e.ID(topic.ID).Cols("repo_count").Update(&topic); err != nil {
  105. return nil, err
  106. }
  107. }
  108. if err := db.Insert(ctx, &RepoTopic{
  109. RepoID: repoID,
  110. TopicID: topic.ID,
  111. }); err != nil {
  112. return nil, err
  113. }
  114. return &topic, nil
  115. }
  116. // removeTopicFromRepo remove a topic from a repo and decrements the topic repo count
  117. func removeTopicFromRepo(ctx context.Context, repoID int64, topic *Topic) error {
  118. topic.RepoCount--
  119. e := db.GetEngine(ctx)
  120. if _, err := e.ID(topic.ID).Cols("repo_count").Update(topic); err != nil {
  121. return err
  122. }
  123. if _, err := e.Delete(&RepoTopic{
  124. RepoID: repoID,
  125. TopicID: topic.ID,
  126. }); err != nil {
  127. return err
  128. }
  129. return nil
  130. }
  131. // RemoveTopicsFromRepo remove all topics from the repo and decrements respective topics repo count
  132. func RemoveTopicsFromRepo(ctx context.Context, repoID int64) error {
  133. e := db.GetEngine(ctx)
  134. _, err := e.Where(
  135. builder.In("id",
  136. builder.Select("topic_id").From("repo_topic").Where(builder.Eq{"repo_id": repoID}),
  137. ),
  138. ).Cols("repo_count").SetExpr("repo_count", "repo_count-1").Update(&Topic{})
  139. if err != nil {
  140. return err
  141. }
  142. if _, err = e.Delete(&RepoTopic{RepoID: repoID}); err != nil {
  143. return err
  144. }
  145. return nil
  146. }
  147. // FindTopicOptions represents the options when fdin topics
  148. type FindTopicOptions struct {
  149. db.ListOptions
  150. RepoID int64
  151. Keyword string
  152. }
  153. func (opts *FindTopicOptions) ToConds() builder.Cond {
  154. cond := builder.NewCond()
  155. if opts.RepoID > 0 {
  156. cond = cond.And(builder.Eq{"repo_topic.repo_id": opts.RepoID})
  157. }
  158. if opts.Keyword != "" {
  159. cond = cond.And(builder.Like{"topic.name", opts.Keyword})
  160. }
  161. return cond
  162. }
  163. func (opts *FindTopicOptions) ToOrders() string {
  164. orderBy := "topic.repo_count DESC"
  165. if opts.RepoID > 0 {
  166. orderBy = "topic.name" // when render topics for a repo, it's better to sort them by name, to get consistent result
  167. }
  168. return orderBy
  169. }
  170. func (opts *FindTopicOptions) ToJoins() []db.JoinFunc {
  171. if opts.RepoID <= 0 {
  172. return nil
  173. }
  174. return []db.JoinFunc{
  175. func(e db.Engine) error {
  176. e.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
  177. return nil
  178. },
  179. }
  180. }
  181. // GetRepoTopicByName retrieves topic from name for a repo if it exist
  182. func GetRepoTopicByName(ctx context.Context, repoID int64, topicName string) (*Topic, error) {
  183. cond := builder.NewCond()
  184. var topic Topic
  185. cond = cond.And(builder.Eq{"repo_topic.repo_id": repoID}).And(builder.Eq{"topic.name": topicName})
  186. sess := db.GetEngine(ctx).Table("topic").Where(cond)
  187. sess.Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id")
  188. has, err := sess.Select("topic.*").Get(&topic)
  189. if has {
  190. return &topic, err
  191. }
  192. return nil, err
  193. }
  194. // AddTopic adds a topic name to a repository (if it does not already have it)
  195. func AddTopic(ctx context.Context, repoID int64, topicName string) (*Topic, error) {
  196. return db.WithTx2(ctx, func(ctx context.Context) (*Topic, error) {
  197. topic, err := GetRepoTopicByName(ctx, repoID, topicName)
  198. if err != nil {
  199. return nil, err
  200. }
  201. if topic != nil {
  202. // Repo already have topic
  203. return topic, nil
  204. }
  205. topic, err = addTopicByNameToRepo(ctx, repoID, topicName)
  206. if err != nil {
  207. return nil, err
  208. }
  209. if err = syncTopicsInRepository(ctx, repoID); err != nil {
  210. return nil, err
  211. }
  212. return topic, nil
  213. })
  214. }
  215. // DeleteTopic removes a topic name from a repository (if it has it)
  216. func DeleteTopic(ctx context.Context, repoID int64, topicName string) (*Topic, error) {
  217. topic, err := GetRepoTopicByName(ctx, repoID, topicName)
  218. if err != nil {
  219. return nil, err
  220. }
  221. if topic == nil {
  222. // Repo doesn't have topic, can't be removed
  223. return nil, nil
  224. }
  225. return db.WithTx2(ctx, func(ctx context.Context) (*Topic, error) {
  226. if err = removeTopicFromRepo(ctx, repoID, topic); err != nil {
  227. return nil, err
  228. }
  229. if err = syncTopicsInRepository(ctx, repoID); err != nil {
  230. return nil, err
  231. }
  232. return topic, nil
  233. })
  234. }
  235. // SaveTopics save topics to a repository
  236. func SaveTopics(ctx context.Context, repoID int64, topicNames ...string) error {
  237. topics, err := db.Find[Topic](ctx, &FindTopicOptions{
  238. RepoID: repoID,
  239. })
  240. if err != nil {
  241. return err
  242. }
  243. return db.WithTx(ctx, func(ctx context.Context) error {
  244. var addedTopicNames []string
  245. for _, topicName := range topicNames {
  246. if strings.TrimSpace(topicName) == "" {
  247. continue
  248. }
  249. var found bool
  250. for _, t := range topics {
  251. if strings.EqualFold(topicName, t.Name) {
  252. found = true
  253. break
  254. }
  255. }
  256. if !found {
  257. addedTopicNames = append(addedTopicNames, topicName)
  258. }
  259. }
  260. var removeTopics []*Topic
  261. for _, t := range topics {
  262. var found bool
  263. for _, topicName := range topicNames {
  264. if strings.EqualFold(topicName, t.Name) {
  265. found = true
  266. break
  267. }
  268. }
  269. if !found {
  270. removeTopics = append(removeTopics, t)
  271. }
  272. }
  273. for _, topicName := range addedTopicNames {
  274. _, err := addTopicByNameToRepo(ctx, repoID, topicName)
  275. if err != nil {
  276. return err
  277. }
  278. }
  279. for _, topic := range removeTopics {
  280. err := removeTopicFromRepo(ctx, repoID, topic)
  281. if err != nil {
  282. return err
  283. }
  284. }
  285. return syncTopicsInRepository(ctx, repoID)
  286. })
  287. }
  288. // GenerateTopics generates topics from a template repository
  289. func GenerateTopics(ctx context.Context, templateRepo, generateRepo *Repository) error {
  290. for _, topic := range templateRepo.Topics {
  291. if _, err := addTopicByNameToRepo(ctx, generateRepo.ID, topic); err != nil {
  292. return err
  293. }
  294. }
  295. return syncTopicsInRepository(ctx, generateRepo.ID)
  296. }
  297. // syncTopicsInRepository makes sure topics in the topics table are copied into the topics field of the repository
  298. func syncTopicsInRepository(ctx context.Context, repoID int64) error {
  299. topicNames := make([]string, 0, 25)
  300. if err := db.GetEngine(ctx).Table("topic").Cols("name").
  301. Join("INNER", "repo_topic", "repo_topic.topic_id = topic.id").
  302. Where("repo_topic.repo_id = ?", repoID).Asc("topic.name").Find(&topicNames); err != nil {
  303. return err
  304. }
  305. if _, err := db.GetEngine(ctx).ID(repoID).Cols("topics").Update(&Repository{
  306. Topics: topicNames,
  307. }); err != nil {
  308. return err
  309. }
  310. return nil
  311. }
  312. // CountOrphanedAttachments returns the number of topics that don't belong to any repository.
  313. func CountOrphanedTopics(ctx context.Context) (int64, error) {
  314. return db.GetEngine(ctx).Where("repo_count = 0").Count(new(Topic))
  315. }
  316. // DeleteOrphanedAttachments delete all topics that don't belong to any repository.
  317. func DeleteOrphanedTopics(ctx context.Context) (int64, error) {
  318. return db.GetEngine(ctx).Where("repo_count = 0").Delete(new(Topic))
  319. }