mirror of
				https://github.com/go-gitea/gitea
				synced 2025-10-31 03:18:24 +00:00 
			
		
		
		
	Fix #33522 The suggestion backend logic now is - If the keyword is empty, returned the latest 5 issues/prs with index desc order - If the keyword is digital, find all issues/prs which `index` has a prefix with that, with index asc order - If the keyword is non-digital or if the queried records less than 5, searching issues/prs title with a `like`, with index desc order ## Empty keyword <img width="310" alt="image" src="https://github.com/user-attachments/assets/1912c634-0d98-4eeb-8542-d54240901f77" /> ## Digital <img width="479" alt="image" src="https://github.com/user-attachments/assets/0356a936-7110-4a24-b21e-7400201bf9b8" /> ## Digital and title contains the digital <img width="363" alt="image" src="https://github.com/user-attachments/assets/6e12f908-28fe-48de-8ccc-09cbeab024d4" /> ## non-Digital <img width="435" alt="image" src="https://github.com/user-attachments/assets/2722bb53-baa2-4d67-a224-522a65f73856" /> <img width="477" alt="image" src="https://github.com/user-attachments/assets/06708dd9-80d1-4a88-b32b-d29072dd1ba6" /> --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
		
			
				
	
	
		
			74 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2025 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package issue
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"strconv"
 | |
| 
 | |
| 	issues_model "code.gitea.io/gitea/models/issues"
 | |
| 	repo_model "code.gitea.io/gitea/models/repo"
 | |
| 	"code.gitea.io/gitea/modules/optional"
 | |
| 	"code.gitea.io/gitea/modules/structs"
 | |
| )
 | |
| 
 | |
| func GetSuggestion(ctx context.Context, repo *repo_model.Repository, isPull optional.Option[bool], keyword string) ([]*structs.Issue, error) {
 | |
| 	var issues issues_model.IssueList
 | |
| 	var err error
 | |
| 	pageSize := 5
 | |
| 	if keyword == "" {
 | |
| 		issues, err = issues_model.FindLatestUpdatedIssues(ctx, repo.ID, isPull, pageSize)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 	} else {
 | |
| 		indexKeyword, _ := strconv.ParseInt(keyword, 10, 64)
 | |
| 		var issueByIndex *issues_model.Issue
 | |
| 		var excludedID int64
 | |
| 		if indexKeyword > 0 {
 | |
| 			issueByIndex, err = issues_model.GetIssueByIndex(ctx, repo.ID, indexKeyword)
 | |
| 			if err != nil && !issues_model.IsErrIssueNotExist(err) {
 | |
| 				return nil, err
 | |
| 			}
 | |
| 			if issueByIndex != nil {
 | |
| 				excludedID = issueByIndex.ID
 | |
| 				pageSize--
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		issues, err = issues_model.FindIssuesSuggestionByKeyword(ctx, repo.ID, keyword, isPull, excludedID, pageSize)
 | |
| 		if err != nil {
 | |
| 			return nil, err
 | |
| 		}
 | |
| 
 | |
| 		if issueByIndex != nil {
 | |
| 			issues = append([]*issues_model.Issue{issueByIndex}, issues...)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	if err := issues.LoadPullRequests(ctx); err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	suggestions := make([]*structs.Issue, 0, len(issues))
 | |
| 	for _, issue := range issues {
 | |
| 		suggestion := &structs.Issue{
 | |
| 			ID:    issue.ID,
 | |
| 			Index: issue.Index,
 | |
| 			Title: issue.Title,
 | |
| 			State: issue.State(),
 | |
| 		}
 | |
| 
 | |
| 		if issue.IsPull && issue.PullRequest != nil {
 | |
| 			suggestion.PullRequest = &structs.PullRequestMeta{
 | |
| 				HasMerged:        issue.PullRequest.HasMerged,
 | |
| 				IsWorkInProgress: issue.PullRequest.IsWorkInProgress(ctx),
 | |
| 			}
 | |
| 		}
 | |
| 		suggestions = append(suggestions, suggestion)
 | |
| 	}
 | |
| 
 | |
| 	return suggestions, nil
 | |
| }
 |