aboutsummaryrefslogtreecommitdiffstats
path: root/modules/indexer/issues/dboptions.go
blob: d0ef1c96b44870e6161952ca8aa0dd5d9e35fe60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package issues

import (
	"code.gitea.io/gitea/models/db"
	issues_model "code.gitea.io/gitea/models/issues"
)

func ToSearchOptions(keyword string, opts *issues_model.IssuesOptions) *SearchOptions {
	searchOpt := &SearchOptions{
		Keyword:   keyword,
		RepoIDs:   opts.RepoIDs,
		AllPublic: false,
		IsPull:    opts.IsPull,
		IsClosed:  opts.IsClosed,
	}

	if opts.ProjectID != 0 {
		searchOpt.ProjectID = &opts.ProjectID
	}

	if len(opts.LabelIDs) == 1 && opts.LabelIDs[0] == 0 {
		searchOpt.NoLabelOnly = true
	} else {
		for _, labelID := range opts.LabelIDs {
			if labelID > 0 {
				searchOpt.IncludedLabelIDs = append(searchOpt.IncludedLabelIDs, labelID)
			} else {
				searchOpt.ExcludedLabelIDs = append(searchOpt.ExcludedLabelIDs, -labelID)
			}
		}
		// opts.IncludedLabelNames and opts.ExcludedLabelNames are not supported here.
		// It's not a TO DO, it's just unnecessary.
	}

	if len(opts.MilestoneIDs) == 1 && opts.MilestoneIDs[0] == db.NoConditionID {
		searchOpt.MilestoneIDs = []int64{0}
	} else {
		searchOpt.MilestoneIDs = opts.MilestoneIDs
	}

	if opts.AssigneeID > 0 {
		searchOpt.AssigneeID = &opts.AssigneeID
	}
	if opts.PosterID > 0 {
		searchOpt.PosterID = &opts.PosterID
	}
	if opts.MentionedID > 0 {
		searchOpt.MentionID = &opts.MentionedID
	}
	if opts.ReviewedID > 0 {
		searchOpt.ReviewedID = &opts.ReviewedID
	}
	if opts.ReviewRequestedID > 0 {
		searchOpt.ReviewRequestedID = &opts.ReviewRequestedID
	}
	if opts.SubscriberID > 0 {
		searchOpt.SubscriberID = &opts.SubscriberID
	}

	if opts.UpdatedAfterUnix > 0 {
		searchOpt.UpdatedAfterUnix = &opts.UpdatedAfterUnix
	}
	if opts.UpdatedBeforeUnix > 0 {
		searchOpt.UpdatedBeforeUnix = &opts.UpdatedBeforeUnix
	}

	searchOpt.Paginator = opts.Paginator

	switch opts.SortType {
	case "":
		searchOpt.SortBy = SortByCreatedDesc
	case "oldest":
		searchOpt.SortBy = SortByCreatedAsc
	case "recentupdate":
		searchOpt.SortBy = SortByUpdatedDesc
	case "leastupdate":
		searchOpt.SortBy = SortByUpdatedAsc
	case "mostcomment":
		searchOpt.SortBy = SortByCommentsDesc
	case "leastcomment":
		searchOpt.SortBy = SortByCommentsAsc
	case "nearduedate":
		searchOpt.SortBy = SortByDeadlineAsc
	case "farduedate":
		searchOpt.SortBy = SortByDeadlineDesc
	case "priority", "priorityrepo", "project-column-sorting":
		// Unsupported sort type for search
		searchOpt.SortBy = SortByUpdatedDesc
	default:
		searchOpt.SortBy = SortByUpdatedDesc
	}

	return searchOpt
}