Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

issue_indexer.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "fmt"
  7. "code.gitea.io/gitea/modules/indexer"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "code.gitea.io/gitea/modules/util"
  11. )
  12. // issueIndexerUpdateQueue queue of issue ids to be updated
  13. var issueIndexerUpdateQueue chan int64
  14. // InitIssueIndexer initialize issue indexer
  15. func InitIssueIndexer() {
  16. indexer.InitIssueIndexer(populateIssueIndexer)
  17. issueIndexerUpdateQueue = make(chan int64, setting.Indexer.UpdateQueueLength)
  18. go processIssueIndexerUpdateQueue()
  19. }
  20. // populateIssueIndexer populate the issue indexer with issue data
  21. func populateIssueIndexer() error {
  22. batch := indexer.IssueIndexerBatch()
  23. for page := 1; ; page++ {
  24. repos, _, err := SearchRepositoryByName(&SearchRepoOptions{
  25. Page: page,
  26. PageSize: 10,
  27. OrderBy: SearchOrderByID,
  28. Private: true,
  29. Collaborate: util.OptionalBoolFalse,
  30. })
  31. if err != nil {
  32. return fmt.Errorf("Repositories: %v", err)
  33. }
  34. if len(repos) == 0 {
  35. return batch.Flush()
  36. }
  37. for _, repo := range repos {
  38. issues, err := Issues(&IssuesOptions{
  39. RepoID: repo.ID,
  40. IsClosed: util.OptionalBoolNone,
  41. IsPull: util.OptionalBoolNone,
  42. })
  43. if err != nil {
  44. return err
  45. }
  46. if err = IssueList(issues).LoadComments(); err != nil {
  47. return err
  48. }
  49. for _, issue := range issues {
  50. if err := batch.Add(issue.update()); err != nil {
  51. return err
  52. }
  53. }
  54. }
  55. }
  56. }
  57. func processIssueIndexerUpdateQueue() {
  58. batch := indexer.IssueIndexerBatch()
  59. for {
  60. var issueID int64
  61. select {
  62. case issueID = <-issueIndexerUpdateQueue:
  63. default:
  64. // flush whatever updates we currently have, since we
  65. // might have to wait a while
  66. if err := batch.Flush(); err != nil {
  67. log.Error(4, "IssueIndexer: %v", err)
  68. }
  69. issueID = <-issueIndexerUpdateQueue
  70. }
  71. issue, err := GetIssueByID(issueID)
  72. if err != nil {
  73. log.Error(4, "GetIssueByID: %v", err)
  74. } else if err = batch.Add(issue.update()); err != nil {
  75. log.Error(4, "IssueIndexer: %v", err)
  76. }
  77. }
  78. }
  79. func (issue *Issue) update() indexer.IssueIndexerUpdate {
  80. comments := make([]string, 0, 5)
  81. for _, comment := range issue.Comments {
  82. if comment.Type == CommentTypeComment {
  83. comments = append(comments, comment.Content)
  84. }
  85. }
  86. return indexer.IssueIndexerUpdate{
  87. IssueID: issue.ID,
  88. Data: &indexer.IssueIndexerData{
  89. RepoID: issue.RepoID,
  90. Title: issue.Title,
  91. Content: issue.Content,
  92. Comments: comments,
  93. },
  94. }
  95. }
  96. // UpdateIssueIndexer add/update an issue to the issue indexer
  97. func UpdateIssueIndexer(issueID int64) {
  98. select {
  99. case issueIndexerUpdateQueue <- issueID:
  100. default:
  101. go func() {
  102. issueIndexerUpdateQueue <- issueID
  103. }()
  104. }
  105. }