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 reviewer @{username} was already 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_REVIEWERS_KEY : & str = "previous-reviewers" ;
105
+
106
+ /// State stored in the database
107
+ #[ derive( Debug , Clone , PartialEq , Default , serde:: Deserialize , serde:: Serialize ) ]
108
+ struct Reviewers {
109
+ // names are stored in lowercase
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,24 @@ 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 (
268
+ ctx : & Context ,
269
+ issue : & Issue ,
270
+ github : & GithubClient ,
271
+ username : & str ,
272
+ ) -> anyhow:: Result < ( ) > {
273
+ let mut db = ctx. db . get ( ) . await ;
274
+ let mut state: IssueData < ' _ , Reviewers > =
275
+ IssueData :: load ( & mut db, & issue, PREVIOUS_REVIEWERS_KEY ) . await ?;
276
+
253
277
// Don't re-assign if already assigned, e.g. on comment edit
254
278
if issue. contain_assignee ( & username) {
255
279
log:: trace!(
256
280
"ignoring assign PR {} to {}, already assigned" ,
257
281
issue. global_id( ) ,
258
282
username,
259
283
) ;
260
- return ;
284
+ return Ok ( ( ) ) ;
261
285
}
262
286
if let Err ( err) = issue. set_assignee ( github, & username) . await {
263
287
log:: warn!(
@@ -280,8 +304,15 @@ async fn set_assignee(issue: &Issue, github: &GithubClient, username: &str) {
280
304
. await
281
305
{
282
306
log:: warn!( "failed to post error comment: {e}" ) ;
307
+ return Err ( e) ;
283
308
}
284
309
}
310
+
311
+ // Record the reviewer in the database
312
+
313
+ state. data . names . insert ( username. to_lowercase ( ) ) ;
314
+ state. save ( ) . await ?;
315
+ Ok ( ( ) )
285
316
}
286
317
287
318
/// Determines who to assign the PR to based on either an `r?` command, or
@@ -300,12 +331,12 @@ async fn determine_assignee(
300
331
config : & AssignConfig ,
301
332
diff : & [ FileDiff ] ,
302
333
) -> anyhow:: Result < ( Option < String > , bool ) > {
303
- let db_client = ctx. db . get ( ) . await ;
334
+ let mut db_client = ctx. db . get ( ) . await ;
304
335
let teams = crate :: team_data:: teams ( & ctx. github ) . await ?;
305
336
if let Some ( name) = assign_command {
306
337
// User included `r?` in the opening PR body.
307
338
match find_reviewer_from_names (
308
- & db_client,
339
+ & mut db_client,
309
340
ctx. workqueue . clone ( ) ,
310
341
& teams,
311
342
config,
@@ -328,7 +359,7 @@ async fn determine_assignee(
328
359
match find_reviewers_from_diff ( config, diff) {
329
360
Ok ( candidates) if !candidates. is_empty ( ) => {
330
361
match find_reviewer_from_names (
331
- & db_client,
362
+ & mut db_client,
332
363
ctx. workqueue . clone ( ) ,
333
364
& teams,
334
365
config,
@@ -347,6 +378,7 @@ async fn determine_assignee(
347
378
e @ FindReviewerError :: NoReviewer { .. }
348
379
| e @ FindReviewerError :: ReviewerIsPrAuthor { .. }
349
380
| e @ FindReviewerError :: ReviewerAlreadyAssigned { .. }
381
+ | e @ FindReviewerError :: ReviewerPreviouslyAssigned { .. }
350
382
| e @ FindReviewerError :: ReviewerOffRotation { .. }
351
383
| e @ FindReviewerError :: DatabaseError ( _)
352
384
| e @ FindReviewerError :: ReviewerAtMaxCapacity { .. } ,
@@ -368,7 +400,7 @@ async fn determine_assignee(
368
400
369
401
if let Some ( fallback) = config. adhoc_groups . get ( "fallback" ) {
370
402
match find_reviewer_from_names (
371
- & db_client,
403
+ & mut db_client,
372
404
ctx. workqueue . clone ( ) ,
373
405
& teams,
374
406
config,
@@ -550,10 +582,9 @@ pub(super) async fn handle_command(
550
582
issue. remove_assignees ( & ctx. github , Selection :: All ) . await ?;
551
583
return Ok ( ( ) ) ;
552
584
}
553
-
554
- let db_client = ctx. db . get ( ) . await ;
585
+ let mut db_client = ctx. db . get ( ) . await ;
555
586
let assignee = match find_reviewer_from_names (
556
- & db_client,
587
+ & mut db_client,
557
588
ctx. workqueue . clone ( ) ,
558
589
& teams,
559
590
config,
@@ -569,7 +600,7 @@ pub(super) async fn handle_command(
569
600
}
570
601
} ;
571
602
572
- set_assignee ( issue, & ctx. github , & assignee) . await ;
603
+ set_assignee ( ctx , issue, & ctx. github , & assignee) . await ? ;
573
604
} else {
574
605
let e = EditIssueBody :: new ( & issue, "ASSIGN" ) ;
575
606
@@ -680,6 +711,8 @@ enum FindReviewerError {
680
711
ReviewerIsPrAuthor { username : String } ,
681
712
/// Requested reviewer is already assigned to that PR
682
713
ReviewerAlreadyAssigned { username : String } ,
714
+ /// Requested reviewer was already assigned previously to that PR.
715
+ ReviewerPreviouslyAssigned { username : String } ,
683
716
/// Data required for assignment could not be loaded from the DB.
684
717
DatabaseError ( String ) ,
685
718
/// The reviewer has too many PRs alreayd assigned.
@@ -726,6 +759,13 @@ impl fmt::Display for FindReviewerError {
726
759
REVIEWER_ALREADY_ASSIGNED . replace( "{username}" , username)
727
760
)
728
761
}
762
+ FindReviewerError :: ReviewerPreviouslyAssigned { username } => {
763
+ write ! (
764
+ f,
765
+ "{}" ,
766
+ REVIEWER_ASSIGNED_BEFORE . replace( "{username}" , username)
767
+ )
768
+ }
729
769
FindReviewerError :: DatabaseError ( error) => {
730
770
write ! ( f, "Database error: {error}" )
731
771
}
@@ -748,7 +788,7 @@ Please select a different reviewer.",
748
788
/// auto-assign groups, or rust-lang team names. It must have at least one
749
789
/// entry.
750
790
async fn find_reviewer_from_names (
751
- db : & DbClient ,
791
+ db : & mut DbClient ,
752
792
workqueue : Arc < RwLock < ReviewerWorkqueue > > ,
753
793
teams : & Teams ,
754
794
config : & AssignConfig ,
@@ -916,7 +956,7 @@ fn expand_teams_and_groups(
916
956
/// Returns a list of candidate usernames (from relevant teams) to choose as a reviewer.
917
957
/// If no reviewer is available, returns an error.
918
958
async fn candidate_reviewers_from_names < ' a > (
919
- db : & DbClient ,
959
+ db : & mut DbClient ,
920
960
workqueue : Arc < RwLock < ReviewerWorkqueue > > ,
921
961
teams : & ' a Teams ,
922
962
config : & ' a AssignConfig ,
@@ -937,6 +977,7 @@ async fn candidate_reviewers_from_names<'a>(
937
977
// Set of candidate usernames to choose from.
938
978
// We go through each expanded candidate and store either success or an error for them.
939
979
let mut candidates: Vec < Result < String , FindReviewerError > > = Vec :: new ( ) ;
980
+ let previous_reviewer_names = get_previous_reviewer_names ( db, issue) . await ;
940
981
941
982
// Step 2: pre-filter candidates based on checks that we can perform quickly
942
983
for reviewer_candidate in expanded {
@@ -949,6 +990,8 @@ async fn candidate_reviewers_from_names<'a>(
949
990
. iter ( )
950
991
. any ( |assignee| name_lower == assignee. login . to_lowercase ( ) ) ;
951
992
993
+ let is_previously_assigned = previous_reviewer_names. contains ( & name_lower) ;
994
+
952
995
// Record the reason why the candidate was filtered out
953
996
let reason = {
954
997
if is_pr_author {
@@ -963,6 +1006,14 @@ async fn candidate_reviewers_from_names<'a>(
963
1006
Some ( FindReviewerError :: ReviewerAlreadyAssigned {
964
1007
username : candidate. clone ( ) ,
965
1008
} )
1009
+ } else if reviewer_candidate. origin == ReviewerCandidateOrigin :: Expanded
1010
+ && is_previously_assigned
1011
+ {
1012
+ // **Only** when r? group is expanded, we consider the reviewer previously assigned
1013
+ // `r? @reviewer` will not consider the reviewer previously assigned
1014
+ Some ( FindReviewerError :: ReviewerPreviouslyAssigned {
1015
+ username : candidate. clone ( ) ,
1016
+ } )
966
1017
} else {
967
1018
None
968
1019
}
@@ -1058,3 +1109,13 @@ async fn candidate_reviewers_from_names<'a>(
1058
1109
. collect ( ) )
1059
1110
}
1060
1111
}
1112
+
1113
+ async fn get_previous_reviewer_names ( db : & mut DbClient , issue : & Issue ) -> HashSet < String > {
1114
+ let state: IssueData < ' _ , Reviewers > =
1115
+ match IssueData :: load ( db, & issue, PREVIOUS_REVIEWERS_KEY ) . await {
1116
+ Ok ( state) => state,
1117
+ Err ( _) => return HashSet :: new ( ) ,
1118
+ } ;
1119
+
1120
+ state. data . names
1121
+ }
0 commit comments