-
Notifications
You must be signed in to change notification settings - Fork 67
/
nag.rs
1340 lines (1129 loc) · 45.8 KB
/
nag.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::collections::BTreeSet;
use std::sync::Mutex;
use chrono::{Duration, Utc};
use diesel::prelude::*;
use itertools::Itertools;
use super::GH;
use crate::domain::github::{GitHubUser, Issue, IssueComment};
use crate::domain::rfcbot::{
FcpConcern, FcpProposal, FcpReviewRequest, FeedbackRequest, NewFcpConcern, NewFcpProposal,
NewFcpReviewRequest, NewFeedbackRequest, NewPoll, NewPollResponseRequest, Poll,
PollResponseRequest,
};
use crate::domain::schema::{
fcp_concern, fcp_proposal, fcp_review_request, githubuser, issue, issuecomment, poll,
poll_response_request,
};
use crate::error::{DashError, DashResult};
use crate::github::models::CommentFromJson;
use crate::teams::SETUP;
use crate::DB_POOL;
use crate::github::command::{FcpDisposition, Label, RfcBotCommand};
impl Issue {
fn remove_label(&self, label: Label) {
let _ = GH.remove_label(&self.repository, self.number, label.as_str());
}
fn add_label(&self, label: Label) -> DashResult<()> {
GH.add_label(&self.repository, self.number, label.as_str())
}
fn close(&self) {
ok_or!(GH.close_issue(&self.repository, self.number), why =>
error!("Unable to close issue {:?}: {:?}", self, why));
}
}
lazy_static! {
static ref NAG_LOCK: Mutex<()> = Mutex::new(());
}
// TODO check if new subteam label added for existing proposals
pub fn update_nags(comment: &IssueComment) -> DashResult<()> {
let _in_progress_marker = NAG_LOCK.lock();
let conn = &*DB_POOL.get()?;
let issue = issue::table.find(comment.fk_issue).first::<Issue>(conn)?;
let author = githubuser::table
.find(comment.fk_user)
.first::<GitHubUser>(conn)?;
let subteam_members = subteam_members(&issue)?;
let all_team_members = all_team_members()?;
// Attempt to parse all commands out of the comment
let mut any = false;
let teams = SETUP.read().unwrap();
for command in RfcBotCommand::from_str_all(&teams, &comment.body) {
any = true;
if let RfcBotCommand::StartPoll { .. } = command {
// Accept poll requests from any known user.
if all_team_members.iter().find(|&u| u == &author).is_none() {
info!("poll requester ({}) is not a known user", author.login);
return Ok(());
}
} else {
// Don't accept most bot commands from non-subteam members.
// Early return because we'll just get here again...
if subteam_members.iter().find(|&u| u == &author).is_none() {
info!(
"command author ({}) doesn't appear in any relevant subteams",
author.login
);
return Ok(());
}
}
debug!("processing rfcbot command: {:?}", &command);
let process = command.process(&author, &issue, comment, &subteam_members);
ok_or!(process, why => {
error!("Unable to process command for comment id {}: {:?}",
comment.id, why);
return Ok(());
});
debug!("rfcbot command is processed");
}
if !any {
ok_or!(resolve_applicable_feedback_requests(&author, &issue, comment),
why => error!("Unable to resolve feedback requests for comment id {}: {:?}",
comment.id, why));
}
evaluate_nags();
Ok(())
}
fn update_proposal_review_status(proposal_id: i32) -> DashResult<()> {
let conn = &*DB_POOL.get()?;
// this is an updated comment from the bot itself
// parse out each "reviewed" status for each user, then update them
let proposal: FcpProposal = fcp_proposal::table.find(proposal_id).first(conn)?;
// don't update any statuses if the fcp is running or closed
if proposal.fcp_start.is_some() || proposal.fcp_closed {
return Ok(());
}
let comment: IssueComment = issuecomment::table
.find(proposal.fk_bot_tracking_comment)
.first(conn)?;
// parse the status comment and mark any new reviews as reviewed
for username in parse_ticky_boxes("proposal", proposal.id, &comment) {
let user: GitHubUser = githubuser::table
.filter(githubuser::login.eq(username))
.first(conn)?;
{
use crate::domain::schema::fcp_review_request::dsl::*;
let mut review_request: FcpReviewRequest = fcp_review_request
.filter(fk_proposal.eq(proposal.id))
.filter(fk_reviewer.eq(user.id))
.first(conn)?;
review_request.reviewed = true;
diesel::update(fcp_review_request.find(review_request.id))
.set(&review_request)
.execute(conn)?;
}
}
Ok(())
}
/// Given a poll, parse out each "responded" status, in the poll's ticky boxes,
// for each user, then update the responded status in the database.
fn update_poll_response_status(poll_id: i32) -> DashResult<()> {
let conn = &*DB_POOL.get()?;
// this is an updated comment from the bot itself
let survey: Poll = poll::table.find(poll_id).first(conn)?;
// don't update any statuses if the poll is closed
if survey.poll_closed {
return Ok(());
}
let comment: IssueComment = issuecomment::table
.find(survey.fk_bot_tracking_comment)
.first(conn)?;
// parse the status comment and mark any new responses as responded
for respondent in parse_ticky_boxes("poll", survey.id, &comment) {
let user = githubuser::table
.filter(githubuser::login.eq(respondent))
.first(conn);
let user: GitHubUser = ok_or_continue!(user, why =>
error!("Can't find respondent {} in the database. \
The poll comment has probably been manually edited. {:?}",
respondent, why)
);
{
use crate::domain::schema::poll_response_request::dsl::*;
let mut review_request: PollResponseRequest = poll_response_request
.filter(fk_poll.eq(survey.id))
.filter(fk_respondent.eq(user.id))
.first(conn)?;
review_request.responded = true;
diesel::update(poll_response_request.find(review_request.id))
.set(&review_request)
.execute(conn)?;
}
}
Ok(())
}
fn parse_ticky_boxes<'a>(
what: &'a str,
id: i32,
comment: &'a IssueComment,
) -> impl Iterator<Item = &'a str> {
comment.body.lines().filter_map(move |line| {
if line.starts_with("* [") {
let l = line.trim_start_matches("* [");
let reviewed = l.starts_with('x');
let remaining = l.trim_start_matches("x] @").trim_start_matches(" ] @");
if let Some(username) = remaining.split_whitespace().next() {
trace!(
"reviewer parsed as reviewed? {} (line: \"{}\")",
reviewed,
l
);
if reviewed {
Some(username)
} else {
None
}
} else {
warn!(
"An empty usename showed up in comment {} for {} {}",
comment.id, what, id
);
None
}
} else {
None
}
})
}
fn evaluate_nags() {
ok_or!(evaluate_pendings(), why =>
error!("Unable to evaluate outstanding proposals: {:?}", why));
ok_or!(evaluate_ffcps(), why =>
error!("Unable to evaluate outstanding ffcps: {:?}", why));
ok_or!(evaluate_polls(), why =>
error!("Unable to evaluate outstanding polls: {:?}", why));
}
fn evaluate_polls() -> DashResult<()> {
use crate::domain::schema::issuecomment::dsl::id as issuecomment_id;
use crate::domain::schema::issuecomment::dsl::*;
use crate::domain::schema::poll::dsl::*;
let conn = &*DB_POOL.get()?;
// first process all "pending" polls (unresponded)
let pending = poll.filter(poll_closed.eq(false)).load::<Poll>(conn);
let pending = ok_or!(pending, why => {
error!("Unable to retrieve list of pending polls: {:?}", why);
throw!(why)
});
for mut survey in pending {
let initiator = githubuser::table
.find(survey.fk_initiator)
.first::<GitHubUser>(conn);
let initiator = ok_or_continue!(initiator, why =>
error!("Unable to retrieve poll initiator for poll id {}: {:?}",
survey.id, why));
let issue = issue::table.find(survey.fk_issue).first::<Issue>(conn);
let issue = ok_or_continue!(issue, why =>
error!("Unable to retrieve issue for poll {}: {:?}",
survey.id, why));
// check to see if any checkboxes were modified before we end up replacing the comment
ok_or_continue!(update_poll_response_status(survey.id), why =>
error!("Unable to update response status for poll {}: {:?}",
survey.id, why));
// get associated responses
let responses = ok_or_continue!(list_poll_response_requests(survey.id), why =>
error!("Unable to retrieve response requests for survey {}: {:?}",
survey.id, why));
// If everyone has answered the poll, close it:
if responses.iter().all(|(_, response)| response.responded) {
survey.poll_closed = true;
let update = diesel::update(poll.find(survey.id))
.set(&survey)
.execute(conn);
ok_or_continue!(update, why =>
error!("Unable to close poll {}: {:?}", survey.id, why));
}
// update existing status comment with responses & concerns
let status_comment = RfcBotComment::new(
&issue,
CommentType::QuestionAsked {
initiator: &initiator,
respondents: &responses,
question: &survey.poll_question,
teams: survey.poll_teams.split(',').collect(),
},
);
let previous_comment: IssueComment = issuecomment
.filter(issuecomment_id.eq(survey.fk_bot_tracking_comment))
.first(conn)?;
if previous_comment.body != status_comment.body {
// if the comment body in the database equals the new one we generated, then no change
// is needed from github (this assumes our DB accurately reflects GH's, which should
// be true in most cases by the time this is called)
let post = status_comment.post(Some(survey.fk_bot_tracking_comment));
ok_or_continue!(post, why =>
error!("Unable to update status comment for poll {}: {:?}",
survey.id, why));
}
}
Ok(())
}
fn evaluate_pendings() -> DashResult<()> {
use crate::domain::schema::fcp_proposal::dsl::*;
use crate::domain::schema::issuecomment::dsl::id as issuecomment_id;
use crate::domain::schema::issuecomment::dsl::*;
use diesel::prelude::*;
let conn = &*DB_POOL.get()?;
// first process all "pending" proposals (unreviewed or remaining concerns)
let pending = fcp_proposal
.filter(fcp_start.is_null())
.load::<FcpProposal>(conn);
let pending_proposals = ok_or!(pending, why => {
error!("Unable to retrieve list of pending proposals: {:?}", why);
throw!(why)
});
for mut proposal in pending_proposals {
let initiator = githubuser::table
.find(proposal.fk_initiator)
.first::<GitHubUser>(conn);
let initiator = ok_or_continue!(initiator, why =>
error!("Unable to retrieve proposal initiator for proposal id {}: {:?}",
proposal.id, why));
let issue = issue::table.find(proposal.fk_issue).first::<Issue>(conn);
let issue = ok_or_continue!(issue, why =>
error!("Unable to retrieve issue for proposal {}: {:?}",
proposal.id, why));
// if the issue has been closed before an FCP starts,
// then we just need to cancel the FCP entirely
if !issue.open {
ok_or_continue!(cancel_fcp(&initiator, &issue, &proposal), why =>
error!("Unable to cancel FCP for proposal {}: {:?}",
proposal.id, why));
}
// check to see if any checkboxes were modified before we end up replacing the comment
ok_or_continue!(update_proposal_review_status(proposal.id), why =>
error!("Unable to update review status for proposal {}: {:?}",
proposal.id, why));
// get associated concerns and reviews
let reviews = ok_or_continue!(list_review_requests(proposal.id), why =>
error!("Unable to retrieve review requests for proposal {}: {:?}",
proposal.id, why));
let concerns = ok_or_continue!(list_concerns_with_authors(proposal.id),
why => error!("Unable to retrieve concerns for proposal {}: {:?}",
proposal.id, why));
let num_outstanding_reviews = reviews.iter().filter(|&&(_, ref r)| !r.reviewed).count();
let num_complete_reviews = reviews.len() - num_outstanding_reviews;
let num_active_concerns = concerns
.iter()
.filter(|&&(_, ref c)| c.fk_resolved_comment.is_none())
.count();
// update existing status comment with reviews & concerns
let status_comment = RfcBotComment::new(
&issue,
CommentType::FcpProposed(
&initiator,
FcpDisposition::from_str(&proposal.disposition)?,
&reviews,
&concerns,
),
);
let previous_comment: IssueComment = issuecomment
.filter(issuecomment_id.eq(proposal.fk_bot_tracking_comment))
.first(conn)?;
if previous_comment.body != status_comment.body {
// if the comment body in the database equals the new one we generated, then no change
// is needed from github (this assumes our DB accurately reflects GH's, which should
// be true in most cases by the time this is called)
let post = status_comment.post(Some(proposal.fk_bot_tracking_comment));
ok_or_continue!(post, why =>
error!("Unable to update status comment for proposal {}: {:?}",
proposal.id, why));
}
let majority_complete = num_outstanding_reviews < num_complete_reviews;
if num_active_concerns == 0 && majority_complete && num_outstanding_reviews < 3 {
// TODO only record the fcp as started if we know that we successfully commented
// i.e. either the comment claims to have posted, or we get a comment back to reconcile
// FCP can start now -- update the database
proposal.fcp_start = Some(Utc::now().naive_utc());
let update = diesel::update(fcp_proposal.find(proposal.id))
.set(&proposal)
.execute(conn);
ok_or_continue!(update, why =>
error!("Unable to mark FCP {} as started: {:?}",
proposal.id, why));
// attempt to add the final-comment-period label
// TODO only add label if FCP > 1 day
use crate::config::CONFIG;
if CONFIG.post_comments {
let label_res = issue.add_label(Label::FCP);
issue.remove_label(Label::PFCP);
let added_label = match label_res {
Ok(()) => true,
Err(why) => {
warn!(
"Unable to add FCP label to {}#{}: {:?}",
&issue.repository, issue.number, why
);
false
}
};
let comment_type = CommentType::FcpAllReviewedNoConcerns {
added_label,
author: &initiator,
status_comment_id: proposal.fk_bot_tracking_comment,
};
// leave a comment for FCP start
let fcp_start_comment = RfcBotComment::new(&issue, comment_type);
ok_or_continue!(fcp_start_comment.post(None), why =>
error!("Unable to post comment for FCP {}'s start: {:?}",
proposal.id, why));
}
}
}
Ok(())
}
fn evaluate_ffcps() -> DashResult<()> {
use crate::domain::schema::fcp_proposal::dsl::*;
use diesel::prelude::*;
let conn = &*DB_POOL.get()?;
// look for any FCP proposals that entered FCP a week or more ago but aren't marked as closed
let one_business_week_ago = Utc::now().naive_utc() - Duration::days(10);
let ffcps = fcp_proposal
.filter(fcp_start.le(one_business_week_ago))
.filter(fcp_closed.eq(false))
.load::<FcpProposal>(conn);
let finished_fcps = ok_or!(ffcps, why => {
error!("Unable to retrieve FCPs that need to be marked as finished: {:?}",
why);
throw!(why);
});
for mut proposal in finished_fcps {
let initiator = githubuser::table
.find(proposal.fk_initiator)
.first::<GitHubUser>(conn);
let initiator = ok_or_continue!(initiator, why =>
error!("Unable to retrieve proposal initiator for proposal id {}: {:?}",
proposal.id,
why));
let issue = issue::table.find(proposal.fk_issue).first::<Issue>(conn);
let issue = ok_or_continue!(issue, why =>
error!("Unable to find issue to match proposal {}: {:?}",
proposal.id, why));
// TODO only update the db if the comment posts, but reconcile if we find out it worked
// update the fcp
proposal.fcp_closed = true;
let update_fcp = diesel::update(fcp_proposal.find(proposal.id))
.set(&proposal)
.execute(conn);
ok_or_continue!(update_fcp, why =>
error!("Unable to update FCP {}: {:?}", proposal.id, why));
// parse the disposition:
let disp = FcpDisposition::from_str(&proposal.disposition)?;
// Add FFCP label and remove FCP label.
let label_res = issue.add_label(Label::FFCP);
issue.remove_label(Label::FCP);
let added_label = match label_res {
Ok(_) => {
if let Err(why) = issue.add_label(Label::ToAnnounce) {
warn!(
"Unable to add to-announce label to {}#{}: {:?}",
&issue.repository, issue.number, why
);
}
true
}
Err(why) => {
warn!(
"Unable to add Finished-FCP label to {}#{}: {:?}",
&issue.repository, issue.number, why
);
false
}
};
// Build the comment:
let comment_type = CommentType::FcpWeekPassed {
added_label,
author: &initiator,
status_comment_id: proposal.fk_bot_tracking_comment,
disposition: disp,
};
let fcp_close_comment = RfcBotComment::new(&issue, comment_type);
// Post it!
ok_or_continue!(fcp_close_comment.post(None), why =>
error!("Unable to post FCP-ending comment for proposal {}: {:?}",
proposal.id, why));
execute_ffcp_actions(&issue, disp);
}
Ok(())
}
fn can_ffcp_close(issue: &Issue) -> bool {
SETUP
.read()
.unwrap()
.should_ffcp_auto_close(&issue.repository)
}
fn can_ffcp_postpone(issue: &Issue) -> bool {
SETUP
.read()
.unwrap()
.should_ffcp_auto_postpone(&issue.repository)
}
fn execute_ffcp_actions(issue: &Issue, disposition: FcpDisposition) {
match disposition {
FcpDisposition::Merge => {
// TODO: This one will require a lot of work to
// auto-merge RFCs and create the tracking issue.
}
FcpDisposition::Close if can_ffcp_close(issue) => {
let _ = issue.add_label(Label::Closed);
issue.remove_label(Label::DispositionClose);
issue.close();
}
FcpDisposition::Postpone if can_ffcp_postpone(issue) => {
let _ = issue.add_label(Label::Postponed);
issue.remove_label(Label::DispositionPostpone);
issue.close();
}
_ => {}
}
}
fn list_review_requests(proposal_id: i32) -> DashResult<Vec<(GitHubUser, FcpReviewRequest)>> {
let conn = &*DB_POOL.get()?;
let reviews = fcp_review_request::table
.filter(fcp_review_request::fk_proposal.eq(proposal_id))
.load::<FcpReviewRequest>(conn)?;
let mut w_reviewers = Vec::with_capacity(reviews.len());
for review in reviews {
let initiator = githubuser::table
.filter(githubuser::id.eq(review.fk_reviewer))
.first::<GitHubUser>(conn)?;
w_reviewers.push((initiator, review));
}
w_reviewers.sort_by(|a, b| a.0.login.cmp(&b.0.login));
Ok(w_reviewers)
}
fn list_poll_response_requests(poll_id: i32) -> DashResult<Vec<(GitHubUser, PollResponseRequest)>> {
let conn = &*DB_POOL.get()?;
let reviews = poll_response_request::table
.filter(poll_response_request::fk_poll.eq(poll_id))
.load::<PollResponseRequest>(conn)?;
let mut w_reviewers = Vec::with_capacity(reviews.len());
for review in reviews {
let initiator = githubuser::table
.filter(githubuser::id.eq(review.fk_respondent))
.first::<GitHubUser>(conn)?;
w_reviewers.push((initiator, review));
}
w_reviewers.sort_by(|a, b| a.0.login.cmp(&b.0.login));
Ok(w_reviewers)
}
fn list_concerns_with_authors(proposal_id: i32) -> DashResult<Vec<(GitHubUser, FcpConcern)>> {
let conn = &*DB_POOL.get()?;
let concerns = fcp_concern::table
.filter(fcp_concern::fk_proposal.eq(proposal_id))
.order(fcp_concern::name)
.load::<FcpConcern>(conn)?;
let mut w_authors = Vec::with_capacity(concerns.len());
for concern in concerns {
let initiator = githubuser::table
.filter(githubuser::id.eq(concern.fk_initiator))
.first::<GitHubUser>(conn)?;
w_authors.push((initiator, concern));
}
Ok(w_authors)
}
fn resolve_applicable_feedback_requests(
author: &GitHubUser,
issue: &Issue,
comment: &IssueComment,
) -> DashResult<()> {
use crate::domain::schema::rfc_feedback_request::dsl::*;
let conn = &*DB_POOL.get()?;
// check for an open feedback request, close since no longer applicable
let existing_request = rfc_feedback_request
.filter(fk_requested.eq(author.id))
.filter(fk_issue.eq(issue.id))
.first::<FeedbackRequest>(conn)
.optional()?;
if let Some(mut request) = existing_request {
request.fk_feedback_comment = Some(comment.id);
diesel::update(rfc_feedback_request.find(request.id))
.set(&request)
.execute(conn)?;
}
Ok(())
}
fn resolve_logins_to_users(member_logins: &[String]) -> DashResult<Vec<GitHubUser>> {
use diesel::pg::expression::dsl::any;
let conn = &*DB_POOL.get()?;
// resolve each member into an actual user
let users = githubuser::table
.filter(githubuser::login.eq(any(member_logins)))
.order(githubuser::login)
.load::<GitHubUser>(conn)?;
Ok(users)
}
/// Check if an issue comment is written by a member of one of the subteams
/// satisfying the given predicate.
fn specific_subteam_members<F>(included: F) -> DashResult<Vec<GitHubUser>>
where
F: Fn(&String) -> bool,
{
let setup = SETUP.read().unwrap();
let teams = setup.teams();
let members = teams
.filter(|&(label, _)| included(&label.0))
.flat_map(|(_, team)| team.member_logins().map(std::string::ToString::to_string))
.collect::<BTreeSet<_>>()
.into_iter() // diesel won't work with btreeset, and dedup has weird lifetime errors
.collect::<Vec<_>>();
resolve_logins_to_users(&members)
}
/// Return a list of all known team members.
fn all_team_members() -> DashResult<Vec<GitHubUser>> { specific_subteam_members(|_| true) }
/// Check if an issue comment is written by a member of one of the subteams
/// labelled on the issue.
fn subteam_members(issue: &Issue) -> DashResult<Vec<GitHubUser>> {
// retrieve all of the teams tagged on this issue
specific_subteam_members(|label| issue.labels.contains(&label))
}
fn cancel_fcp(author: &GitHubUser, issue: &Issue, existing: &FcpProposal) -> DashResult<()> {
use crate::domain::schema::fcp_proposal::dsl::*;
let conn = &*DB_POOL.get()?;
// if exists delete FCP with associated concerns, reviews, feedback requests
// db schema has ON DELETE CASCADE
diesel::delete(fcp_proposal.filter(id.eq(existing.id))).execute(conn)?;
// leave github comment stating that FCP proposal cancelled
let comment = RfcBotComment::new(issue, CommentType::FcpProposalCancelled(author));
let _ = comment.post(None);
[
Label::FCP,
Label::PFCP,
Label::DispositionMerge,
Label::DispositionClose,
Label::DispositionPostpone,
]
.iter()
.for_each(|&lab| issue.remove_label(lab));
Ok(())
}
fn existing_proposal(issue: &Issue) -> DashResult<Option<FcpProposal>> {
use crate::domain::schema::fcp_proposal::dsl::*;
let conn = &*DB_POOL.get()?;
Ok(fcp_proposal
.filter(fk_issue.eq(issue.id))
.first::<FcpProposal>(conn)
.optional()?)
}
fn post_insert_comment(issue: &Issue, comment: CommentType<'_>) -> DashResult<IssueComment> {
let conn = &*DB_POOL.get()?;
let comment = RfcBotComment::new(issue, comment);
let comment = comment.post(None)?;
info!("Posted base comment to github, no reviewers listed yet");
// at this point our new comment doesn't yet exist in the database, so
// we need to insert it
let comment = comment.with_repo(&issue.repository)?;
if let Err(why) = diesel::insert_into(issuecomment::table)
.values(&comment)
.execute(conn)
{
warn!(
"issue inserting new record, maybe received webhook for it: {:?}",
why
);
}
Ok(comment)
}
impl<'a> RfcBotCommand<'a> {
pub fn process(
self,
author: &GitHubUser,
issue: &Issue,
comment: &IssueComment,
team_members: &[GitHubUser],
) -> DashResult<()> {
use self::RfcBotCommand::*;
match self {
StartPoll { teams, question } => process_poll(author, issue, comment, question, teams),
FcpPropose(disp) => process_fcp_propose(author, issue, comment, team_members, disp),
FcpCancel => process_fcp_cancel(author, issue),
Reviewed => process_reviewed(author, issue),
NewConcern(concern_name) => process_new_concern(author, issue, comment, concern_name),
ResolveConcern(concern_name) => {
process_resolve_concern(author, issue, comment, concern_name)
}
FeedbackRequest(username) => process_feedback_request(author, issue, username),
}
}
}
fn process_poll(
author: &GitHubUser,
issue: &Issue,
comment: &IssueComment,
question: &str,
teams: BTreeSet<&str>,
) -> DashResult<()> {
use crate::domain::schema::poll::dsl::*;
let conn = &*DB_POOL.get()?;
let tmp_teams;
let teams = if teams.is_empty() {
let setup = SETUP.read().unwrap();
tmp_teams = setup
.teams()
.filter(|&(label, _)| issue.labels.contains(&label.0))
.map(|(label, _)| label.0.clone())
.collect::<BTreeSet<_>>();
tmp_teams.iter().map(std::string::String::as_str).collect()
} else {
teams
};
let members = specific_subteam_members(|l| teams.contains(&**l))?;
info!("adding a new poll to issue.");
// leave github comment stating that question is asked, ping respondents
let gh_comment = post_insert_comment(
issue,
CommentType::QuestionAsked {
initiator: author,
teams: teams.clone(),
question,
respondents: &[],
},
)?;
let teams_str = Itertools::intersperse(teams.iter().cloned(), ",").collect::<String>();
let new_poll = NewPoll {
fk_issue: issue.id,
fk_initiator: author.id,
fk_initiating_comment: comment.id,
fk_bot_tracking_comment: gh_comment.id,
poll_question: question,
poll_created_at: Utc::now().naive_utc(),
poll_closed: false,
poll_teams: &*teams_str,
};
let new_poll = diesel::insert_into(poll)
.values(&new_poll)
.get_result::<Poll>(conn)?;
debug!("poll inserted into the database");
// generate response requests for all relevant subteam members
let response_requests = members
.iter()
.map(|member| NewPollResponseRequest {
fk_poll: new_poll.id,
fk_respondent: member.id,
// let's assume the initiator has answered it
responded: member.id == author.id,
})
.collect::<Vec<_>>();
diesel::insert_into(poll_response_request::table)
.values(&response_requests)
.execute(conn)?;
// they're in the database, but now we need them paired with githubuser
let response_requests = list_poll_response_requests(new_poll.id)?;
debug!("poll response requests inserted into the database");
// we have all of the review requests, generate a new comment and post it
let new_gh_comment = RfcBotComment::new(
issue,
CommentType::QuestionAsked {
initiator: author,
teams,
question,
respondents: &*response_requests,
},
);
new_gh_comment.post(Some(gh_comment.id))?;
debug!("github comment updated with poll respondents");
Ok(())
}
fn process_fcp_propose(
author: &GitHubUser,
issue: &Issue,
comment: &IssueComment,
team_members: &[GitHubUser],
disp: FcpDisposition,
) -> DashResult<()> {
debug!("processing fcp proposal: {:?}", disp);
use crate::domain::schema::fcp_proposal::dsl::*;
if existing_proposal(issue)?.is_none() {
let conn = &*DB_POOL.get()?;
// if not exists, create new FCP proposal
info!("proposal is a new FCP, creating...");
// leave github comment stating that FCP is proposed, ping reviewers
let gh_comment =
post_insert_comment(issue, CommentType::FcpProposed(author, disp, &[], &[]))?;
let proposal = NewFcpProposal {
fk_issue: issue.id,
fk_initiator: author.id,
fk_initiating_comment: comment.id,
fk_bot_tracking_comment: gh_comment.id,
disposition: disp.repr(),
fcp_start: None,
fcp_closed: false,
};
let proposal = diesel::insert_into(fcp_proposal)
.values(&proposal)
.get_result::<FcpProposal>(conn)?;
debug!("proposal inserted into the database");
// generate review requests for all relevant subteam members
let review_requests = team_members
.iter()
.map(|member| NewFcpReviewRequest {
fk_proposal: proposal.id,
fk_reviewer: member.id,
// let's assume the initiator has reviewed it
reviewed: member.id == author.id,
})
.collect::<Vec<_>>();
diesel::insert_into(fcp_review_request::table)
.values(&review_requests)
.execute(conn)?;
// they're in the database, but now we need them paired with githubuser
let review_requests = list_review_requests(proposal.id)?;
debug!("review requests inserted into the database");
// we have all of the review requests, generate a new comment and post it
let new_gh_comment = RfcBotComment::new(
issue,
CommentType::FcpProposed(author, disp, &review_requests, &[]),
);
new_gh_comment.post(Some(gh_comment.id))?;
debug!("github comment updated with reviewers");
}
Ok(())
}
fn process_fcp_cancel(author: &GitHubUser, issue: &Issue) -> DashResult<()> {
if let Some(existing) = existing_proposal(issue)? {
cancel_fcp(author, issue, &existing)?;
}
Ok(())
}
fn process_reviewed(author: &GitHubUser, issue: &Issue) -> DashResult<()> {
// set a reviewed entry for the comment author on this issue
if let Some(proposal) = existing_proposal(issue)? {
use crate::domain::schema::fcp_review_request::dsl::*;
let conn = &*DB_POOL.get()?;
let review_request = fcp_review_request
.filter(fk_proposal.eq(proposal.id))
.filter(fk_reviewer.eq(author.id))
.first::<FcpReviewRequest>(conn)
.optional()?;
if let Some(mut review_request) = review_request {
// store an FK to the comment marking for review (not null fk here means
// reviewed)
review_request.reviewed = true;
diesel::update(fcp_review_request.find(review_request.id))
.set(&review_request)
.execute(conn)?;
}
}
Ok(())
}
fn process_new_concern(
author: &GitHubUser,
issue: &Issue,
comment: &IssueComment,
concern_name: &str,
) -> DashResult<()> {
if let Some(mut proposal) = existing_proposal(issue)? {
// check for existing concern
use crate::domain::schema::fcp_concern::dsl::*;
use crate::domain::schema::fcp_proposal::dsl::*;
let conn = &*DB_POOL.get()?;
let existing_concern = fcp_concern
.filter(fk_proposal.eq(proposal.id))
.filter(name.eq(concern_name))
.first::<FcpConcern>(conn)
.optional()?;
if existing_concern.is_none() {
// if not exists, create new concern with this author as creator
let new_concern = NewFcpConcern {
fk_proposal: proposal.id,
fk_initiator: author.id,
fk_resolved_comment: None,
name: concern_name,
fk_initiating_comment: comment.id,
};
diesel::insert_into(fcp_concern)