Skip to content

Commit 2e234a6

Browse files
authored
feat(reset-webhooks) (#17)
1 parent 74b00b4 commit 2e234a6

File tree

3 files changed

+108
-2
lines changed

3 files changed

+108
-2
lines changed

src/ApplicationFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Continuous\Cli\Command\Pipeline\PipelineExportCommand;
1212
use Continuous\Cli\Command\Package\PackageDownloadCommand;
1313
use Continuous\Cli\Command\Project\ProjectListCommand;
14+
use Continuous\Cli\Command\Project\ProjectResetHooksCommand;
1415
use Continuous\Cli\Command\Repository\RepositoryListCommand;
1516
use Symfony\Component\Console\Application;
1617

@@ -34,6 +35,7 @@ public function create()
3435
$application->add(new CompanyListCommand());
3536
$application->add(new RepositoryListCommand());
3637
$application->add(new ProjectListCommand());
38+
$application->add(new ProjectResetHooksCommand());
3739
$application->add(new BuildListCommand());
3840
$application->add(new BuildStartCommand());
3941
$application->add(new BuildStopCommand());

src/Command/CommandAbstract.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
9494
]);
9595
}
9696

97-
protected function showLoader($output, $message = '')
97+
protected function showLoader($output, $message = '', $max = 1)
9898
{
99-
$this->loader = new ProgressBar($output, 1);
99+
$this->loader = new ProgressBar($output, $max);
100100

101101
if ($message) {
102102
$this->loader->setFormatDefinition('custom', ' %current%/%max% -- %message%');
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)