From 26abaf3be12c27577e99c2035fcd21436b5a8f45 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Wed, 22 Nov 2023 15:48:23 +0800 Subject: [PATCH] * Adjust file tree get type for repo. --- module/gitlab/model.php | 2 +- module/repo/model.php | 52 ++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 9285a340f5..72a417d121 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -3049,6 +3049,6 @@ class gitlabModel extends model if(!$gitlab) return array(); $url = rtrim($gitlab->url, '/') . '/api/graphql' . "?private_token={$gitlab->token}"; - return json_decode(commonModel::http($url, $query)); + return json_decode(commonModel::http($url, $query, array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1))); } } diff --git a/module/repo/model.php b/module/repo/model.php index 29c42fe872..a1ea206ce3 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -3236,23 +3236,18 @@ class repoModel extends model */ public function getGitlabFilesByPath(object $repo, string $path = '', string $branch = ''): array { - if(!$branch) $branch = $this->cookie->branch; - - $fullPath = trim(str_replace($repo->client, '', $repo->codePath), '/'); - $query = array('query' => 'query { project(fullPath: "' . $fullPath . '") {repository {tree(path: "' . trim($path, '/') . '", ref: "' . $branch . '") {trees {nodes {name path}} blobs {nodes {name path}}}}}}'); - $response = $this->loadModel('gitlab')->apiGetByGraphql($repo->serviceHost, $query); - if(!isset($response->data->project->repository)) return array(); - - $folderList = $response->data->project->repository->tree->trees->nodes; - $fileList = $response->data->project->repository->tree->blobs->nodes; + $fileList = $this->getTreeByGraphql($repo, $path, $branch, 'blobs'); + $folderList = $this->getTreeByGraphql($repo, $path, $branch, 'trees'); + if(empty($fileList) && empty($folderList)) return array(); $files = array(); $folders = array(); - $dirList = array(); $fileSort = $dirSort = array(); // Use it to sort array. foreach($fileList as $file) { + if(in_array($file->name, $fileSort)) continue; + $base64Name = base64_encode($file->path); $fileInfo = new stdclass(); @@ -3269,6 +3264,8 @@ class repoModel extends model foreach($folderList as $dir) { + if(in_array($dir->name, $dirSort)) continue; + $base64Name = base64_encode($dir->path); $folder = new stdclass(); @@ -3280,7 +3277,6 @@ class repoModel extends model $folder->kind = 'dir'; $folder->items = array('url' => helper::createLink('repo', 'ajaxGetFiles', "repoID={$repo->id}&branch={$branch}&path=" . helper::safe64Encode($dir->path))); - $dirList[] = $dir->name; $folders[] = $folder; $dirSort[] = $dir->name; } @@ -3289,4 +3285,38 @@ class repoModel extends model return array_merge($folders, $files); } + + /** + * 通过Graphql获取GitLab文件列表。 + * Get GitLab files by Graphql. + * + * @param object $repo + * @param string $path + * @param string $branch + * @param string $type + * @access public + * @return array + */ + public function getTreeByGraphql(object $repo, string $path = '', string $branch = '', string $type = 'blobs'): array + { + if(!$branch) $branch = $this->cookie->branch; + + $this->loadModel('gitlab'); + $fileList = array(); + $endCursor = ''; + $hasNextPage = true; + $fullPath = trim(str_replace($repo->client, '', $repo->codePath), '/'); + while($hasNextPage) + { + $query = array('query' => 'query { project(fullPath: "' . $fullPath . '") {repository {tree(path: "' . trim($path, '/') . '", ref: "' . $branch . '") {' . $type . '(after: "' . $endCursor . '") {pageInfo {endCursor hasNextPage} nodes {name path}}}}}}'); + $response = $this->gitlab->apiGetByGraphql($repo->serviceHost, $query); + + if(!$endCursor && !isset($response->data->project->repository)) return array(); + + $fileList = array_merge($fileList, $response->data->project->repository->tree->{$type}->nodes); + $hasNextPage = $response->data->project->repository->tree->{$type}->pageInfo->hasNextPage; + $endCursor = $response->data->project->repository->tree->{$type}->pageInfo->endCursor; + } + return $fileList; + } }