code.gitea.io/gitea@v1.22.3/modules/indexer/issues/db/options.go (about)

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