6
6
import functools
7
7
from . import comments
8
8
from . import utils
9
+ from .parse_issue_comment import parse_issue_comment
9
10
from .auth import verify as verify_auth
10
11
from .utils import lazy_debug
11
12
import logging
15
16
import sqlite3
16
17
import requests
17
18
from contextlib import contextmanager
18
- from itertools import chain
19
19
from queue import Queue
20
20
import os
21
21
import sys
@@ -469,28 +469,20 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
469
469
my_username ,
470
470
)
471
471
472
- words = list ( chain . from_iterable ( re . findall ( r'\S+' , x ) for x in body . splitlines () if '@' + my_username in x )) # noqa
473
- if words [ 1 :] == [ "are" , "you" , "still" , "there?" ] and realtime :
474
- state . add_comment (
475
- ":cake: {} \n \n " . format (
476
- random . choice ( PORTAL_TURRET_DIALOG ), PORTAL_TURRET_IMAGE )
477
- )
478
- for i , word in reversed ( list ( enumerate ( words ))) :
472
+ hooks = []
473
+ if 'hooks' in global_cfg :
474
+ hooks = list ( global_cfg [ 'hooks' ]. keys ())
475
+
476
+ commands = parse_issue_comment ( username , body , sha , my_username , hooks )
477
+
478
+ for command in commands :
479
479
found = True
480
- if word == 'r+' or word . startswith ( 'r=' ) :
480
+ if command . action == 'approve' :
481
481
if not _reviewer_auth_verified ():
482
482
continue
483
483
484
- if not sha and i + 1 < len (words ):
485
- cur_sha = sha_or_blank (words [i + 1 ])
486
- else :
487
- cur_sha = sha
488
-
489
- approver = word [len ('r=' ):] if word .startswith ('r=' ) else username
490
-
491
- # Ignore "r=me"
492
- if approver == 'me' :
493
- continue
484
+ approver = command .actor
485
+ cur_sha = command .commit
494
486
495
487
# Ignore WIP PRs
496
488
is_wip = False
@@ -582,7 +574,7 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
582
574
)
583
575
state .change_labels (LabelEvent .APPROVED )
584
576
585
- elif word == 'r- ' :
577
+ elif command . action == 'unapprove ' :
586
578
if not verify_auth (username , repo_label , repo_cfg , state ,
587
579
AuthState .REVIEWER , realtime , my_username ):
588
580
continue
@@ -592,14 +584,12 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
592
584
if realtime :
593
585
state .change_labels (LabelEvent .REJECTED )
594
586
595
- elif word . startswith ( 'p=' ) :
587
+ elif command . action == 'prioritize' :
596
588
if not verify_auth (username , repo_label , repo_cfg , state ,
597
589
AuthState .TRY , realtime , my_username ):
598
590
continue
599
- try :
600
- pvalue = int (word [len ('p=' ):])
601
- except ValueError :
602
- continue
591
+
592
+ pvalue = command .priority
603
593
604
594
if pvalue > global_cfg ['max_priority' ]:
605
595
if realtime :
@@ -611,12 +601,12 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
611
601
state .priority = pvalue
612
602
state .save ()
613
603
614
- elif word . startswith ( 'delegate=' ) :
604
+ elif command . action == 'delegate' :
615
605
if not verify_auth (username , repo_label , repo_cfg , state ,
616
606
AuthState .REVIEWER , realtime , my_username ):
617
607
continue
618
608
619
- state .delegate = word [ len ( 'delegate=' ):]
609
+ state .delegate = command . delegate_to
620
610
state .save ()
621
611
622
612
if realtime :
@@ -625,14 +615,14 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
625
615
.format (state .delegate )
626
616
)
627
617
628
- elif word == 'delegate- ' :
618
+ elif command . action == 'undelegate ' :
629
619
# TODO: why is this a TRY?
630
620
if not _try_auth_verified ():
631
621
continue
632
622
state .delegate = ''
633
623
state .save ()
634
624
635
- elif word == 'delegate+ ' :
625
+ elif command . action == 'delegate-author ' :
636
626
if not _reviewer_auth_verified ():
637
627
continue
638
628
@@ -645,7 +635,7 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
645
635
.format (state .delegate )
646
636
)
647
637
648
- elif word == 'retry' and realtime :
638
+ elif command . action == 'retry' and realtime :
649
639
if not _try_auth_verified ():
650
640
continue
651
641
state .set_status ('' )
@@ -654,7 +644,7 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
654
644
state .record_retry_log (command_src , body )
655
645
state .change_labels (event )
656
646
657
- elif word in ['try' , 'try- ' ] and realtime :
647
+ elif command . action in ['try' , 'untry ' ] and realtime :
658
648
if not _try_auth_verified ():
659
649
continue
660
650
if state .status == '' and state .approved_by :
@@ -665,7 +655,7 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
665
655
)
666
656
continue
667
657
668
- state .try_ = word == 'try'
658
+ state .try_ = command . action == 'try'
669
659
670
660
state .merge_sha = ''
671
661
state .init_build_res ([])
@@ -676,14 +666,14 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
676
666
# any meaningful labeling events.
677
667
state .change_labels (LabelEvent .TRY )
678
668
679
- elif word in WORDS_TO_ROLLUP :
669
+ elif command . action == 'rollup' :
680
670
if not _try_auth_verified ():
681
671
continue
682
- state .rollup = WORDS_TO_ROLLUP [ word ]
672
+ state .rollup = command . rollup_value
683
673
684
674
state .save ()
685
675
686
- elif word == 'force' and realtime :
676
+ elif command . action == 'force' and realtime :
687
677
if not _try_auth_verified ():
688
678
continue
689
679
if 'buildbot' in repo_cfg :
@@ -712,61 +702,58 @@ def parse_commands(body, username, repo_label, repo_cfg, state, my_username,
712
702
':bomb: Buildbot returned an error: `{}`' .format (err )
713
703
)
714
704
715
- elif word == 'clean' and realtime :
705
+ elif command . action == 'clean' and realtime :
716
706
if not _try_auth_verified ():
717
707
continue
718
708
state .merge_sha = ''
719
709
state .init_build_res ([])
720
710
721
711
state .save ()
722
- elif (word == 'hello?' or word == 'ping' ) and realtime :
723
- state .add_comment (":sleepy: I'm awake I'm awake" )
724
- elif word .startswith ('treeclosed=' ):
712
+
713
+ elif command .action == 'ping' and realtime :
714
+ if command .ping_type == 'portal' :
715
+ state .add_comment (
716
+ ":cake: {}\n \n " .format (
717
+ random .choice (PORTAL_TURRET_DIALOG ),
718
+ PORTAL_TURRET_IMAGE )
719
+ )
720
+ else :
721
+ state .add_comment (":sleepy: I'm awake I'm awake" )
722
+
723
+ elif command .action == 'treeclosed' :
725
724
if not _reviewer_auth_verified ():
726
725
continue
727
- try :
728
- treeclosed = int (word [len ('treeclosed=' ):])
729
- state .change_treeclosed (treeclosed , command_src )
730
- except ValueError :
731
- pass
726
+ state .change_treeclosed (command .treeclosed_value , command_src )
732
727
state .save ()
733
- elif word == 'treeclosed-' :
728
+
729
+ elif command .action == 'untreeclosed' :
734
730
if not _reviewer_auth_verified ():
735
731
continue
736
732
state .change_treeclosed (- 1 , None )
737
733
state .save ()
738
- elif 'hooks' in global_cfg :
739
- hook_found = False
740
- for hook in global_cfg ['hooks' ]:
741
- hook_cfg = global_cfg ['hooks' ][hook ]
742
- if hook_cfg ['realtime' ] and not realtime :
734
+
735
+ elif command .action == 'hook' :
736
+ hook = command .hook_name
737
+ hook_cfg = global_cfg ['hooks' ][hook ]
738
+ if hook_cfg ['realtime' ] and not realtime :
739
+ continue
740
+ if hook_cfg ['access' ] == "reviewer" :
741
+ if not _reviewer_auth_verified ():
743
742
continue
744
- if word == hook or word .startswith ('%s=' % hook ):
745
- if hook_cfg ['access' ] == "reviewer" :
746
- if not _reviewer_auth_verified ():
747
- continue
748
- else :
749
- if not _try_auth_verified ():
750
- continue
751
- hook_found = True
752
- extra_data = ""
753
- if word .startswith ('%s=' % hook ):
754
- extra_data = word .split ("=" )[1 ]
755
- Thread (
756
- target = handle_hook_response ,
757
- args = [state , hook_cfg , body , extra_data ]
758
- ).start ()
759
- if not hook_found :
760
- found = False
743
+ else :
744
+ if not _try_auth_verified ():
745
+ continue
746
+ Thread (
747
+ target = handle_hook_response ,
748
+ args = [state , hook_cfg , body , command .hook_extra ]
749
+ ).start ()
761
750
762
751
else :
763
752
found = False
764
753
765
754
if found :
766
755
state_changed = True
767
756
768
- words [i ] = ''
769
-
770
757
return state_changed
771
758
772
759
0 commit comments