mirror of
https://github.com/FriendsOfPHP/Sismo.git
synced 2026-03-24 00:22:06 +01:00
Implement Logger Notifier
This commit is contained in:
@@ -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/" }
|
||||
|
||||
70
src/Sismo/Contrib/LoggerNotifier.php
Normal file
70
src/Sismo/Contrib/LoggerNotifier.php
Normal 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);
|
||||
}
|
||||
}
|
||||
49
tests/Sismo/Tests/Contrib/LoggerNotifierTest.php
Normal file
49
tests/Sismo/Tests/Contrib/LoggerNotifierTest.php
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user