mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-13 16:44:23 +02:00
@@ -21,12 +21,23 @@ use Monolog\Logger;
|
|||||||
* Room - HipChat Room Id or name, where messages are sent
|
* Room - HipChat Room Id or name, where messages are sent
|
||||||
* Name - Name used to send the message (from)
|
* Name - Name used to send the message (from)
|
||||||
* notify - Should the message trigger a notification in the clients
|
* notify - Should the message trigger a notification in the clients
|
||||||
|
* version - The API version to use (HipChatHandler::API_V1 | HipChatHandler::API_V2)
|
||||||
*
|
*
|
||||||
* @author Rafael Dohms <rafael@doh.ms>
|
* @author Rafael Dohms <rafael@doh.ms>
|
||||||
* @see https://www.hipchat.com/docs/api
|
* @see https://www.hipchat.com/docs/api
|
||||||
*/
|
*/
|
||||||
class HipChatHandler extends SocketHandler
|
class HipChatHandler extends SocketHandler
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Use API version 1
|
||||||
|
*/
|
||||||
|
const API_V1 = 'v1';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use API version v2
|
||||||
|
*/
|
||||||
|
const API_V2 = 'v2';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The maximum allowed length for the name used in the "from" field.
|
* The maximum allowed length for the name used in the "from" field.
|
||||||
*/
|
*/
|
||||||
@@ -68,19 +79,25 @@ class HipChatHandler extends SocketHandler
|
|||||||
private $host;
|
private $host;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $token HipChat API Token
|
* @var string
|
||||||
* @param string $room The room that should be alerted of the message (Id or Name)
|
|
||||||
* @param string $name Name used in the "from" field
|
|
||||||
* @param bool $notify Trigger a notification in clients or not
|
|
||||||
* @param int $level The minimum logging level at which this handler will be triggered
|
|
||||||
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
|
|
||||||
* @param bool $useSSL Whether to connect via SSL.
|
|
||||||
* @param string $format The format of the messages (default to text, can be set to html if you have html in the messages)
|
|
||||||
* @param string $host The HipChat server hostname.
|
|
||||||
*/
|
*/
|
||||||
public function __construct($token, $room, $name = 'Monolog', $notify = false, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $format = 'text', $host = 'api.hipchat.com')
|
private $version;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $token HipChat API Token
|
||||||
|
* @param string $room The room that should be alerted of the message (Id or Name)
|
||||||
|
* @param string $name Name used in the "from" field. Not used for v2
|
||||||
|
* @param bool $notify Trigger a notification in clients or not
|
||||||
|
* @param int $level The minimum logging level at which this handler will be triggered
|
||||||
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
|
||||||
|
* @param bool $useSSL Whether to connect via SSL.
|
||||||
|
* @param string $format The format of the messages (default to text, can be set to html if you have html in the messages)
|
||||||
|
* @param string $host The HipChat server hostname.
|
||||||
|
* @param string $version The HipChat API version (default HipChatHandler::API_V1)
|
||||||
|
*/
|
||||||
|
public function __construct($token, $room, $name = 'Monolog', $notify = false, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $format = 'text', $host = 'api.hipchat.com', $version = self::API_V1)
|
||||||
{
|
{
|
||||||
if (!$this->validateStringLength($name, static::MAXIMUM_NAME_LENGTH)) {
|
if ($version == self::API_V1 && !$this->validateStringLength($name, static::MAXIMUM_NAME_LENGTH)) {
|
||||||
throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.');
|
throw new \InvalidArgumentException('The supplied name is too long. HipChat\'s v1 API supports names up to 15 UTF-8 characters.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +110,7 @@ class HipChatHandler extends SocketHandler
|
|||||||
$this->room = $room;
|
$this->room = $room;
|
||||||
$this->format = $format;
|
$this->format = $format;
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
|
$this->version = $version;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -117,14 +135,20 @@ class HipChatHandler extends SocketHandler
|
|||||||
private function buildContent($record)
|
private function buildContent($record)
|
||||||
{
|
{
|
||||||
$dataArray = array(
|
$dataArray = array(
|
||||||
'from' => $this->name,
|
'notify' => $this->version == self::API_V1 ?
|
||||||
'room_id' => $this->room,
|
($this->notify ? 1 : 0) :
|
||||||
'notify' => $this->notify,
|
($this->notify ? 'true' : 'false'),
|
||||||
'message' => $record['formatted'],
|
'message' => $record['formatted'],
|
||||||
'message_format' => $this->format,
|
'message_format' => $this->format,
|
||||||
'color' => $this->getAlertColor($record['level']),
|
'color' => $this->getAlertColor($record['level']),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// if we are using the legacy API then we need to send some additional information
|
||||||
|
if ($this->version == self::API_V1) {
|
||||||
|
$dataArray['room_id'] = $this->room;
|
||||||
|
$dataArray['from'] = $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
return http_build_query($dataArray);
|
return http_build_query($dataArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,7 +160,14 @@ class HipChatHandler extends SocketHandler
|
|||||||
*/
|
*/
|
||||||
private function buildHeader($content)
|
private function buildHeader($content)
|
||||||
{
|
{
|
||||||
$header = "POST /v1/rooms/message?format=json&auth_token=".$this->token." HTTP/1.1\r\n";
|
if ($this->version == self::API_V1) {
|
||||||
|
$header = "POST /v1/rooms/message?format=json&auth_token={$this->token} HTTP/1.1\r\n";
|
||||||
|
} else {
|
||||||
|
// needed for rooms with special (spaces, etc) characters in the name
|
||||||
|
$room = rawurlencode($this->room);
|
||||||
|
$header = "POST /v2/room/{$room}/notification?auth_token={$this->token} HTTP/1.1\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
$header .= "Host: {$this->host}\r\n";
|
$header .= "Host: {$this->host}\r\n";
|
||||||
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
|
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
|
||||||
$header .= "Content-Length: " . strlen($content) . "\r\n";
|
$header .= "Content-Length: " . strlen($content) . "\r\n";
|
||||||
|
@@ -37,7 +37,7 @@ class HipChatHandlerTest extends TestCase
|
|||||||
|
|
||||||
public function testWriteCustomHostHeader()
|
public function testWriteCustomHostHeader()
|
||||||
{
|
{
|
||||||
$this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar');
|
$this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar');
|
||||||
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$content = fread($this->res, 1024);
|
||||||
@@ -47,12 +47,69 @@ class HipChatHandlerTest extends TestCase
|
|||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWriteV2() {
|
||||||
|
$this->createHandler('myToken', 'room1', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||||
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||||
|
fseek($this->res, 0);
|
||||||
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
|
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testWriteV2Notify() {
|
||||||
|
$this->createHandler('myToken', 'room1', 'Monolog', true, 'hipchat.foo.bar', 'v2');
|
||||||
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||||
|
fseek($this->res, 0);
|
||||||
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
|
$this->assertRegexp('/POST \/v2\/room\/room1\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testRoomSpaces() {
|
||||||
|
$this->createHandler('myToken', 'room name', 'Monolog', false, 'hipchat.foo.bar', 'v2');
|
||||||
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||||
|
fseek($this->res, 0);
|
||||||
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
|
$this->assertRegexp('/POST \/v2\/room\/room%20name\/notification\?auth_token=.* HTTP\/1.1\\r\\nHost: hipchat.foo.bar\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @depends testWriteHeader
|
* @depends testWriteHeader
|
||||||
*/
|
*/
|
||||||
public function testWriteContent($content)
|
public function testWriteContent($content)
|
||||||
{
|
{
|
||||||
$this->assertRegexp('/from=Monolog&room_id=room1¬ify=0&message=test1&message_format=text&color=red$/', $content);
|
$this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testWriteCustomHostHeader
|
||||||
|
*/
|
||||||
|
public function testWriteContentNotify($content)
|
||||||
|
{
|
||||||
|
$this->assertRegexp('/notify=1&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testWriteV2
|
||||||
|
*/
|
||||||
|
public function testWriteContentV2($content)
|
||||||
|
{
|
||||||
|
$this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testWriteV2Notify
|
||||||
|
*/
|
||||||
|
public function testWriteContentV2Notify($content)
|
||||||
|
{
|
||||||
|
$this->assertRegexp('/notify=true&message=test1&message_format=text&color=red$/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWriteWithComplexMessage()
|
public function testWriteWithComplexMessage()
|
||||||
@@ -141,9 +198,9 @@ class HipChatHandlerTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com')
|
private function createHandler($token = 'myToken', $room = 'room1', $name = 'Monolog', $notify = false, $host = 'api.hipchat.com', $version = 'v1')
|
||||||
{
|
{
|
||||||
$constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host);
|
$constructorArgs = array($token, $room, $name, $notify, Logger::DEBUG, true, true, 'text', $host, $version);
|
||||||
$this->res = fopen('php://memory', 'a');
|
$this->res = fopen('php://memory', 'a');
|
||||||
$this->handler = $this->getMock(
|
$this->handler = $this->getMock(
|
||||||
'\Monolog\Handler\HipChatHandler',
|
'\Monolog\Handler\HipChatHandler',
|
||||||
@@ -175,4 +232,9 @@ class HipChatHandlerTest extends TestCase
|
|||||||
{
|
{
|
||||||
$hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere');
|
$hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCreateWithTooLongNameV2() {
|
||||||
|
// creating a handler with too long of a name but using the v2 api doesn't matter.
|
||||||
|
$hipChatHandler = new \Monolog\Handler\HipChatHandler('token', 'room', 'SixteenCharsHere', false, Logger::CRITICAL, true, true, 'test', 'api.hipchat.com', 'v2');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user