Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions src/PHPCensor/Command/ScheduleBuildCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?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;
$this->buildStore = $buildStore;
}

/**
* {@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)
{
$sinceDays = $input->getArgument('days');
$date = new \DateTime('now');
$difference = new \DateInterval("P{$sinceDays}D");
$date->sub($difference);

$projects = $this->projectStore->getAll()['items'];
/** @var Project $project */
foreach ($projects as $project) {

$latestBuild = $this->buildStore->getLatestBuilds($project->getId(), 1);

if ($latestBuild['count'] > 0) {
/** @var Build $build */
$build = $latestBuild['items'][0];
if ((int)$build->getStatus() === 1 || (int)$build->getStatus() === 0) {
// If it's running or just created, we don't want to reschedule already.
continue;
}
if ($date < $build->getFinished()) {
// If finished date is newer then the specified since days, we don't want to reschedule
continue;
}
}
try {
$this->buildService->createBuild($project, null);
$output->writeln("Build Created for {$project->getTitle()}");
} catch (\Exception $e) {
$output->writeln('<error>Failed</error>');
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
}
}
}
}
2 changes: 2 additions & 0 deletions src/PHPCensor/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPCensor\Command\RebuildCommand;
use PHPCensor\Command\RebuildQueueCommand;
use PHPCensor\Command\RunCommand;
use PHPCensor\Command\ScheduleBuildCommand;
use PHPCensor\Command\WorkerCommand;
use PHPCensor\Logging\Handler;
use PHPCensor\Service\BuildService;
Expand Down Expand Up @@ -134,5 +135,6 @@ public function __construct($name = 'PHP Censor - Continuous Integration for PHP
$this->add(new CreateBuildCommand($projectStore, new BuildService($buildStore)));
$this->add(new WorkerCommand($logger));
$this->add(new RebuildQueueCommand($logger));
$this->add(new ScheduleBuildCommand($projectStore, $buildStore, new BuildService($buildStore)));
}
}