Skip to content

Commit 9463f08

Browse files
authored
Merge pull request #2063 from Kobzol/stream-commands-dm
Make it possible to run `docs-update` and `ping-goals` also through DMs
2 parents 8c613ad + 3a5c7aa commit 9463f08

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

src/zulip.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::utils::pluralize;
1515
use crate::zulip::api::{MessageApiResponse, Recipient};
1616
use crate::zulip::client::ZulipClient;
1717
use crate::zulip::commands::{
18-
parse_cli, ChatCommand, LookupCmd, StreamCommand, WorkqueueCmd, WorkqueueLimit,
18+
parse_cli, ChatCommand, LookupCmd, PingGoalsArgs, StreamCommand, WorkqueueCmd, WorkqueueLimit,
1919
};
2020
use anyhow::{format_err, Context as _};
2121
use rust_team_data::v1::TeamKind;
@@ -225,6 +225,8 @@ async fn handle_command<'a>(
225225
ChatCommand::Whoami => whoami_cmd(&ctx, gh_id).await,
226226
ChatCommand::Lookup(cmd) => lookup_cmd(&ctx, cmd).await,
227227
ChatCommand::Work(cmd) => workqueue_commands(ctx, gh_id, cmd).await,
228+
ChatCommand::PingGoals(args) => ping_goals_cmd(ctx, gh_id, &args).await,
229+
ChatCommand::DocsUpdate => trigger_docs_update(message_data, &ctx.zulip),
228230
};
229231

230232
let output = output?;
@@ -281,32 +283,35 @@ async fn handle_command<'a>(
281283
StreamCommand::Read => post_waiter(&ctx, message_data, WaitingMessage::start_reading())
282284
.await
283285
.map_err(|e| format_err!("Failed to await at this time: {e:?}")),
284-
StreamCommand::PingGoals {
285-
threshold,
286-
next_update,
287-
} => {
288-
if project_goals::check_project_goal_acl(&ctx.github, gh_id).await? {
289-
ping_project_goals_owners(
290-
&ctx.github,
291-
&ctx.zulip,
292-
false,
293-
threshold as i64,
294-
&format!("on {next_update}"),
295-
)
296-
.await
297-
.map_err(|e| format_err!("Failed to await at this time: {e:?}"))?;
298-
Ok(None)
299-
} else {
300-
Err(format_err!(
301-
"That command is only permitted for those running the project-goal program.",
302-
))
303-
}
304-
}
286+
StreamCommand::PingGoals(args) => ping_goals_cmd(ctx, gh_id, &args).await,
305287
StreamCommand::DocsUpdate => trigger_docs_update(message_data, &ctx.zulip),
306288
}
307289
}
308290
}
309291

292+
async fn ping_goals_cmd(
293+
ctx: &Context,
294+
gh_id: u64,
295+
args: &PingGoalsArgs,
296+
) -> anyhow::Result<Option<String>> {
297+
if project_goals::check_project_goal_acl(&ctx.github, gh_id).await? {
298+
ping_project_goals_owners(
299+
&ctx.github,
300+
&ctx.zulip,
301+
false,
302+
args.threshold as i64,
303+
&format!("on {}", args.next_update),
304+
)
305+
.await
306+
.map_err(|e| format_err!("Failed to await at this time: {e:?}"))?;
307+
Ok(None)
308+
} else {
309+
Err(format_err!(
310+
"That command is only permitted for those running the project-goal program.",
311+
))
312+
}
313+
}
314+
310315
/// Returns true if we should notify user who was impersonated by someone who executed this command.
311316
/// More or less, the following holds: `sensitive` == `not read-only`.
312317
fn is_sensitive_command(cmd: &ChatCommand) -> bool {
@@ -315,8 +320,10 @@ fn is_sensitive_command(cmd: &ChatCommand) -> bool {
315320
| ChatCommand::Add { .. }
316321
| ChatCommand::Move { .. }
317322
| ChatCommand::Meta { .. } => true,
318-
ChatCommand::Whoami => false,
319-
ChatCommand::Lookup(_) => false,
323+
ChatCommand::Whoami
324+
| ChatCommand::DocsUpdate
325+
| ChatCommand::PingGoals(_)
326+
| ChatCommand::Lookup(_) => false,
320327
ChatCommand::Work(cmd) => match cmd {
321328
WorkqueueCmd::Show => false,
322329
WorkqueueCmd::SetPrLimit { .. } => true,

src/zulip/commands.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub enum ChatCommand {
3636
/// Inspect or modify your reviewer workqueue.
3737
#[clap(subcommand)]
3838
Work(WorkqueueCmd),
39+
/// Ping project goal owners.
40+
PingGoals(PingGoalsArgs),
41+
/// Update docs
42+
DocsUpdate,
3943
}
4044

4145
#[derive(clap::Parser, Debug, PartialEq)]
@@ -151,16 +155,19 @@ pub enum StreamCommand {
151155
/// Read a document.
152156
Read,
153157
/// Ping project goal owners.
154-
PingGoals {
155-
/// Number of days before an update is considered stale
156-
threshold: u64,
157-
/// Date of next update
158-
next_update: String,
159-
},
158+
PingGoals(PingGoalsArgs),
160159
/// Update docs
161160
DocsUpdate,
162161
}
163162

163+
#[derive(clap::Parser, Debug, PartialEq)]
164+
pub struct PingGoalsArgs {
165+
/// Number of days before an update is considered stale
166+
pub threshold: u64,
167+
/// Date of next update
168+
pub next_update: String,
169+
}
170+
164171
/// Helper function to parse CLI arguments without any colored help or error output.
165172
pub fn parse_cli<'a, T: Parser, I: Iterator<Item = &'a str>>(input: I) -> anyhow::Result<T> {
166173
// Add a fake first argument, which is expected by clap

0 commit comments

Comments
 (0)