From cadcdcd3d252cde1ad8d2c6e0e961553b072f1ef Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Mon, 29 Jul 2013 17:34:21 +0100 Subject: [PATCH 1/2] Added new controller to accept Gitlab-like webhooks --- PHPCI/Application.php | 2 +- PHPCI/Controller/GitlabController.php | 61 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 PHPCI/Controller/GitlabController.php diff --git a/PHPCI/Application.php b/PHPCI/Application.php index 3378a050..d7d0a4fe 100644 --- a/PHPCI/Application.php +++ b/PHPCI/Application.php @@ -32,7 +32,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', 'BuildStatus')); + $externalAction = in_array($this->controllerName, array('Bitbucket', 'Github', 'Gitlab', 'BuildStatus')); $skipValidation = ($externalAction || $sessionAction); if($skipValidation || $this->validateSession()) { diff --git a/PHPCI/Controller/GitlabController.php b/PHPCI/Controller/GitlabController.php new file mode 100644 index 00000000..f3c1dd35 --- /dev/null +++ b/PHPCI/Controller/GitlabController.php @@ -0,0 +1,61 @@ +, Dan Cryer +* @package PHPCI +* @subpackage Web +*/ +class GitlabController extends \PHPCI\Controller +{ + public function init() + { + $this->_buildStore = Store\Factory::getStore('Build'); + } + + /** + * Called by Gitlab Webhooks: + */ + public function webhook($project) + { + $payload = json_decode(file_get_contents("php://input"), true); + + try { + $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'); + } + + try { + $build = $this->_buildStore->save($build); + $build->sendStatusPostback(); + } catch (\Exception $ex) { + header('HTTP/1.1 500 Internal Server Error'); + header('Ex: ' . $ex->getMessage()); + die('FAIL'); + } + + die('OK'); + } +} From 4133bb79055fba9cfe5a5b93ca763724abb6897a Mon Sep 17 00:00:00 2001 From: Alex Russell Date: Tue, 30 Jul 2013 14:58:00 +0100 Subject: [PATCH 2/2] Extended phpcs plugin to allow for extra commandline arguments. - Now accepts paths in the --standards argument (and correctly prepends the build directory) so it actually works - Now accepts the --tabwidth argument so that if your project favours tabs over spaces you can still just use PSR2 and you don't have to sacrifice using the Generic.WhiteSpace.ScopeIndent rule because it is spaces-only - Now accpets the --encoding argument. I haven't used this before, but looking in the phpcs documentation it looks like a very useful one to have as most people code in utf-8 but phpcs defaults to iso-8859-1 and it can apparently "cause double-encoding problems when generating UTF-8 encoded XML reports" --- PHPCI/Plugin/PhpCodeSniffer.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/PHPCI/Plugin/PhpCodeSniffer.php b/PHPCI/Plugin/PhpCodeSniffer.php index 4c3c2680..8e28fd22 100644 --- a/PHPCI/Plugin/PhpCodeSniffer.php +++ b/PHPCI/Plugin/PhpCodeSniffer.php @@ -26,6 +26,8 @@ class PhpCodeSniffer implements \PHPCI\Plugin $this->phpci = $phpci; $this->directory = isset($options['directory']) ? $options['directory'] : $phpci->buildPath; $this->standard = isset($options['standard']) ? $options['standard'] : 'PSR2'; + $this->tab_width = isset($options['tab_width']) ? $options['tab_width'] : ''; + $this->encoding = isset($options['encoding']) ? $options['encoding'] : ''; } /** @@ -39,7 +41,27 @@ class PhpCodeSniffer implements \PHPCI\Plugin $ignore = ' --ignore=' . implode(',', $this->phpci->ignore); } - $cmd = PHPCI_BIN_DIR . 'phpcs --standard=%s %s "%s"'; - return $this->phpci->executeCommand($cmd, $this->standard, $ignore, $this->phpci->buildPath); + $standard = ''; + + if (strpos($this->standard, '/') !== false) { + $standard = ' --standard='.$this->directory.$this->standard; + } else { + $standard = ' --standard='.$this->standard; + } + + $tab_width = ''; + + if (strlen($this->tab_width)) { + $tab_width = ' --tab-width='.$this->tab_width; + } + + $encoding = ''; + + if (strlen($this->encoding)) { + $encoding = ' --encoding='.$this->encoding; + } + + $cmd = PHPCI_BIN_DIR . 'phpcs %s %s %s %s "%s"'; + return $this->phpci->executeCommand($cmd, $standard, $ignore, $tab_width, $encoding, $this->phpci->buildPath); } }