1+ <?php
2+
3+ namespace Continuous \Cli \Command \Project ;
4+
5+ use Continuous \Cli \Command \CommandAbstract ;
6+ use Continuous \Sdk \Collection ;
7+ use Continuous \Sdk \Entity \Project ;
8+ use Symfony \Component \Console \Helper \QuestionHelper ;
9+ use Symfony \Component \Console \Helper \Table ;
10+ use Symfony \Component \Console \Input \InputInterface ;
11+ use Symfony \Component \Console \Input \InputOption ;
12+ use Symfony \Component \Console \Output \OutputInterface ;
13+ use Symfony \Component \Console \Question \ConfirmationQuestion ;
14+
15+ class ProjectResetHooksCommand extends CommandAbstract
16+ {
17+ protected function configure ()
18+ {
19+ $ this
20+ ->setName ('project:reset-hooks ' )
21+ ->setDescription ('reset provider hooks. (YOU MUST HAVE WRITE ACCESS TO RESET HOOKS) ' )
22+ ->setHelp ('This command help you to reset the provider hooks and ssh key on the repository configured with ContinuousPHP. ' )
23+ ;
24+
25+ $ this
26+ ->addOption (
27+ 'filter-name ' ,
28+ null ,
29+ InputOption::VALUE_OPTIONAL ,
30+ 'filter apply on name of repositories result '
31+ );
32+ }
33+
34+ /**
35+ * @param InputInterface $input
36+ * @param OutputInterface $output
37+ */
38+ protected function execute (InputInterface $ input , OutputInterface $ output )
39+ {
40+ parent ::execute ($ input , $ output );
41+
42+ $ filterName = $ input ->getOption ('filter-name ' );
43+ $ this ->showLoader ($ output , 'Loading projects from providers (github, bitbucket, gitlab)... ' );
44+
45+ /** @var Collection $collection */
46+ $ collection = $ this ->continuousClient ->getProjects ();
47+ $ rows = [];
48+
49+ /** @var Project[] $projects */
50+ $ projects = [];
51+
52+ $ this ->hideLoader ($ output );
53+
54+ foreach ($ collection as $ id => $ project ) {
55+ $ name = $ project ->get ('name ' );
56+
57+ if (!$ project ->get ('canEditSettings ' )) {
58+ continue ;
59+ }
60+
61+ if (null !== $ filterName && false === strpos (strtolower ($ name ), strtolower ($ filterName ))) {
62+ continue ;
63+ }
64+
65+ $ projects [] = $ project ;
66+
67+ $ rows [] = [
68+ $ project ->getProvider ()->get ('name ' ),
69+ $ name ,
70+ ];
71+ }
72+
73+ $ table = new Table ($ output );
74+ $ table
75+ ->setHeaders (['Provider ' , 'Name ' ])
76+ ->setRows ($ rows )
77+ ->render ()
78+ ;
79+
80+ $ question = new ConfirmationQuestion (
81+ 'Confirm reset hooks for ALL the repositories listed? [Y/n] ' ,
82+ true ,
83+ '/^(y|yes|oui)/i '
84+ );
85+ $ ack = new QuestionHelper ();
86+
87+ if (!$ ack ->ask ($ input , $ output , $ question )) {
88+ return ;
89+ }
90+
91+ $ this ->showLoader ($ output , 'Reset hooks in progress... ' , count ($ projects ));
92+
93+ foreach ($ projects as $ project ) {
94+ $ params = [
95+ 'provider ' => static ::mapProviderToSdk ($ project ->getProvider ()->get ('name ' )),
96+ 'repository ' => $ project ->get ('name ' ),
97+ ];
98+ $ this ->continuousClient ->resetWebHooks ($ params );
99+ $ this ->loader ->advance ();
100+ }
101+
102+ $ this ->hideLoader ($ output );
103+ }
104+ }
0 commit comments