From 45d243f0ab93daae869ffb43180cb7abc7a36786 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Tue, 17 Nov 2015 18:52:08 +0100 Subject: [PATCH 1/3] Always send the "from" name in the hipchat handler --- src/Monolog/Handler/HipChatHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/HipChatHandler.php b/src/Monolog/Handler/HipChatHandler.php index 34d3437f..fca90b7f 100644 --- a/src/Monolog/Handler/HipChatHandler.php +++ b/src/Monolog/Handler/HipChatHandler.php @@ -141,12 +141,12 @@ class HipChatHandler extends SocketHandler 'message' => $record['formatted'], 'message_format' => $this->format, 'color' => $this->getAlertColor($record['level']), + 'from' => $this->name, ); // 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); From fa38530fdca0147f336d29b134b3140b69701541 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Wed, 18 Nov 2015 10:25:04 +0100 Subject: [PATCH 2/3] Always append "from" field when sending to HipChat --- src/Monolog/Handler/HipChatHandler.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Handler/HipChatHandler.php b/src/Monolog/Handler/HipChatHandler.php index fca90b7f..f9da86eb 100644 --- a/src/Monolog/Handler/HipChatHandler.php +++ b/src/Monolog/Handler/HipChatHandler.php @@ -141,7 +141,6 @@ class HipChatHandler extends SocketHandler 'message' => $record['formatted'], 'message_format' => $this->format, 'color' => $this->getAlertColor($record['level']), - 'from' => $this->name, ); // if we are using the legacy API then we need to send some additional information @@ -149,6 +148,12 @@ class HipChatHandler extends SocketHandler $dataArray['room_id'] = $this->room; } + // append the sender name if it is set + // always append it if we use the v1 api (it is required in v1) + if ($this->version == self::API_V1 || $this->name !== null) { + $dataArray['from'] = (string) $this->name; + } + return http_build_query($dataArray); } From 538349c4c4790bcfd6cb1d663c12d0df9ff6afc8 Mon Sep 17 00:00:00 2001 From: Jannik Zschiesche Date: Wed, 18 Nov 2015 10:25:09 +0100 Subject: [PATCH 3/3] Fixed HipChat tests --- tests/Monolog/Handler/HipChatHandlerTest.php | 29 ++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/Monolog/Handler/HipChatHandlerTest.php b/tests/Monolog/Handler/HipChatHandlerTest.php index 462dac86..b158a031 100644 --- a/tests/Monolog/Handler/HipChatHandlerTest.php +++ b/tests/Monolog/Handler/HipChatHandlerTest.php @@ -21,6 +21,7 @@ use Monolog\Logger; class HipChatHandlerTest extends TestCase { private $res; + /** @var HipChatHandler */ private $handler; public function testWriteHeader() @@ -91,6 +92,18 @@ class HipChatHandlerTest extends TestCase $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=Monolog$/', $content); } + public function testWriteContentV1WithoutName() + { + $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v1'); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/notify=0&message=test1&message_format=text&color=red&room_id=room1&from=$/', $content); + + return $content; + } + /** * @depends testWriteCustomHostHeader */ @@ -104,7 +117,7 @@ class HipChatHandlerTest extends TestCase */ public function testWriteContentV2($content) { - $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content); + $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red&from=Monolog$/', $content); } /** @@ -112,7 +125,19 @@ class HipChatHandlerTest extends TestCase */ public function testWriteContentV2Notify($content) { - $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red$/', $content); + $this->assertRegexp('/notify=true&message=test1&message_format=text&color=red&from=Monolog$/', $content); + } + + public function testWriteContentV2WithoutName() + { + $this->createHandler('myToken', 'room1', null, false, 'hipchat.foo.bar', 'v2'); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/notify=false&message=test1&message_format=text&color=red$/', $content); + + return $content; } public function testWriteWithComplexMessage()