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 += 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