Page MenuHomeDevCentral
Paste P146

app/Listeners/PhabricatorListener.php
ActivePublic

Authored by dereckson on Dec 21 2015, 17:11.
Tags
None
Referenced Files
F15169: app/Listeners/PhabricatorListener.php
Dec 21 2015, 17:11
Subscribers
None
<?php
namespace Nasqueron\Notifications\Listeners;
use Nasqueron\Notifications\Actions\NotifyNewCommitsAction;
use Nasqueron\Notifications\Events\GitHubPayloadEvent;
use Nasqueron\Notifications\Events\ReportEvent;
use Nasqueron\Notifications\Phabricator\PhabricatorAPI;
use Nasqueron\Notifications\Phabricator\PhabricatorAPIException;
use Event;
class PhabricatorListener {
///
/// GitHub → Phabricator
///
/**
* Handles payload events
*
* @param GitHubPayloadEvent $event The GitHub payload event
*/
public function onGitHubPayload (GitHubPayloadEvent $event) {
if ($event->event === 'push') {
$this->notifyNewCommits($event);
}
}
/**
* @return string the repository call sign "OPS", or "" if not in Phabricator
*/
private static function getCallSign (PhabricatorAPI $api, $remoteURI) {
$reply = $api->call(
'repository.query',
[ 'remoteURIs[0]' => $remoteURI ]
);
if (!count($reply)) {
return "";
}
return PhabricatorAPI::getFirstResult($reply)->callsign;
}
/**
* Notifies Phabricator new commit are t$callSignhere
*/
public function notifyNewCommits (GitHubPayloadEvent $event) {
$api = PhabricatorAPI::forProject($event->door);
if (!$api) {
// We don't have a Phabricator instance for this project.
return;
}
$callSign = static::getCallSign(
$api,
$event->payload->repository->clone_url
);
if ($callSign === "") {
return;
}
$actionToReport = new NotifyNewCommitsAction($callSign);
try {
$api->call(
'diffusion.looksoon',
[ 'callsigns[0]' => $callSign ]
);
} catch (PhabricatorAPIException $ex) {
$actionToReport->attachException($ex);
}
Event::fire(new ReportEvent($actionToReport));
}
///
/// Events listening
///
/**
* Register the listeners for the subscriber.
*
* @param Illuminate\Events\Dispatcher $events
*/
public function subscribe ($events) {
$class = 'Nasqueron\Notifications\Listeners\PhabricatorListener';
$events->listen(
'Nasqueron\Notifications\Events\GitHubPayloadEvent',
"$class@onGitHubPayload"
);
}
}

Event Timeline

dereckson changed the title of this paste from untitled to app/Listeners/PhabricatorListener.php.