You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

options.go 3.3KB

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