summaryrefslogtreecommitdiff
path: root/app/Integration/BitbucketWebhook.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-02-08 21:13:59 -0500
committerFrederic Guillot <fred@kanboard.net>2015-02-08 21:13:59 -0500
commit6f94ce6af3072543ee62d64016931ed424f800a7 (patch)
tree348d250d786dba40c14828cef3fb347116a0fa1a /app/Integration/BitbucketWebhook.php
parent02f7c8d33d60b41f5c2a5335f9f6ee56f236a7fe (diff)
Add Bitbucket webhook
Diffstat (limited to 'app/Integration/BitbucketWebhook.php')
-rw-r--r--app/Integration/BitbucketWebhook.php98
1 files changed, 98 insertions, 0 deletions
diff --git a/app/Integration/BitbucketWebhook.php b/app/Integration/BitbucketWebhook.php
new file mode 100644
index 00000000..9f82d5c0
--- /dev/null
+++ b/app/Integration/BitbucketWebhook.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Integration;
+
+use Event\GenericEvent;
+use Event\TaskEvent;
+use Model\Task;
+
+/**
+ * Bitbucket Webhook
+ *
+ * @package integration
+ * @author Frederic Guillot
+ */
+class BitbucketWebhook extends Base
+{
+ /**
+ * Events
+ *
+ * @var string
+ */
+ const EVENT_COMMIT = 'bitbucket.webhook.commit';
+
+ /**
+ * Project id
+ *
+ * @access private
+ * @var integer
+ */
+ private $project_id = 0;
+
+ /**
+ * Set the project id
+ *
+ * @access public
+ * @param integer $project_id Project id
+ */
+ public function setProjectId($project_id)
+ {
+ $this->project_id = $project_id;
+ }
+
+ /**
+ * Parse events
+ *
+ * @access public
+ * @param array $payload Gitlab event
+ * @return boolean
+ */
+ public function parsePayload(array $payload)
+ {
+ if (! empty($payload['commits'])) {
+
+ foreach ($payload['commits'] as $commit) {
+
+ if ($this->handleCommit($commit)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * 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;
+ }
+}