diff options
Diffstat (limited to 'app/Model')
-rw-r--r-- | app/Model/Acl.php | 3 | ||||
-rw-r--r-- | app/Model/Action.php | 1 | ||||
-rw-r--r-- | app/Model/GithubWebhook.php | 81 | ||||
-rw-r--r-- | app/Model/Task.php | 18 |
4 files changed, 102 insertions, 1 deletions
diff --git a/app/Model/Acl.php b/app/Model/Acl.php index aea13e8c..f92b3021 100644 --- a/app/Model/Acl.php +++ b/app/Model/Acl.php @@ -18,9 +18,10 @@ class Acl extends Base */ private $public_actions = array( 'user' => array('login', 'check', 'google', 'github'), - 'task' => array('add', 'readonly'), + 'task' => array('readonly'), 'board' => array('readonly'), 'project' => array('feed'), + 'webhook' => array('task', 'github'), ); /** diff --git a/app/Model/Action.php b/app/Model/Action.php index a318c5b0..a0c992aa 100644 --- a/app/Model/Action.php +++ b/app/Model/Action.php @@ -65,6 +65,7 @@ class Action extends Base Task::EVENT_CLOSE => t('Closing a task'), Task::EVENT_CREATE_UPDATE => t('Task creation or modification'), Task::EVENT_ASSIGNEE_CHANGE => t('Task assignee change'), + GithubWebhook::EVENT_COMMIT => t('Github commit received'), ); } diff --git a/app/Model/GithubWebhook.php b/app/Model/GithubWebhook.php new file mode 100644 index 00000000..688ab601 --- /dev/null +++ b/app/Model/GithubWebhook.php @@ -0,0 +1,81 @@ +<?php + +namespace Model; + +/** + * Github Webhook model + * + * @package model + * @author Frederic Guillot + */ +class GithubWebhook extends Base +{ + /** + * Events + * + * @var string + */ + const EVENT_ISSUE_OPENED = 'github.webhook.issue.opened'; + const EVENT_ISSUE_CLOSED = 'github.webhook.issue.closed'; + const EVENT_ISSUE_LABELED = 'github.webhook.issue.labeled'; + const EVENT_ISSUE_COMMENT = 'github.webhook.issue.commented'; + const EVENT_COMMIT = 'github.webhook.commit'; + + /** + * Parse Github events + * + * @access public + * @param string $type Github event type + * @param string $payload Raw Github event (JSON) + */ + public function parsePayload($type, $payload) + { + $payload = json_decode($payload, true); + + switch ($type) { + case 'push': + return $this->parsePushEvent($payload); + case 'issues': + return $this->parseIssueEvent($payload); + } + } + + /** + * Parse Push events (list of commits) + * + * @access public + * @param array $payload Event data + */ + public function parsePushEvent(array $payload) + { + foreach ($payload['commits'] as $commit) { + + $task_id = $this->task->getTaskIdFromText($commit['message']); + + if (! $task_id) { + continue; + } + + $task = $this->task->getById($task_id); + + if (! $task) { + continue; + } + + if ($task['is_active'] == Task::STATUS_OPEN) { + $this->event->trigger(self::EVENT_COMMIT, array('task_id' => $task_id) + $task); + } + } + } + + /** + * Parse issue events + * + * @access public + * @param array $payload Event data + */ + public function parseIssueEvent(array $payload) + { + + } +} diff --git a/app/Model/Task.php b/app/Model/Task.php index 262e8d76..8708f128 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -696,4 +696,22 @@ class Task extends Base return false; } + + /** + * Get a the task id from a text + * + * Example: "Fix bug #1234" will return 1234 + * + * @access public + * @param string $message Text + * @return integer + */ + public function getTaskIdFromText($message) + { + if (preg_match('!#(\d+)!i', $message, $matches) && isset($matches[1])) { + return $matches[1]; + } + + return 0; + } } |