diff options
Diffstat (limited to 'models/issues')
-rw-r--r-- | models/issues/pull.go | 30 | ||||
-rw-r--r-- | models/issues/pull_test.go | 29 | ||||
-rw-r--r-- | models/issues/review.go | 25 | ||||
-rw-r--r-- | models/issues/review_test.go | 17 |
4 files changed, 93 insertions, 8 deletions
diff --git a/models/issues/pull.go b/models/issues/pull.go index 218a265741..3f37d8d243 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -175,9 +175,10 @@ type PullRequest struct { ChangedProtectedFiles []string `xorm:"TEXT JSON"` - IssueID int64 `xorm:"INDEX"` - Issue *Issue `xorm:"-"` - Index int64 + IssueID int64 `xorm:"INDEX"` + Issue *Issue `xorm:"-"` + Index int64 + RequestedReviewers []*user_model.User `xorm:"-"` HeadRepoID int64 `xorm:"INDEX"` HeadRepo *repo_model.Repository `xorm:"-"` @@ -302,6 +303,29 @@ func (pr *PullRequest) LoadHeadRepo(ctx context.Context) (err error) { return nil } +// LoadRequestedReviewers loads the requested reviewers. +func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error { + if len(pr.RequestedReviewers) > 0 { + return nil + } + + reviews, err := GetReviewsByIssueID(pr.Issue.ID) + if err != nil { + return err + } + + if len(reviews) > 0 { + err = LoadReviewers(ctx, reviews) + if err != nil { + return err + } + for _, review := range reviews { + pr.RequestedReviewers = append(pr.RequestedReviewers, review.Reviewer) + } + } + return nil +} + // LoadBaseRepo loads the target repository. ErrRepoNotExist may be returned. func (pr *PullRequest) LoadBaseRepo(ctx context.Context) (err error) { if pr.BaseRepo != nil { diff --git a/models/issues/pull_test.go b/models/issues/pull_test.go index 0b06095213..7a183ac312 100644 --- a/models/issues/pull_test.go +++ b/models/issues/pull_test.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/gitea/models/db" issues_model "code.gitea.io/gitea/models/issues" "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" "github.com/stretchr/testify/assert" ) @@ -74,6 +75,34 @@ func TestPullRequestsNewest(t *testing.T) { } } +func TestLoadRequestedReviewers(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1}) + assert.NoError(t, pull.LoadIssue(db.DefaultContext)) + issue := pull.Issue + assert.NoError(t, issue.LoadRepo(db.DefaultContext)) + assert.Len(t, pull.RequestedReviewers, 0) + + user1, err := user_model.GetUserByID(db.DefaultContext, 1) + assert.NoError(t, err) + + comment, err := issues_model.AddReviewRequest(issue, user1, &user_model.User{}) + assert.NoError(t, err) + assert.NotNil(t, comment) + + assert.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext)) + assert.Len(t, pull.RequestedReviewers, 1) + + comment, err = issues_model.RemoveReviewRequest(issue, user1, &user_model.User{}) + assert.NoError(t, err) + assert.NotNil(t, comment) + + pull.RequestedReviewers = nil + assert.NoError(t, pull.LoadRequestedReviewers(db.DefaultContext)) + assert.Empty(t, pull.RequestedReviewers) +} + func TestPullRequestsOldest(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) prs, count, err := issues_model.PullRequests(1, &issues_model.PullRequestsOptions{ diff --git a/models/issues/review.go b/models/issues/review.go index ed30bce149..06cf132a48 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -162,6 +162,27 @@ func (r *Review) LoadReviewer(ctx context.Context) (err error) { return err } +// LoadReviewers loads reviewers +func LoadReviewers(ctx context.Context, reviews []*Review) (err error) { + reviewerIds := make([]int64, len(reviews)) + for i := 0; i < len(reviews); i++ { + reviewerIds[i] = reviews[i].ReviewerID + } + reviewers, err := user_model.GetPossibleUserByIDs(ctx, reviewerIds) + if err != nil { + return err + } + + userMap := make(map[int64]*user_model.User, len(reviewers)) + for _, reviewer := range reviewers { + userMap[reviewer.ID] = reviewer + } + for _, review := range reviews { + review.Reviewer = userMap[review.ReviewerID] + } + return nil +} + // LoadReviewerTeam loads reviewer team func (r *Review) LoadReviewerTeam(ctx context.Context) (err error) { if r.ReviewerTeamID == 0 || r.ReviewerTeam != nil { @@ -520,8 +541,8 @@ func GetReviews(ctx context.Context, opts *GetReviewOptions) ([]*Review, error) return reviews, sess.Find(&reviews) } -// GetReviewersByIssueID gets the latest review of each reviewer for a pull request -func GetReviewersByIssueID(issueID int64) ([]*Review, error) { +// GetReviewsByIssueID gets the latest review of each reviewer for a pull request +func GetReviewsByIssueID(issueID int64) ([]*Review, error) { reviews := make([]*Review, 0, 10) sess := db.GetEngine(db.DefaultContext) diff --git a/models/issues/review_test.go b/models/issues/review_test.go index 3221496577..0cb621812c 100644 --- a/models/issues/review_test.go +++ b/models/issues/review_test.go @@ -132,11 +132,22 @@ func TestGetReviewersByIssueID(t *testing.T) { UpdatedUnix: 946684814, }) - allReviews, err := issues_model.GetReviewersByIssueID(issue.ID) - for _, reviewer := range allReviews { - assert.NoError(t, reviewer.LoadReviewer(db.DefaultContext)) + allReviews, err := issues_model.GetReviewsByIssueID(issue.ID) + assert.NoError(t, err) + for _, review := range allReviews { + assert.NoError(t, review.LoadReviewer(db.DefaultContext)) } + if assert.Len(t, allReviews, 3) { + for i, review := range allReviews { + assert.Equal(t, expectedReviews[i].Reviewer, review.Reviewer) + assert.Equal(t, expectedReviews[i].Type, review.Type) + assert.Equal(t, expectedReviews[i].UpdatedUnix, review.UpdatedUnix) + } + } + + allReviews, err = issues_model.GetReviewsByIssueID(issue.ID) assert.NoError(t, err) + assert.NoError(t, issues_model.LoadReviewers(db.DefaultContext, allReviews)) if assert.Len(t, allReviews, 3) { for i, review := range allReviews { assert.Equal(t, expectedReviews[i].Reviewer, review.Reviewer) |