From aea5d157deadc4101e390fee08a612fc083dcdbe Mon Sep 17 00:00:00 2001 From: Ivan Klimchuk Date: Thu, 24 Sep 2015 12:59:46 +0300 Subject: [PATCH] Added new plugin to library and some improvements --- _build/build.transport.php | 2 +- _build/data/transport.plugins.php | 8 ++- .../plugins/NewCommentTicketsSlackify.php | 58 +++++++++++++++++++ .../model/slackify/Attachment/Field.php | 4 +- .../slackify/Attachment/Link.php} | 21 +++++++ .../model/slackify/Slackify.class.php | 1 + docs/changelog.txt | 6 ++ 7 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 core/components/slackify/elements/plugins/NewCommentTicketsSlackify.php rename core/components/slackify/{elements/plugins/TicketsSlackNotify.php => model/slackify/Attachment/Link.php} (74%) diff --git a/_build/build.transport.php b/_build/build.transport.php index 5e65fe4..de606a5 100644 --- a/_build/build.transport.php +++ b/_build/build.transport.php @@ -37,7 +37,7 @@ define('PKG_NAME', 'Slackify'); define('PKG_NAME_LOWER', strtolower(PKG_NAME)); -define('PKG_VERSION', '0.5.0'); +define('PKG_VERSION', '0.6.2'); define('PKG_RELEASE', 'beta'); require_once 'xpdo/xpdo/xpdo.class.php'; diff --git a/_build/data/transport.plugins.php b/_build/data/transport.plugins.php index 34d470f..1d60589 100644 --- a/_build/data/transport.plugins.php +++ b/_build/data/transport.plugins.php @@ -27,10 +27,16 @@ $list = [ 'PageNotFoundSlackify' => [ - 'description' => 'This plugin sends message to defined channel in Slack when 404 error occurs on site.', + 'description' => 'This plugin sends messages to defined channel in Slack when 404 error occurs on site.', 'events' => [ 'OnPageNotFound' ] + ], + 'NewCommentTicketsSlackify' => [ + 'description' => 'This plugin sends messages to defined channel in Slack when somebody add new comment to a ticket.', + 'events' => [ + 'OnCommentSave' + ] ] ]; diff --git a/core/components/slackify/elements/plugins/NewCommentTicketsSlackify.php b/core/components/slackify/elements/plugins/NewCommentTicketsSlackify.php new file mode 100644 index 0000000..96d9e62 --- /dev/null +++ b/core/components/slackify/elements/plugins/NewCommentTicketsSlackify.php @@ -0,0 +1,58 @@ +event->name) { + case 'OnCommentSave': + + $slackify = $modx->getService('slackify'); + + /** @var TicketComment $comment */ + $comment =& $object; + + /** @var TicketThread $ticket */ + $thread = $comment->getOne('Thread'); + + /** @var Ticket $ticket */ + $ticket = $thread->getOne('Ticket'); + + $a = new Attachment(); + $a->setPretext('Somebody left mention to the important theme on site'); + + $a->setColor(new Color('#00FF00')); // green + $a->setAuthor(new Author($comment->get('name'), 'mailto:' . $comment->get('email'))); + + $a->setTitle(new Title("left comment to ticket '{$ticket->get('pagetitle')}'")); + $a->setText($comment->get('text')); // raw + + $a->addField(new Field('When', $comment->get('createdon'), true)); + $a->addField(new Field('Published', $comment->get('published') ? 'True' : 'False', true)); + $a->addField(new Field('Ticket', new Link($modx->makeUrl($ticket->get('id'), 'web', '', 'full'), $ticket->get('pagetitle')))); + + $message = new Message('*New comment*'); + $message->attach($a); + + $slackify->send($message); + break; +} diff --git a/core/components/slackify/model/slackify/Attachment/Field.php b/core/components/slackify/model/slackify/Attachment/Field.php index d6bb12c..4b11ee8 100644 --- a/core/components/slackify/model/slackify/Attachment/Field.php +++ b/core/components/slackify/model/slackify/Attachment/Field.php @@ -58,8 +58,8 @@ public function equalsTo(Field $that) public function jsonSerialize() { return [ - 'title' => $this->title, - 'value' => $this->value, + 'title' => (string) $this->title, + 'value' => (string) $this->value, 'short' => (boolean) $this->short ]; } diff --git a/core/components/slackify/elements/plugins/TicketsSlackNotify.php b/core/components/slackify/model/slackify/Attachment/Link.php similarity index 74% rename from core/components/slackify/elements/plugins/TicketsSlackNotify.php rename to core/components/slackify/model/slackify/Attachment/Link.php index e655587..85aae55 100644 --- a/core/components/slackify/elements/plugins/TicketsSlackNotify.php +++ b/core/components/slackify/model/slackify/Attachment/Link.php @@ -23,3 +23,24 @@ * SOFTWARE. */ +class Link +{ + protected $url; + protected $title = ''; + + public function __construct($url, $title = '') + { + $this->url = $url; + $this->title = $title; + } + + public function __toString() + { + $link = $this->url; + if ($this->title) { + $link .= '|' . $this->title; + } + + return str_replace('{link}', $link, '<{link}>'); + } +} diff --git a/core/components/slackify/model/slackify/Slackify.class.php b/core/components/slackify/model/slackify/Slackify.class.php index 28313a6..a2f6adc 100644 --- a/core/components/slackify/model/slackify/Slackify.class.php +++ b/core/components/slackify/model/slackify/Slackify.class.php @@ -27,6 +27,7 @@ include_once 'Attachment/Color.php'; include_once 'Attachment/Field.php'; include_once 'Attachment/Title.php'; +include_once 'Attachment/Link.php'; include_once 'Attachment.php'; include_once 'Message.php'; diff --git a/docs/changelog.txt b/docs/changelog.txt index cead050..d5f2571 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -1,6 +1,12 @@ This file shows the changes in recent releases of Slackify. +Slackify 0.6.2-beta (September 24, 2015) +======================================== +- Added class Link for generates valid links for Slack +- Added plugin NewCommentTicketsSlackify for send notifies when new comments added + Slackify 0.5.0-beta (September 23, 2015) +======================================== - Added plugin for notify to Slack when 404 error happens Slackify 0.4.10-alpha (September 23, 2015)