/** * Add hooks for a repositories. * @throws ValidationErrors */ function add_git_hooks() { //echo Router::assemble("hookcall"); $project = $this->active_project; $project_id = $project->getId(); $logged_user = $this->logged_user; $user_id = $logged_user->getId(); $repo_id = array_var($_GET, 'project_source_repository_id'); //project objects id $repo_obj = new ProjectSourceRepository($repo_id); $src_repo_id = $repo_obj->getIntegerField1(); $urls_exists = ProjectGitolite::urls_exists($src_repo_id); if (is_array($urls_exists) && count($urls_exists) > 0) { $url_array = @unserialize($urls_exists["webhook_urls"]); } else { $url_array = array(); } //$src_repo->find $this->response->assign(array('form_action' => Router::assemble('add_hooks_git', array('project_slug' => $this->active_project->getSlug(), 'project_source_repository_id' => $repo_id)), 'url_array' => $url_array, 'web_user' => $web_user, 'test_url' => Router::assemble('test_hooks_url', array('project_slug' => $this->active_project->getSlug(), 'project_source_repository_id' => $repo_id)), 'webuser_pub_key' => $webuser_pub_key)); if ($this->request->isSubmitted()) { // check for form submission $post_data = $this->request->post(); //print_r($post_data); try { $errors = new ValidationErrors(); $webhooks_url = $post_data["webhooks"]; $array_urls = array(); foreach ($webhooks_url as $key => $value) { if (!filter_var($value, FILTER_VALIDATE_URL) && $value != "") { $errors->addError("{$value} is not a valid URL."); } else { $array_urls[] = $value; } } if ($errors->hasErrors()) { throw $errors; } DB::beginWork('Add URL @ ' . __CLASS__); if (is_array($array_urls) && count($array_urls) > 0) { $urls_exists = ProjectGitolite::urls_exists($src_repo_id); $array_urls = array_filter($array_urls); $array_urls_str = serialize($array_urls); if (!is_array($urls_exists) || count($urls_exists) == 0) { $web_hooks_add = ProjectGitolite::insert_urls($array_urls_str, $src_repo_id, $this->logged_user->getId()); if (!$web_hooks_add) { $errors->addError('Problem occured while saving data, please try again.'); throw $errors; } } else { $web_hooks_update = ProjectGitolite::update_web_hooks($array_urls_str, $src_repo_id, $this->logged_user->getId()); } DB::commit('URL Added @ ' . __CLASS__); $this->response->ok(); } else { $errors->addError("Error while saving URL's."); throw $errors; } } catch (Exception $e) { DB::rollback('Failed to add url @ ' . __CLASS__); $this->response->exception($e); } } }