2024-10-29 03:20:49 -06:00
|
|
|
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
|
|
|
issue_indexer "code.gitea.io/gitea/modules/indexer/issues"
|
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2024-10-30 14:06:36 -06:00
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2024-10-29 03:20:49 -06:00
|
|
|
"code.gitea.io/gitea/services/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IssueSuggestions returns a list of issue suggestions
|
|
|
|
func IssueSuggestions(ctx *context.Context) {
|
|
|
|
keyword := ctx.Req.FormValue("q")
|
|
|
|
|
|
|
|
canReadIssues := ctx.Repo.CanRead(unit.TypeIssues)
|
|
|
|
canReadPulls := ctx.Repo.CanRead(unit.TypePullRequests)
|
|
|
|
|
|
|
|
var isPull optional.Option[bool]
|
|
|
|
if canReadPulls && !canReadIssues {
|
|
|
|
isPull = optional.Some(true)
|
|
|
|
} else if canReadIssues && !canReadPulls {
|
|
|
|
isPull = optional.Some(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
searchOpt := &issue_indexer.SearchOptions{
|
|
|
|
Paginator: &db.ListOptions{
|
|
|
|
Page: 0,
|
|
|
|
PageSize: 5,
|
|
|
|
},
|
|
|
|
Keyword: keyword,
|
|
|
|
RepoIDs: []int64{ctx.Repo.Repository.ID},
|
|
|
|
IsPull: isPull,
|
|
|
|
IsClosed: nil,
|
|
|
|
SortBy: issue_indexer.SortByUpdatedDesc,
|
|
|
|
}
|
|
|
|
|
|
|
|
ids, _, err := issue_indexer.SearchIssues(ctx, searchOpt)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SearchIssues", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
issues, err := issues_model.GetIssuesByIDs(ctx, ids, true)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("FindIssuesByIDs", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-30 14:06:36 -06:00
|
|
|
suggestions := make([]*structs.Issue, 0, len(issues))
|
2024-10-29 03:20:49 -06:00
|
|
|
|
|
|
|
for _, issue := range issues {
|
2024-10-30 14:06:36 -06:00
|
|
|
suggestion := &structs.Issue{
|
2024-10-29 03:20:49 -06:00
|
|
|
ID: issue.ID,
|
2024-10-30 14:06:36 -06:00
|
|
|
Index: issue.Index,
|
2024-10-29 03:20:49 -06:00
|
|
|
Title: issue.Title,
|
2024-10-30 14:06:36 -06:00
|
|
|
State: issue.State(),
|
2024-10-29 03:20:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if issue.IsPull {
|
|
|
|
if err := issue.LoadPullRequest(ctx); err != nil {
|
|
|
|
ctx.ServerError("LoadPullRequest", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if issue.PullRequest != nil {
|
2024-10-30 14:06:36 -06:00
|
|
|
suggestion.PullRequest = &structs.PullRequestMeta{
|
|
|
|
HasMerged: issue.PullRequest.HasMerged,
|
|
|
|
IsWorkInProgress: issue.PullRequest.IsWorkInProgress(ctx),
|
2024-10-29 03:20:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions = append(suggestions, suggestion)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(http.StatusOK, suggestions)
|
|
|
|
}
|