Files
EasySoft-ZenTaoPMS/module/gitlab/model.php
2021-06-16 13:16:17 +08:00

300 lines
8.5 KiB
PHP

<?php
/**
* The model file of gitlab module of ZenTaoPMS.
*
* @copyright Copyright 2009-2015 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
* @license ZPL (http://zpl.pub/page/zplv12.html)
* @author Chenqi <chenqi@cnezsoft.com>
* @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)
{
$api = rtrim($host, '/') . "/api/v4/user?private_token=$token";
return json_decode(commonModel::http($api));
}
/**
* 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;
$user->avatar = $gitlabUser->avatar_url;
$users[] = $user;
}
return $users;
}
/**
* Get matched gitlab users.
*
* @param array $gitlabUsers
* @param array $zentaoUsers
* @access public
* @return array
*/
public function getMatchedUsers($gitlabUsers, $zentaoUsers)
{
$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;
}
}
$matchedUsers = array();
foreach($gitlabUsers 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)
{
$gitlabUser->zentaoAccount = current($matchedZentaoUsers);
$matchedUsers[] = $gitlabUser;
}
}
return $matchedUsers;
}
/**
* 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;
}
/**
* Get hooks.
*
* @param int $gitlabID
* @param int $projectID
* @access public
* @return void
*/
public function apiGetHooks($gitlabID, $projectID)
{
$apiRoot = $this->getApiRoot($gitlabID);
$apiPath = "/projects/{$projectID}/hooks";
$url = sprintf($apiRoot, $apiPath);
$apiJsonRes = commonModel::http($url);
return $apiJsonRes;
}
/**
* Get specific hook.
*
* @param int $gitlabID
* @param int $projectID
* @param int $hookID
* @access public
* @return void
*/
public function apiGetHook($gitlabID, $projectID, $hookID)
{
$apiRoot = $this->getApiRoot($gitlabID);
$apiPath = "/projects/$projectID/hooks/$hookID)";
$url = sprintf($apiRoot, $apiPath);
$apiJsonRes = commonModel::http($url);
return;
}
/**
* Create hook.
*
* @param int $gitlabID
* @param int $projectID
* @param int $url
* @param int $token
* @access public
* @return void
*/
public function apiCreateHook($gitlabID, $projectID, $url, $token)
{
$apiRoot = $this->getApiRoot($gitlabID);
$args = "&enable_ssl_verification=false&issues_events=true&merge_requests_events=true&push_events=true&tag_push_events=true&url={$url}&token={$token}";
$apiPath = "/projects/{$projectID}/hooks";
$url = sprintf($apiRoot, $apiPath) . $args;
$apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'post')");
return $apiJsonRes;
}
/**
* Delete hook.
*
* @param int $gitlabID
* @param int $projectID
* @param int $hookID
* @access public
* @return void
*/
public function apiDeleteHook($gitlabID, $projectID, $hookID)
{
$apiRoot = $this->getApiRoot($gitlabID);
$apiPath = "/projects/{$projectID}/hooks/{$hookID}";
$url = sprintf($apiRoot, $apiPath);
$apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'delete')");
return $apiJsonRes;
}
/**
* Update hook.
*
* @param int $gitlabID
* @param int $projectID
* @param int $hookID
* @access public
* @return void
*/
public function apiUpdateHook($gitlabID, $projectID, $hookID)
{
$apiRoot = $this->getApiRoot($gitlabID);
$args = "&enable_ssl_verification=false&issues_events=true&merge_requests_events=true&push_events=true&tag_push_events=true&url={$url}&token={$token}";
$apiPath = "/projects/{$projectID}/hooks/{$hookID}";
$url = sprintf($apiRoot, $apiPath) . $args;
$apiJsonRes = commonModel::http($url,"options=array('CURLOPT_CUSTOMREQUEST'=>'put')");
return $apiJsonRes;
}
public function pushTask($task, $gitlabID,$projectID)
{
}
public function pushBug($bug, $gitlabID,$projectID)
{
}
}