Update poll command to use services.
This commit is contained in:
parent
86de54b658
commit
a461fb580d
|
@ -9,15 +9,16 @@
|
||||||
|
|
||||||
namespace PHPCI\Command;
|
namespace PHPCI\Command;
|
||||||
|
|
||||||
use b8\Store\Factory;
|
|
||||||
use b8\HttpClient;
|
use b8\HttpClient;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use PHPCI\Helper\Lang;
|
use PHPCI\Helper\Lang;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Yaml\Parser;
|
use PHPCI\Config;
|
||||||
use PHPCI\Model\Build;
|
use PHPCI\Model\Build;
|
||||||
|
use PHPCI\Store\BuildStore;
|
||||||
|
use PHPCI\Store\ProjectStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run console command - Poll github for latest commit id
|
* Run console command - Poll github for latest commit id
|
||||||
|
@ -28,15 +29,39 @@ use PHPCI\Model\Build;
|
||||||
class PollCommand extends Command
|
class PollCommand extends Command
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var \Monolog\Logger
|
* @var Config
|
||||||
|
*/
|
||||||
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Logger
|
||||||
*/
|
*/
|
||||||
protected $logger;
|
protected $logger;
|
||||||
|
|
||||||
public function __construct(Logger $logger)
|
/**
|
||||||
|
* @var BuildStore
|
||||||
|
*/
|
||||||
|
protected $buildStore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var ProjectStore
|
||||||
|
*/
|
||||||
|
protected $projectStore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var HttpClient
|
||||||
|
*/
|
||||||
|
protected $githubClient;
|
||||||
|
|
||||||
|
public function __construct(Config $config, Logger $logger, BuildStore $buildStore, ProjectStore $projectStore, HttpClient $githubClient)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->config = $config;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
$this->buildStore = $buildStore;
|
||||||
|
$this->projectStore = $projectStore;
|
||||||
|
$this->githubClient = $githubClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configure()
|
protected function configure()
|
||||||
|
@ -51,27 +76,19 @@ class PollCommand extends Command
|
||||||
*/
|
*/
|
||||||
protected function execute(InputInterface $input, OutputInterface $output)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$parser = new Parser();
|
$token = $this->config->get('phpci.github.token');
|
||||||
$yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml');
|
|
||||||
$this->settings = $parser->parse($yaml);
|
|
||||||
|
|
||||||
$token = $this->settings['phpci']['github']['token'];
|
|
||||||
|
|
||||||
if (!$token) {
|
if (!$token) {
|
||||||
$this->logger->error(Lang::get('no_token'));
|
$this->logger->error(Lang::get('no_token'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$buildStore = Factory::getStore('Build');
|
|
||||||
|
|
||||||
$this->logger->addInfo(Lang::get('finding_projects'));
|
$this->logger->addInfo(Lang::get('finding_projects'));
|
||||||
$projectStore = Factory::getStore('Project');
|
$result = $this->projectStore->getWhere();
|
||||||
$result = $projectStore->getWhere();
|
|
||||||
$this->logger->addInfo(Lang::get('found_n_projects', count($result['items'])));
|
$this->logger->addInfo(Lang::get('found_n_projects', count($result['items'])));
|
||||||
|
|
||||||
foreach ($result['items'] as $project) {
|
foreach ($result['items'] as $project) {
|
||||||
$http = new HttpClient('https://api.github.com');
|
$commits = $this->githubClient->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token));
|
||||||
$commits = $http->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token));
|
|
||||||
|
|
||||||
$last_commit = $commits['body'][0]['sha'];
|
$last_commit = $commits['body'][0]['sha'];
|
||||||
$last_committer = $commits['body'][0]['commit']['committer']['email'];
|
$last_committer = $commits['body'][0]['commit']['committer']['email'];
|
||||||
|
@ -90,14 +107,14 @@ class PollCommand extends Command
|
||||||
$build->setStatus(Build::STATUS_NEW);
|
$build->setStatus(Build::STATUS_NEW);
|
||||||
$build->setBranch($project->getBranch());
|
$build->setBranch($project->getBranch());
|
||||||
$build->setCreated(new \DateTime());
|
$build->setCreated(new \DateTime());
|
||||||
$build->setCommitMessage($message);
|
$build->setCommitMessage($message);
|
||||||
if (!empty($last_committer)) {
|
if (!empty($last_committer)) {
|
||||||
$build->setCommitterEmail($last_committer);
|
$build->setCommitterEmail($last_committer);
|
||||||
}
|
}
|
||||||
$buildStore->save($build);
|
$this->buildStore->save($build);
|
||||||
|
|
||||||
$project->setLastCommit($last_commit);
|
$project->setLastCommit($last_commit);
|
||||||
$projectStore->save($project);
|
$this->projectStore->save($project);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
16
services.yml
16
services.yml
|
@ -6,22 +6,22 @@ parameters:
|
||||||
|
|
||||||
services:
|
services:
|
||||||
storage.user:
|
storage.user:
|
||||||
class: PHPCI\Store\Base\UserStore
|
class: PHPCI\Store\UserStore
|
||||||
factory: [%storage.factory%, getStore]
|
factory: [%storage.factory%, getStore]
|
||||||
arguments:
|
arguments:
|
||||||
- User
|
- User
|
||||||
storage.project:
|
storage.project:
|
||||||
class: PHPCI\Store\Base\ProjectStore
|
class: PHPCI\Store\ProjectStore
|
||||||
factory: [%storage.factory%, getStore]
|
factory: [%storage.factory%, getStore]
|
||||||
arguments:
|
arguments:
|
||||||
- Project
|
- Project
|
||||||
storage.build:
|
storage.build:
|
||||||
class: PHPCI\Store\Base\BuildStore
|
class: PHPCI\Store\BuildStore
|
||||||
factory: [%storage.factory%, getStore]
|
factory: [%storage.factory%, getStore]
|
||||||
arguments:
|
arguments:
|
||||||
- Build
|
- Build
|
||||||
storage.build_meta:
|
storage.build_meta:
|
||||||
class: PHPCI\Store\Base\BuildMetaStore
|
class: PHPCI\Store\BuildMetaStore
|
||||||
factory: [%storage.factory%, getStore]
|
factory: [%storage.factory%, getStore]
|
||||||
arguments:
|
arguments:
|
||||||
- BuildMeta
|
- BuildMeta
|
||||||
|
@ -31,6 +31,10 @@ services:
|
||||||
http.response:
|
http.response:
|
||||||
class: b8\Http\Response
|
class: b8\Http\Response
|
||||||
arguments: []
|
arguments: []
|
||||||
|
http_client.github:
|
||||||
|
class: b8\HttpClient
|
||||||
|
arguments:
|
||||||
|
- https://api.github.com
|
||||||
config:
|
config:
|
||||||
class: PHPCI\Config
|
class: PHPCI\Config
|
||||||
arguments: [%config_file%]
|
arguments: [%config_file%]
|
||||||
|
@ -85,7 +89,11 @@ services:
|
||||||
console.command.poll:
|
console.command.poll:
|
||||||
class: PHPCI\Command\PollCommand
|
class: PHPCI\Command\PollCommand
|
||||||
arguments:
|
arguments:
|
||||||
|
- @config
|
||||||
- @console.logger
|
- @console.logger
|
||||||
|
- @storage.build
|
||||||
|
- @storage.project
|
||||||
|
- @http_client.github
|
||||||
console.command.create_admin:
|
console.command.create_admin:
|
||||||
class: PHPCI\Command\CreateAdminCommand
|
class: PHPCI\Command\CreateAdminCommand
|
||||||
arguments:
|
arguments:
|
||||||
|
|
Loading…
Reference in a new issue