Skip to content

Commit

Permalink
Merge pull request #257 from tienvx/reporters
Browse files Browse the repository at this point in the history
Add reporter plugins
  • Loading branch information
tienvx authored Jun 1, 2019
2 parents 9085bfe + 1417d4a commit ddac3e9
Show file tree
Hide file tree
Showing 53 changed files with 691 additions and 216 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
"symfony/validator": "^v4.3",
"symfony/messenger": "^v4.3",
"symfony/process": "^v4.3",
"symfony/serializer": "^4.3",
"doctrine/doctrine-bundle": "^1.10",
"doctrine/orm": "^2.6",
"dunglas/doctrine-json-odm": "^0.1.3",
"symfony/monolog-bundle": "^3.3",
"league/flysystem-bundle": "^1.1"
},
"require-dev": {
Expand Down Expand Up @@ -74,6 +72,9 @@
}
},
"suggest": {
"ext-amqp": "Needed to support amqp transport"
"ext-amqp": "Needed to support amqp transport",
"alek13/slack": "Send bug report to Slack",
"symfony/swiftmailer-bundle": "Send bug report to an email",
"symfony/twig-bundle": "Send bug report to an email with html format"
}
}
3 changes: 3 additions & 0 deletions src/Command/ReducePathCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

class ReducePathCommand extends AbstractCommand
{
/**
* @var PathReducerManager
*/
private $pathReducerManager;

public function __construct(PathReducerManager $pathReducerManager)
Expand Down
50 changes: 9 additions & 41 deletions src/Command/ReportBugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@

use Doctrine\ORM\EntityManagerInterface;
use Exception;
use League\Flysystem\FilesystemInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Tienvx\Bundle\MbtBundle\Entity\Bug;
use Tienvx\Bundle\MbtBundle\Graph\Path;
use Tienvx\Bundle\MbtBundle\Subject\SubjectManager;
use Tienvx\Bundle\MbtBundle\Reporter\ReporterManager;

class ReportBugCommand extends Command
{
Expand All @@ -22,28 +19,16 @@ class ReportBugCommand extends Command
private $entityManager;

/**
* @var LoggerInterface
* @var ReporterManager
*/
private $logger;

/**
* @var SubjectManager
*/
protected $subjectManager;

/**
* @var FilesystemInterface
*/
protected $mbtStorage;
protected $reporterManager;

public function __construct(
EntityManagerInterface $entityManager,
SubjectManager $subjectManager,
FilesystemInterface $mbtStorage
ReporterManager $reporterManager
) {
$this->entityManager = $entityManager;
$this->subjectManager = $subjectManager;
$this->mbtStorage = $mbtStorage;
$this->reporterManager = $reporterManager;

parent::__construct();
}
Expand All @@ -57,11 +42,6 @@ protected function configure()
->addArgument('bug-id', InputArgument::REQUIRED, 'The bug id to report.');
}

public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
Expand All @@ -70,10 +50,6 @@ public function setLogger(LoggerInterface $logger)
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
if (!$this->logger instanceof LoggerInterface) {
throw new Exception("Can not report bug: No monolog's handlers with channel 'mbt' were defined");
}

$bugId = $input->getArgument('bug-id');

$callback = function () use ($bugId) {
Expand All @@ -94,17 +70,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}

$path = Path::unserialize($bug->getPath());
$model = $bug->getTask()->getModel();
$subject = $this->subjectManager->createSubject($model);

$subject->setFilesystem($this->mbtStorage);

$this->logger->error($bug->getBugMessage(), [
'bug' => $bug,
'path' => $path,
'model' => $model,
'subject' => $subject,
]);
foreach ($bug->getTask()->getReporters() as $reporter) {
$reporterPlugin = $this->reporterManager->getReporter($reporter);
$reporterPlugin->report($bug);
}
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/GeneratorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function process(ContainerBuilder $container)
return;
}

if (!$generators = $this->findTaggedServices($container, $this->generatorTag, PluginInterface::class, 'getName')) {
if (!$generators = $this->findTaggedServices($container, $this->generatorTag)) {
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->generatorTag, $this->generatorService));
}

Expand Down
34 changes: 0 additions & 34 deletions src/DependencyInjection/Compiler/MonologChannelPass.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/PathReducerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function process(ContainerBuilder $container)
return;
}

if (!$pathReducers = $this->findTaggedServices($container, $this->pathReducerTag, PluginInterface::class, 'getName')) {
if (!$pathReducers = $this->findTaggedServices($container, $this->pathReducerTag)) {
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->pathReducerTag, $this->pathReducerService));
}

Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/Compiler/PluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@

interface PluginInterface
{
public static function getName();
public static function getName(): string;

public static function support(): bool;
}
41 changes: 41 additions & 0 deletions src/DependencyInjection/Compiler/ReporterPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Tienvx\Bundle\MbtBundle\DependencyInjection\Compiler;

use Exception;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;

class ReporterPass implements CompilerPassInterface
{
use TaggedServiceTrait;

private $reporterService;
private $reporterTag;

public function __construct(string $reporterService = 'mbt.reporter_manager', string $reporterTag = 'mbt.reporter')
{
$this->reporterService = $reporterService;
$this->reporterTag = $reporterTag;
}

/**
* @param ContainerBuilder $container
*
* @throws Exception
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->reporterService)) {
return;
}

if (!$reporters = $this->findTaggedServices($container, $this->reporterTag)) {
throw new RuntimeException(sprintf('You must tag at least one service as "%s" to use the "%s" service.', $this->reporterTag, $this->reporterService));
}

$reporterDefinition = $container->getDefinition($this->reporterService);
$reporterDefinition->replaceArgument(0, $reporters);
}
}
3 changes: 1 addition & 2 deletions src/DependencyInjection/Compiler/SubjectPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Exception;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Tienvx\Bundle\MbtBundle\Subject\SubjectInterface;

class SubjectPass implements CompilerPassInterface
{
Expand All @@ -31,7 +30,7 @@ public function process(ContainerBuilder $container)
return;
}

$subjects = $this->findTaggedServices($container, $this->subjectTag, SubjectInterface::class, 'support', false);
$subjects = $this->findTaggedServices($container, $this->subjectTag, false);

$subjectDefinition = $container->getDefinition($this->subjectService);
$subjectDefinition->replaceArgument(0, $subjects);
Expand Down
11 changes: 7 additions & 4 deletions src/DependencyInjection/Compiler/TaggedServiceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait TaggedServiceTrait
*
* @throws Exception
*/
private function findTaggedServices(ContainerBuilder $container, string $tagName, string $interface, string $method, bool $reference = true)
private function findTaggedServices(ContainerBuilder $container, string $tagName, bool $reference = true)
{
$services = [];
foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
Expand All @@ -32,13 +32,16 @@ private function findTaggedServices(ContainerBuilder $container, string $tagName
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId));
}
if (!$r->isSubclassOf($interface)) {
if (!$r->isSubclassOf(PluginInterface::class)) {
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $serviceId, PluginInterface::class));
}
$class = $r->name;

$serviceName = call_user_func([$class, $method]);
$services[$serviceName] = $reference ? (new Reference($serviceId)) : $class;
$support = call_user_func([$class, 'support']);
if ($support) {
$serviceName = call_user_func([$class, 'getName']);
$services[$serviceName] = $reference ? (new Reference($serviceId)) : $class;
}
}

return $services;
Expand Down
6 changes: 6 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public function getConfigTreeBuilder()
->floatNode('transition_coverage')->defaultValue(100)->min(0)->max(100)->end()
->floatNode('place_coverage')->defaultValue(100)->min(0)->max(100)->end()
->scalarNode('default_bug_title')->defaultValue('New bug found')->end()
// 'https://hooks.slack.com/...'
->scalarNode('slack_hook_url')->defaultValue('')->end()
// '#operations'
->scalarNode('slack_channel')->defaultValue('')->end()
->scalarNode('email_from')->defaultValue('')->end()
->scalarNode('email_to')->defaultValue('')->end()
->end()
;

Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/TienvxMbtExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Tienvx\Bundle\MbtBundle\Generator\ProbabilityGenerator;
use Tienvx\Bundle\MbtBundle\Generator\RandomGenerator;
use Tienvx\Bundle\MbtBundle\PathReducer\PathReducerInterface;
use Tienvx\Bundle\MbtBundle\Reporter\EmailReporter;
use Tienvx\Bundle\MbtBundle\Reporter\ReporterInterface;
use Tienvx\Bundle\MbtBundle\Reporter\SlackReporter;
use Tienvx\Bundle\MbtBundle\Subject\SubjectInterface;

/**
Expand Down Expand Up @@ -46,6 +49,9 @@ public function load(array $configs, ContainerBuilder $container)
$container->registerForAutoconfiguration(SubjectInterface::class)
->setLazy(true)
->addTag('mbt.subject');
$container->registerForAutoconfiguration(ReporterInterface::class)
->setLazy(true)
->addTag('mbt.reporter');
}

private function registerCommandConfiguration(array $config, ContainerBuilder $container)
Expand All @@ -63,5 +69,13 @@ private function registerGeneratorConfiguration(array $config, ContainerBuilder

$probabilityGeneratorDefinition = $container->getDefinition(ProbabilityGenerator::class);
$probabilityGeneratorDefinition->addMethodCall('setMaxPathLength', [$config['max_path_length']]);

$slackReporterDefinition = $container->getDefinition(SlackReporter::class);
$slackReporterDefinition->addMethodCall('setSlackHookUrl', [$config['slack_hook_url']]);
$slackReporterDefinition->addMethodCall('setSlackChannel', [$config['slack_channel']]);

$emailReporterDefinition = $container->getDefinition(EmailReporter::class);
$emailReporterDefinition->addMethodCall('setEmailFrom', [$config['email_from']]);
$emailReporterDefinition->addMethodCall('setEmailTo', [$config['email_to']]);
}
}
11 changes: 6 additions & 5 deletions src/Entity/Bug.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tienvx\Bundle\MbtBundle\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Exception;
use Symfony\Component\Validator\Constraints as Assert;
Expand Down Expand Up @@ -63,14 +64,14 @@ class Bug
private $messagesCount = 0;

/**
* @var \DateTime
* @var DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;

/**
* @var \DateTime
* @var DateTime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
Expand Down Expand Up @@ -179,11 +180,11 @@ public function getUpdatedAt()
public function prePersist()
{
if (!$this->getCreatedAt()) {
$this->setCreatedAt(new \DateTime());
$this->setCreatedAt(new DateTime());
}

if (!$this->getUpdatedAt()) {
$this->setUpdatedAt(new \DateTime());
$this->setUpdatedAt(new DateTime());
}
}

Expand All @@ -195,7 +196,7 @@ public function prePersist()
public function preUpdate()
{
if (!$this->getUpdatedAt()) {
$this->setUpdatedAt(new \DateTime());
$this->setUpdatedAt(new DateTime());
}
}
}
Loading

0 comments on commit ddac3e9

Please sign in to comment.