mirror of
https://we.phorge.it/source/phorge.git
synced 2025-03-03 16:09:17 +01:00
Add an example notification handler to the IRC bot
Summary: Simple notificaiton handler that reads the difx event timeline and posts notifications to IRC. Test Plan: Ran it in #phabricator. Reviewers: btrahan, jungejason Reviewed By: btrahan CC: aran, btrahan Differential Revision: https://secure.phabricator.com/D1337
This commit is contained in:
parent
13d930b232
commit
684d12d5db
7 changed files with 106 additions and 7 deletions
|
@ -7,10 +7,13 @@
|
|||
],
|
||||
"handlers" : [
|
||||
"PhabricatorIRCProtocolHandler",
|
||||
"PhabricatorIRCObjectNameHandler"
|
||||
"PhabricatorIRCObjectNameHandler",
|
||||
"PhabricatorIRCDifferentialNotificationHandler"
|
||||
],
|
||||
|
||||
"conduit.uri" : null,
|
||||
"conduit.user" : null,
|
||||
"conduit.cert" : null
|
||||
"conduit.cert" : null,
|
||||
|
||||
"notification.channels" : ["#phabot-test"]
|
||||
}
|
||||
|
|
|
@ -489,6 +489,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorHelpController' => 'applications/help/controller/base',
|
||||
'PhabricatorHelpKeyboardShortcutController' => 'applications/help/controller/keyboardshortcut',
|
||||
'PhabricatorIRCBot' => 'infrastructure/daemon/irc/bot',
|
||||
'PhabricatorIRCDifferentialNotificationHandler' => 'infrastructure/daemon/irc/handler/differentialnotification',
|
||||
'PhabricatorIRCHandler' => 'infrastructure/daemon/irc/handler/base',
|
||||
'PhabricatorIRCMessage' => 'infrastructure/daemon/irc/message',
|
||||
'PhabricatorIRCObjectNameHandler' => 'infrastructure/daemon/irc/handler/objectname',
|
||||
|
@ -1157,6 +1158,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorHelpController' => 'PhabricatorController',
|
||||
'PhabricatorHelpKeyboardShortcutController' => 'PhabricatorHelpController',
|
||||
'PhabricatorIRCBot' => 'PhabricatorDaemon',
|
||||
'PhabricatorIRCDifferentialNotificationHandler' => 'PhabricatorIRCHandler',
|
||||
'PhabricatorIRCObjectNameHandler' => 'PhabricatorIRCHandler',
|
||||
'PhabricatorIRCProtocolHandler' => 'PhabricatorIRCHandler',
|
||||
'PhabricatorInfrastructureTestCase' => 'PhabricatorTestCase',
|
||||
|
|
|
@ -33,6 +33,7 @@ These are the configuration values it reads:
|
|||
IRCBot.
|
||||
- ##conduit.uri##, ##conduit.user##, ##conduit.cert## Conduit configuration,
|
||||
see below.
|
||||
- ##notification.channels## Notification configuration, see below.
|
||||
|
||||
= Handlers =
|
||||
|
||||
|
@ -46,6 +47,9 @@ the IRC bot. These are the default handlers available:
|
|||
- @{class:PhabricatorIRCObjectNameHandler} This handler looks for users
|
||||
mentioning Phabricator objects like "T123" and "D345" in chat, looks them
|
||||
up, and says their name with a link to the object. Requires conduit.
|
||||
- @{class:PhabricatorIRCDifferentialNotificationHandler} This handler posts
|
||||
notifications about changes to revisions to IRC to the channels listed in
|
||||
##notification.channels##.
|
||||
|
||||
You can also write your own handlers, by extending
|
||||
@{class:PhabricatorIRCHandler}.
|
||||
|
@ -53,9 +57,9 @@ You can also write your own handlers, by extending
|
|||
= Conduit =
|
||||
|
||||
Some handlers (e.g., @{class:PhabricatorIRCObjectNameHandler}) need to read data
|
||||
from Phabricator. They do this over Conduit, Phabricator's HTTP API. You can
|
||||
use this method to allow other scripts or programs to access Phabricator's data
|
||||
from different servers and in different languages.
|
||||
from Phabricator over Conduit, Phabricator's HTTP API. You can use this method
|
||||
to allow other scripts or programs to access Phabricator's data from different
|
||||
servers and in different languages.
|
||||
|
||||
To allow the bot to access Conduit, you need to create a user that it can login
|
||||
with. To do this, login to Phabricator as an administrator and go to
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Facebook, Inc.
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -177,6 +177,10 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
|
|||
$routed_message = $this->processReadBuffer();
|
||||
} while ($routed_message);
|
||||
|
||||
foreach ($this->handlers as $handler) {
|
||||
$handler->runBackgroundTasks();
|
||||
}
|
||||
|
||||
} while (true);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Facebook, Inc.
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
@ -49,4 +49,8 @@ abstract class PhabricatorIRCHandler {
|
|||
|
||||
abstract public function receiveMessage(PhabricatorIRCMessage $message);
|
||||
|
||||
public function runBackgroundTasks() {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @group irc
|
||||
*/
|
||||
class PhabricatorIRCDifferentialNotificationHandler
|
||||
extends PhabricatorIRCHandler {
|
||||
|
||||
private $skippedOldEvents;
|
||||
|
||||
public function receiveMessage(PhabricatorIRCMessage $message) {
|
||||
return;
|
||||
}
|
||||
|
||||
public function runBackgroundTasks() {
|
||||
$iterator = new PhabricatorTimelineIterator('ircdiffx', array('difx'));
|
||||
|
||||
if (!$this->skippedOldEvents) {
|
||||
// Since we only want to post notifications about new events, skip
|
||||
// everything that's happened in the past when we start up so we'll
|
||||
// only process real-time events.
|
||||
foreach ($iterator as $event) {
|
||||
// Ignore all old events.
|
||||
}
|
||||
$this->skippedOldEvents = true;
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($iterator as $event) {
|
||||
$data = $event->getData();
|
||||
if (!$data) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$actor_phid = $data['actor_phid'];
|
||||
$phids = array($actor_phid);
|
||||
$handles = id(new PhabricatorObjectHandleData($phids))->loadHandles();
|
||||
$verb = DifferentialAction::getActionPastTenseVerb($data['action']);
|
||||
|
||||
$actor_name = $handles[$actor_phid]->getName();
|
||||
$message = "{$actor_name} {$verb} revision D".$data['revision_id'].".";
|
||||
|
||||
foreach ($this->getConfig('notification.channels') as $channel) {
|
||||
$this->write('PRIVMSG', "{$channel} :{$message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is automatically generated. Lint this module to rebuild it.
|
||||
* @generated
|
||||
*/
|
||||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/differential/constants/action');
|
||||
phutil_require_module('phabricator', 'applications/phid/handle/data');
|
||||
phutil_require_module('phabricator', 'infrastructure/daemon/irc/handler/base');
|
||||
phutil_require_module('phabricator', 'infrastructure/daemon/timeline/cursor/iterator');
|
||||
|
||||
phutil_require_module('phutil', 'utils');
|
||||
|
||||
|
||||
phutil_require_source('PhabricatorIRCDifferentialNotificationHandler.php');
|
Loading…
Add table
Reference in a new issue