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