-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathping.rs
More file actions
92 lines (82 loc) · 2.81 KB
/
Copy pathping.rs
File metadata and controls
92 lines (82 loc) · 2.81 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
//! Purpose: Allow any user to ping a pre-selected group of people on GitHub via comments.
//!
//! The set of "teams" which can be pinged is intentionally restricted via configuration.
//!
//! Parsing is done in the `parser::command::ping` module.
use std::borrow::Cow;
use crate::{
config::PingConfig,
errors::user_error,
github::{self, Event},
handlers::Context,
};
use parser::command::ping::PingCommand;
pub(super) async fn handle_command(
ctx: &Context,
config: &PingConfig,
event: &Event,
team_name: PingCommand,
) -> anyhow::Result<()> {
let is_team_member = matches!(ctx.team.is_team_member(&event.user().login).await, Ok(true));
if !is_team_member {
return user_error!("Only Rust team members can ping teams.");
}
let Some((gh_team, config)) = config.get_by_name(&team_name.team) else {
return user_error!(format!(
"This team (`{}`) cannot be pinged via this command; \
it may need to be added to `triagebot.toml` on the default branch.",
team_name.team,
));
};
let Some(team) = ctx.team.get_team(gh_team).await? else {
return user_error!(format!(
"This team (`{}`) does not exist in the team repository.",
team_name.team,
));
};
#[expect(
clippy::collapsible_if,
reason = "in the outer `if`, we check for `config`"
)]
if let Some(label) = &config.label {
if let Err(err) = event
.issue()
.unwrap()
.add_labels(
&ctx.github,
vec![github::Label {
name: label.clone(),
}],
)
.await
{
return user_error!(format!("Error adding team label (`{}`): {:?}.", label, err));
}
}
let mut users = Vec::new();
if let Some(gh) = team.github {
let repo = event.issue().expect("has issue").repository();
// Ping all github teams associated with this team repo team that are in this organization.
// We cannot ping across organizations, but this should not matter, as teams should be
// sync'd to the org for which triagebot is configured.
for gh_team in gh.teams.iter().filter(|t| t.org == repo.organization) {
users.push(format!("@{}/{}", gh_team.org, gh_team.name));
}
} else {
for member in &team.members {
users.push(format!("@{}", member.github));
}
}
let ping_msg: Cow<_> = if users.is_empty() {
"no known users to ping?".into()
} else {
format!("cc {}", users.join(" ")).into()
};
let comment = format!("{}\n\n{}", config.message, ping_msg);
event
.issue()
.expect("issue")
.post_comment(&ctx.github, &comment)
.await?;
Ok(())
}