gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package issues
  4. import (
  5. "strings"
  6. "code.gitea.io/gitea/models/db"
  7. issues_model "code.gitea.io/gitea/models/issues"
  8. "code.gitea.io/gitea/modules/indexer/issues/internal"
  9. "code.gitea.io/gitea/modules/optional"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOptions {
  13. if opts.IssueIDs != nil {
  14. setting.PanicInDevOrTesting("Indexer SearchOptions doesn't support IssueIDs")
  15. }
  16. searchOpt := &SearchOptions{
  17. Keyword: keyword,
  18. RepoIDs: opts.RepoIDs,
  19. AllPublic: opts.AllPublic,
  20. IsPull: opts.IsPull,
  21. IsClosed: opts.IsClosed,
  22. IsArchived: opts.IsArchived,
  23. }
  24. if len(opts.LabelIDs) == 1 && opts.LabelIDs[0] == 0 {
  25. searchOpt.NoLabelOnly = true
  26. } else {
  27. for _, labelID := range opts.LabelIDs {
  28. if labelID > 0 {
  29. searchOpt.IncludedLabelIDs = append(searchOpt.IncludedLabelIDs, labelID)
  30. } else {
  31. searchOpt.ExcludedLabelIDs = append(searchOpt.ExcludedLabelIDs, -labelID)
  32. }
  33. }
  34. // opts.IncludedLabelNames and opts.ExcludedLabelNames are not supported here.
  35. // It's not a TO DO, it's just unnecessary.
  36. }
  37. if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID {
  38. searchOpt.MilestoneIDs = []int64{0}
  39. } else {
  40. searchOpt.MilestoneIDs = opts.MilestoneIDs
  41. }
  42. if opts.ProjectID > 0 {
  43. searchOpt.ProjectID = optional.Some(opts.ProjectID)
  44. } else if opts.ProjectID == db.NoConditionID { // FIXME: this is inconsistent from other places
  45. searchOpt.ProjectID = optional.Some[int64](0) // Those issues with no project(projectid==0)
  46. }
  47. searchOpt.AssigneeID = opts.AssigneeID
  48. // See the comment of issues_model.SearchOptions for the reason why we need to convert
  49. convertID := func(id int64) optional.Option[int64] {
  50. if id > 0 {
  51. return optional.Some(id)
  52. }
  53. if id == db.NoConditionID {
  54. return optional.None[int64]()
  55. }
  56. return nil
  57. }
  58. searchOpt.ProjectColumnID = convertID(opts.ProjectColumnID)
  59. searchOpt.PosterID = opts.PosterID
  60. searchOpt.MentionID = convertID(opts.MentionedID)
  61. searchOpt.ReviewedID = convertID(opts.ReviewedID)
  62. searchOpt.ReviewRequestedID = convertID(opts.ReviewRequestedID)
  63. searchOpt.SubscriberID = convertID(opts.SubscriberID)
  64. if opts.UpdatedAfterUnix > 0 {
  65. searchOpt.UpdatedAfterUnix = optional.Some(opts.UpdatedAfterUnix)
  66. }
  67. if opts.UpdatedBeforeUnix > 0 {
  68. searchOpt.UpdatedBeforeUnix = optional.Some(opts.UpdatedBeforeUnix)
  69. }
  70. searchOpt.Paginator = opts.Paginator
  71. switch opts.SortType {
  72. case "", "latest":
  73. searchOpt.SortBy = SortByCreatedDesc
  74. case "oldest":
  75. searchOpt.SortBy = SortByCreatedAsc
  76. case "recentupdate":
  77. searchOpt.SortBy = SortByUpdatedDesc
  78. case "leastupdate":
  79. searchOpt.SortBy = SortByUpdatedAsc
  80. case "mostcomment":
  81. searchOpt.SortBy = SortByCommentsDesc
  82. case "leastcomment":
  83. searchOpt.SortBy = SortByCommentsAsc
  84. case "nearduedate":
  85. searchOpt.SortBy = SortByDeadlineAsc
  86. case "farduedate":
  87. searchOpt.SortBy = SortByDeadlineDesc
  88. case "priority", "priorityrepo", "project-column-sorting":
  89. // Unsupported sort type for search
  90. fallthrough
  91. default:
  92. if strings.HasPrefix(opts.SortType, issues_model.ScopeSortPrefix) {
  93. searchOpt.SortBy = internal.SortBy(opts.SortType)
  94. } else {
  95. searchOpt.SortBy = SortByUpdatedDesc
  96. }
  97. }
  98. return searchOpt
  99. }