forked from KnpLabs/KnpBundles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an anonymous consumer to handle github post-push hooks
- Loading branch information
Showing
3 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Knp/Bundle/KnpBundlesBundle/Consumer/GithubHookConsumer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Knp\Bundle\KnpBundlesBundle\Consumer; | ||
|
||
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface; | ||
use OldSound\RabbitMqBundle\RabbitMq\Producer; | ||
|
||
use Symfony\Component\HttpKernel\Log\LoggerInterface; | ||
|
||
use Doctrine\Common\Persistence\ObjectManager; | ||
|
||
/** | ||
* | ||
*/ | ||
class GithubHookConsumer implements ConsumerInterface | ||
{ | ||
/** | ||
* @var OldSound\RabbitMqBundle\RabbitMq\Producer | ||
*/ | ||
private $producer; | ||
|
||
/** | ||
* @var Doctrine\Common\Persistence\ObjectManager | ||
*/ | ||
private $manager; | ||
|
||
/** | ||
* @var Symfony\Component\HttpKernel\Log\LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
public function __construct(ObjectManager $manager, Producer $producer) | ||
{ | ||
$this->producer = $producer; | ||
$this->manager = $manager; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function execute($msg) | ||
{ | ||
if ($this->logger) { | ||
$this->logger->info('[GithubHookConsumer] Received a github post push hook'); | ||
} | ||
|
||
if (is_null($payload = json_decode($msg->body))) { | ||
if ($this->logger) { | ||
$this->logger->err('[GithubHookConsumer] Unable to decode payload'); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$bundles = $this->manager->getRepository('KnpBundlesBundle:Bundle'); | ||
|
||
$bundle = $bundles->findOneBy(array( | ||
'name' => $payload->repository->name, | ||
'username' => $payload->repository->owner->name | ||
)); | ||
|
||
if (!$bundle) { | ||
if ($this->logger) { | ||
$this->logger->warn(sprintf('[GithubHookConsumer] unknown bundle %s/%s', | ||
$payload->repository->name, | ||
$payload->repository->owner->name)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$this->producer->publish(serialize(array('bundle_id' => $bundle->getId()))); | ||
} | ||
|
||
public function setLogger(LoggerInterface $logger) | ||
{ | ||
$this->logger = $logger; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters