Skip to content

Commit

Permalink
Add an anonymous consumer to handle github post-push hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Pouclet authored and ubermuda committed Mar 23, 2012
1 parent 4730d3b commit 84f4e2d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,15 @@ old_sound_rabbit_mq:
exchange_options: {name: 'upload-bundle', type: direct, durable: true }
queue_options: {name: 'upload-bundle'}
callback: knp_bundles.consumer.update_bundle
anon_consumers:
github_hook:
connection: default
exchange_options: {name: 'github-hooks', type: topic }
callback: knp_bundles.consumer.github_hook

nelmio_solarium:
adapter:
class: Solarium_Client_Adapter_Http
host: %solarium.host%
port: %solarium.port%
path: %solarium.path%
path: %solarium.path%
79 changes: 79 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Consumer/GithubHookConsumer.php
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;
}
}
10 changes: 10 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ parameters:
knp_bundles.trending_bundle_twitterer.class: Knp\Bundle\KnpBundlesBundle\Twitterer\TrendingBundleTwitterer
knp_bundles.trending_bundle_tweet.template: "Discover {name}, today's trending #Symfony2 bundle {url} on #KnpBundles"
knp_bundles.consumer.update_bundle.class: Knp\Bundle\KnpBundlesBundle\Consumer\UpdateBundleConsumer
knp_bundles.consumer.github_hook.class: Knp\Bundle\KnpBundlesBundle\Consumer\GithubHookConsumer
knp_bundles.user.manager.class: Knp\Bundle\KnpBundlesBundle\Entity\UserManager
knp_bundles.output.class: Symfony\Component\Console\Output\NullOutput
knp_bundles.travis.class: Knp\Bundle\KnpBundlesBundle\Travis\Travis
Expand Down Expand Up @@ -124,3 +125,12 @@ services:
- @knp_bundles.imagine
calls:
- [ setCacheDir, [%kernel.cache_dir%] ]

knp_bundles.consumer.github_hook:
public: false
class: %knp_bundles.consumer.github_hook.class%
arguments:
- @doctrine.orm.entity_manager
- @old_sound_rabbit_mq.update_bundle_producer
calls:
- [ setLogger, [@logger]]

0 comments on commit 84f4e2d

Please sign in to comment.