gitea源码

options.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package db
  4. import (
  5. "context"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models/db"
  9. issue_model "code.gitea.io/gitea/models/issues"
  10. "code.gitea.io/gitea/modules/container"
  11. "code.gitea.io/gitea/modules/indexer/issues/internal"
  12. "code.gitea.io/gitea/modules/optional"
  13. )
  14. func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_model.IssuesOptions, error) {
  15. var sortType string
  16. switch options.SortBy {
  17. case internal.SortByCreatedAsc:
  18. sortType = "oldest"
  19. case internal.SortByUpdatedAsc:
  20. sortType = "leastupdate"
  21. case internal.SortByCommentsAsc:
  22. sortType = "leastcomment"
  23. case internal.SortByDeadlineDesc:
  24. sortType = "farduedate"
  25. case internal.SortByCreatedDesc:
  26. sortType = "newest"
  27. case internal.SortByUpdatedDesc:
  28. sortType = "recentupdate"
  29. case internal.SortByCommentsDesc:
  30. sortType = "mostcomment"
  31. case internal.SortByDeadlineAsc:
  32. sortType = "nearduedate"
  33. default:
  34. if strings.HasPrefix(string(options.SortBy), issue_model.ScopeSortPrefix) {
  35. sortType = string(options.SortBy)
  36. } else {
  37. sortType = "newest"
  38. }
  39. }
  40. // See the comment of issues_model.SearchOptions for the reason why we need to convert
  41. convertID := func(id optional.Option[int64]) int64 {
  42. if !id.Has() {
  43. return 0
  44. }
  45. value := id.Value()
  46. if value == 0 {
  47. return db.NoConditionID
  48. }
  49. return value
  50. }
  51. opts := &issue_model.IssuesOptions{
  52. Paginator: options.Paginator,
  53. RepoIDs: options.RepoIDs,
  54. AllPublic: options.AllPublic,
  55. RepoCond: nil,
  56. AssigneeID: options.AssigneeID,
  57. PosterID: options.PosterID,
  58. MentionedID: convertID(options.MentionID),
  59. ReviewRequestedID: convertID(options.ReviewRequestedID),
  60. ReviewedID: convertID(options.ReviewedID),
  61. SubscriberID: convertID(options.SubscriberID),
  62. ProjectID: convertID(options.ProjectID),
  63. ProjectColumnID: convertID(options.ProjectColumnID),
  64. IsClosed: options.IsClosed,
  65. IsPull: options.IsPull,
  66. IncludedLabelNames: nil,
  67. ExcludedLabelNames: nil,
  68. IncludeMilestones: nil,
  69. SortType: sortType,
  70. UpdatedAfterUnix: options.UpdatedAfterUnix.Value(),
  71. UpdatedBeforeUnix: options.UpdatedBeforeUnix.Value(),
  72. PriorityRepoID: 0,
  73. IsArchived: options.IsArchived,
  74. Owner: nil,
  75. Team: nil,
  76. Doer: nil,
  77. }
  78. if len(options.MilestoneIDs) == 1 && options.MilestoneIDs[0] == 0 {
  79. opts.MilestoneIDs = []int64{db.NoConditionID}
  80. } else {
  81. opts.MilestoneIDs = options.MilestoneIDs
  82. }
  83. if options.NoLabelOnly {
  84. opts.LabelIDs = []int64{0} // Be careful, it's zero, not db.NoConditionID
  85. } else {
  86. opts.LabelIDs = make([]int64, 0, len(options.IncludedLabelIDs)+len(options.ExcludedLabelIDs))
  87. opts.LabelIDs = append(opts.LabelIDs, options.IncludedLabelIDs...)
  88. for _, id := range options.ExcludedLabelIDs {
  89. opts.LabelIDs = append(opts.LabelIDs, -id)
  90. }
  91. if len(options.IncludedLabelIDs) == 0 && len(options.IncludedAnyLabelIDs) > 0 {
  92. labels, err := issue_model.GetLabelsByIDs(ctx, options.IncludedAnyLabelIDs, "name")
  93. if err != nil {
  94. return nil, fmt.Errorf("GetLabelsByIDs: %v", err)
  95. }
  96. set := container.Set[string]{}
  97. for _, label := range labels {
  98. if !set.Contains(label.Name) {
  99. set.Add(label.Name)
  100. opts.IncludedLabelNames = append(opts.IncludedLabelNames, label.Name)
  101. }
  102. }
  103. }
  104. }
  105. return opts, nil
  106. }