diff --git a/PHPCI/Command/PollCommand.php b/PHPCI/Command/PollCommand.php new file mode 100644 index 00000000..e6a1f0de --- /dev/null +++ b/PHPCI/Command/PollCommand.php @@ -0,0 +1,105 @@ + + * @package PHPCI + * @subpackage Console + */ +class PollCommand extends Command +{ + /** + * @var \Monolog\Logger + */ + protected $logger; + + public function __construct(Logger $logger, $name = null) + { + parent::__construct($name); + $this->logger = $logger; + } + + protected function configure() + { + $this + ->setName('phpci:poll-github') + ->setDescription('Poll github to check if we need to start a build.'); + } + + /** + * Pulls all pending builds from the database and runs them. + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $parser = new Parser(); + $yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml'); + $this->settings = $parser->parse($yaml); + + $token = $this->settings['phpci']['github']['token']; + + if(!$token) + { + $this->logger->error("No github token found"); + exit(); + } + + $buildStore = Factory::getStore('Build'); + + $this->logger->addInfo("Finding projects to poll"); + $projectStore = Factory::getStore('Project'); + $result = $projectStore->getWhere(); + $this->logger->addInfo(sprintf("Found %d projects", count($result['items']))); + + foreach ($result['items'] as $project) { + //$project = ProjectFactory::getProject($project); + + $http = new HttpClient('https://api.github.com'); + $commits = $http->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token)); + + $last_commit = $commits['body'][0]['sha']; + + $this->logger->info("Last commit to github for " . $project->getTitle() . " is " . $last_commit); + + if($project->getLastCommit() != $last_commit) + { + $this->logger->info("Last commit is different from database, adding new build for " . $project->getTitle()); + + $build = new Build(); + $build->setProjectId($project->getId()); + $build->setCommitId($last_commit); + $build->setStatus(0); + $build->setBranch($project->getType() === 'hg' ? 'default' : 'master'); + $build->setCreated(new \DateTime()); + + $buildStore->save($build); + + $project->setLastCommit($last_commit); + $projectStore->save($project); + } + } + + $this->logger->addInfo("Finished processing builds"); + } +} + diff --git a/PHPCI/Model/Base/ProjectBase.php b/PHPCI/Model/Base/ProjectBase.php index f23e1d7f..e3e12ecc 100644 --- a/PHPCI/Model/Base/ProjectBase.php +++ b/PHPCI/Model/Base/ProjectBase.php @@ -40,6 +40,7 @@ class ProjectBase extends Model 'type' => null, 'token' => null, 'access_information' => null, + 'last_commit' => null, ); /** @@ -54,6 +55,7 @@ class ProjectBase extends Model 'type' => 'getType', 'token' => 'getToken', 'access_information' => 'getAccessInformation', + 'last_commit' => 'getLastCommit', // Foreign key getters: ); @@ -70,6 +72,7 @@ class ProjectBase extends Model 'type' => 'setType', 'token' => 'setToken', 'access_information' => 'setAccessInformation', + 'last_commit' => 'setLastCommit', // Foreign key setters: ); @@ -115,6 +118,12 @@ class ProjectBase extends Model 'nullable' => true, 'default' => null, ), + 'last_commit' => array( + 'type' => 'varchar', + 'length' => 250, + 'nullable' => true, + 'default' => null, + ), ); /** @@ -215,6 +224,18 @@ class ProjectBase extends Model return $rtn; } + /** + * Get the value of LastCommit / last_commit. + * + * @return string + */ + public function getLastCommit() + { + $rtn = $this->data['last_commit']; + + return $rtn; + } + /** * Set the value of Id / id. * @@ -349,6 +370,24 @@ class ProjectBase extends Model $this->_setModified('access_information'); } + /** + * Set the value of LastCommit / last_commit. + * + * @param $value string + */ + public function setLastCommit($value) + { + $this->_validateString('LastCommit', $value); + + if ($this->data['last_commit'] === $value) { + return; + } + + $this->data['last_commit'] = $value; + + $this->_setModified('last_commit'); + } + /** * Get Build models by ProjectId for this Project. * diff --git a/console b/console old mode 100755 new mode 100644 index fc639be4..3358dce7 --- a/console +++ b/console @@ -17,6 +17,7 @@ use PHPCI\Command\GenerateCommand; use PHPCI\Command\UpdateCommand; use PHPCI\Command\InstallCommand; use PHPCI\Command\DaemonCommand; +use PHPCI\Command\PollCommand; use Symfony\Component\Console\Application; $loggerConfig = new \PHPCI\Helper\LoggerConfig(__DIR__ . "/loggerconfig.php"); @@ -28,5 +29,6 @@ $application->add(new InstallCommand); $application->add(new UpdateCommand($loggerConfig->GetFor('UpdateCommand'))); $application->add(new GenerateCommand); $application->add(new DaemonCommand($loggerConfig->GetFor('DaemonCommand'))); +$application->add(new PollCommand($loggerConfig->GetFor('PollCommand'))); $application->run();