-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathteams.rs
308 lines (259 loc) · 8.66 KB
/
teams.rs
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
use std::collections::BTreeMap;
use std::sync::{Arc, RwLock};
use diesel::prelude::*;
use super::DB_POOL;
use crate::domain::github::GitHubUser;
use crate::error::*;
use crate::github::GH;
const UPDATE_CONFIG_EVERY_MIN: u64 = 5;
//==============================================================================
// Public API
//==============================================================================
type TeamsMap = BTreeMap<TeamLabel, Team>;
lazy_static! {
pub static ref SETUP: Arc<RwLock<RfcbotConfig>> =
Arc::new(RwLock::new(read_rfcbot_cfg_validated()));
}
#[derive(Debug, Deserialize)]
pub struct RfcbotConfig {
fcp_behaviors: BTreeMap<String, FcpBehavior>,
teams: RfcbotTeams,
#[serde(skip)]
cached_teams: TeamsMap,
}
impl RfcbotConfig {
/// Retrive an iterator over all the team labels.
pub fn team_labels(&self) -> impl Iterator<Item = &TeamLabel> {
self.teams().map(|(k, _)| k)
}
/// Retrive an iterator over all the (team label, team) pairs.
pub fn teams(&self) -> impl Iterator<Item = (&TeamLabel, &Team)> {
match &self.teams {
RfcbotTeams::Local(teams) => teams.iter(),
RfcbotTeams::Remote { .. } => self.cached_teams.iter(),
}
}
/// Are we allowed to auto-close issues after F-FCP in this repo?
pub fn should_ffcp_auto_close(&self, repo: &str) -> bool {
self.fcp_behaviors
.get(repo)
.map(|fcp| fcp.close)
.unwrap_or_default()
}
/// Are we allowed to auto-postpone issues after F-FCP in this repo?
pub fn should_ffcp_auto_postpone(&self, repo: &str) -> bool {
self.fcp_behaviors
.get(repo)
.map(|fcp| fcp.postpone)
.unwrap_or_default()
}
// Update the list of teams from external sources, if needed
fn update(&mut self) -> Result<(), DashError> {
#[derive(Deserialize)]
struct ToDeserialize {
teams: TeamsMap,
}
if let RfcbotTeams::Remote { ref url } = &self.teams {
let de: ToDeserialize = reqwest::blocking::get(url)?.error_for_status()?.json()?;
self.cached_teams = de.teams;
}
Ok(())
}
}
#[derive(Debug, Deserialize)]
pub struct FcpBehavior {
#[serde(default)]
close: bool,
#[serde(default)]
postpone: bool,
}
// This enum definition mixes both struct-style and tuple-style variants: this is intentionally
// done to get the wanted deserialization behavior from serde. Since this is an untagged enum from
// serde's point of view it will deserialize a RfcbotTeams::Remote when it encounters a key named
// url with a string in it, otherwise the normal team map.
#[derive(Debug, Deserialize)]
#[serde(untagged)]
enum RfcbotTeams {
Local(TeamsMap),
Remote { url: String },
}
#[derive(Debug, Deserialize)]
pub struct Team {
ping: String,
members: Vec<String>,
}
impl Team {
pub fn ping(&self) -> &str {
&self.ping
}
pub fn member_logins(&self) -> impl Iterator<Item = &str> {
self.members.iter().map(std::string::String::as_str)
}
}
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Deserialize)]
#[serde(transparent)]
pub struct TeamLabel(pub String);
pub fn start_updater_thread() {
let _ = crate::utils::spawn_thread("teams updater", UPDATE_CONFIG_EVERY_MIN, || {
let mut teams = SETUP.write().unwrap();
teams.update()?;
for (_name, team) in teams.teams() {
team.validate()?;
}
Ok(())
});
}
//==============================================================================
// Implementation details
//==============================================================================
/// Read the validated `rfcbot.toml` configuration file.
fn read_rfcbot_cfg_validated() -> RfcbotConfig {
let cfg = read_rfcbot_cfg();
cfg.teams().map(|(_, v)| v).for_each(|team| {
team.validate().expect(
"unable to verify team member from database.
if you're running this for tests, make sure you've pulled github users from prod",
)
});
cfg
}
/// Read the unprocessed `rfcbot.toml` configuration file.
fn read_rfcbot_cfg() -> RfcbotConfig {
let mut config = read_rfcbot_cfg_from(include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/rfcbot.toml"
)));
config.update().expect("couldn't update the configuration!");
config
}
fn read_rfcbot_cfg_from(input: &str) -> RfcbotConfig {
toml::from_str(input).expect("couldn't parse rfcbot.toml!")
}
impl Team {
fn validate(&self) -> DashResult<()> {
use crate::domain::schema::githubuser::dsl::*;
use std::thread::sleep;
use std::time::Duration;
// GitHub rate limits using OAuth is 5_000 requests / hour =~ 1.39s
const RATE_LIMIT_DEPLAY: Duration = Duration::from_millis(1390);
let conn = &*(DB_POOL.get()?);
let gh = &*(GH);
// bail if they don't exist, but we don't want to actually keep the id in ram
for member_login in self.member_logins() {
if githubuser
.filter(login.eq(member_login))
.first::<GitHubUser>(conn)
.is_err()
{
crate::github::handle_user(&conn, &gh.get_user(member_login)?)?;
sleep(RATE_LIMIT_DEPLAY);
info!("loaded into the database user {}", member_login);
}
}
Ok(())
}
}
//==============================================================================
// Tests
//==============================================================================
#[cfg(test)]
pub mod test {
use super::*;
lazy_static! {
pub static ref TEST_SETUP: RfcbotConfig = read_rfcbot_cfg_from(
r#"
[fcp_behaviors]
[fcp_behaviors."rust-lang/alpha"]
close = true
postpone = true
[fcp_behaviors."foobar/beta"]
close = false
[fcp_behaviors."bazquux/gamma"]
postpone = false
[fcp_behaviors."wibble/epsilon"]
[teams]
[teams.T-avengers]
name = "The Avengers"
ping = "marvel/avengers"
members = [
"hulk",
"thor",
"thevision",
"blackwidow",
"spiderman",
"captainamerica",
]
[teams.justice-league]
name = "Justice League of America"
ping = "dc-comics/justice-league"
members = [
"superman",
"wonderwoman",
"aquaman",
"batman",
"theflash"
]
"#
);
}
#[test]
fn setup_parser_correct() {
let cfg = &*TEST_SETUP;
// Labels are correct:
assert_eq!(
cfg.team_labels().map(|tl| tl.0.clone()).collect::<Vec<_>>(),
vec!["T-avengers", "justice-league"]
);
// Teams are correct:
let map: BTreeMap<_, _> = cfg.teams().map(|(k, v)| (k.0.clone(), v)).collect();
let avengers = map.get("T-avengers").unwrap();
//assert_eq!(avengers.name, "The Avengers");
//assert_eq!(avengers.ping, "marvel/avengers");
assert_eq!(
avengers.member_logins().collect::<Vec<_>>(),
vec![
"hulk",
"thor",
"thevision",
"blackwidow",
"spiderman",
"captainamerica"
]
);
let jsa = map.get("justice-league").unwrap();
//assert_eq!(jsa.name, "Justice League of America");
//assert_eq!(jsa.ping, "dc-comics/justice-league");
assert_eq!(
jsa.member_logins().collect::<Vec<_>>(),
vec!["superman", "wonderwoman", "aquaman", "batman", "theflash"]
);
// Random non-existent team does not exist:
assert!(map.get("random").is_none());
// FFCP behavior correct:
assert!(cfg.should_ffcp_auto_close("rust-lang/alpha"));
assert!(cfg.should_ffcp_auto_postpone("rust-lang/alpha"));
assert!(!cfg.should_ffcp_auto_close("foobar/beta"));
assert!(!cfg.should_ffcp_auto_postpone("foobar/beta"));
assert!(!cfg.should_ffcp_auto_close("bazquux/gamma"));
assert!(!cfg.should_ffcp_auto_postpone("bazquux/gamma"));
assert!(!cfg.should_ffcp_auto_close("wibble/epsilon"));
assert!(!cfg.should_ffcp_auto_postpone("wibble/epsilon"));
assert!(!cfg.should_ffcp_auto_close("random"));
assert!(!cfg.should_ffcp_auto_postpone("random"));
}
#[test]
fn cfg_file_wellformed() {
// Just parse it and ensure that we get no panics for now!
// This is a crap test; but, better than nothing.
let _ = read_rfcbot_cfg();
}
#[test]
#[ignore = "we now load all usernames from the team repo"]
fn team_members_exist() {
crate::utils::setup_test_env();
let setup = SETUP.read().unwrap();
for (label, _) in setup.teams() {
println!("found team {:?}", label);
}
}
}