|
| 1 | +package repo |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "code.gitea.io/gitea/models/db" |
| 7 | + issues_model "code.gitea.io/gitea/models/issues" |
| 8 | + issue_indexer "code.gitea.io/gitea/modules/indexer/issues" |
| 9 | + "code.gitea.io/gitea/services/context" |
| 10 | +) |
| 11 | + |
| 12 | +type issueSuggestion struct { |
| 13 | + ID int64 `json:"id"` |
| 14 | + Title string `json:"title"` |
| 15 | + State string `json:"state"` |
| 16 | + PullRequest *struct { |
| 17 | + Merged bool `json:"merged"` |
| 18 | + Draft bool `json:"draft"` |
| 19 | + } `json:"pull_request,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +// IssueSuggestions returns a list of issue suggestions |
| 23 | +func IssueSuggestions(ctx *context.Context) { |
| 24 | + keyword := ctx.Req.FormValue("q") |
| 25 | + |
| 26 | + searchOpt := &issue_indexer.SearchOptions{ |
| 27 | + Paginator: &db.ListOptions{ |
| 28 | + Page: 0, |
| 29 | + PageSize: 5, |
| 30 | + }, |
| 31 | + Keyword: keyword, |
| 32 | + RepoIDs: []int64{ctx.Repo.Repository.ID}, |
| 33 | + IsPull: nil, |
| 34 | + IsClosed: nil, |
| 35 | + SortBy: issue_indexer.SortByUpdatedDesc, |
| 36 | + } |
| 37 | + |
| 38 | + ids, _, err := issue_indexer.SearchIssues(ctx, searchOpt) |
| 39 | + if err != nil { |
| 40 | + ctx.ServerError("SearchIssues", err) |
| 41 | + return |
| 42 | + } |
| 43 | + issues, err := issues_model.GetIssuesByIDs(ctx, ids, true) |
| 44 | + if err != nil { |
| 45 | + ctx.ServerError("FindIssuesByIDs", err) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + suggestions := make([]*issueSuggestion, 0, len(issues)) |
| 50 | + |
| 51 | + for _, issue := range issues { |
| 52 | + suggestion := &issueSuggestion{ |
| 53 | + ID: issue.ID, |
| 54 | + Title: issue.Title, |
| 55 | + State: string(issue.State()), |
| 56 | + } |
| 57 | + |
| 58 | + if issue.IsPull { |
| 59 | + if err := issue.LoadPullRequest(ctx); err != nil { |
| 60 | + ctx.ServerError("LoadPullRequest", err) |
| 61 | + return |
| 62 | + } |
| 63 | + if issue.PullRequest != nil { |
| 64 | + suggestion.PullRequest = &struct { |
| 65 | + Merged bool `json:"merged"` |
| 66 | + Draft bool `json:"draft"` |
| 67 | + }{ |
| 68 | + Merged: issue.PullRequest.HasMerged, |
| 69 | + Draft: issue.PullRequest.IsWorkInProgress(ctx), |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + suggestions = append(suggestions, suggestion) |
| 75 | + } |
| 76 | + |
| 77 | + ctx.JSON(http.StatusOK, suggestions) |
| 78 | +} |
0 commit comments