* @package product * @version $Id: $ * @link http://www.zentao.net */ class gitlabModel extends model { /** * Get a gitlab by id. * * @param int $id * @access public * @return object */ public function getByID($id) { return $this->loadModel('pipeline')->getByID($id); } /** * Get gitlab list. * * @param string $orderBy * @param object $pager * @access public * @return array */ public function getList($orderBy = 'id_desc', $pager = null) { return $this->loadModel('pipeline')->getList('gitlab', $orderBy, $pager); } /** * Get gitlab pairs * * @return array */ public function getPairs() { return $this->loadModel('pipeline')->getPairs('gitlab'); } /** * Create a gitlab. * * @access public * @return bool */ public function create() { return $this->loadModel('pipeline')->create('gitlab'); } /** * Update a gitlab. * * @param int $id * @access public * @return bool */ public function update($id) { return $this->loadModel('pipeline')->update($id); } /** * Get current user. * * @param string $host * @param string $token * @access public * @return array */ public function apiGetCurrentUser($host, $token) { if(strpos($host, 'http') !== 0) return array('result' => 'fail', 'message' => array('url' => array($this->lang->gitlab->hostError))); if(!$this->post->token) return array('result' => 'fail', 'message' => array('token' => array($this->lang->gitlab->tokenError))); $api = rtrim($host, '/') . "/api/v4/user?private_token=$token"; $response = json_decode(commonModel::http($api)); if(!is_object($response)) return array('result' => 'fail', 'message' => array('url' => array($this->lang->gitlab->hostError))); if(isset($response->is_admin) and $response->is_admin == true) return array('result' => 'success'); return array('result' => 'fail', 'message' => array('token' => array($this->lang->gitlab->tokenError))); } /** * Get gitlab user list. * * @param string $host * @param string $token * @access public * @return array */ public function apiGetUsers($gitlab) { $api = rtrim($gitlab->url, '/') . '/api/v4/users?private_token=' . $gitlab->token; $response = json_decode(commonModel::http($api)); if (!$response) return array(); $users = array(); foreach($response as $gitlabUser) { $user = new stdclass; $user->id = $gitlabUser->id; $user->realname = $gitlabUser->name; $user->account = $gitlabUser->username; $user->email = $gitlabUser->email; $users[] = $user; } a($users); return $users; } public function getMatchedUsers($gitlabUsers) { $zentaoUsers = $this->dao->select('account,email,realname')->from(TABLE_USER)->fetchAll('account'); $matches = new stdclass; foreach($gitlabUsers as $gitlabUser) { foreach($zentaoUsers as $zentaoUser) { if($gitlabUser->account == $zentaoUser->account) $matches->accounts[$gitlabUser->account][] = $zentaoUser->account; if($gitlabUser->realname == $zentaoUser->realname) $matches->names[$gitlabUser->realname][] = $zentaoUser->account; if($gitlabUser->email == $zentaoUser->email) $matches->emails[$gitlabUser->email][] = $zentaoUser->account; } } foreach($gitlabUser as $gitlabUser) { $matchedZentaoUsers = array(); if(isset($matches->accounts[$gitlabUser->account])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->accounts[$gitlabUser->account]); if(isset($matches->emails[$gitlabUser->email])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->emails[$gitlabUser->email]); if(isset($matches->names[$gitlabUser->realname])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->names[$gitlabUser->realname]); $matchedZentaoUsers = array_unique($matchedZentaoUsers); if(count($matchedZentaoUsers) == 1) { $matchedUsers[$gitlabUser->id] = current($matchedZentaoUsers); } else { $unmatchedUsers[$gitlabUser->id] = $gitlabUser; } } return array('matched' => $matchedUsers, 'unmatched' => $unmatchedUsers); } /** * Get projects of one gitlab. * * @param int $id * @access public * @return void */ public function apiGetProjects($id) { $gitlab = $this->getByID($id); if(!$gitlab) return array(); $host = rtrim($gitlab->url, '/'); $host .= '/api/v4/projects'; $allResults = array(); for($page = 1; true; $page ++) { $results = json_decode(commonModel::http($host . "?private_token={$gitlab->token}&simple=true&membership=true&page={$page}&per_page=100")); if(empty($results) or $page > 10) break; $allResults = $allResults + $results; } return $allResults; } /** * Get gitlab api base url with access_token * * @param int $id * @access public * @return string gitlab api base url with access_token */ public function getApiRoot($id) { $gitlab = $this->getByID($id); if(!$gitlab) return ""; $gitlab_url = rtrim($gitlab->url, '/').'/api/v4%s'."?private_token={$gitlab->token}"; return $gitlab_url; } public function apiGetHooks($gitlab_id, $project_id) { $host = $this->getApiRoot($gitlab_id); $api_path = sprintf('/projects/%s/hooks', $project_id); $host = sprintf($host, $api_path); $api_json = commonModel::http($host); return $api_json; } public function apiGetHook($gitlab_id, $project_id, $hook_id) { return; } public function apiCreateHook($gitlab_id, $project_id, $url, $token) { return; } public function apiDeleteHook($gitlab_id, $project_id, $hook_id) { return; } public function apiUpdateHook($gitlab_id, $project_id, $hook_id) { return; } }