Command to poll github for new commits
This commit is contained in:
parent
8c127d692c
commit
4d3372af88
105
PHPCI/Command/PollCommand.php
Normal file
105
PHPCI/Command/PollCommand.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* PHPCI - Continuous Integration for PHP
|
||||
*
|
||||
* @copyright Copyright 2013, Block 8 Limited.
|
||||
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
||||
* @link http://www.phptesting.org/
|
||||
*/
|
||||
|
||||
namespace PHPCI\Command;
|
||||
|
||||
use b8\Store\Factory;
|
||||
use b8\HttpClient;
|
||||
use Monolog\Logger;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Yaml\Parser;
|
||||
use PHPCI\Model\Build;
|
||||
|
||||
/**
|
||||
* Run console command - Poll github for latest commit id
|
||||
* @author Jimmy Cleuren <jimmy.cleuren@gmail.com>
|
||||
* @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");
|
||||
}
|
||||
}
|
||||
|
|
@ -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.
|
||||
*
|
||||
|
|
2
console
Executable file → Normal file
2
console
Executable file → Normal file
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue