Skip to content

Commit 7a45c5d

Browse files
authored
Bugfix - single reviewer could approve multiple times to overcome minimum (#13)
1 parent 6425a94 commit 7a45c5d

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Code Ownership & Review Assignment Tool - GitHub CODEOWNERS but better
44

55
[![Go Report Card](https://goreportcard.com/badge/github.com/multimediallc/codeowners-plus)](https://goreportcard.com/report/github.com/multimediallc/codeowners-plus?kill_cache=1)
66
[![Tests](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml/badge.svg)](https://github.com/multimediallc/codeowners-plus/actions/workflows/go.yml)
7-
![Coverage](https://img.shields.io/badge/Coverage-59.9%25-yellow)
7+
![Coverage](https://img.shields.io/badge/Coverage-60.1%25-yellow)
88
[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)
99
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
1010

internal/github/gh.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"io"
7+
"maps"
78
"slices"
89
"strings"
910
"time"
@@ -120,12 +121,22 @@ func (gh *Client) InitReviews() error {
120121
allReviews = f.Filtered(allReviews, func(review *github.PullRequestReview) bool {
121122
return review.User.GetLogin() != gh.PR.User.GetLogin()
122123
})
124+
// use descending chronological order (default ascending)
125+
slices.Reverse(allReviews)
123126
gh.reviews = allReviews
124127
return nil
125128
}
126129

127130
func (gh *Client) approvals() []*github.PullRequestReview {
131+
seen := make(map[string]bool, 0)
128132
approvals := f.Filtered(gh.reviews, func(approval *github.PullRequestReview) bool {
133+
userName := approval.GetUser().GetLogin()
134+
if _, ok := seen[userName]; ok {
135+
// we only care about the most recent reviews for each user
136+
return false
137+
} else {
138+
seen[userName] = true
139+
}
129140
return approval.GetState() == "APPROVED"
130141
})
131142
return approvals
@@ -206,15 +217,17 @@ func (gh *Client) GetAlreadyReviewed() ([]string, error) {
206217
}
207218

208219
func reviewerAlreadyReviewed(reviews []*github.PullRequestReview, userReviewerMap ghUserReviewerMap) []string {
209-
reviewsReviewers := make([]string, 0, len(reviews))
220+
reviewsReviewers := make(map[string]bool, len(reviews))
210221
for _, review := range reviews {
211222
reviewingUser := review.GetUser().GetLogin()
212223
if reviewers, ok := userReviewerMap[reviewingUser]; ok {
213-
reviewsReviewers = append(reviewsReviewers, reviewers...)
224+
for _, reviewer := range reviewers {
225+
reviewsReviewers[reviewer] = true
226+
}
214227
}
215228
}
216229

217-
return reviewsReviewers
230+
return slices.Collect(maps.Keys(reviewsReviewers))
218231
}
219232

220233
func (gh *Client) GetCurrentlyRequested() ([]string, error) {

0 commit comments

Comments
 (0)