diff --git a/api/v1/entries/repo.php b/api/v1/entries/repo.php new file mode 100644 index 0000000000..aec7dc4799 --- /dev/null +++ b/api/v1/entries/repo.php @@ -0,0 +1,39 @@ + + * @package repo + * @version 1 + * @link http://www.zentao.net + */ +class repoEntry extends baseEntry +{ + + /** + * Repo webhook. + * + * @access public + * @return void + */ + public function post() + { + $id= $this->param('id'); + if(empty($id)) return; + + $model = $this->loadModel('repo'); + + $repo = $model->getRepoByID($id); + if(empty($repo)) return; + + $headers = getallheaders(); + $event = isset($headers['X-Gitlab-Event']) ? $headers['X-Gitlab-Event'] : ''; + $token = isset($headers['X-Gitlab-Token']) ? $headers['X-Gitlab-Token'] : ''; + if(empty($event) || empty($token)) return; + + $model->handleWebhook($event, $token, $this->requestBody, $repo); + } + +} diff --git a/config/routes.php b/config/routes.php index c55fa34bb2..aa5a7f328c 100644 --- a/config/routes.php +++ b/config/routes.php @@ -89,4 +89,6 @@ $routes['/z/folders/:id'] = 'zfolder'; $routes['/z/files/:id'] = 'zfile'; $routes['/z/files/:id/content'] = 'zfileContent'; +$routes['/gitlab/webhook'] = 'repo'; + $config->routes = $routes; diff --git a/module/gitlab/model.php b/module/gitlab/model.php index f1478d1352..7ed9a94262 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -12,6 +12,9 @@ class gitlabModel extends model { + + const HOOK_PUSH_EVENT = 'Push Hook'; + /** * Get a gitlab by id. * @@ -2052,4 +2055,5 @@ class gitlabModel extends model } if(!$reponse) dao::$errors[] = false; } + } diff --git a/module/repo/control.php b/module/repo/control.php index 8e11c63d0a..1d9951eedd 100644 --- a/module/repo/control.php +++ b/module/repo/control.php @@ -1172,4 +1172,29 @@ class repo extends control $productPairs = $this->repo->getProductsByRepo($repoID); echo html::select('product', array('') + $productPairs, key($productPairs), "class='form-control chosen'"); } + + /** + * Handle gitlab webhook. + * + * @param string $event + * @param string $token + * @param string $data + * @param object $gitlab + * @access public + * @return void + */ + public function handleWebhook($event, $token, $data, $gitlab) + { + /* Check gitlab token */ + switch($event) + { + case static::HOOK_PUSH_EVENT: + $repo = $this->loadModel('repo')->getRepoByPipeline($gitlab->type, $gitlab->id); + /* Update code commit history. */ + $commentGroup = $this->loadModel('job')->getTriggerGroup('commit', array($repo->id)); + $this->loadModel('git')->updateCommit($repo, $commentGroup, false); + break; + } + } + } diff --git a/module/repo/model.php b/module/repo/model.php index 15e2560858..bf706dce07 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -1,6 +1,8 @@ app->user->account; - $this->app->user->account = $log->author; + if(isset($this->app->user)) + { + $account = $this->app->user->account; + $this->app->user->account = $log->author; + } $action = new stdclass(); $action->actor = $log->author; @@ -1613,7 +1618,7 @@ class repoModel extends model } } - $this->app->user->account = $account; + if(isset($this->app->user)) $this->app->user->account = $account; } /** @@ -1789,4 +1794,30 @@ class repoModel extends model ->andWhere('path')->eq($projectID) ->fetchAll(); } + + /** + * Handle gitlab webhook. + * + * @param string $event + * @param string $token + * @param string $data + * @param object $repo + * @access public + * @return void + */ + public function handleWebhook($event, $token, $data, $repo) + { + /* Check gitlab token */ + switch($event) + { + case static::HOOK_PUSH_EVENT: + unset($repo->acl); + unset($repo->desc); + /* Update code commit history. */ + $commentGroup = $this->loadModel('job')->getTriggerGroup('commit', array($repo->id)); + $this->loadModel('git')->updateCommit($repo, $commentGroup, false); + break; + } + } + }