2013-11-18 22:49:18 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
2014-05-12 18:26:17 +02:00
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
2013-11-18 22:49:18 +01:00
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
2014-05-12 18:26:17 +02:00
|
|
|
* @link https://www.phptesting.org/
|
2013-11-18 22:49:18 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace PHPCI\Command;
|
|
|
|
|
|
|
|
use b8\Store\Factory;
|
|
|
|
use b8\HttpClient;
|
|
|
|
use Monolog\Logger;
|
2014-12-04 16:48:52 +01:00
|
|
|
use PHPCI\Helper\Lang;
|
2013-11-18 22:49:18 +01:00
|
|
|
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')
|
2014-12-04 16:48:52 +01:00
|
|
|
->setDescription(Lang::get('poll_github'));
|
2013-11-18 22:49:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'];
|
|
|
|
|
2013-11-19 22:47:12 +01:00
|
|
|
if (!$token) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->error(Lang::get('no_token'));
|
2013-11-18 22:49:18 +01:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
$buildStore = Factory::getStore('Build');
|
|
|
|
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->addInfo(Lang::get('finding_projects'));
|
2013-11-18 22:49:18 +01:00
|
|
|
$projectStore = Factory::getStore('Project');
|
|
|
|
$result = $projectStore->getWhere();
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->addInfo(Lang::get('found_n_projects', count($result['items'])));
|
2013-11-18 22:49:18 +01:00
|
|
|
|
|
|
|
foreach ($result['items'] as $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'];
|
2013-12-15 21:21:12 +01:00
|
|
|
$last_committer = $commits['body'][0]['commit']['committer']['email'];
|
2014-10-18 16:52:07 +02:00
|
|
|
$message = $commits['body'][0]['commit']['message'];
|
2013-11-18 22:49:18 +01:00
|
|
|
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->info(Lang::get('last_commit_is', $project->getTitle(), $last_commit));
|
2013-11-18 22:49:18 +01:00
|
|
|
|
2013-11-19 22:47:12 +01:00
|
|
|
if ($project->getLastCommit() != $last_commit && $last_commit != "") {
|
2014-02-27 15:12:19 +01:00
|
|
|
$this->logger->info(
|
2014-12-04 16:48:52 +01:00
|
|
|
Lang::get('adding_new_build')
|
2014-02-27 15:12:19 +01:00
|
|
|
);
|
2013-11-18 22:49:18 +01:00
|
|
|
|
|
|
|
$build = new Build();
|
|
|
|
$build->setProjectId($project->getId());
|
|
|
|
$build->setCommitId($last_commit);
|
2013-12-07 15:29:25 +01:00
|
|
|
$build->setStatus(Build::STATUS_NEW);
|
2014-06-11 15:18:31 +02:00
|
|
|
$build->setBranch($project->getBranch());
|
2013-11-18 22:49:18 +01:00
|
|
|
$build->setCreated(new \DateTime());
|
2014-10-18 16:52:07 +02:00
|
|
|
$build->setCommitMessage($message);
|
2013-12-15 21:21:12 +01:00
|
|
|
if (!empty($last_committer)) {
|
|
|
|
$build->setCommitterEmail($last_committer);
|
|
|
|
}
|
2013-11-18 22:49:18 +01:00
|
|
|
$buildStore->save($build);
|
|
|
|
|
|
|
|
$project->setLastCommit($last_commit);
|
|
|
|
$projectStore->save($project);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->addInfo(Lang::get('finished_processing_builds'));
|
2013-11-18 22:49:18 +01:00
|
|
|
}
|
|
|
|
}
|