From 5266b821446b14b79a3c5a5c77c57791b985f0a9 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sun, 28 Dec 2014 22:22:15 -0500 Subject: Add Gitlab webhook --- app/Integration/Base.php | 49 +++++ app/Integration/GithubWebhook.php | 375 ++++++++++++++++++++++++++++++++++++++ app/Integration/GitlabWebhook.php | 213 ++++++++++++++++++++++ 3 files changed, 637 insertions(+) create mode 100644 app/Integration/Base.php create mode 100644 app/Integration/GithubWebhook.php create mode 100644 app/Integration/GitlabWebhook.php (limited to 'app/Integration') diff --git a/app/Integration/Base.php b/app/Integration/Base.php new file mode 100644 index 00000000..babf8c8f --- /dev/null +++ b/app/Integration/Base.php @@ -0,0 +1,49 @@ +container = $container; + } + + /** + * Load automatically class from the container + * + * @access public + * @param string $name + * @return mixed + */ + public function __get($name) + { + return $this->container[$name]; + } +} diff --git a/app/Integration/GithubWebhook.php b/app/Integration/GithubWebhook.php new file mode 100644 index 00000000..fd0b49f6 --- /dev/null +++ b/app/Integration/GithubWebhook.php @@ -0,0 +1,375 @@ +project_id = $project_id; + } + + /** + * Parse Github events + * + * @access public + * @param string $type Github event type + * @param array $payload Github event + * @return boolean + */ + public function parsePayload($type, array $payload) + { + switch ($type) { + case 'push': + return $this->parsePushEvent($payload); + case 'issues': + return $this->parseIssueEvent($payload); + case 'issue_comment': + return $this->parseCommentIssueEvent($payload); + } + + return false; + } + + /** + * Parse Push events (list of commits) + * + * @access public + * @param array $payload Event data + * @return boolean + */ + public function parsePushEvent(array $payload) + { + foreach ($payload['commits'] as $commit) { + + $task_id = $this->task->getTaskIdFromText($commit['message']); + + if (! $task_id) { + continue; + } + + $task = $this->taskFinder->getById($task_id); + + if (! $task) { + continue; + } + + if ($task['is_active'] == Task::STATUS_OPEN && $task['project_id'] == $this->project_id) { + $this->container['dispatcher']->dispatch( + self::EVENT_COMMIT, + new GenericEvent(array('task_id' => $task_id) + $task) + ); + } + } + + return true; + } + + /** + * Parse issue events + * + * @access public + * @param array $payload Event data + * @return boolean + */ + public function parseIssueEvent(array $payload) + { + switch ($payload['action']) { + case 'opened': + return $this->handleIssueOpened($payload['issue']); + case 'closed': + return $this->handleIssueClosed($payload['issue']); + case 'reopened': + return $this->handleIssueReopened($payload['issue']); + case 'assigned': + return $this->handleIssueAssigned($payload['issue']); + case 'unassigned': + return $this->handleIssueUnassigned($payload['issue']); + case 'labeled': + return $this->handleIssueLabeled($payload['issue'], $payload['label']); + case 'unlabeled': + return $this->handleIssueUnlabeled($payload['issue'], $payload['label']); + } + + return false; + } + + /** + * Parse comment issue events + * + * @access public + * @param array $payload Event data + * @return boolean + */ + public function parseCommentIssueEvent(array $payload) + { + $task = $this->taskFinder->getByReference($payload['issue']['number']); + $user = $this->user->getByUsername($payload['comment']['user']['login']); + + if ($task && $user) { + + $event = array( + 'project_id' => $this->project_id, + 'reference' => $payload['comment']['id'], + 'comment' => $payload['comment']['body'], + 'user_id' => $user['id'], + 'task_id' => $task['id'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_COMMENT, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle new issues + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueOpened(array $issue) + { + $event = array( + 'project_id' => $this->project_id, + 'reference' => $issue['number'], + 'title' => $issue['title'], + 'description' => $issue['body']."\n\n[".t('Github Issue').']('.$issue['html_url'].')', + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_OPENED, + new GenericEvent($event) + ); + + return true; + } + + /** + * Handle issue closing + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueClosed(array $issue) + { + $task = $this->taskFinder->getByReference($issue['number']); + + if ($task) { + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['number'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_CLOSED, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle issue reopened + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueReopened(array $issue) + { + $task = $this->taskFinder->getByReference($issue['number']); + + if ($task) { + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['number'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_REOPENED, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle issue assignee change + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueAssigned(array $issue) + { + $user = $this->user->getByUsername($issue['assignee']['login']); + $task = $this->taskFinder->getByReference($issue['number']); + + if ($user && $task) { + + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'owner_id' => $user['id'], + 'reference' => $issue['number'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_ASSIGNEE_CHANGE, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle unassigned issue + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueUnassigned(array $issue) + { + $task = $this->taskFinder->getByReference($issue['number']); + + if ($task) { + + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'owner_id' => 0, + 'reference' => $issue['number'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_ASSIGNEE_CHANGE, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle labeled issue + * + * @access public + * @param array $issue Issue data + * @param array $label Label data + * @return boolean + */ + public function handleIssueLabeled(array $issue, array $label) + { + $task = $this->taskFinder->getByReference($issue['number']); + + if ($task) { + + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['number'], + 'label' => $label['name'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_LABEL_CHANGE, + new GenericEvent($event) + ); + + return true; + } + + return false; + } + + /** + * Handle unlabeled issue + * + * @access public + * @param array $issue Issue data + * @param array $label Label data + * @return boolean + */ + public function handleIssueUnlabeled(array $issue, array $label) + { + $task = $this->taskFinder->getByReference($issue['number']); + + if ($task) { + + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['number'], + 'label' => $label['name'], + 'category_id' => 0, + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_LABEL_CHANGE, + new GenericEvent($event) + ); + + return true; + } + + return false; + } +} diff --git a/app/Integration/GitlabWebhook.php b/app/Integration/GitlabWebhook.php new file mode 100644 index 00000000..f5df32a6 --- /dev/null +++ b/app/Integration/GitlabWebhook.php @@ -0,0 +1,213 @@ +project_id = $project_id; + } + + /** + * Parse events + * + * @access public + * @param array $payload Gitlab event + * @return boolean + */ + public function parsePayload(array $payload) + { + switch ($this->getType($payload)) { + case self::TYPE_PUSH: + return $this->handlePushEvent($payload); + case self::TYPE_ISSUE; + return $this->handleIssueEvent($payload); + } + + return false; + } + + /** + * Get event type + * + * @access public + * @param array $payload Gitlab event + * @return string + */ + public function getType(array $payload) + { + if (isset($payload['object_kind']) && $payload['object_kind'] === 'issue') { + return self::TYPE_ISSUE; + } + + if (isset($payload['commits'])) { + return self::TYPE_PUSH; + } + + return ''; + } + + /** + * Parse push event + * + * @access public + * @param array $payload Gitlab event + * @return boolean + */ + public function handlePushEvent(array $payload) + { + foreach ($payload['commits'] as $commit) { + $this->handleCommit($commit); + } + + return true; + } + + /** + * Parse commit + * + * @access public + * @param array $commit Gitlab commit + * @return boolean + */ + public function handleCommit(array $commit) + { + $task_id = $this->task->getTaskIdFromText($commit['message']); + + if (! $task_id) { + return false; + } + + $task = $this->taskFinder->getById($task_id); + + if (! $task) { + return false; + } + + if ($task['is_active'] == Task::STATUS_OPEN && $task['project_id'] == $this->project_id) { + + $this->container['dispatcher']->dispatch( + self::EVENT_COMMIT, + new TaskEvent(array('task_id' => $task_id) + $task) + ); + + return true; + } + + return false; + } + + /** + * Parse issue event + * + * @access public + * @param array $payload Gitlab event + * @return boolean + */ + public function handleIssueEvent(array $payload) + { + switch ($payload['object_attributes']['state']) { + case 'opened': + return $this->handleIssueOpened($payload['object_attributes']); + case 'closed': + return $this->handleIssueClosed($payload['object_attributes']); + } + + return false; + } + + /** + * Handle new issues + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueOpened(array $issue) + { + $event = array( + 'project_id' => $this->project_id, + 'reference' => $issue['id'], + 'title' => $issue['title'], + 'description' => $issue['description']."\n\n[".t('Gitlab Issue').']('.$issue['url'].')', + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_OPENED, + new GenericEvent($event) + ); + + return true; + } + + /** + * Handle issue closing + * + * @access public + * @param array $issue Issue data + * @return boolean + */ + public function handleIssueClosed(array $issue) + { + $task = $this->taskFinder->getByReference($issue['id']); + + if ($task) { + $event = array( + 'project_id' => $this->project_id, + 'task_id' => $task['id'], + 'reference' => $issue['id'], + ); + + $this->container['dispatcher']->dispatch( + self::EVENT_ISSUE_CLOSED, + new GenericEvent($event) + ); + + return true; + } + + return false; + } +} -- cgit v1.2.3