Skip to content

Commit

Permalink
Merge branch 'main' into fix-repo-file-table
Browse files Browse the repository at this point in the history
# Conflicts:
#	web_src/css/index.css
  • Loading branch information
wxiaoguang committed Dec 11, 2024
2 parents 562e63b + 18061af commit 5d60d68
Show file tree
Hide file tree
Showing 97 changed files with 1,197 additions and 739 deletions.
12 changes: 7 additions & 5 deletions models/db/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ const (
SearchOrderByForksReverse SearchOrderBy = "num_forks DESC"
)

const (
// Which means a condition to filter the records which don't match any id.
// It's different from zero which means the condition could be ignored.
NoConditionID = -1
)
// NoConditionID means a condition to filter the records which don't match any id.
// eg: "milestone_id=-1" means "find the items without any milestone.
const NoConditionID int64 = -1

// NonExistingID means a condition to match no result (eg: a non-existing user)
// It doesn't use -1 or -2 because they are used as builtin users.
const NonExistingID int64 = -1000000
20 changes: 18 additions & 2 deletions models/fixtures/review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
reviewer_id: 1
issue_id: 2
content: "Demo Review"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -12,6 +13,7 @@
reviewer_id: 534543
issue_id: 534543
content: "Invalid Review #1"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -20,6 +22,7 @@
reviewer_id: 1
issue_id: 343545
content: "Invalid Review #2"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -28,6 +31,7 @@
reviewer_id: 1
issue_id: 2
content: "Pending Review"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -36,6 +40,7 @@
reviewer_id: 1
issue_id: 3
content: "New review 1"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810
-
Expand All @@ -61,8 +66,8 @@
type: 1
reviewer_id: 4
issue_id: 3
original_author_id: 0
content: "New review 5"
original_author_id: 0
commit_id: 8091a55037cd59e47293aca02981b5a67076b364
stale: true
updated_unix: 946684813
Expand All @@ -73,16 +78,17 @@
reviewer_id: 2
issue_id: 3
content: "New review 3 rejected"
original_author_id: 0
updated_unix: 946684814
created_unix: 946684814
original_author_id: 0

-
id: 10
type: 3
reviewer_id: 100
issue_id: 3
content: "a deleted user's review"
original_author_id: 0
official: true
updated_unix: 946684815
created_unix: 946684815
Expand Down Expand Up @@ -112,6 +118,7 @@
reviewer_id: 5
issue_id: 11
content: "old review from user5"
original_author_id: 0
updated_unix: 946684820
created_unix: 946684820

Expand All @@ -121,6 +128,7 @@
reviewer_id: 5
issue_id: 11
content: "duplicate review from user5 (latest)"
original_author_id: 0
updated_unix: 946684830
created_unix: 946684830

Expand All @@ -130,6 +138,7 @@
reviewer_id: 6
issue_id: 11
content: "singular review from org6 and final review for this pr"
original_author_id: 0
updated_unix: 946684831
created_unix: 946684831

Expand All @@ -139,6 +148,7 @@
reviewer_id: 20
issue_id: 20
content: "review request for user20"
original_author_id: 0
updated_unix: 946684832
created_unix: 946684832

Expand All @@ -148,6 +158,7 @@
reviewer_id: 20
issue_id: 20
content: "review approved by user20"
original_author_id: 0
updated_unix: 946684833
created_unix: 946684833

Expand All @@ -158,6 +169,7 @@
reviewer_team_id: 5
issue_id: 20
content: "review request for team5"
original_author_id: 0
updated_unix: 946684834
created_unix: 946684834

Expand All @@ -168,6 +180,7 @@
reviewer_team_id: 0
issue_id: 20
content: "review request for user15"
original_author_id: 0
updated_unix: 946684835
created_unix: 946684835

Expand All @@ -177,6 +190,7 @@
reviewer_id: 1
issue_id: 2
content: "Review Comment"
original_author_id: 0
updated_unix: 946684810
created_unix: 946684810

Expand All @@ -186,6 +200,7 @@
reviewer_id: 5
issue_id: 3
content: "reviewed by user5"
original_author_id: 0
commit_id: 4a357436d925b5c974181ff12a994538ddc5a269
updated_unix: 946684816
created_unix: 946684816
Expand All @@ -196,5 +211,6 @@
reviewer_id: 5
issue_id: 3
content: "review request for user5"
original_author_id: 0
updated_unix: 946684817
created_unix: 946684817
39 changes: 23 additions & 16 deletions models/issues/issue_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type IssuesOptions struct { //nolint
RepoIDs []int64 // overwrites RepoCond if the length is not 0
AllPublic bool // include also all public repositories
RepoCond builder.Cond
AssigneeID int64
PosterID int64
AssigneeID optional.Option[int64]
PosterID optional.Option[int64]
MentionedID int64
ReviewRequestedID int64
ReviewedID int64
Expand Down Expand Up @@ -231,15 +231,8 @@ func applyConditions(sess *xorm.Session, opts *IssuesOptions) {
sess.And("issue.is_closed=?", opts.IsClosed.Value())
}

if opts.AssigneeID > 0 {
applyAssigneeCondition(sess, opts.AssigneeID)
} else if opts.AssigneeID == db.NoConditionID {
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_assignees)")
}

if opts.PosterID > 0 {
applyPosterCondition(sess, opts.PosterID)
}
applyAssigneeCondition(sess, opts.AssigneeID)
applyPosterCondition(sess, opts.PosterID)

if opts.MentionedID > 0 {
applyMentionedCondition(sess, opts.MentionedID)
Expand Down Expand Up @@ -359,13 +352,27 @@ func issuePullAccessibleRepoCond(repoIDstr string, userID int64, org *organizati
return cond
}

func applyAssigneeCondition(sess *xorm.Session, assigneeID int64) {
sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
And("issue_assignees.assignee_id = ?", assigneeID)
func applyAssigneeCondition(sess *xorm.Session, assigneeID optional.Option[int64]) {
// old logic: 0 is also treated as "not filtering assignee", because the "assignee" was read as FormInt64
if !assigneeID.Has() || assigneeID.Value() == 0 {
return
}
if assigneeID.Value() == db.NoConditionID {
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_assignees)")
} else {
sess.Join("INNER", "issue_assignees", "issue.id = issue_assignees.issue_id").
And("issue_assignees.assignee_id = ?", assigneeID.Value())
}
}

func applyPosterCondition(sess *xorm.Session, posterID int64) {
sess.And("issue.poster_id=?", posterID)
func applyPosterCondition(sess *xorm.Session, posterID optional.Option[int64]) {
if !posterID.Has() {
return
}
// poster doesn't need to support db.NoConditionID(-1), so just use the value as-is
if posterID.Has() {
sess.And("issue.poster_id=?", posterID.Value())
}
}

func applyMentionedCondition(sess *xorm.Session, mentionedID int64) {
Expand Down
10 changes: 2 additions & 8 deletions models/issues/issue_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,9 @@ func applyIssuesOptions(sess *xorm.Session, opts *IssuesOptions, issueIDs []int6

applyProjectCondition(sess, opts)

if opts.AssigneeID > 0 {
applyAssigneeCondition(sess, opts.AssigneeID)
} else if opts.AssigneeID == db.NoConditionID {
sess.Where("issue.id NOT IN (SELECT issue_id FROM issue_assignees)")
}
applyAssigneeCondition(sess, opts.AssigneeID)

if opts.PosterID > 0 {
applyPosterCondition(sess, opts.PosterID)
}
applyPosterCondition(sess, opts.PosterID)

if opts.MentionedID > 0 {
applyMentionedCondition(sess, opts.MentionedID)
Expand Down
3 changes: 2 additions & 1 deletion models/issues/issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestIssues(t *testing.T) {
}{
{
issues_model.IssuesOptions{
AssigneeID: 1,
AssigneeID: optional.Some(int64(1)),
SortType: "oldest",
},
[]int64{1, 6},
Expand Down
59 changes: 59 additions & 0 deletions models/issues/pull_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"

"xorm.io/builder"
"xorm.io/xorm"
)

Expand Down Expand Up @@ -240,6 +241,64 @@ func (prs PullRequestList) GetIssueIDs() []int64 {
})
}

func (prs PullRequestList) LoadReviewCommentsCounts(ctx context.Context) (map[int64]int, error) {
issueIDs := prs.GetIssueIDs()
countsMap := make(map[int64]int, len(issueIDs))
counts := make([]struct {
IssueID int64
Count int
}, 0, len(issueIDs))
if err := db.GetEngine(ctx).Select("issue_id, count(*) as count").
Table("comment").In("issue_id", issueIDs).And("type = ?", CommentTypeReview).
GroupBy("issue_id").Find(&counts); err != nil {
return nil, err
}
for _, c := range counts {
countsMap[c.IssueID] = c.Count
}
return countsMap, nil
}

func (prs PullRequestList) LoadReviews(ctx context.Context) (ReviewList, error) {
issueIDs := prs.GetIssueIDs()
reviews := make([]*Review, 0, len(issueIDs))

subQuery := builder.Select("max(id) as id").
From("review").
Where(builder.In("issue_id", issueIDs)).
And(builder.In("`type`", ReviewTypeApprove, ReviewTypeReject, ReviewTypeRequest)).
And(builder.Eq{
"dismissed": false,
"original_author_id": 0,
"reviewer_team_id": 0,
}).
GroupBy("issue_id, reviewer_id")
// Get latest review of each reviewer, sorted in order they were made
if err := db.GetEngine(ctx).In("id", subQuery).OrderBy("review.updated_unix ASC").Find(&reviews); err != nil {
return nil, err
}

teamReviewRequests := make([]*Review, 0, 5)
subQueryTeam := builder.Select("max(id) as id").
From("review").
Where(builder.In("issue_id", issueIDs)).
And(builder.Eq{
"original_author_id": 0,
}).And(builder.Neq{
"reviewer_team_id": 0,
}).
GroupBy("issue_id, reviewer_team_id")
if err := db.GetEngine(ctx).In("id", subQueryTeam).OrderBy("review.updated_unix ASC").Find(&teamReviewRequests); err != nil {
return nil, err
}

if len(teamReviewRequests) > 0 {
reviews = append(reviews, teamReviewRequests...)
}

return reviews, nil
}

// HasMergedPullRequestInRepo returns whether the user(poster) has merged pull-request in the repo
func HasMergedPullRequestInRepo(ctx context.Context, repoID, posterID int64) (bool, error) {
return db.GetEngine(ctx).
Expand Down
64 changes: 64 additions & 0 deletions models/issues/pull_list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package issues_test

import (
"testing"

"code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/unittest"

"github.com/stretchr/testify/assert"
)

func TestPullRequestList_LoadAttributes(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
assert.NoError(t, issues_model.PullRequestList(prs).LoadAttributes(db.DefaultContext))
for _, pr := range prs {
assert.NotNil(t, pr.Issue)
assert.Equal(t, pr.IssueID, pr.Issue.ID)
}

assert.NoError(t, issues_model.PullRequestList([]*issues_model.PullRequest{}).LoadAttributes(db.DefaultContext))
}

func TestPullRequestList_LoadReviewCommentsCounts(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
reviewComments, err := issues_model.PullRequestList(prs).LoadReviewCommentsCounts(db.DefaultContext)
assert.NoError(t, err)
assert.Len(t, reviewComments, 2)
for _, pr := range prs {
assert.EqualValues(t, reviewComments[pr.IssueID], 1)
}
}

func TestPullRequestList_LoadReviews(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())

prs := []*issues_model.PullRequest{
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}),
unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2}),
}
reviewList, err := issues_model.PullRequestList(prs).LoadReviews(db.DefaultContext)
assert.NoError(t, err)
// 1, 7, 8, 9, 10, 22
assert.Len(t, reviewList, 6)
assert.EqualValues(t, 1, reviewList[0].ID)
assert.EqualValues(t, 7, reviewList[1].ID)
assert.EqualValues(t, 8, reviewList[2].ID)
assert.EqualValues(t, 9, reviewList[3].ID)
assert.EqualValues(t, 10, reviewList[4].ID)
assert.EqualValues(t, 22, reviewList[5].ID)
}
Loading

0 comments on commit 5d60d68

Please sign in to comment.