Implement Logger Notifier

This commit is contained in:
Marsel
2015-10-29 20:59:10 +05:00
committed by Grégoire Pineau
parent 5b3cec6b87
commit 540a276fd1
3 changed files with 121 additions and 1 deletions

View File

@@ -34,7 +34,8 @@
"symfony/class-loader": "~2.2"
},
"suggest": {
"jolicode/jolinotif": "Use the best notifier available on your system (Mac, Linux or Windows)"
"jolicode/jolinotif": "Use the best notifier available on your system (Mac, Linux or Windows)",
"monolog/monolog": "Use Monolog handlers as notifiers"
},
"autoload": {
"psr-0": { "Sismo": "src/" }

View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the Sismo utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sismo\Contrib;
use Psr\Log\LoggerInterface;
use Sismo\Commit;
use Sismo\Notifier\Notifier;
/**
* Notifies builds via Monolog or any other PSR logger.
* Logger must have info log level.
*
* Here is a usage example of Monolog with Slack handler:
*
* $message = <<<MESSAGE
* Message: %message%
* Author: %author%
* Commit: %sha%
* MESSAGE;
*
* $slack_handler = new SlackHandler(
* 'slack_code',
* '#channel',
* 'Monolog',
* 'true',
* 'null',
* Logger::INFO
* );
*
* $monolog = new Monolog\Logger('sismo', [$slack_handler]);
* $notifier = new Sismo\Contrib\LoggerNotifier($monolog);
*
* @author Marsel Arduanov <arduanov@gmail.com>
*/
class LoggerNotifier extends Notifier
{
protected $logger;
protected $messageFormat;
/**
* Constructor.
*
* @param LoggerInterface $logger
* @param string $messageFormat
*/
public function __construct(LoggerInterface $logger, $messageFormat = '')
{
$this->logger = $logger;
$this->messageFormat = $messageFormat;
}
/**
* @inherit
*/
public function notify(Commit $commit)
{
$message = $this->format($this->messageFormat, $commit);
return ($commit->getStatusCode() == 'success') ? $this->logger->info($message) : $this->logger->critical($message);
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Sismo utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Sismo\Tests\Contrib;
use Sismo\Commit;
use Sismo\Contrib\LoggerNotifier;
use Sismo\Project;
class LoggerNotifierTest extends \PHPUnit_Framework_TestCase
{
public function testNotify()
{
$project = new Project('Twig');
$successCommit = new Commit($project, '123455');
$successCommit->setAuthor('Fabien');
$successCommit->setMessage('Bar');
$successCommit->setStatusCode('success');
$failedCommit = new Commit($project, '123456');
$failedCommit->setAuthor('Fabien');
$failedCommit->setMessage('Foo');
$failedCommit->setStatusCode('failed');
$project->setCommits(array(
$failedCommit,
$successCommit,
));
$logger = $this->getMock('Psr\Log\NullLogger');
$logger->expects($this->once())->method('info');
$logger->expects($this->once())->method('critical');
$notifier = new LoggerNotifier($logger);
//notify success commit
$notifier->notify($successCommit);
//notify failed commit
$notifier->notify($failedCommit);
}
}