-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcmd.rs
411 lines (367 loc) · 15 KB
/
cmd.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! This module defines the commands supported and the logic to parse them from
//! GitHub events.
use anyhow::Result;
use lazy_static::lazy_static;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tracing::error;
use crate::{
cfg_repo::{Cfg, CfgError},
github::{
split_full_name, DynGH, Event, IssueCommentEventAction, IssueEventAction, PullRequestEventAction,
},
};
/// Available commands.
const CMD_CREATE_VOTE: &str = "vote";
const CMD_CANCEL_VOTE: &str = "cancel-vote";
const CMD_CHECK_VOTE: &str = "check-vote";
lazy_static! {
/// Regex used to detect commands in issues/prs comments.
static ref CMD: Regex = Regex::new(r"(?m)^/(vote|cancel-vote|check-vote)-?([a-zA-Z0-9]*)\s*$")
.expect("invalid CMD regexp");
}
/// Represents a command to be executed, usually created from a GitHub event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[allow(clippy::enum_variant_names)]
pub(crate) enum Command {
CreateVote(CreateVoteInput),
CancelVote(CancelVoteInput),
CheckVote(CheckVoteInput),
}
impl Command {
/// Try to create a new command from an event.
///
/// A command can be created from an event in two different ways:
///
/// 1. A manual command is found in the event (e.g. `/vote` on comment).
/// 2. The event triggers the creation of an automatic command (e.g. a new
/// PR is created and one of the affected files matches a predefined
/// pattern).
///
/// Manual commands have preference, and if one is found, automatic ones
/// won't be processed.
///
pub(crate) async fn from_event(gh: DynGH, event: &Event) -> Option<Self> {
if let Some(cmd) = Command::from_event_manual(event) {
Some(cmd)
} else {
match Command::from_event_automatic(gh, event).await {
Ok(cmd) => cmd,
Err(err) => {
error!(?err, ?event, "error processing automatic command");
None
}
}
}
}
/// Get manual command from event, if available.
fn from_event_manual(event: &Event) -> Option<Self> {
// Get the content where we'll try to extract the command from
let content = match event {
Event::Issue(event) if event.action == IssueEventAction::Opened => &event.issue.body,
Event::IssueComment(event) if event.action == IssueCommentEventAction::Created => {
&event.comment.body
}
Event::PullRequest(event) if event.action == PullRequestEventAction::Opened => {
&event.pull_request.body
}
_ => return None,
};
// Create a new command from the content (if possible)
if let Some(content) = content {
if let Some(captures) = CMD.captures(content) {
let cmd = captures.get(1)?.as_str();
let profile = match captures.get(2)?.as_str() {
"" => None,
profile => Some(profile),
};
match cmd {
CMD_CREATE_VOTE => {
return Some(Command::CreateVote(CreateVoteInput::new(profile, event)))
}
CMD_CANCEL_VOTE => return Some(Command::CancelVote(CancelVoteInput::new(event))),
CMD_CHECK_VOTE => return Some(Command::CheckVote(CheckVoteInput::new(event))),
_ => return None,
}
}
}
None
}
/// Create automatic command from event, if applicable.
async fn from_event_automatic(gh: DynGH, event: &Event) -> Result<Option<Self>> {
match event {
// Pull request opened
Event::PullRequest(event) if event.action == PullRequestEventAction::Opened => {
// Get configuration
let inst_id = event.installation.id as u64;
let (owner, repo) = split_full_name(&event.repository.full_name);
let cfg = match Cfg::get(gh.clone(), inst_id, owner, repo).await {
Err(CfgError::ConfigNotFound) => return Ok(None),
Err(err) => return Err(err.into()),
Ok(cfg) => cfg,
};
// Process automation if enabled
if let Some(automation) = cfg.automation {
if !automation.enabled || automation.rules.is_empty() {
return Ok(None);
}
// Check if any of the PR files matches the automation rules
let pr_number = event.pull_request.number;
let pr_files = gh.get_pr_files(inst_id, owner, repo, pr_number).await?;
for rule in automation.rules {
if rule.matches(pr_files.as_slice())? {
let cmd = Command::CreateVote(CreateVoteInput::new(
Some(&rule.profile),
&Event::PullRequest(event.clone()),
));
return Ok(Some(cmd));
}
}
}
Ok(None)
}
_ => Ok(None),
}
}
}
/// Information required to create a new vote.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct CreateVoteInput {
pub profile_name: Option<String>,
pub created_by: String,
pub installation_id: i64,
pub issue_id: i64,
pub issue_number: i64,
pub issue_title: String,
pub is_pull_request: bool,
pub repository_full_name: String,
pub organization: Option<String>,
}
impl CreateVoteInput {
/// Create a new `CreateVoteInput` instance from the profile and event
/// provided.
pub(crate) fn new(profile_name: Option<&str>, event: &Event) -> Self {
match event {
Event::Issue(event) => Self {
profile_name: profile_name.map(ToString::to_string),
created_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_id: event.issue.id,
issue_number: event.issue.number,
issue_title: event.issue.title.clone(),
is_pull_request: event.issue.pull_request.is_some(),
repository_full_name: event.repository.full_name.clone(),
organization: event.organization.as_ref().map(|o| o.login.clone()),
},
Event::IssueComment(event) => Self {
profile_name: profile_name.map(ToString::to_string),
created_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_id: event.issue.id,
issue_number: event.issue.number,
issue_title: event.issue.title.clone(),
is_pull_request: event.issue.pull_request.is_some(),
repository_full_name: event.repository.full_name.clone(),
organization: event.organization.as_ref().map(|o| o.login.clone()),
},
Event::PullRequest(event) => Self {
profile_name: profile_name.map(ToString::to_string),
created_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_id: event.pull_request.id,
issue_number: event.pull_request.number,
issue_title: event.pull_request.title.clone(),
is_pull_request: true,
repository_full_name: event.repository.full_name.clone(),
organization: event.organization.as_ref().map(|o| o.login.clone()),
},
}
}
}
/// Information required to cancel an open vote.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct CancelVoteInput {
pub cancelled_by: String,
pub installation_id: i64,
pub issue_number: i64,
pub is_pull_request: bool,
pub repository_full_name: String,
}
impl CancelVoteInput {
/// Create a new `CancelVoteInput` instance from the event provided.
pub(crate) fn new(event: &Event) -> Self {
match event {
Event::Issue(event) => Self {
cancelled_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_number: event.issue.number,
is_pull_request: event.issue.pull_request.is_some(),
repository_full_name: event.repository.full_name.clone(),
},
Event::IssueComment(event) => Self {
cancelled_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_number: event.issue.number,
is_pull_request: event.issue.pull_request.is_some(),
repository_full_name: event.repository.full_name.clone(),
},
Event::PullRequest(event) => Self {
cancelled_by: event.sender.login.clone(),
installation_id: event.installation.id,
issue_number: event.pull_request.number,
is_pull_request: true,
repository_full_name: event.repository.full_name.clone(),
},
}
}
}
/// Information required to check the status of an open vote.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub(crate) struct CheckVoteInput {
pub issue_number: i64,
pub repository_full_name: String,
}
impl CheckVoteInput {
/// Create a new `CheckVoteInput` instance from the event provided.
pub(crate) fn new(event: &Event) -> Self {
match event {
Event::Issue(event) => Self {
issue_number: event.issue.number,
repository_full_name: event.repository.full_name.clone(),
},
Event::IssueComment(event) => Self {
issue_number: event.issue.number,
repository_full_name: event.repository.full_name.clone(),
},
Event::PullRequest(event) => Self {
issue_number: event.pull_request.number,
repository_full_name: event.repository.full_name.clone(),
},
}
}
}
#[cfg(test)]
mod tests {
use std::{sync::Arc, vec};
use futures::future;
use mockall::predicate::eq;
use crate::{
github::{File, MockGH},
testutil::*,
};
use super::*;
#[test]
fn manual_command_from_issue_event_unsupported_action() {
let mut event = setup_test_issue_event();
event.action = IssueEventAction::Other;
event.issue.body = Some(format!("/{CMD_CREATE_VOTE}"));
let event = Event::Issue(event);
assert_eq!(Command::from_event_manual(&event), None);
}
#[test]
fn manual_command_from_issue_event_no_cmd() {
let mut event = setup_test_issue_event();
event.action = IssueEventAction::Opened;
event.issue.body = Some("Hi!".to_string());
let event = Event::Issue(event);
assert_eq!(Command::from_event_manual(&event), None);
}
#[test]
fn manual_command_from_issue_event_create_vote_cmd_default_profile() {
let mut event = setup_test_issue_event();
event.action = IssueEventAction::Opened;
event.issue.body = Some(format!("/{CMD_CREATE_VOTE}"));
let event = Event::Issue(event);
assert_eq!(
Command::from_event_manual(&event),
Some(Command::CreateVote(CreateVoteInput::new(None, &event)))
);
}
#[test]
fn manual_command_from_issue_event_create_vote_cmd_profile1() {
let mut event = setup_test_issue_event();
event.action = IssueEventAction::Opened;
event.issue.body = Some(format!("/{CMD_CREATE_VOTE}-{PROFILE_NAME}"));
let event = Event::Issue(event);
assert_eq!(
Command::from_event_manual(&event),
Some(Command::CreateVote(CreateVoteInput::new(
Some("profile1"),
&event
)))
);
}
#[test]
fn manual_command_from_issue_comment_event_unsupported_action() {
let mut event = setup_test_issue_comment_event();
event.action = IssueCommentEventAction::Other;
event.issue.body = Some(CMD_CREATE_VOTE.to_string());
let event = Event::IssueComment(event);
assert_eq!(Command::from_event_manual(&event), None);
}
#[test]
fn manual_command_from_issue_comment_event_create_vote_cmd_default_profile() {
let mut event = setup_test_issue_comment_event();
event.action = IssueCommentEventAction::Created;
event.comment.body = Some(format!("/{CMD_CREATE_VOTE}"));
let event = Event::IssueComment(event);
assert_eq!(
Command::from_event_manual(&event),
Some(Command::CreateVote(CreateVoteInput::new(None, &event)))
);
}
#[test]
fn manual_command_from_issue_comment_event_cancel_vote_cmd() {
let mut event = setup_test_issue_comment_event();
event.action = IssueCommentEventAction::Created;
event.comment.body = Some(format!("/{CMD_CANCEL_VOTE}"));
let event = Event::IssueComment(event);
assert_eq!(
Command::from_event_manual(&event),
Some(Command::CancelVote(CancelVoteInput::new(&event)))
);
}
#[test]
fn manual_command_from_pr_event_unsupported_action() {
let mut event = setup_test_pr_event();
event.action = PullRequestEventAction::Other;
event.pull_request.body = Some(CMD_CREATE_VOTE.to_string());
let event = Event::PullRequest(event);
assert_eq!(Command::from_event_manual(&event), None);
}
#[test]
fn manual_command_from_pr_event_create_vote_cmd_default_profile() {
let mut event = setup_test_pr_event();
event.action = PullRequestEventAction::Opened;
event.pull_request.body = Some(format!("/{CMD_CREATE_VOTE}"));
let event = Event::PullRequest(event);
assert_eq!(
Command::from_event_manual(&event),
Some(Command::CreateVote(CreateVoteInput::new(None, &event)))
);
}
#[tokio::test]
async fn automatic_command_from_pr_event() {
let mut gh = MockGH::new();
gh.expect_get_config_file()
.with(eq(INST_ID), eq(ORG), eq(REPO))
.times(1)
.returning(|_, _, _| Box::pin(future::ready(Some(get_test_valid_config()))));
gh.expect_get_pr_files()
.with(eq(INST_ID), eq(ORG), eq(REPO), eq(ISSUE_NUM))
.times(1)
.returning(|_, _, _, _| {
Box::pin(future::ready(Ok(vec![File {
filename: "README.md".to_string(),
}])))
});
let gh = Arc::new(gh);
let mut event = setup_test_pr_event();
event.action = PullRequestEventAction::Opened;
let event = Event::PullRequest(event);
assert_eq!(
Command::from_event_automatic(gh, &event.clone()).await.unwrap(),
Some(Command::CreateVote(CreateVoteInput::new(Some("default"), &event)))
);
}
}