Handle Team Reviews (#515)

at the moment we crash with an nil exeption if there exist team reviews

this fix it and add support to display them

Reviewed-on: https://gitea.com/gitea/tea/pulls/515
Reviewed-by: Norwin <noerw@noreply.gitea.io>
Reviewed-by: wxiaoguang <wxiaoguang@noreply.gitea.io>
This commit is contained in:
6543 2022-09-29 10:49:24 +08:00
parent a37377d181
commit c1c7870ceb
1 changed files with 26 additions and 11 deletions

View File

@ -41,7 +41,7 @@ func PullDetails(pr *gitea.PullRequest, reviews []*gitea.PullReview, ciStatus *g
out += "---\n" out += "---\n"
} }
out += formatReviews(reviews) out += formatReviews(pr, reviews)
if ciStatus != nil { if ciStatus != nil {
var summary, errors string var summary, errors string
@ -90,35 +90,50 @@ func formatPRState(pr *gitea.PullRequest) string {
return string(pr.State) return string(pr.State)
} }
func formatReviews(reviews []*gitea.PullReview) string { func formatReviews(pr *gitea.PullRequest, reviews []*gitea.PullReview) string {
result := "" result := ""
if len(reviews) == 0 { if len(reviews) == 0 {
return result return result
} }
// deduplicate reviews by user (via review time & userID), // deduplicate reviews by user (via review time & userID),
reviewByUser := make(map[int64]*gitea.PullReview) reviewByUserOrTeam := make(map[string]*gitea.PullReview)
for _, review := range reviews { for _, review := range reviews {
switch review.State { switch review.State {
case gitea.ReviewStateApproved, case gitea.ReviewStateApproved,
gitea.ReviewStateRequestChanges, gitea.ReviewStateRequestChanges,
gitea.ReviewStateRequestReview: gitea.ReviewStateRequestReview:
if r, ok := reviewByUser[review.Reviewer.ID]; !ok || review.Submitted.After(r.Submitted) { if review.Reviewer != nil {
reviewByUser[review.Reviewer.ID] = review if r, ok := reviewByUserOrTeam[fmt.Sprintf("user_%d", review.Reviewer.ID)]; !ok || review.Submitted.After(r.Submitted) {
reviewByUserOrTeam[fmt.Sprintf("user_%d", review.Reviewer.ID)] = review
}
} else if review.ReviewerTeam != nil {
if r, ok := reviewByUserOrTeam[fmt.Sprintf("team_%d", review.ReviewerTeam.ID)]; !ok || review.Submitted.After(r.Submitted) {
reviewByUserOrTeam[fmt.Sprintf("team_%d", review.ReviewerTeam.ID)] = review
}
} }
} }
} }
// group reviews by type // group reviews by type
usersByState := make(map[gitea.ReviewStateType][]string) reviewByState := make(map[gitea.ReviewStateType][]string)
for _, r := range reviewByUser { for _, r := range reviewByUserOrTeam {
u := r.Reviewer.UserName if r.Reviewer != nil {
users := usersByState[r.State] reviewByState[r.State] = append(reviewByState[r.State],
usersByState[r.State] = append(users, u) r.Reviewer.UserName,
)
} else if r.ReviewerTeam != nil {
// only pulls to orgs can have team reviews
org := pr.Base.Repository.Owner
reviewByState[r.State] = append(reviewByState[r.State],
fmt.Sprintf("%s/%s", org.UserName, r.ReviewerTeam.Name),
)
}
} }
// stringify // stringify
for state, user := range usersByState { for state, user := range reviewByState {
result += fmt.Sprintf("- %s by @%s\n", state, strings.Join(user, ", @")) result += fmt.Sprintf("- %s by @%s\n", state, strings.Join(user, ", @"))
} }
return result return result