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" : [
|
"handlers" : [
|
||||||
"PhabricatorIRCProtocolHandler",
|
"PhabricatorIRCProtocolHandler",
|
||||||
"PhabricatorIRCObjectNameHandler"
|
"PhabricatorIRCObjectNameHandler",
|
||||||
|
"PhabricatorIRCDifferentialNotificationHandler"
|
||||||
],
|
],
|
||||||
|
|
||||||
"conduit.uri" : null,
|
"conduit.uri" : null,
|
||||||
"conduit.user" : 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',
|
'PhabricatorHelpController' => 'applications/help/controller/base',
|
||||||
'PhabricatorHelpKeyboardShortcutController' => 'applications/help/controller/keyboardshortcut',
|
'PhabricatorHelpKeyboardShortcutController' => 'applications/help/controller/keyboardshortcut',
|
||||||
'PhabricatorIRCBot' => 'infrastructure/daemon/irc/bot',
|
'PhabricatorIRCBot' => 'infrastructure/daemon/irc/bot',
|
||||||
|
'PhabricatorIRCDifferentialNotificationHandler' => 'infrastructure/daemon/irc/handler/differentialnotification',
|
||||||
'PhabricatorIRCHandler' => 'infrastructure/daemon/irc/handler/base',
|
'PhabricatorIRCHandler' => 'infrastructure/daemon/irc/handler/base',
|
||||||
'PhabricatorIRCMessage' => 'infrastructure/daemon/irc/message',
|
'PhabricatorIRCMessage' => 'infrastructure/daemon/irc/message',
|
||||||
'PhabricatorIRCObjectNameHandler' => 'infrastructure/daemon/irc/handler/objectname',
|
'PhabricatorIRCObjectNameHandler' => 'infrastructure/daemon/irc/handler/objectname',
|
||||||
|
@ -1157,6 +1158,7 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorHelpController' => 'PhabricatorController',
|
'PhabricatorHelpController' => 'PhabricatorController',
|
||||||
'PhabricatorHelpKeyboardShortcutController' => 'PhabricatorHelpController',
|
'PhabricatorHelpKeyboardShortcutController' => 'PhabricatorHelpController',
|
||||||
'PhabricatorIRCBot' => 'PhabricatorDaemon',
|
'PhabricatorIRCBot' => 'PhabricatorDaemon',
|
||||||
|
'PhabricatorIRCDifferentialNotificationHandler' => 'PhabricatorIRCHandler',
|
||||||
'PhabricatorIRCObjectNameHandler' => 'PhabricatorIRCHandler',
|
'PhabricatorIRCObjectNameHandler' => 'PhabricatorIRCHandler',
|
||||||
'PhabricatorIRCProtocolHandler' => 'PhabricatorIRCHandler',
|
'PhabricatorIRCProtocolHandler' => 'PhabricatorIRCHandler',
|
||||||
'PhabricatorInfrastructureTestCase' => 'PhabricatorTestCase',
|
'PhabricatorInfrastructureTestCase' => 'PhabricatorTestCase',
|
||||||
|
|
|
@ -33,6 +33,7 @@ These are the configuration values it reads:
|
||||||
IRCBot.
|
IRCBot.
|
||||||
- ##conduit.uri##, ##conduit.user##, ##conduit.cert## Conduit configuration,
|
- ##conduit.uri##, ##conduit.user##, ##conduit.cert## Conduit configuration,
|
||||||
see below.
|
see below.
|
||||||
|
- ##notification.channels## Notification configuration, see below.
|
||||||
|
|
||||||
= Handlers =
|
= Handlers =
|
||||||
|
|
||||||
|
@ -46,6 +47,9 @@ the IRC bot. These are the default handlers available:
|
||||||
- @{class:PhabricatorIRCObjectNameHandler} This handler looks for users
|
- @{class:PhabricatorIRCObjectNameHandler} This handler looks for users
|
||||||
mentioning Phabricator objects like "T123" and "D345" in chat, looks them
|
mentioning Phabricator objects like "T123" and "D345" in chat, looks them
|
||||||
up, and says their name with a link to the object. Requires conduit.
|
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
|
You can also write your own handlers, by extending
|
||||||
@{class:PhabricatorIRCHandler}.
|
@{class:PhabricatorIRCHandler}.
|
||||||
|
@ -53,9 +57,9 @@ You can also write your own handlers, by extending
|
||||||
= Conduit =
|
= Conduit =
|
||||||
|
|
||||||
Some handlers (e.g., @{class:PhabricatorIRCObjectNameHandler}) need to read data
|
Some handlers (e.g., @{class:PhabricatorIRCObjectNameHandler}) need to read data
|
||||||
from Phabricator. They do this over Conduit, Phabricator's HTTP API. You can
|
from Phabricator over Conduit, Phabricator's HTTP API. You can use this method
|
||||||
use this method to allow other scripts or programs to access Phabricator's data
|
to allow other scripts or programs to access Phabricator's data from different
|
||||||
from different servers and in different languages.
|
servers and in different languages.
|
||||||
|
|
||||||
To allow the bot to access Conduit, you need to create a user that it can login
|
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
|
with. To do this, login to Phabricator as an administrator and go to
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 Facebook, Inc.
|
* Copyright 2012 Facebook, Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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();
|
$routed_message = $this->processReadBuffer();
|
||||||
} while ($routed_message);
|
} while ($routed_message);
|
||||||
|
|
||||||
|
foreach ($this->handlers as $handler) {
|
||||||
|
$handler->runBackgroundTasks();
|
||||||
|
}
|
||||||
|
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright 2011 Facebook, Inc.
|
* Copyright 2012 Facebook, Inc.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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);
|
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