20
20
//! `assign.owners` config, it will auto-select an assignee based on the files
21
21
//! the PR modifies.
22
22
23
+ use crate :: db:: issue_data:: IssueData ;
23
24
use crate :: db:: review_prefs:: { get_review_prefs_batch, RotationMode } ;
24
25
use crate :: github:: UserId ;
25
26
use crate :: handlers:: pr_tracking:: ReviewerWorkqueue ;
@@ -92,9 +93,23 @@ const REVIEWER_ALREADY_ASSIGNED: &str =
92
93
93
94
Please choose another assignee." ;
94
95
96
+ const REVIEWER_ASSIGNED_BEFORE : & str = "Requested reviewers are assigned before.
97
+
98
+ Please choose another assignee by using `r? @reviewer`." ;
99
+
95
100
// Special account that we use to prevent assignment.
96
101
const GHOST_ACCOUNT : & str = "ghost" ;
97
102
103
+ /// Key for the state in the database
104
+ const PREVIOUS_REVIEWER_KEY : & str = "previous-reviewer" ;
105
+
106
+ /// State stored in the database
107
+ #[ derive( Debug , Clone , PartialEq , Default , serde:: Deserialize , serde:: Serialize ) ]
108
+ struct Reviewers {
109
+ /// List of the last warnings in the most recent comment.
110
+ names : HashSet < String > ,
111
+ }
112
+
98
113
/// Assignment data stored in the issue/PR body.
99
114
#[ derive( Debug , PartialEq , Eq , serde:: Serialize , serde:: Deserialize ) ]
100
115
struct AssignData {
@@ -217,7 +232,7 @@ pub(super) async fn handle_input(
217
232
None
218
233
} ;
219
234
if let Some ( assignee) = assignee {
220
- set_assignee ( & event. issue , & ctx. github , & assignee) . await ;
235
+ set_assignee ( & ctx , & event. issue , & ctx. github , & assignee) . await ? ;
221
236
}
222
237
223
238
if let Some ( welcome) = welcome {
@@ -249,15 +264,19 @@ fn is_self_assign(assignee: &str, pr_author: &str) -> bool {
249
264
}
250
265
251
266
/// Sets the assignee of a PR, alerting any errors.
252
- async fn set_assignee ( issue : & Issue , github : & GithubClient , username : & str ) {
267
+ async fn set_assignee ( ctx : & Context , issue : & Issue , github : & GithubClient , username : & str ) -> anyhow:: Result < ( ) > {
268
+ let mut db = ctx. db . get ( ) . await ;
269
+ let mut state: IssueData < ' _ , Reviewers > =
270
+ IssueData :: load ( & mut db, & issue, PREVIOUS_REVIEWER_KEY ) . await ?;
271
+
253
272
// Don't re-assign if already assigned, e.g. on comment edit
254
273
if issue. contain_assignee ( & username) {
255
274
log:: trace!(
256
275
"ignoring assign PR {} to {}, already assigned" ,
257
276
issue. global_id( ) ,
258
277
username,
259
278
) ;
260
- return ;
279
+ return Ok ( ( ) ) ;
261
280
}
262
281
if let Err ( err) = issue. set_assignee ( github, & username) . await {
263
282
log:: warn!(
@@ -280,8 +299,14 @@ async fn set_assignee(issue: &Issue, github: &GithubClient, username: &str) {
280
299
. await
281
300
{
282
301
log:: warn!( "failed to post error comment: {e}" ) ;
302
+ return Err ( e) ;
283
303
}
284
304
}
305
+
306
+ // Record the reviewer in the database
307
+ state. data . names . insert ( username. to_string ( ) ) ;
308
+ state. save ( ) . await ?;
309
+ Ok ( ( ) )
285
310
}
286
311
287
312
/// Determines who to assign the PR to based on either an `r?` command, or
@@ -300,12 +325,12 @@ async fn determine_assignee(
300
325
config : & AssignConfig ,
301
326
diff : & [ FileDiff ] ,
302
327
) -> anyhow:: Result < ( Option < String > , bool ) > {
303
- let db_client = ctx. db . get ( ) . await ;
328
+ let mut db_client = ctx. db . get ( ) . await ;
304
329
let teams = crate :: team_data:: teams ( & ctx. github ) . await ?;
305
330
if let Some ( name) = assign_command {
306
331
// User included `r?` in the opening PR body.
307
332
match find_reviewer_from_names (
308
- & db_client,
333
+ & mut db_client,
309
334
ctx. workqueue . clone ( ) ,
310
335
& teams,
311
336
config,
@@ -328,7 +353,7 @@ async fn determine_assignee(
328
353
match find_reviewers_from_diff ( config, diff) {
329
354
Ok ( candidates) if !candidates. is_empty ( ) => {
330
355
match find_reviewer_from_names (
331
- & db_client,
356
+ & mut db_client,
332
357
ctx. workqueue . clone ( ) ,
333
358
& teams,
334
359
config,
@@ -347,6 +372,7 @@ async fn determine_assignee(
347
372
e @ FindReviewerError :: NoReviewer { .. }
348
373
| e @ FindReviewerError :: ReviewerIsPrAuthor { .. }
349
374
| e @ FindReviewerError :: ReviewerAlreadyAssigned { .. }
375
+ | e @ FindReviewerError :: ReviewerPreviouslyAssigned { .. }
350
376
| e @ FindReviewerError :: ReviewerOffRotation { .. }
351
377
| e @ FindReviewerError :: DatabaseError ( _)
352
378
| e @ FindReviewerError :: ReviewerAtMaxCapacity { .. } ,
@@ -368,7 +394,7 @@ async fn determine_assignee(
368
394
369
395
if let Some ( fallback) = config. adhoc_groups . get ( "fallback" ) {
370
396
match find_reviewer_from_names (
371
- & db_client,
397
+ & mut db_client,
372
398
ctx. workqueue . clone ( ) ,
373
399
& teams,
374
400
config,
@@ -550,10 +576,9 @@ pub(super) async fn handle_command(
550
576
issue. remove_assignees ( & ctx. github , Selection :: All ) . await ?;
551
577
return Ok ( ( ) ) ;
552
578
}
553
-
554
- let db_client = ctx. db . get ( ) . await ;
579
+ let mut db_client = ctx. db . get ( ) . await ;
555
580
let assignee = match find_reviewer_from_names (
556
- & db_client,
581
+ & mut db_client,
557
582
ctx. workqueue . clone ( ) ,
558
583
& teams,
559
584
config,
@@ -569,7 +594,7 @@ pub(super) async fn handle_command(
569
594
}
570
595
} ;
571
596
572
- set_assignee ( issue, & ctx. github , & assignee) . await ;
597
+ set_assignee ( ctx , issue, & ctx. github , & assignee) . await ? ;
573
598
} else {
574
599
let e = EditIssueBody :: new ( & issue, "ASSIGN" ) ;
575
600
@@ -680,6 +705,8 @@ enum FindReviewerError {
680
705
ReviewerIsPrAuthor { username : String } ,
681
706
/// Requested reviewer is already assigned to that PR
682
707
ReviewerAlreadyAssigned { username : String } ,
708
+ /// Requested reviewer is already assigned previously to that PR.
709
+ ReviewerPreviouslyAssigned { username : String } ,
683
710
/// Data required for assignment could not be loaded from the DB.
684
711
DatabaseError ( String ) ,
685
712
/// The reviewer has too many PRs alreayd assigned.
@@ -726,6 +753,13 @@ impl fmt::Display for FindReviewerError {
726
753
REVIEWER_ALREADY_ASSIGNED . replace( "{username}" , username)
727
754
)
728
755
}
756
+ FindReviewerError :: ReviewerPreviouslyAssigned { username } => {
757
+ write ! (
758
+ f,
759
+ "{}" ,
760
+ REVIEWER_ASSIGNED_BEFORE . replace( "{username}" , username)
761
+ )
762
+ }
729
763
FindReviewerError :: DatabaseError ( error) => {
730
764
write ! ( f, "Database error: {error}" )
731
765
}
@@ -748,7 +782,7 @@ Please select a different reviewer.",
748
782
/// auto-assign groups, or rust-lang team names. It must have at least one
749
783
/// entry.
750
784
async fn find_reviewer_from_names (
751
- db : & DbClient ,
785
+ db : & mut DbClient ,
752
786
workqueue : Arc < RwLock < ReviewerWorkqueue > > ,
753
787
teams : & Teams ,
754
788
config : & AssignConfig ,
@@ -916,7 +950,7 @@ fn expand_teams_and_groups(
916
950
/// Returns a list of candidate usernames (from relevant teams) to choose as a reviewer.
917
951
/// If no reviewer is available, returns an error.
918
952
async fn candidate_reviewers_from_names < ' a > (
919
- db : & DbClient ,
953
+ db : & mut DbClient ,
920
954
workqueue : Arc < RwLock < ReviewerWorkqueue > > ,
921
955
teams : & ' a Teams ,
922
956
config : & ' a AssignConfig ,
@@ -925,6 +959,7 @@ async fn candidate_reviewers_from_names<'a>(
925
959
) -> Result < HashSet < String > , FindReviewerError > {
926
960
// Step 1: expand teams and groups into candidate names
927
961
let expanded = expand_teams_and_groups ( teams, issue, config, names) ?;
962
+ let expansion_happend = expanded. iter ( ) . any ( |c| c. origin == ReviewerCandidateOrigin :: Expanded ) ;
928
963
let expanded_count = expanded. len ( ) ;
929
964
930
965
// Was it a request for a single user, i.e. `r? @username`?
@@ -937,6 +972,7 @@ async fn candidate_reviewers_from_names<'a>(
937
972
// Set of candidate usernames to choose from.
938
973
// We go through each expanded candidate and store either success or an error for them.
939
974
let mut candidates: Vec < Result < String , FindReviewerError > > = Vec :: new ( ) ;
975
+ let previous_reviewer_names = get_previous_reviewer_names ( db, issue) . await ;
940
976
941
977
// Step 2: pre-filter candidates based on checks that we can perform quickly
942
978
for reviewer_candidate in expanded {
@@ -949,6 +985,8 @@ async fn candidate_reviewers_from_names<'a>(
949
985
. iter ( )
950
986
. any ( |assignee| name_lower == assignee. login . to_lowercase ( ) ) ;
951
987
988
+ let is_previously_assigned = previous_reviewer_names. contains ( & reviewer_candidate. name ) ;
989
+
952
990
// Record the reason why the candidate was filtered out
953
991
let reason = {
954
992
if is_pr_author {
@@ -963,6 +1001,12 @@ async fn candidate_reviewers_from_names<'a>(
963
1001
Some ( FindReviewerError :: ReviewerAlreadyAssigned {
964
1002
username : candidate. clone ( ) ,
965
1003
} )
1004
+ } else if expansion_happend && is_previously_assigned {
1005
+ // **Only** when r? group is expanded, we consider the reviewer previously assigned
1006
+ // `r? @reviewer` will not consider the reviewer previously assigned
1007
+ Some ( FindReviewerError :: ReviewerPreviouslyAssigned {
1008
+ username : candidate. clone ( ) ,
1009
+ } )
966
1010
} else {
967
1011
None
968
1012
}
@@ -1058,3 +1102,13 @@ async fn candidate_reviewers_from_names<'a>(
1058
1102
. collect ( ) )
1059
1103
}
1060
1104
}
1105
+
1106
+ async fn get_previous_reviewer_names ( db : & mut DbClient , issue : & Issue ) -> HashSet < String > {
1107
+ let state: IssueData < ' _ , Reviewers > =
1108
+ match IssueData :: load ( db, & issue, PREVIOUS_REVIEWER_KEY ) . await {
1109
+ Ok ( state) => state,
1110
+ Err ( _) => return HashSet :: new ( ) ,
1111
+ } ;
1112
+
1113
+ state. data . names
1114
+ }
0 commit comments