Skip to content

Commit 94cca88

Browse files
authored
Fix null requested_reviewer from API (#31773)
If the assign the pull request review to a team, it did not show the members of the team in the "requested_reviewers" field, so the field was null. As a solution, I added the team members to the array. fix #31764
1 parent c649a04 commit 94cca88

File tree

5 files changed

+90
-16
lines changed

5 files changed

+90
-16
lines changed

models/issues/pull.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type PullRequest struct {
165165
Issue *Issue `xorm:"-"`
166166
Index int64
167167
RequestedReviewers []*user_model.User `xorm:"-"`
168+
RequestedReviewersTeams []*org_model.Team `xorm:"-"`
168169
isRequestedReviewersLoaded bool `xorm:"-"`
169170

170171
HeadRepoID int64 `xorm:"INDEX"`
@@ -305,7 +306,28 @@ func (pr *PullRequest) LoadRequestedReviewers(ctx context.Context) error {
305306
}
306307
pr.isRequestedReviewersLoaded = true
307308
for _, review := range reviews {
308-
pr.RequestedReviewers = append(pr.RequestedReviewers, review.Reviewer)
309+
if review.ReviewerID != 0 {
310+
pr.RequestedReviewers = append(pr.RequestedReviewers, review.Reviewer)
311+
}
312+
}
313+
314+
return nil
315+
}
316+
317+
// LoadRequestedReviewersTeams loads the requested reviewers teams.
318+
func (pr *PullRequest) LoadRequestedReviewersTeams(ctx context.Context) error {
319+
reviews, err := GetReviewsByIssueID(ctx, pr.Issue.ID)
320+
if err != nil {
321+
return err
322+
}
323+
if err = reviews.LoadReviewersTeams(ctx); err != nil {
324+
return err
325+
}
326+
327+
for _, review := range reviews {
328+
if review.ReviewerTeamID != 0 {
329+
pr.RequestedReviewersTeams = append(pr.RequestedReviewersTeams, review.ReviewerTeam)
330+
}
309331
}
310332

311333
return nil

models/issues/review_list.go

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88

99
"code.gitea.io/gitea/models/db"
10+
organization_model "code.gitea.io/gitea/models/organization"
1011
user_model "code.gitea.io/gitea/models/user"
1112
"code.gitea.io/gitea/modules/container"
1213
"code.gitea.io/gitea/modules/optional"
@@ -37,6 +38,34 @@ func (reviews ReviewList) LoadReviewers(ctx context.Context) error {
3738
return nil
3839
}
3940

41+
// LoadReviewersTeams loads reviewers teams
42+
func (reviews ReviewList) LoadReviewersTeams(ctx context.Context) error {
43+
reviewersTeamsIDs := make([]int64, 0)
44+
for _, review := range reviews {
45+
if review.ReviewerTeamID != 0 {
46+
reviewersTeamsIDs = append(reviewersTeamsIDs, review.ReviewerTeamID)
47+
}
48+
}
49+
50+
teamsMap := make(map[int64]*organization_model.Team, 0)
51+
for _, teamID := range reviewersTeamsIDs {
52+
team, err := organization_model.GetTeamByID(ctx, teamID)
53+
if err != nil {
54+
return err
55+
}
56+
57+
teamsMap[teamID] = team
58+
}
59+
60+
for _, review := range reviews {
61+
if review.ReviewerTeamID != 0 {
62+
review.ReviewerTeam = teamsMap[review.ReviewerTeamID]
63+
}
64+
}
65+
66+
return nil
67+
}
68+
4069
func (reviews ReviewList) LoadIssues(ctx context.Context) error {
4170
issueIDs := container.FilterSlice(reviews, func(review *Review) (int64, bool) {
4271
return review.IssueID, true

modules/structs/pull.go

+16-15
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import (
99

1010
// PullRequest represents a pull request
1111
type PullRequest struct {
12-
ID int64 `json:"id"`
13-
URL string `json:"url"`
14-
Index int64 `json:"number"`
15-
Poster *User `json:"user"`
16-
Title string `json:"title"`
17-
Body string `json:"body"`
18-
Labels []*Label `json:"labels"`
19-
Milestone *Milestone `json:"milestone"`
20-
Assignee *User `json:"assignee"`
21-
Assignees []*User `json:"assignees"`
22-
RequestedReviewers []*User `json:"requested_reviewers"`
23-
State StateType `json:"state"`
24-
Draft bool `json:"draft"`
25-
IsLocked bool `json:"is_locked"`
26-
Comments int `json:"comments"`
12+
ID int64 `json:"id"`
13+
URL string `json:"url"`
14+
Index int64 `json:"number"`
15+
Poster *User `json:"user"`
16+
Title string `json:"title"`
17+
Body string `json:"body"`
18+
Labels []*Label `json:"labels"`
19+
Milestone *Milestone `json:"milestone"`
20+
Assignee *User `json:"assignee"`
21+
Assignees []*User `json:"assignees"`
22+
RequestedReviewers []*User `json:"requested_reviewers"`
23+
RequestedReviewersTeams []*Team `json:"requested_reviewers_teams"`
24+
State StateType `json:"state"`
25+
Draft bool `json:"draft"`
26+
IsLocked bool `json:"is_locked"`
27+
Comments int `json:"comments"`
2728
// number of review comments made on the diff of a PR review (not including comments on commits or issues in a PR)
2829
ReviewComments int `json:"review_comments"`
2930
Additions int `json:"additions"`

services/convert/pull.go

+15
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,25 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
106106
log.Error("LoadRequestedReviewers[%d]: %v", pr.ID, err)
107107
return nil
108108
}
109+
if err = pr.LoadRequestedReviewersTeams(ctx); err != nil {
110+
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
111+
return nil
112+
}
113+
109114
for _, reviewer := range pr.RequestedReviewers {
110115
apiPullRequest.RequestedReviewers = append(apiPullRequest.RequestedReviewers, ToUser(ctx, reviewer, nil))
111116
}
112117

118+
for _, reviewerTeam := range pr.RequestedReviewersTeams {
119+
convertedTeam, err := ToTeam(ctx, reviewerTeam, true)
120+
if err != nil {
121+
log.Error("LoadRequestedReviewersTeams[%d]: %v", pr.ID, err)
122+
return nil
123+
}
124+
125+
apiPullRequest.RequestedReviewersTeams = append(apiPullRequest.RequestedReviewersTeams, convertedTeam)
126+
}
127+
113128
if pr.Issue.ClosedUnix != 0 {
114129
apiPullRequest.Closed = pr.Issue.ClosedUnix.AsTimePtr()
115130
}

templates/swagger/v1_json.tmpl

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)