From 72ff0bbbbfc1114632398567229fd247bb571356 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Fri, 22 Dec 2023 19:20:57 +0800 Subject: [PATCH] * Adjust graphql api logic for devops. --- module/gitlab/model.php | 22 ++++++++++++++-------- module/repo/control.php | 2 +- module/repo/model.php | 5 ++--- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 9799be79c2..0311bc840a 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -2845,19 +2845,26 @@ class gitlabModel extends model * 通过graphql的api获取数据。 * Get data by api graphql. * - * @param int $gitlabID - * @param array $query + * @param object $repo + * @param string $query * @access public * @return object|array|null */ - public function apiGetByGraphql(int $gitlabID, array $query): object|array|null + public function apiGetByGraphql(object $repo, string $query): object|array|null { static $gitlab; - if(empty($gitlab)) $gitlab = $this->getByID($gitlabID); + if(empty($gitlab)) $gitlab = $this->getByID((int)$repo->serviceHost); if(!$gitlab) return array(); + /* Compatible with HTTP and HTTPS domain. */ + $repo->client = str_replace('https://', 'http://', $repo->client); + $repo->codePath = str_replace('https://', 'http://', $repo->codePath); + + $fullPath = trim(str_replace($repo->client, '', $repo->codePath), '/'); + $query = sprintf($query, $fullPath); + $url = rtrim($gitlab->url, '/') . '/api/graphql' . "?private_token={$gitlab->token}"; - return json_decode(commonModel::http($url, $query, array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1))); + return json_decode(commonModel::http($url, array('query' => $query), array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1))); } /** @@ -2872,9 +2879,8 @@ class gitlabModel extends model */ public function getFileLastCommit(object $repo, string $path, string $branch = 'HEAD'): object|array|null { - $fullPath = trim(str_replace($repo->client, '', $repo->codePath), '/'); - $query = array('query' => 'query {project(fullPath: "' . $fullPath . '") {repository {tree(path: "' . trim($path, '/') . '", ref: "' . $branch . '") {lastCommit {sha message author {name username} authorName authoredDate}}}}}'); - $response = $this->apiGetByGraphql($repo->serviceHost, $query); + $query = 'query {project(fullPath: "%s") {repository {tree(path: "' . trim($path, '/') . '", ref: "' . $branch . '") {lastCommit {sha message author {name username} authorName authoredDate}}}}}'; + $response = $this->apiGetByGraphql($repo, $query); if(!isset($response->data->project->repository->tree->lastCommit)) return null; return $response->data->project->repository->tree->lastCommit; diff --git a/module/repo/control.php b/module/repo/control.php index d091cbce58..def3d45d63 100644 --- a/module/repo/control.php +++ b/module/repo/control.php @@ -1587,7 +1587,7 @@ class repo extends control public function ajaxGetFileCommitInfo() { $repo = $this->repo->getByID((int)$this->post->repoID); - $commit = $this->loadModel('gitlab')->getFileLastCommit($repo, $this->post->path, $this->post->branch); + $commit = $this->loadModel('gitlab')->getFileLastCommit($repo, (string)$this->post->path, (string)$this->post->branch); $commit->comment = $this->repo->replaceCommentLink($commit->message); echo json_encode($commit); } diff --git a/module/repo/model.php b/module/repo/model.php index 5eebbaea95..26626a28d8 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -2538,11 +2538,10 @@ class repoModel extends model $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 {sha name path}}}}}}'); - $response = $this->gitlab->apiGetByGraphql((int)$repo->serviceHost, $query); + $query = 'query { project(fullPath: "%s") {repository {tree(path: "' . trim($path, '/') . '", ref: "' . $branch . '") {' . $type . '(after: "' . $endCursor . '") {pageInfo {endCursor hasNextPage} nodes {sha name path}}}}}}'; + $response = $this->gitlab->apiGetByGraphql($repo, $query); if(!$endCursor && !isset($response->data->project->repository)) return array();