Skip to content

Commit 60b750e

Browse files
committed
Remove duplicate functions
1 parent ab083c8 commit 60b750e

File tree

4 files changed

+71
-149
lines changed

4 files changed

+71
-149
lines changed

models/conversations/comment.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
user_model "code.gitea.io/gitea/models/user"
1616
"code.gitea.io/gitea/modules/container"
1717
"code.gitea.io/gitea/modules/log"
18-
"code.gitea.io/gitea/modules/optional"
1918
"code.gitea.io/gitea/modules/structs"
2019
"code.gitea.io/gitea/modules/timeutil"
2120
"code.gitea.io/gitea/modules/translation"
@@ -276,15 +275,10 @@ type FindCommentsOptions struct {
276275
db.ListOptions
277276
RepoID int64
278277
ConversationID int64
279-
ReviewID int64
280278
Since int64
281279
Before int64
282-
Line int64
283-
TreePath string
284280
Type CommentType
285281
ConversationIDs []int64
286-
Invalidated optional.Option[bool]
287-
IsPull optional.Option[bool]
288282
}
289283

290284
// ToConds implements FindOptions interface

routers/api/v1/repo/conversation_comment.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
conversations_model "code.gitea.io/gitea/models/conversations"
1212
"code.gitea.io/gitea/models/unit"
1313
user_model "code.gitea.io/gitea/models/user"
14-
"code.gitea.io/gitea/modules/optional"
1514
api "code.gitea.io/gitea/modules/structs"
1615
"code.gitea.io/gitea/modules/web"
1716
"code.gitea.io/gitea/routers/api/v1/utils"
@@ -253,16 +252,8 @@ func ListRepoConversationComments(ctx *context.APIContext) {
253252
return
254253
}
255254

256-
var isPull optional.Option[bool]
257255
canReadConversation := ctx.Repo.CanRead(unit.TypeConversations)
258-
canReadPull := ctx.Repo.CanRead(unit.TypePullRequests)
259-
if canReadConversation && canReadPull {
260-
isPull = optional.None[bool]()
261-
} else if canReadConversation {
262-
isPull = optional.Some(false)
263-
} else if canReadPull {
264-
isPull = optional.Some(true)
265-
} else {
256+
if !canReadConversation {
266257
ctx.NotFound()
267258
return
268259
}
@@ -273,7 +264,6 @@ func ListRepoConversationComments(ctx *context.APIContext) {
273264
Type: conversations_model.CommentTypeComment,
274265
Since: since,
275266
Before: before,
276-
IsPull: isPull,
277267
}
278268

279269
comments, err := conversations_model.FindComments(ctx, opts)

routers/web/repo/conversation.go

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -430,72 +430,7 @@ func SearchConversations(ctx *context.Context) {
430430
isClosed = optional.Some(false)
431431
}
432432

433-
var (
434-
repoIDs []int64
435-
allPublic bool
436-
)
437-
{
438-
// find repos user can access (for conversation search)
439-
opts := &repo_model.SearchRepoOptions{
440-
Private: false,
441-
AllPublic: true,
442-
TopicOnly: false,
443-
Collaborate: optional.None[bool](),
444-
// This needs to be a column that is not nil in fixtures or
445-
// MySQL will return different results when sorting by null in some cases
446-
OrderBy: db.SearchOrderByAlphabetically,
447-
Actor: ctx.Doer,
448-
}
449-
if ctx.IsSigned {
450-
opts.Private = true
451-
opts.AllLimited = true
452-
}
453-
if ctx.FormString("owner") != "" {
454-
owner, err := user_model.GetUserByName(ctx, ctx.FormString("owner"))
455-
if err != nil {
456-
if user_model.IsErrUserNotExist(err) {
457-
ctx.Error(http.StatusBadRequest, "Owner not found", err.Error())
458-
} else {
459-
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
460-
}
461-
return
462-
}
463-
opts.OwnerID = owner.ID
464-
opts.AllLimited = false
465-
opts.AllPublic = false
466-
opts.Collaborate = optional.Some(false)
467-
}
468-
if ctx.FormString("team") != "" {
469-
if ctx.FormString("owner") == "" {
470-
ctx.Error(http.StatusBadRequest, "", "Owner organisation is required for filtering on team")
471-
return
472-
}
473-
team, err := organization.GetTeam(ctx, opts.OwnerID, ctx.FormString("team"))
474-
if err != nil {
475-
if organization.IsErrTeamNotExist(err) {
476-
ctx.Error(http.StatusBadRequest, "Team not found", err.Error())
477-
} else {
478-
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
479-
}
480-
return
481-
}
482-
opts.TeamID = team.ID
483-
}
484-
485-
if opts.AllPublic {
486-
allPublic = true
487-
opts.AllPublic = false // set it false to avoid returning too many repos, we could filter by indexer
488-
}
489-
repoIDs, _, err = repo_model.SearchRepositoryIDs(ctx, opts)
490-
if err != nil {
491-
ctx.Error(http.StatusInternalServerError, "SearchRepositoryIDs", err.Error())
492-
return
493-
}
494-
if len(repoIDs) == 0 {
495-
// no repos found, don't let the indexer return all repos
496-
repoIDs = []int64{0}
497-
}
498-
}
433+
repoIDs, allPublic := GetUserAccessibleRepo(ctx)
499434

500435
keyword := ctx.FormTrim("q")
501436
if strings.IndexByte(keyword, 0) >= 0 {
@@ -568,6 +503,74 @@ func SearchConversations(ctx *context.Context) {
568503
ctx.JSON(http.StatusOK, convert.ToConversationList(ctx, ctx.Doer, conversations))
569504
}
570505

506+
func GetUserAccessibleRepo(ctx *context.Context) ([]int64, bool) {
507+
var (
508+
repoIDs []int64
509+
allPublic bool
510+
)
511+
// find repos user can access (for conversation search)
512+
opts := &repo_model.SearchRepoOptions{
513+
Private: false,
514+
AllPublic: true,
515+
TopicOnly: false,
516+
Collaborate: optional.None[bool](),
517+
// This needs to be a column that is not nil in fixtures or
518+
// MySQL will return different results when sorting by null in some cases
519+
OrderBy: db.SearchOrderByAlphabetically,
520+
Actor: ctx.Doer,
521+
}
522+
if ctx.IsSigned {
523+
opts.Private = true
524+
opts.AllLimited = true
525+
}
526+
if ctx.FormString("owner") != "" {
527+
owner, err := user_model.GetUserByName(ctx, ctx.FormString("owner"))
528+
if err != nil {
529+
if user_model.IsErrUserNotExist(err) {
530+
ctx.Error(http.StatusBadRequest, "Owner not found", err.Error())
531+
} else {
532+
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
533+
}
534+
return nil, false
535+
}
536+
opts.OwnerID = owner.ID
537+
opts.AllLimited = false
538+
opts.AllPublic = false
539+
opts.Collaborate = optional.Some(false)
540+
}
541+
if ctx.FormString("team") != "" {
542+
if ctx.FormString("owner") == "" {
543+
ctx.Error(http.StatusBadRequest, "", "Owner organisation is required for filtering on team")
544+
return nil, false
545+
}
546+
team, err := organization.GetTeam(ctx, opts.OwnerID, ctx.FormString("team"))
547+
if err != nil {
548+
if organization.IsErrTeamNotExist(err) {
549+
ctx.Error(http.StatusBadRequest, "Team not found", err.Error())
550+
} else {
551+
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
552+
}
553+
return nil, false
554+
}
555+
opts.TeamID = team.ID
556+
}
557+
558+
if opts.AllPublic {
559+
allPublic = true
560+
opts.AllPublic = false // set it false to avoid returning too many repos, we could filter by indexer
561+
}
562+
repoIDs, _, err := repo_model.SearchRepositoryIDs(ctx, opts)
563+
if err != nil {
564+
ctx.Error(http.StatusInternalServerError, "SearchRepositoryIDs", err.Error())
565+
return nil, false
566+
}
567+
if len(repoIDs) == 0 {
568+
// no repos found, don't let the indexer return all repos
569+
repoIDs = []int64{0}
570+
}
571+
return repoIDs, allPublic
572+
}
573+
571574
// ListConversations list the conversations of a repository
572575
func ListConversations(ctx *context.Context) {
573576
before, since, err := context.GetQueryBeforeSince(ctx.Base)

routers/web/repo/issue.go

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,72 +2575,7 @@ func SearchIssues(ctx *context.Context) {
25752575
isClosed = optional.Some(false)
25762576
}
25772577

2578-
var (
2579-
repoIDs []int64
2580-
allPublic bool
2581-
)
2582-
{
2583-
// find repos user can access (for issue search)
2584-
opts := &repo_model.SearchRepoOptions{
2585-
Private: false,
2586-
AllPublic: true,
2587-
TopicOnly: false,
2588-
Collaborate: optional.None[bool](),
2589-
// This needs to be a column that is not nil in fixtures or
2590-
// MySQL will return different results when sorting by null in some cases
2591-
OrderBy: db.SearchOrderByAlphabetically,
2592-
Actor: ctx.Doer,
2593-
}
2594-
if ctx.IsSigned {
2595-
opts.Private = true
2596-
opts.AllLimited = true
2597-
}
2598-
if ctx.FormString("owner") != "" {
2599-
owner, err := user_model.GetUserByName(ctx, ctx.FormString("owner"))
2600-
if err != nil {
2601-
if user_model.IsErrUserNotExist(err) {
2602-
ctx.Error(http.StatusBadRequest, "Owner not found", err.Error())
2603-
} else {
2604-
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
2605-
}
2606-
return
2607-
}
2608-
opts.OwnerID = owner.ID
2609-
opts.AllLimited = false
2610-
opts.AllPublic = false
2611-
opts.Collaborate = optional.Some(false)
2612-
}
2613-
if ctx.FormString("team") != "" {
2614-
if ctx.FormString("owner") == "" {
2615-
ctx.Error(http.StatusBadRequest, "", "Owner organisation is required for filtering on team")
2616-
return
2617-
}
2618-
team, err := organization.GetTeam(ctx, opts.OwnerID, ctx.FormString("team"))
2619-
if err != nil {
2620-
if organization.IsErrTeamNotExist(err) {
2621-
ctx.Error(http.StatusBadRequest, "Team not found", err.Error())
2622-
} else {
2623-
ctx.Error(http.StatusInternalServerError, "GetUserByName", err.Error())
2624-
}
2625-
return
2626-
}
2627-
opts.TeamID = team.ID
2628-
}
2629-
2630-
if opts.AllPublic {
2631-
allPublic = true
2632-
opts.AllPublic = false // set it false to avoid returning too many repos, we could filter by indexer
2633-
}
2634-
repoIDs, _, err = repo_model.SearchRepositoryIDs(ctx, opts)
2635-
if err != nil {
2636-
ctx.Error(http.StatusInternalServerError, "SearchRepositoryIDs", err.Error())
2637-
return
2638-
}
2639-
if len(repoIDs) == 0 {
2640-
// no repos found, don't let the indexer return all repos
2641-
repoIDs = []int64{0}
2642-
}
2643-
}
2578+
repoIDs, allPublic := GetUserAccessibleRepo(ctx)
26442579

26452580
keyword := ctx.FormTrim("q")
26462581
if strings.IndexByte(keyword, 0) >= 0 {

0 commit comments

Comments
 (0)