Skip to content

Commit 0f14a19

Browse files
committed
Configure rotation mode through Zulip
1 parent c0caaa1 commit 0f14a19

File tree

1 file changed

+60
-18
lines changed

1 file changed

+60
-18
lines changed

src/zulip.rs

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::db::notifications::add_metadata;
22
use crate::db::notifications::{self, delete_ping, move_indices, record_ping, Identifier};
3-
use crate::db::review_prefs::{get_review_prefs, upsert_review_prefs};
3+
use crate::db::review_prefs::{get_review_prefs, upsert_review_prefs, RotationMode};
44
use crate::github::{get_id_for_username, GithubClient, User};
55
use crate::handlers::docs_update::docs_update;
66
use crate::handlers::pr_tracking::get_assigned_prs;
@@ -270,6 +270,15 @@ async fn workqueue_commands(
270270

271271
let db_client = ctx.db.get().await;
272272

273+
let gh_username = username_from_gh_id(&ctx.github, gh_id)
274+
.await?
275+
.ok_or_else(|| anyhow::anyhow!("Cannot find your GitHub username in the team database"))?;
276+
let user = User {
277+
login: gh_username.clone(),
278+
id: gh_id,
279+
};
280+
let review_prefs = get_review_prefs(&db_client, gh_id).await?;
281+
273282
let response = match subcommand {
274283
"show" => {
275284
let mut assigned_prs = get_assigned_prs(ctx, gh_id)
@@ -287,17 +296,26 @@ async fn workqueue_commands(
287296
let review_prefs = get_review_prefs(&db_client, gh_id)
288297
.await
289298
.context("cannot get review preferences")?;
290-
let capacity = match review_prefs.and_then(|p| p.max_assigned_prs) {
299+
let capacity = match review_prefs.as_ref().and_then(|p| p.max_assigned_prs) {
291300
Some(max) => max.to_string(),
292301
None => String::from("Not set (i.e. unlimited)"),
293302
};
303+
let rotation_mode = review_prefs
304+
.as_ref()
305+
.map(|p| p.rotation_mode)
306+
.unwrap_or_default();
307+
let rotation_mode = match rotation_mode {
308+
RotationMode::OnRotation => "on rotation",
309+
RotationMode::OffRotation => "off rotation",
310+
};
294311

295312
let mut response = format!(
296313
"`rust-lang/rust` PRs in your review queue: {prs} ({} {})\n",
297314
assigned_prs.len(),
298315
pluralize("PR", assigned_prs.len())
299316
);
300317
writeln!(response, "Review capacity: {capacity}\n")?;
318+
writeln!(response, "Rotation mode: *{rotation_mode}*\n")?;
301319
writeln!(response, "*Note that only certain PRs that are assigned to you are included in your review queue.*")?;
302320
response
303321
}
@@ -315,21 +333,16 @@ async fn workqueue_commands(
315333
)?)
316334
}
317335
}
318-
None => anyhow::bail!("Missing parameter."),
319-
};
320-
let gh_username = username_from_gh_id(&ctx.github, gh_id)
321-
.await?
322-
.ok_or_else(|| {
323-
anyhow::anyhow!("Cannot find your GitHub username in the team database")
324-
})?;
325-
326-
let user = User {
327-
login: gh_username.clone(),
328-
id: gh_id,
336+
None => anyhow::bail!("Missing parameter. See `work help` for more information."),
329337
};
330-
upsert_review_prefs(&db_client, user, max_assigned_prs)
331-
.await
332-
.context("Error occurred while setting review preferences.")?;
338+
upsert_review_prefs(
339+
&db_client,
340+
user,
341+
max_assigned_prs,
342+
review_prefs.map(|p| p.rotation_mode).unwrap_or_default(),
343+
)
344+
.await
345+
.context("Error occurred while setting review preferences.")?;
333346
tracing::info!("Setting max assignment PRs of `{gh_username}` to {max_assigned_prs:?}");
334347
format!(
335348
"Review capacity set to {}",
@@ -339,8 +352,37 @@ async fn workqueue_commands(
339352
}
340353
)
341354
}
342-
"help" => r"work show => show your assigned PRs
343-
work set-pr-limit <number>|unlimited => set the maximum number of PRs you can be assigned to"
355+
"set-rotation-mode" => {
356+
let rotation_mode = match words.next() {
357+
Some(value) => {
358+
if words.next().is_some() {
359+
anyhow::bail!("Too many parameters.");
360+
}
361+
match value {
362+
"on" => RotationMode::OnRotation,
363+
"off" => RotationMode::OffRotation,
364+
_ => anyhow::bail!("Unknown rotation mode {value}. Use `on` or `off`.")
365+
}
366+
}
367+
None => anyhow::bail!("Missing parameter. See `work help` for more information."),
368+
};
369+
upsert_review_prefs(
370+
&db_client,
371+
user,
372+
review_prefs.and_then(|p| p.max_assigned_prs.map(|v| v as u32)),
373+
rotation_mode,
374+
)
375+
.await
376+
.context("Error occurred while setting review preferences.")?;
377+
tracing::info!("Setting rotation mode `{gh_username}` to {rotation_mode:?}");
378+
format!(
379+
"Rotation mode set to {rotation_mode:?}"
380+
)
381+
}
382+
"help" => r"`work show`: show your assigned PRs
383+
`work set-pr-limit <number>|unlimited`: set the maximum number of PRs you can be assigned to
384+
`work set-rotation-mode <off|on>`: configure if you are *on* rotation or *off* rotation (e.g. when you are on a vacation)
385+
"
344386
.to_string(),
345387
_ => anyhow::bail!("Invalid subcommand."),
346388
};

0 commit comments

Comments
 (0)