From 3c8ed45c46f400dbe1df33bd49ba3da883d101e0 Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Fri, 24 Jul 2015 12:36:02 +0200 Subject: [PATCH 1/3] implement bitbucket webhooks this fixes issue https://github.com/Block8/PHPCI/issues/1015 and adds support for the new bitbucket webhooks --- PHPCI/Controller/WebhookController.php | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/PHPCI/Controller/WebhookController.php b/PHPCI/Controller/WebhookController.php index c448f163..95e94d15 100644 --- a/PHPCI/Controller/WebhookController.php +++ b/PHPCI/Controller/WebhookController.php @@ -80,11 +80,69 @@ class WebhookController extends \b8\Controller } /** - * Called by Bitbucket POST service. + * Called by Bitbucket. */ public function bitbucket($projectId) { $project = $this->fetchProject($projectId, 'bitbucket'); + + /* + * support both old services and new webhooks + */ + + if ($payload = $this->getParam('payload')) { + return $this->bitbucketService(json_decode($payload, true), $project); + } + + $payload = json_decode(file_get_contents("php://input"), true); + + if (empty($payload['push']['changes'])) { + /* + * invalid event from bitbucket + */ + return [ + 'status' => 'failed', + 'commits' => [] + ]; + } + + return $this->bitbucketWebhook($payload, $project); + } + + /** + * Bitbucket webhooks. + */ + protected function bitbucketWebhook($payload, $project) + { + $results = array(); + $status = 'failed'; + foreach ($payload['push']['changes'] as $commit) { + try { + $email = $commit['new']['target']['author']['raw']; + $email = substr($email, 0, strpos($email, '>')); + $email = substr($email, strpos($email, '<') + 1); + + $results[$commit['new']['target']['hash']] = $this->createBuild( + $project, + $commit['new']['target']['hash'], + $commit['new']['name'], + $email, + $commit['new']['target']['message'] + ); + $status = 'ok'; + } catch (Exception $ex) { + $results[$commit['new']['target']['hash']] = array('status' => 'failed', 'error' => $ex->getMessage()); + } + } + + return array('status' => $status, 'commits' => $results); + } + + /** + * Bitbucket POST service. + */ + protected function bitbucketService($payload, $project) + { $payload = json_decode($this->getParam('payload'), true); $results = array(); From 66ffea12f045a3c0b774ee52b382eef424dca9cb Mon Sep 17 00:00:00 2001 From: Michael Schramm Date: Fri, 24 Jul 2015 13:02:46 +0200 Subject: [PATCH 2/3] remove whitespace --- PHPCI/Controller/WebhookController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Controller/WebhookController.php b/PHPCI/Controller/WebhookController.php index 95e94d15..d2f9b694 100644 --- a/PHPCI/Controller/WebhookController.php +++ b/PHPCI/Controller/WebhookController.php @@ -101,7 +101,7 @@ class WebhookController extends \b8\Controller * invalid event from bitbucket */ return [ - 'status' => 'failed', + 'status' => 'failed', 'commits' => [] ]; } From fe9289eda5c5dae39aa29277c7c0d14436a446f3 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Fri, 23 Oct 2015 13:55:04 -0700 Subject: [PATCH 3/3] Remove whitespace from empty lines, reformatted inline comments --- PHPCI/Controller/WebhookController.php | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/PHPCI/Controller/WebhookController.php b/PHPCI/Controller/WebhookController.php index d2f9b694..b43bd1cd 100644 --- a/PHPCI/Controller/WebhookController.php +++ b/PHPCI/Controller/WebhookController.php @@ -85,30 +85,25 @@ class WebhookController extends \b8\Controller public function bitbucket($projectId) { $project = $this->fetchProject($projectId, 'bitbucket'); - - /* - * support both old services and new webhooks - */ - + + // Support both old services and new webhooks if ($payload = $this->getParam('payload')) { return $this->bitbucketService(json_decode($payload, true), $project); } - + $payload = json_decode(file_get_contents("php://input"), true); - + if (empty($payload['push']['changes'])) { - /* - * invalid event from bitbucket - */ + // Invalid event from bitbucket return [ 'status' => 'failed', 'commits' => [] ]; } - + return $this->bitbucketWebhook($payload, $project); } - + /** * Bitbucket webhooks. */ @@ -137,7 +132,7 @@ class WebhookController extends \b8\Controller return array('status' => $status, 'commits' => $results); } - + /** * Bitbucket POST service. */