summaryrefslogtreecommitdiff
path: root/app/Integration
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-04-18 18:44:45 -0400
committerFrederic Guillot <fred@kanboard.net>2015-04-18 18:44:45 -0400
commit370b5a0fd7c1dba60e3b973506ba087adba42be0 (patch)
tree8da109b4fc90062d6eebb69d4ae2efca4da1bac3 /app/Integration
parentf53bb88d10836e5c31efb958683d8bf3829eecbf (diff)
Add Slack and Hipchat integrations for each projects
Diffstat (limited to 'app/Integration')
-rw-r--r--app/Integration/Base.php1
-rw-r--r--app/Integration/Hipchat.php90
-rw-r--r--app/Integration/SlackWebhook.php60
3 files changed, 113 insertions, 38 deletions
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 = '<img src="http://kanboard.net/assets/img/favicon-32x32.png"/>';
- $html .= '<strong>'.$project['name'].'</strong><br/>';
- $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 .= '<br/><a href="'.$this->config->get('application_url');
- $html .= $this->helper->u('task', 'show', array('task_id' => $task_id, 'project_id' => $project_id)).'">';
- $html .= t('view the task on Kanboard').'</a>';
- }
+ $html = '<img src="http://kanboard.net/assets/img/favicon-32x32.png"/>';
+ $html .= '<strong>'.$project['name'].'</strong>'.(isset($event['task']['title']) ? '<br/>'.$event['task']['title'] : '').'<br/>';
+ $html .= $this->projectActivity->getTitle($event);
- $payload = array(
- 'message' => $html,
- 'color' => 'yellow',
- );
+ if ($this->config->get('application_url')) {
+ $html .= '<br/><a href="'.$this->config->get('application_url');
+ $html .= $this->helper->u('task', 'show', array('task_id' => $task_id, 'project_id' => $project_id)).'">';
+ $html .= t('view the task on Kanboard').'</a>';
+ }
- $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
@@ -11,6 +11,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
*
* @access public
@@ -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('&quot;', '"', $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('&quot;', '"', $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);
+ }
}
}