From c1c7870ceb159974271f9954287bf54400b92f7c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 29 Sep 2022 10:49:24 +0800 Subject: [PATCH] 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 Reviewed-by: wxiaoguang --- modules/print/pull.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/modules/print/pull.go b/modules/print/pull.go index 0ba3bc3..f5462d7 100644 --- a/modules/print/pull.go +++ b/modules/print/pull.go @@ -41,7 +41,7 @@ func PullDetails(pr *gitea.PullRequest, reviews []*gitea.PullReview, ciStatus *g out += "---\n" } - out += formatReviews(reviews) + out += formatReviews(pr, reviews) if ciStatus != nil { var summary, errors string @@ -90,35 +90,50 @@ func formatPRState(pr *gitea.PullRequest) string { return string(pr.State) } -func formatReviews(reviews []*gitea.PullReview) string { +func formatReviews(pr *gitea.PullRequest, reviews []*gitea.PullReview) string { result := "" if len(reviews) == 0 { return result } // deduplicate reviews by user (via review time & userID), - reviewByUser := make(map[int64]*gitea.PullReview) + reviewByUserOrTeam := make(map[string]*gitea.PullReview) for _, review := range reviews { switch review.State { case gitea.ReviewStateApproved, gitea.ReviewStateRequestChanges, gitea.ReviewStateRequestReview: - if r, ok := reviewByUser[review.Reviewer.ID]; !ok || review.Submitted.After(r.Submitted) { - reviewByUser[review.Reviewer.ID] = review + if review.Reviewer != nil { + 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 - usersByState := make(map[gitea.ReviewStateType][]string) - for _, r := range reviewByUser { - u := r.Reviewer.UserName - users := usersByState[r.State] - usersByState[r.State] = append(users, u) + reviewByState := make(map[gitea.ReviewStateType][]string) + for _, r := range reviewByUserOrTeam { + if r.Reviewer != nil { + reviewByState[r.State] = append(reviewByState[r.State], + 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 - for state, user := range usersByState { + for state, user := range reviewByState { result += fmt.Sprintf("- %s by @%s\n", state, strings.Join(user, ", @")) } return result