diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 0723c2ad..77bd7170 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -29,7 +29,7 @@ class Application extends b8\Application // Validate the user's session unless it is a login/logout action or a web hook: $sessionAction = ($this->controllerName == 'Session' && in_array($this->action, array('login', 'logout'))); - $externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'Gitlab', 'BuildStatus')); + $externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'Gitlab', 'BuildStatus', 'Git')); $skipValidation = ($externalAction || $sessionAction); if ($skipValidation || $this->validateSession()) { diff --git a/PHPCI/Controller/GitController.php b/PHPCI/Controller/GitController.php new file mode 100644 index 00000000..396ecaa9 --- /dev/null +++ b/PHPCI/Controller/GitController.php @@ -0,0 +1,69 @@ + + */ +class GitController extends \PHPCI\Controller +{ + public function init() + { + $this->_buildStore = Store\Factory::getStore('Build'); + } + + /** + * Called by POSTing to /git/webhook/?branch=&commit= + * + * @param string $project + */ + public function webhook($project) + { + $branch = $this->getParam('branch'); + $commit = $this->getParam('commit'); + + try { + $build = new Build(); + $build->setProjectId($project); + + if ($branch !== null && trim($branch) !== '') { + $build->setBranch($branch); + } else { + $build->setBranch('master'); + } + + if ($commit !== null && trim($commit) !== '') { + $build->setCommitId($commit); + } + + $build->setStatus(0); + $build->setLog(''); + $build->setCreated(new \DateTime()); + } catch (\Exception $ex) { + header('HTTP/1.1 400 Bad Request'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + try { + $this->_buildStore->save($build); + } catch (\Exception $ex) { + header('HTTP/1.1 500 Internal Server Error'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + die('OK'); + } +}