-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathpr_tracking.rs
More file actions
688 lines (613 loc) · 21 KB
/
Copy pathpr_tracking.rs
File metadata and controls
688 lines (613 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
//! This module updates the PR workqueue of the Rust project contributors
//! Runs after a PR has been assigned or unassigned
//!
//! Purpose:
//!
//! - Adds the PR to the workqueue of one team member (after the PR has been assigned or reopened)
//! - Removes the PR from the workqueue of one team member (after the PR has been unassigned or closed)
use crate::github::{GitHubUser, GitHubUserType, UserId};
use crate::github::{Label, PullRequestNumber};
use crate::{
config::ReviewPrefsConfig,
github::{IssuesAction, IssuesEvent},
handlers::Context,
};
use futures::TryStreamExt;
use octocrab::Octocrab;
use octocrab::models::IssueState;
use octocrab::params::pulls::Sort;
use octocrab::params::{Direction, State};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::{RwLock, RwLockWriteGuard};
use tracing as log;
/// Repositories for which we track the reviewer workqueue.
pub fn get_review_tracked_repositories() -> Vec<TrackedRepository> {
vec![
TrackedRepository::new("rust-lang", "rust"),
TrackedRepository::new("rust-lang", "cargo"),
TrackedRepository::new("rust-lang", "rust-clippy"),
]
}
#[derive(Clone, Debug)]
pub struct AssignedPullRequest {
pub title: String,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct TrackedRepository {
owner: String,
name: String,
}
impl TrackedRepository {
pub fn new(owner: &str, name: &str) -> Self {
Self {
owner: owner.to_owned(),
name: name.to_owned(),
}
}
fn from_full_name(name: &str) -> Option<Self> {
let (owner, name) = name.split_once('/')?;
Some(Self::new(owner, name))
}
pub fn owner(&self) -> &str {
&self.owner
}
pub fn name(&self) -> &str {
&self.name
}
pub fn full_name(&self) -> String {
format!("{}/{}", self.owner, self.name)
}
}
/// Maps users to a set of currently assigned open non-draft pull requests in a single repository.
/// We store this map in memory, rather than in the DB, because it can get desynced when webhooks
/// are missed.
/// It is thus reloaded when triagebot starts and also periodically, so it is not needed to store it
/// in the DB.
#[derive(Debug, Default)]
pub struct ReviewerWorkqueue {
reviewers: HashMap<UserId, HashMap<PullRequestNumber, AssignedPullRequest>>,
}
impl ReviewerWorkqueue {
pub fn new(
reviewers: HashMap<UserId, HashMap<PullRequestNumber, AssignedPullRequest>>,
) -> Self {
Self { reviewers }
}
pub fn assigned_pr_count(&self, user_id: UserId) -> u64 {
self.reviewers
.get(&user_id)
.map(|prs| prs.len() as u64)
.unwrap_or(0)
}
}
/// Stores per-repository reviewer workqueues.
/// Each workqueue is behind its own `Arc<RwLock<...>>` so repos can be locked independently.
pub struct RepositoryWorkqueueMap {
repos: HashMap<TrackedRepository, Arc<RwLock<ReviewerWorkqueue>>>,
}
impl RepositoryWorkqueueMap {
pub fn new(repos: HashMap<TrackedRepository, Arc<RwLock<ReviewerWorkqueue>>>) -> Self {
Self { repos }
}
pub fn get(&self, full_name: &str) -> Option<Arc<RwLock<ReviewerWorkqueue>>> {
let repo = TrackedRepository::from_full_name(full_name)?;
self.repos.get(&repo).cloned()
}
/// Returns an iterator over all repositories that are being tracked.
pub fn tracked_repositories(
&self,
) -> impl Iterator<Item = (&TrackedRepository, &Arc<RwLock<ReviewerWorkqueue>>)> {
self.repos.iter()
}
}
pub(super) enum ReviewPrefsInput {
Assigned { assignee: GitHubUser },
Unassigned { assignee: GitHubUser },
OtherChange,
}
pub(super) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
config: Option<&ReviewPrefsConfig>,
) -> Result<Option<ReviewPrefsInput>, String> {
// NOTE: this config check MUST exist. Else, the triagebot will emit an error
// about this feature not being enabled
if config.is_none() {
return Ok(None);
}
// Execute this handler only if this is a PR ...
if !event.issue.is_pr() {
return Ok(None);
}
// ... and if the action is an assignment or unassignment with an assignee
match &event.action {
IssuesAction::Assigned { assignee } => Ok(Some(ReviewPrefsInput::Assigned {
assignee: assignee.clone(),
})),
IssuesAction::Unassigned { assignee } => Ok(Some(ReviewPrefsInput::Unassigned {
assignee: assignee.clone(),
})),
// We don't need to handle Opened explicitly, because that will trigger the Assigned event
IssuesAction::Reopened
| IssuesAction::ReadyForReview
| IssuesAction::ConvertedToDraft
| IssuesAction::Closed
| IssuesAction::Deleted
| IssuesAction::Transferred
| IssuesAction::Labeled { .. }
| IssuesAction::Unlabeled { .. } => Ok(Some(ReviewPrefsInput::OtherChange)),
_ => Ok(None),
}
}
pub(super) async fn handle_input(
ctx: &Context,
_config: &ReviewPrefsConfig,
event: &IssuesEvent,
input: ReviewPrefsInput,
) -> anyhow::Result<()> {
log::info!("Handling event action {:?} in PR tracking", event.action);
let pr = &event.issue;
let pr_number = event.issue.number;
let repo_name = &event.repository.full_name;
let Some(workqueue_arc) = ctx.workqueue_map.get(repo_name) else {
log::debug!("Repository {repo_name} does not have a tracked workqueue, skipping");
return Ok(());
};
let mut workqueue = workqueue_arc.write().await;
// If the PR doesn't wait for a review, remove it from the workqueue completely.
// This handles situations such as labels being modified, which make the PR no longer to be
// in the "waiting for a review" state, or the PR being closed/merged.
if !waits_for_a_review(&pr.labels, &pr.assignees, &pr.user, pr.is_open(), pr.draft) {
log::info!(
"Removing PR {pr_number} from workqueue, because it is not waiting for a review.",
);
delete_pr_from_all_queues(&mut workqueue, pr_number);
return Ok(());
}
let assigned_pr = AssignedPullRequest {
title: pr.title.clone(),
};
match input {
// The PR was assigned to a specific user, and it is waiting for a review.
ReviewPrefsInput::Assigned { assignee } => {
log::info!(
"Adding PR {pr_number} to workqueue of {} because they were assigned.",
assignee.login
);
upsert_pr_into_user_queue(&mut workqueue, assignee.id, pr_number, assigned_pr);
}
ReviewPrefsInput::Unassigned { assignee } => {
log::info!(
"Removing PR {pr_number} from workqueue of {} because they were unassigned.",
assignee.login
);
delete_pr_from_user_queue(&mut workqueue, assignee.id, pr_number);
}
// Some other change has happened (e.g. labels changed or the PR being reopened).
// Make sure that all assigned users have the PR in their queue.
// When a PR is opened, it might not yet contain all the information needed to determine
// whether it waits for a reviewer or not. For example, when you open a PR,
// triagebot might apply the "S-waiting-on-review" (or similar) label to it, which we
// currently use to determine whether a PR is truly assigned to someone or not.
// We thus need to refresh the queue state after every relevant state change that we
// receive.
ReviewPrefsInput::OtherChange => {
for assignee in &event.issue.assignees {
if upsert_pr_into_user_queue(
&mut workqueue,
assignee.id,
pr_number,
assigned_pr.clone(),
) {
log::info!("Adding PR {pr_number} to workqueue of {}.", assignee.login);
}
}
}
}
Ok(())
}
/// Loads the workqueue (mapping of open PRs assigned to users) from GitHub
pub async fn load_workqueue(
client: &Octocrab,
repo: &TrackedRepository,
) -> anyhow::Result<ReviewerWorkqueue> {
tracing::debug!("Loading workqueue for {}/{}", repo.owner, repo.name);
let prs = retrieve_pull_request_assignments(&repo.owner, &repo.name, client).await?;
// Aggregate PRs by user
let aggregated: HashMap<UserId, HashMap<PullRequestNumber, AssignedPullRequest>> = prs
.into_iter()
.fold(HashMap::new(), |mut acc, (user, pr_number, pr)| {
let prs = acc.entry(user.id).or_default();
prs.insert(pr_number, pr);
acc
});
tracing::debug!("PR assignments for `{}`:\n{aggregated:?}", repo.full_name());
Ok(ReviewerWorkqueue::new(aggregated))
}
/// Retrieve tuples of (user, PR number) where
/// the given user is assigned as a reviewer for that PR
/// and the PR is considered to be "waiting for a review", according to the semantics
/// of the reviewer workqueue.
/// See the [`waits_for_a_review`] function.
pub async fn retrieve_pull_request_assignments(
owner: &str,
repository: &str,
client: &Octocrab,
) -> anyhow::Result<Vec<(GitHubUser, PullRequestNumber, AssignedPullRequest)>> {
let mut assignments = vec![];
// We use the REST API to fetch open pull requests, as it is much (~5-10x)
// faster than using GraphQL here.
let stream = client
.pulls(owner, repository)
.list()
.state(State::Open)
.direction(Direction::Ascending)
.sort(Sort::Created)
.per_page(100)
.send()
.await?
.into_stream(client);
let mut stream = std::pin::pin!(stream);
while let Some(pr) = stream.try_next().await? {
let labels = pr
.labels
.unwrap_or_default()
.into_iter()
.map(|l| Label { name: l.name })
.collect::<Vec<Label>>();
let assignees = pr
.assignees
.as_ref()
.map(|authors| authors.iter().map(GitHubUser::from).collect::<Vec<_>>())
.unwrap_or_default();
let author = pr
.user
.as_ref()
.map(|author| GitHubUser::from(author.as_ref()))
.unwrap_or_else(|| GitHubUser {
login: "ghost".to_string(),
id: 0,
r#type: GitHubUserType::Bot,
});
if waits_for_a_review(
&labels,
&assignees,
&author,
pr.state == Some(IssueState::Open),
pr.draft.unwrap_or_default(),
) {
for user in pr.assignees.unwrap_or_default() {
assignments.push((
Into::<GitHubUser>::into(&user),
pr.number,
AssignedPullRequest {
title: pr.title.clone().unwrap_or_default(),
},
));
}
}
}
assignments.sort_by(|a, b| a.0.id.cmp(&b.0.id));
Ok(assignments)
}
/// Get pull request assignments for a team member in a specific repo.
pub async fn get_assigned_prs(
ctx: &Context,
repo: &str,
user_id: UserId,
) -> HashMap<PullRequestNumber, AssignedPullRequest> {
let Some(workqueue) = ctx.workqueue_map.get(repo) else {
return Default::default();
};
workqueue
.read()
.await
.reviewers
.get(&user_id)
.cloned()
.unwrap_or_default()
}
/// Add a PR to the workqueue of a team member.
/// Updates data of the pull request if it already was in the workqueue.
/// Ensures no accidental PR duplicates.
///
/// Returns true if the PR was actually inserted.
fn upsert_pr_into_user_queue(
workqueue: &mut RwLockWriteGuard<ReviewerWorkqueue>,
user_id: UserId,
pr: PullRequestNumber,
assigned_pr: AssignedPullRequest,
) -> bool {
workqueue
.reviewers
.entry(user_id)
.or_default()
.insert(pr, assigned_pr)
.is_none()
}
/// Delete a PR from the workqueue of a team member.
fn delete_pr_from_user_queue(
workqueue: &mut ReviewerWorkqueue,
user_id: UserId,
pr: PullRequestNumber,
) {
if let Some(queue) = workqueue.reviewers.get_mut(&user_id) {
queue.remove(&pr);
}
}
/// Delete a PR from the workqueue completely.
fn delete_pr_from_all_queues(workqueue: &mut ReviewerWorkqueue, pr: PullRequestNumber) {
for queue in workqueue.reviewers.values_mut() {
queue.retain(|pr_number, _| *pr_number != pr);
}
}
/// Returns true if the workqueue should assume that this PR is actually waiting for a reviewer.
/// The function receives atomic attributes so that it is compatible both with triagebot's
/// `Issue` struct (used for incremental updates) and octocrab's `PullRequest` struct (used for
/// batch PR loads).
///
/// Note: this functionality is currently hardcoded for rust-lang/rust, other repos might use
/// different labels.
fn waits_for_a_review(
labels: &[Label],
assignees: &[GitHubUser],
author: &GitHubUser,
is_open: bool,
is_draft: bool,
) -> bool {
let is_blocked = labels
.iter()
.any(|l| l.name == "S-blocked" || l.name == "S-inactive");
let is_rollup = labels.iter().any(|l| l.name == "rollup");
let is_waiting_for_reviewer = labels.iter().any(|l| l.name == "S-waiting-on-review");
let is_assigned_to_author = assignees.contains(author);
let has_capacity_tracking_opt_out = labels
.iter()
.any(|l| l.name == "S-no-work-capacity-tracking");
is_open
&& !is_draft
&& !is_blocked
&& !is_rollup
&& is_waiting_for_reviewer
&& !is_assigned_to_author
&& !has_capacity_tracking_opt_out
}
#[cfg(test)]
mod tests {
use crate::config::Config;
use crate::github::{GitHubUser, Issue, IssuesAction, IssuesEvent, Repository};
use crate::github::{Label, PullRequestNumber};
use crate::handlers::pr_tracking::{
AssignedPullRequest, handle_input, parse_input, upsert_pr_into_user_queue,
};
use crate::tests::github::{default_test_user, issue, pull_request, user};
use crate::tests::{TestContext, run_db_test};
#[tokio::test]
async fn add_pr_to_workqueue_on_assign() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
run_handler(
&ctx,
IssuesAction::Assigned {
assignee: user.clone(),
},
pull_request()
.number(10)
.labels(vec!["S-waiting-on-review"])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[10]).await;
Ok(ctx)
})
.await;
}
#[tokio::test]
async fn ignore_blocked_pr() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
run_handler(
&ctx,
IssuesAction::Assigned {
assignee: user.clone(),
},
pull_request()
.labels(vec!["S-waiting-on-review", "S-blocked"])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[]).await;
Ok(ctx)
})
.await;
}
#[tokio::test]
async fn remove_pr_from_workqueue_on_unassign() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
set_assigned_prs(&ctx, &user, &[10]).await;
run_handler(
&ctx,
IssuesAction::Unassigned {
assignee: user.clone(),
},
pull_request()
.number(10)
.labels(vec!["S-waiting-on-review"])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[]).await;
Ok(ctx)
})
.await;
}
#[tokio::test]
async fn add_pr_to_workqueue_on_label() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
run_handler(
&ctx,
IssuesAction::Assigned {
assignee: user.clone(),
},
pull_request().number(10).call(),
)
.await;
check_assigned_prs(&ctx, &user, &[]).await;
run_handler(
&ctx,
IssuesAction::Labeled {
label: Label {
name: "S-waiting-on-review".to_string(),
},
},
pull_request()
.number(10)
.labels(vec!["S-waiting-on-review"])
.assignees(vec![user.clone()])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[10]).await;
Ok(ctx)
})
.await;
}
#[tokio::test]
async fn remove_pr_from_workqueue_on_pr_closed() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
set_assigned_prs(&ctx, &user, &[10]).await;
run_handler(
&ctx,
IssuesAction::Closed,
pull_request()
.number(10)
.assignees(vec![user.clone()])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[]).await;
Ok(ctx)
})
.await;
}
#[tokio::test]
async fn add_pr_to_workqueue_on_pr_reopen() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
set_assigned_prs(&ctx, &user, &[42]).await;
run_handler(
&ctx,
IssuesAction::Reopened,
pull_request()
.number(10)
.labels(vec!["S-waiting-on-review"])
.assignees(vec![user.clone()])
.call(),
)
.await;
check_assigned_prs(&ctx, &user, &[10, 42]).await;
Ok(ctx)
})
.await;
}
// Make sure that we only consider pull requests, not issues.
#[tokio::test]
async fn ignore_issue_assignments() {
run_db_test(|ctx| async move {
let user = user("Martin", 2);
run_handler(
&ctx,
IssuesAction::Assigned {
assignee: user.clone(),
},
issue().number(10).call(),
)
.await;
check_assigned_prs(&ctx, &user, &[]).await;
Ok(ctx)
})
.await;
}
const TEST_REPO: &str = "rust-lang-test/triagebot-test";
async fn check_assigned_prs(
ctx: &TestContext,
user: &GitHubUser,
expected_prs: &[PullRequestNumber],
) {
let workqueue_arc = ctx
.handler_ctx()
.workqueue_map
.get(TEST_REPO)
.expect("test repo workqueue should exist");
let mut assigned = workqueue_arc
.read()
.await
.reviewers
.get(&user.id)
.cloned()
.unwrap_or_default()
.into_keys()
.collect::<Vec<_>>();
assigned.sort();
assert_eq!(assigned, expected_prs);
}
async fn set_assigned_prs(ctx: &TestContext, user: &GitHubUser, prs: &[PullRequestNumber]) {
{
let workqueue_arc = ctx
.handler_ctx()
.workqueue_map
.get(TEST_REPO)
.expect("test repo workqueue should exist");
let mut workqueue = workqueue_arc.write().await;
for &pr in prs {
upsert_pr_into_user_queue(
&mut workqueue,
user.id,
pr,
AssignedPullRequest {
title: format!("PR {pr}"),
},
);
}
}
check_assigned_prs(&ctx, user, prs).await;
}
async fn run_handler(ctx: &TestContext, action: IssuesAction, issue: Issue) {
let handler_ctx = ctx.handler_ctx();
let config = create_config().pr_tracking;
let event = IssuesEvent {
action,
issue,
changes: None,
before: None,
after: None,
repository: Repository {
full_name: "rust-lang-test/triagebot-test".to_string(),
default_branch: "main".to_string(),
fork: false,
parent: None,
},
sender: default_test_user(),
};
let input = parse_input(&handler_ctx, &event, config.as_ref())
.await
.unwrap();
if let Some(input) = input {
handle_input(&handler_ctx, &config.unwrap(), &event, input)
.await
.unwrap()
}
}
fn create_config() -> Config {
toml::from_str::<Config>(
r#"
[pr-tracking]
"#,
)
.unwrap()
}
}