Skip to content

Commit

Permalink
Allow setting review assignment limit
Browse files Browse the repository at this point in the history
  • Loading branch information
apiraino committed Apr 16, 2024
1 parent 1cf4ceb commit 4398ef5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/handlers/pull_requests_assignment_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,22 @@ WHERE r.user_id = $1;";
.unwrap();
Ok(row.into())
}

pub async fn set_review_prefs(
db: &DbClient,
user_id: u64,
max: u32,
) -> anyhow::Result<ReviewPrefs> {
let q = "
UPDATE review_prefs r
SET max_assigned_prs = $2
FROM users u
WHERE r.user_id=$1 AND u.user_id=r.user_id
RETURNING u.username, r.*";
let row = db
.query_one(q, &[&(max as i32), &(user_id as i64)])
.await
.context("Error retrieving review preferences")
.unwrap();
Ok(row.into())
}
32 changes: 30 additions & 2 deletions src/zulip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::db::notifications::add_metadata;
use crate::db::notifications::{self, delete_ping, move_indices, record_ping, Identifier};
use crate::github::{self, GithubClient};
use crate::handlers::docs_update::docs_update;
use crate::handlers::pull_requests_assignment_update::get_review_prefs;
use crate::handlers::pull_requests_assignment_update::{get_review_prefs, set_review_prefs};
use crate::handlers::Context;
use anyhow::{format_err, Context as _};
use std::env;
Expand Down Expand Up @@ -157,7 +157,7 @@ fn handle_command<'a>(
Some("meta") => add_meta_notification(&ctx, gh_id, words).await
.map_err(|e| format_err!("Failed to parse `meta` command. Synopsis: meta <num> <text>: Add <text> to your notification identified by <num> (>0)\n\nError: {e:?}")),
Some("work") => query_pr_assignments(&ctx, gh_id, words).await
.map_err(|e| format_err!("Failed to parse `work` command. Synopsis: work <show>: shows your current PRs assignment\n\nError: {e:?}")),
.map_err(|e| format_err!("Failed to parse `work` command. Synopsis:\nwork <show>: shows your current PRs assignment\nwork set <max>: set your max number of assigned PRs to review\n\nError: {e:?}")),
_ => {
while let Some(word) = next {
if word == "@**triagebot**" {
Expand Down Expand Up @@ -206,6 +206,18 @@ async fn query_pr_assignments(
gh_id: u64,
mut words: impl Iterator<Item = &str>,
) -> anyhow::Result<Option<String>> {
let testers = [
6098822, // apiraino
20113453, // matthewjasper
31162821, // jackh726
43851243, // fee1-dead
74931857, // albertlarsan68
];

if !testers.contains(&gh_id) {
anyhow::bail!("Sorry, this feature is currently restricted to testers.")
}

let subcommand = match words.next() {
Some(subcommand) => subcommand,
None => anyhow::bail!("no subcommand provided"),
Expand All @@ -221,6 +233,22 @@ async fn query_pr_assignments(
}
rec?
}
"set" => {
let max = match words.next() {
Some(max_value) => {
if words.next().is_some() {
anyhow::bail!("Too many parameters.");
}
max_value
.parse::<u32>()
.context("Wrong parameter format. Must be a positive integer.")?
}
None => anyhow::bail!("Missing parameter."),
};
set_review_prefs(&db_client, gh_id, max)
.await
.context("Error occurred while setting review preferences.")?
}
_ => anyhow::bail!("Invalid subcommand."),
};

Expand Down

0 comments on commit 4398ef5

Please sign in to comment.