Jump to content


farlay

Member Since 15 Aug 2019
Offline Last Active Aug 16 2019 11:18 AM
-----

Topics I've Started

Listener not responding to commit event (swarm module)

15 August 2019 - 12:23 PM

Hello,

I am working on a module that automatically requests a review for each commit. Following the slack example from the documentation I added a listener to the commit event with a priority set to -110 but I don't seem to get any response when a change is actually committed.

When I write to the log in the onBootstrap function I can see that the module is actually loaded but I can't get anything else out of it.

Any idea what I'm doing wrong? Source is added below

Module.php
<?php

namespace ReviewAutoRequest;
use Zend\eventManager\Event;
use Zend\Log\Logger;

class Module {
public function getConfig() {
	 return include __DIR__ . '/config/module.config.php';
}

public function onBootstrap(Event $event) {
	 $application = $event->getApplication();
	 $services = $application->getServiceManager();
	 $logger = $services->get('logger');
	 $logger->info("ReviewAutoRequest bootstrapping");
}
}

module.config.php
<?php

$listeners = [ReviewAutoRequest\Listener\ReviewAutoRequestListener::class];

return [
'listeners' => $listeners,
'service_manager' =>[
	 'factories' => array_fill_keys(
		 $listeners,
		 Events\Listener\ListenerFactory::class
	 )
],
Events\Listener\ListenerFactory::EVENT_LISTENER_CONFIG => [
	 Events\Listener\ListenerFactory::COMMIT => [
		 ReviewAutoRequest\Listener\ReviewAutoRequestListener::class => [
			 [
				 Events\Listener\ListenerFactory::PRIORITY => -110,
				 Events\Listener\ListenerFactory::CALLBACK => 'handleCommit',
				 Events\Listener\ListenerFactory::MANAGER_CONTEXT => 'queue'
			 ]
		 ]
	 ]
]
];

ReviewAutoRequestListener.php
<?php

namespace ReviewAutoRequest\Listener;

use Events\Listener\AbstractEventListener;
use P4\Spec\Change;
use P4\Spec\Exception\NotFoundException;
use Zend\EventManager\Event;
use Zend\Http\Client;
use Zend\Http\Request;

class ReviewAutoRequestListener extends AbstractEventListener {
public function handleCommit (Event $event) {
	 $logger = $this->services->get('logger');
	 $logger->info("ReviewAutoRequest: handle commit");
	
	 $p4Admin = $this->services->get('p4_admin');
	 $change = $event->getParam('change');

	 if(!$change instanceof Change){
		 try {
			 $change = Change::fetchById($event->getParam('id'), $p4Admin);
			 $event->setParam('change', $change);
		 } catch (NotFoundException $e) {
			 $logger->err("ReviewAutoRequest event not found:" + $e.getMessage());
		 } catch (\InvalidArgumentException $e) {
			 $logger->err("ReviewAutoRequest invalid argument:" + $e.getMessage());
		 }
	 }

	 if (!$change instanceof Change || !$change->isSubmitted()) {
		 $logger->info("ReviewAutoRequest: no changes found");
		 return;
	 }

	 try {
		 $id = $change.getParam('id');
		 $this->requestReview($id);
	 } catch (\Exception $e){
		 $logger->err("ReviewAutoRequest post exception:" . $e->getMessage());
		 return;
	 }
}
}

custom.modules.config.php
<?php
\Zend\Loader\AutoloaderFactory::factory(
array(
	 'Zend\Loader\StandardAutoloader' => array(
		 'namespaces' => array(
			 'ReviewAutoRequest' => BASE_PATH . '/module/ReviewAutoRequest/src',
		 )
	 )
)
);
return [
'ReviewAutoRequest'
];