From c7081db530b1a0e6a1d2ea130e438d300edb42bf Mon Sep 17 00:00:00 2001 From: xiawenlong Date: Thu, 18 Nov 2021 15:31:26 +0800 Subject: [PATCH] * Finish task 29162,29163 --- module/common/model.php | 84 +++++++++++++++++++++++ module/gitlab/control.php | 18 ++++- module/gitlab/js/browseproject.js | 20 ++++++ module/gitlab/lang/en.php | 2 + module/gitlab/lang/zh-cn.php | 4 +- module/gitlab/model.php | 21 +++--- module/gitlab/view/browseproject.html.php | 14 ++++ 7 files changed, 149 insertions(+), 14 deletions(-) create mode 100644 module/gitlab/js/browseproject.js diff --git a/module/common/model.php b/module/common/model.php index df20f14caa..e7018a02f8 100644 --- a/module/common/model.php +++ b/module/common/model.php @@ -2495,6 +2495,90 @@ EOD; } } + + /** + * Http response with header. + * + * @param string $url + * @param string|array $data + * @param array $options This is option and value pair, like CURLOPT_HEADER => true. Use curl_setopt function to set options. + * @param array $headers Set request headers. + * @static + * @access public + * @return string + */ + public static function httpWithHeader($url, $data = null, $options = array(), $headers = array()) + { + global $lang, $app; + if(!extension_loaded('curl')) return json_encode(array('result' => 'fail', 'message' => $lang->error->noCurlExt)); + + commonModel::$requestErrors = array(); + + if(!is_array($headers)) $headers = (array)$headers; + $headers[] = "API-RemoteIP: " . zget($_SERVER, 'REMOTE_ADDR', ''); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); + curl_setopt($curl, CURLOPT_USERAGENT, 'Sae T OAuth2 v0.1'); + curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30); + curl_setopt($curl, CURLOPT_TIMEOUT, 30); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($curl, CURLOPT_ENCODING, ""); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); + curl_setopt($curl, CURLOPT_HEADER, true); + curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE); + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); + curl_setopt($curl, CURLOPT_URL, $url); + + if(!empty($data)) + { + if(is_object($data)) $data = (array) $data; + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); + } + + if($options) curl_setopt_array($curl, $options); + $response = curl_exec($curl); + $errors = curl_error($curl); + + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); + $headerString = substr($response, 0, $headerSize); + $body = substr($response, $headerSize); + + /* Parse header. */ + $header = explode("\n", $headerString); + $newHeader = array(); + foreach($header as $item) + { + $field = explode(':', $item); + if(count($field) < 2) continue; + $headerkey = array_shift($field); + $newHeader[$headerkey] = join('', $field); + } + curl_close($curl); + + + $logFile = $app->getLogRoot() . 'saas.'. date('Ymd') . '.log.php'; + if(!file_exists($logFile)) file_put_contents($logFile, ''); + + $fh = @fopen($logFile, 'a'); + if($fh) + { + fwrite($fh, date('Ymd H:i:s') . ": " . $app->getURI() . "\n"); + fwrite($fh, "url: " . $url . "\n"); + if(!empty($data)) fwrite($fh, "data: " . print_r($data, true) . "\n"); + fwrite($fh, "results:" . print_r($response, true) . "\n"); + if(!empty($errors)) fwrite($fh, "errors: " . $errors . "\n"); + fclose($fh); + } + + if($errors) commonModel::$requestErrors[] = $errors; + + return array('body' => $body, 'header' => $newHeader); + } + /** * Http. * diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 1aad585492..41799adb97 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -575,15 +575,27 @@ class gitlab extends control /** * Browse gitlab project. * - * @param int $gitlabID + * @param int $gitlabID + * @param string $keyword + * @param int $recTotal + * @param int $recPerPage + * @param int $pageID * @access public * @return void */ - public function browseProject($gitlabID) + public function browseProject($gitlabID, $keyword = '',$recTotal = 0, $recPerPage = 15, $pageID = 1) { + + $this->app->loadClass('pager', $static = true); + $pager = new pager($recTotal, $recPerPage, $pageID); + + $result = $this->gitlab->apiGetProjects($gitlabID, $keyword, $pager); + + $this->view->keyword = $keyword; + $this->view->pager = $result['pager']; $this->view->title = $this->lang->gitlab->common . $this->lang->colon . $this->lang->gitlab->browseProject; $this->view->gitlabID = $gitlabID; - $this->view->gitlabProjectList = $this->gitlab->apiGetProjects($gitlabID); + $this->view->gitlabProjectList = $result['projects']; $this->display(); } diff --git a/module/gitlab/js/browseproject.js b/module/gitlab/js/browseproject.js new file mode 100644 index 0000000000..838cbb39fd --- /dev/null +++ b/module/gitlab/js/browseproject.js @@ -0,0 +1,20 @@ +$(document).ready(function() +{ + $('#projectSearch').click(function() + { + triggerSearch(); + }); + $('#keyword').keypress(function(event) + { + if(event.which == 13) triggerSearch(); + }) + +}); + +function triggerSearch() +{ + var keyword = $('#keyword').val(); + vars = vars.replace('%s', keyword); + var link = createLink('gitlab', 'browseProject', 'gitlabID=' + gitlabID + '&' + vars); + window.location.href = link; +} diff --git a/module/gitlab/lang/en.php b/module/gitlab/lang/en.php index eb460eb6df..420ab7cd57 100644 --- a/module/gitlab/lang/en.php +++ b/module/gitlab/lang/en.php @@ -2,6 +2,7 @@ $lang->gitlab = new stdclass; $lang->gitlab->common = 'GitLab'; $lang->gitlab->browse = 'GitLab Browse'; +$lang->gitlab->search = 'Search'; $lang->gitlab->create = 'Create GitLab'; $lang->gitlab->edit = 'Edit GitLab'; $lang->gitlab->view = 'View GitLab'; @@ -20,6 +21,7 @@ $lang->gitlab->lastUpdate = 'Last Update'; $lang->gitlab->confirmAddWebhook = 'Are you sure about creating Webhook?'; $lang->gitlab->failCreateWebhook = 'Failed to create Webhook, please view the log'; $lang->gitlab->manageProjectMembers = 'Manage project member'; +$lang->gitlab->placeholderSearch = 'Project name'; $lang->gitlab->browseAction = 'GitLab List'; $lang->gitlab->deleteAction = 'Delete GitLab'; diff --git a/module/gitlab/lang/zh-cn.php b/module/gitlab/lang/zh-cn.php index 71958c83b5..1e8a6d48a4 100644 --- a/module/gitlab/lang/zh-cn.php +++ b/module/gitlab/lang/zh-cn.php @@ -2,6 +2,7 @@ $lang->gitlab = new stdclass; $lang->gitlab->common = 'GitLab'; $lang->gitlab->browse = '浏览GitLab'; +$lang->gitlab->search = '搜索'; $lang->gitlab->create = '添加GitLab'; $lang->gitlab->edit = '编辑GitLab'; $lang->gitlab->view = '查看GitLab'; @@ -20,6 +21,7 @@ $lang->gitlab->lastUpdate = '最后更新'; $lang->gitlab->confirmAddWebhook = '您确定创建Webhook吗?'; $lang->gitlab->failCreateWebhook = 'Webhook创建失败,请查看日志'; $lang->gitlab->manageProjectMembers = '项目成员管理'; +$lang->gitlab->placeholderSearch = '请输入项目名称'; $lang->gitlab->browseAction = 'GitLab列表'; $lang->gitlab->deleteAction = '删除GitLab'; @@ -143,4 +145,4 @@ $lang->gitlab->group->manageMembers = '群组成员管 $lang->gitlab->group->memberName = '账号'; $lang->gitlab->group->memberAccessLevel = '角色权限'; $lang->gitlab->group->memberExpiresAt = '过期时间'; -$lang->gitlab->group->repeatError = "群组成员不能重复添加"; \ No newline at end of file +$lang->gitlab->group->repeatError = "群组成员不能重复添加"; diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 6dd282d812..82973ff27c 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -572,11 +572,13 @@ class gitlabModel extends model /** * Get projects of one gitlab. * - * @param int $gitlabID + * @param int $gitlabID + * @param string $keyword + * @param object $pager * @access public * @return array */ - public function apiGetProjects($gitlabID) + public function apiGetProjects($gitlabID, $keyword = '', $pager) { $gitlab = $this->getByID($gitlabID); if(!$gitlab) return array(); @@ -584,15 +586,14 @@ class gitlabModel extends model $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&page={$page}&per_page=100")); - if(!empty($results)) $allResults = array_merge($allResults, $results); - if(count($results)<100 or $page > 10) break; - } + $result = commonModel::httpWithHeader($host . "?private_token={$gitlab->token}&simple=true&&per_page={$pager->recPerPage}&order_by=id&page={$pager->pageID}&search={$keyword}"); - return $allResults; + $header = $result['header']; + $recTotal = $header['X-Total']; + $recPerPage = $header['X-Per-Page']; + $pager = pager::init($recTotal, $recPerPage, $pager->pageID); + + return array('pager' => $pager, 'projects' => json_decode($result['body'])); } /** diff --git a/module/gitlab/view/browseproject.html.php b/module/gitlab/view/browseproject.html.php index af7e7ec78f..7710966ee5 100644 --- a/module/gitlab/view/browseproject.html.php +++ b/module/gitlab/view/browseproject.html.php @@ -11,7 +11,18 @@ */ ?> +recTotal}&recPerPage={$pager->recPerPage}&pageID=1")?> +