2017-10-04 22:40:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PHPCensor\Command;
|
|
|
|
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Model\Project;
|
|
|
|
use PHPCensor\Service\BuildService;
|
|
|
|
use PHPCensor\Store\BuildStore;
|
|
|
|
use PHPCensor\Store\ProjectStore;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Schedules build command - creates a build for a project if it hasn't run for a specified time
|
|
|
|
*
|
|
|
|
* @author Vincent Vermeulen <vincent@redant.nl>
|
|
|
|
*/
|
|
|
|
class ScheduleBuildCommand extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ProjectStore
|
|
|
|
*/
|
|
|
|
protected $projectStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BuildStore
|
|
|
|
*/
|
|
|
|
protected $buildStore;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var BuildService
|
|
|
|
*/
|
|
|
|
protected $buildService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ProjectStore $projectStore
|
|
|
|
* @param BuildStore $buildStore
|
|
|
|
* @param BuildService $buildService
|
|
|
|
*/
|
|
|
|
public function __construct(ProjectStore $projectStore, BuildStore $buildStore, BuildService $buildService)
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->projectStore = $projectStore;
|
|
|
|
$this->buildService = $buildService;
|
2017-10-08 16:41:32 +02:00
|
|
|
$this->buildStore = $buildStore;
|
2017-10-04 22:40:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('php-censor:schedule-build')
|
|
|
|
->setDescription('Schedules a build for active projects which have not been ran by X days')
|
|
|
|
->addArgument('days', InputArgument::REQUIRED, 'Since specified days');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2017-10-08 16:41:32 +02:00
|
|
|
$sinceDays = $input->getArgument('days');
|
|
|
|
$date = new \DateTime('now');
|
2017-10-04 22:40:29 +02:00
|
|
|
$difference = new \DateInterval("P{$sinceDays}D");
|
|
|
|
$date->sub($difference);
|
|
|
|
|
2017-10-11 13:55:03 +02:00
|
|
|
$projects = $this->projectStore->getAll();
|
|
|
|
$projects = $projects['items'];
|
2017-10-04 22:40:29 +02:00
|
|
|
/** @var Project $project */
|
|
|
|
foreach ($projects as $project) {
|
2017-10-05 17:43:33 +02:00
|
|
|
$latestBuild = $this->buildStore->getLatestBuilds($project->getId(), 1);
|
|
|
|
|
2017-10-08 16:41:32 +02:00
|
|
|
if ($latestBuild) {
|
2017-10-04 22:40:29 +02:00
|
|
|
/** @var Build $build */
|
2017-10-08 16:41:32 +02:00
|
|
|
$build = $latestBuild[0];
|
|
|
|
$status = (integer)$build->getStatus();
|
|
|
|
if ($status === Build::STATUS_RUNNING || $status === Build::STATUS_PENDING) {
|
2017-10-04 22:40:29 +02:00
|
|
|
// If it's running or just created, we don't want to reschedule already.
|
|
|
|
continue;
|
|
|
|
}
|
2017-10-15 16:58:36 +02:00
|
|
|
if ($date < $build->getFinishDate()) {
|
2017-10-04 22:40:29 +02:00
|
|
|
// If finished date is newer then the specified since days, we don't want to reschedule
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2017-10-08 16:41:32 +02:00
|
|
|
|
2017-10-04 22:40:29 +02:00
|
|
|
try {
|
2017-04-13 17:00:29 +02:00
|
|
|
$this->buildService->createBuild($project, null, '', null, null, null, null, Build::SOURCE_PERIODICAL);
|
2017-10-04 22:40:29 +02:00
|
|
|
$output->writeln("Build Created for {$project->getTitle()}");
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$output->writeln('<error>Failed</error>');
|
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|