From a07ba4020ce4c7123e7126f9d82e43a6ecfcc64f Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Fri, 10 May 2013 16:25:37 +0100 Subject: [PATCH] Adding proper Bitbucket support --- PHPCI/Controller/BitbucketController.php | 48 ++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/PHPCI/Controller/BitbucketController.php b/PHPCI/Controller/BitbucketController.php index 6413cefd..88c43cd0 100644 --- a/PHPCI/Controller/BitbucketController.php +++ b/PHPCI/Controller/BitbucketController.php @@ -15,34 +15,36 @@ class BitbucketController extends b8\Controller public function webhook($project) { - $payload = json_decode($this->getParam('payload'), true); + $payload = json_decode(file_get_contents('php://input'), true); + $branches = array(); + $commits = array(); - try + foreach($payload['commits'] as $commit) { - $build = new Build(); - $build->setProjectId($project); - $build->setCommitId($payload['after']); - $build->setStatus(0); - $build->setLog(''); - $build->setCreated(new \DateTime()); - $build->setBranch(str_replace('refs/heads/', '', $payload['ref'])); - } - catch(\Exception $ex) - { - header('HTTP/1.1 400 Bad Request'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); + if(!in_array($commit['branch'], $branches)) + { + $branches[] = $commit['branch']; + $commits[$commit['branch']] = $commit['raw_node']; + } } - try + foreach($branches as $branch) { - $this->_buildStore->save($build); - } - catch(\Exception $ex) - { - header('HTTP/1.1 500 Internal Server Error'); - header('Ex: ' . $ex->getMessage()); - die('FAIL'); + try + { + + $build = new Build(); + $build->setProjectId($project); + $build->setCommitId($commits[$branch]); + $build->setStatus(0); + $build->setLog(''); + $build->setCreated(new \DateTime()); + $build->setBranch($branch); + $this->_buildStore->save($build); + } + catch(\Exception $ex) + { + } } die('OK');