Skip to content

Commit

Permalink
feat(reset-webhooks)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierozi committed Nov 5, 2018
1 parent 0194b2c commit 3a75735
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/ApplicationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Continuous\Cli\Command\Pipeline\PipelineExportCommand;
use Continuous\Cli\Command\Package\PackageDownloadCommand;
use Continuous\Cli\Command\Project\ProjectListCommand;
use Continuous\Cli\Command\Project\ProjectResetHooksCommand;
use Continuous\Cli\Command\Repository\RepositoryListCommand;
use Symfony\Component\Console\Application;

Expand All @@ -33,6 +34,7 @@ public function create()
$application->add(new CompanyListCommand());
$application->add(new RepositoryListCommand());
$application->add(new ProjectListCommand());
$application->add(new ProjectResetHooksCommand());
$application->add(new BuildListCommand());
$application->add(new BuildStartCommand());
$application->add(new BuildStopCommand());
Expand Down
4 changes: 2 additions & 2 deletions src/Command/CommandAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
]);
}

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

if ($message) {
$this->loader->setFormatDefinition('custom', ' %current%/%max% -- %message%');
Expand Down
104 changes: 104 additions & 0 deletions src/Command/Project/ProjectResetHooksCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace Continuous\Cli\Command\Project;

use Continuous\Cli\Command\CommandAbstract;
use Continuous\Sdk\Collection;
use Continuous\Sdk\Entity\Project;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class ProjectResetHooksCommand extends CommandAbstract
{
protected function configure()
{
$this
->setName('project:reset-hooks')
->setDescription('reset provider hooks. (YOU MUST HAVE WRITE ACCESS TO RESET HOOKS)')
->setHelp('This command help you to reset the provider hooks and ssh key on the repository configured with ContinuousPHP.')
;

$this
->addOption(
'filter-name',
null,
InputOption::VALUE_OPTIONAL,
'filter apply on name of repositories result'
);
}

/**
* @param InputInterface $input
* @param OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);

$filterName = $input->getOption('filter-name');
$this->showLoader($output, 'Loading projects from providers (github, bitbucket, gitlab)...');

/** @var Collection $collection */
$collection = $this->continuousClient->getProjects();
$rows = [];

/** @var Project[] $projects */
$projects = [];

$this->hideLoader($output);

foreach ($collection as $id => $project) {
$name = $project->get('name');

if (!$project->get('canEditSettings')) {
continue;
}

if (null !== $filterName && false === strpos(strtolower($name), strtolower($filterName))) {
continue;
}

$projects[] = $project;

$rows[] = [
$project->getProvider()->get('name'),
$name,
];
}

$table = new Table($output);
$table
->setHeaders(['Provider', 'Name'])
->setRows($rows)
->render()
;

$question = new ConfirmationQuestion(
'Confirm reset hooks for ALL the repositories listed? [Y/n]',
true,
'/^(y|yes|oui)/i'
);
$ack = new QuestionHelper();

if (!$ack->ask($input, $output, $question)) {
return;
}

$this->showLoader($output, 'Reset hooks in progress...', count($projects));

foreach ($projects as $project) {
$params = [
'provider' => static::mapProviderToSdk($project->getProvider()->get('name')),
'repository' => $project->get('name'),
];
$this->continuousClient->resetWebHooks($params);
$this->loader->advance();
}

$this->hideLoader($output);
}
}

0 comments on commit 3a75735

Please sign in to comment.