From 370b5a0fd7c1dba60e3b973506ba087adba42be0 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 18 Apr 2015 18:44:45 -0400 Subject: Add Slack and Hipchat integrations for each projects --- app/Integration/Base.php | 1 + app/Integration/Hipchat.php | 90 +++++++++++++++++++++++++++++----------- app/Integration/SlackWebhook.php | 60 ++++++++++++++++++++------- 3 files changed, 113 insertions(+), 38 deletions(-) (limited to 'app/Integration') diff --git a/app/Integration/Base.php b/app/Integration/Base.php index c6387fe2..9daa3eb0 100644 --- a/app/Integration/Base.php +++ b/app/Integration/Base.php @@ -11,6 +11,7 @@ use Pimple\Container; * @author Frederic Guillot * * @property \Model\ProjectActivity $projectActivity + * @property \Model\ProjectIntegration $projectIntegration * @property \Model\Task $task * @property \Model\TaskFinder $taskFinder * @property \Model\User $user diff --git a/app/Integration/Hipchat.php b/app/Integration/Hipchat.php index 1306af6d..d0a48e42 100644 --- a/app/Integration/Hipchat.php +++ b/app/Integration/Hipchat.php @@ -3,7 +3,7 @@ namespace Integration; /** - * Hipchat Webhook + * Hipchat * * @package integration * @author Frederic Guillot @@ -11,7 +11,45 @@ namespace Integration; class Hipchat extends Base { /** - * Send message to the Hipchat room + * Return true if Hipchat is enabled for this project or globally + * + * @access public + * @param integer $project_id + * @return boolean + */ + public function isActivated($project_id) + { + return $this->config->get('integration_hipchat') == 1 || $this->projectIntegration->hasValue($project_id, 'hipchat', 1); + } + + /** + * Get API parameters + * + * @access public + * @param integer $project_id + * @return array + */ + public function getParameters($project_id) + { + if ($this->config->get('integration_hipchat') == 1) { + return array( + 'api_url' => $this->config->get('integration_hipchat_api_url'), + 'room_id' => $this->config->get('integration_hipchat_room_id'), + 'room_token' => $this->config->get('integration_hipchat_room_token'), + ); + } + + $options = $this->projectIntegration->getParameters($project_id); + + return array( + 'api_url' => $options['hipchat_api_url'], + 'room_id' => $options['hipchat_room_id'], + 'room_token' => $options['hipchat_room_token'], + ); + } + + /** + * Send the notification if activated * * @access public * @param integer $project_id Project id @@ -21,33 +59,37 @@ class Hipchat extends Base */ public function notify($project_id, $task_id, $event_name, array $event) { - $project = $this->project->getbyId($project_id); + if ($this->isActivated($project_id)) { - $event['event_name'] = $event_name; - $event['author'] = $this->user->getFullname($this->session['user']); + $params = $this->getParameters($project_id); + $project = $this->project->getbyId($project_id); - $html = ''; - $html .= ''.$project['name'].'
'; - $html .= $this->projectActivity->getTitle($event); + $event['event_name'] = $event_name; + $event['author'] = $this->user->getFullname($this->session['user']); - if ($this->config->get('application_url')) { - $html .= '
'; - $html .= t('view the task on Kanboard').''; - } + $html = ''; + $html .= ''.$project['name'].''.(isset($event['task']['title']) ? '
'.$event['task']['title'] : '').'
'; + $html .= $this->projectActivity->getTitle($event); - $payload = array( - 'message' => $html, - 'color' => 'yellow', - ); + if ($this->config->get('application_url')) { + $html .= '
'; + $html .= t('view the task on Kanboard').''; + } - $url = sprintf( - '%s/v2/room/%s/notification?auth_token=%s', - $this->config->get('integration_hipchat_api_url'), - $this->config->get('integration_hipchat_room_id'), - $this->config->get('integration_hipchat_room_token') - ); + $payload = array( + 'message' => $html, + 'color' => 'yellow', + ); - $this->httpClient->post($url, $payload); + $url = sprintf( + '%s/v2/room/%s/notification?auth_token=%s', + $params['api_url'], + $params['room_id'], + $params['room_token'] + ); + + $this->httpClient->post($url, $payload); + } } } diff --git a/app/Integration/SlackWebhook.php b/app/Integration/SlackWebhook.php index 1c2ea781..b64096fb 100644 --- a/app/Integration/SlackWebhook.php +++ b/app/Integration/SlackWebhook.php @@ -10,6 +10,35 @@ namespace Integration; */ class SlackWebhook extends Base { + /** + * Return true if Slack is enabled for this project or globally + * + * @access public + * @param integer $project_id + * @return boolean + */ + public function isActivated($project_id) + { + return $this->config->get('integration_slack_webhook') == 1 || $this->projectIntegration->hasValue($project_id, 'slack', 1); + } + + /** + * Get wehbook url + * + * @access public + * @param integer $project_id + * @return string + */ + public function getWebhookUrl($project_id) + { + if ($this->config->get('integration_slack_webhook') == 1) { + return $this->config->get('integration_slack_webhook_url'); + } + + $options = $this->projectIntegration->getParameters($project_id); + return $options['slack_webhook_url']; + } + /** * Send message to the incoming Slack webhook * @@ -21,23 +50,26 @@ class SlackWebhook extends Base */ public function notify($project_id, $task_id, $event_name, array $event) { - $project = $this->project->getbyId($project_id); + if ($this->isActivated($project_id)) { - $event['event_name'] = $event_name; - $event['author'] = $this->user->getFullname($this->session['user']); + $project = $this->project->getbyId($project_id); - $payload = array( - 'text' => '*['.$project['name'].']* '.str_replace('"', '"', $this->projectActivity->getTitle($event)), - 'username' => 'Kanboard', - 'icon_url' => 'http://kanboard.net/assets/img/favicon.png', - ); + $event['event_name'] = $event_name; + $event['author'] = $this->user->getFullname($this->session['user']); - if ($this->config->get('application_url')) { - $payload['text'] .= ' - <'.$this->config->get('application_url'); - $payload['text'] .= $this->helper->u('task', 'show', array('task_id' => $task_id, 'project_id' => $project_id)); - $payload['text'] .= '|'.t('view the task on Kanboard').'>'; - } + $payload = array( + 'text' => '*['.$project['name'].']* '.str_replace('"', '"', $this->projectActivity->getTitle($event)).(isset($event['task']['title']) ? ' ('.$event['task']['title'].')' : ''), + 'username' => 'Kanboard', + 'icon_url' => 'http://kanboard.net/assets/img/favicon.png', + ); - $this->httpClient->post($this->config->get('integration_slack_webhook_url'), $payload); + if ($this->config->get('application_url')) { + $payload['text'] .= ' - <'.$this->config->get('application_url'); + $payload['text'] .= $this->helper->u('task', 'show', array('task_id' => $task_id, 'project_id' => $project_id)); + $payload['text'] .= '|'.t('view the task on Kanboard').'>'; + } + + $this->httpClient->post($this->getWebhookUrl($project_id), $payload); + } } } -- cgit v1.2.3