From 75bf140f6d02cac3c6a9d8a31658817dda69fdd5 Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Tue, 12 Jul 2022 15:37:16 +0800 Subject: [PATCH] * Modify repo page data. --- lib/scm/gitea.class.php | 161 ++++++++++++++++----------------------- lib/scm/gitlab.class.php | 17 +++++ lib/scm/scm.class.php | 13 ++++ module/repo/config.php | 3 + module/repo/control.php | 6 +- module/repo/model.php | 2 +- 6 files changed, 102 insertions(+), 100 deletions(-) diff --git a/lib/scm/gitea.class.php b/lib/scm/gitea.class.php index 41b8285932..84b02072d3 100644 --- a/lib/scm/gitea.class.php +++ b/lib/scm/gitea.class.php @@ -34,7 +34,7 @@ class gitea public function ls($path, $revision = 'HEAD') { if(!scm::checkRevision($revision)) return array(); - $api = "tree"; + $api = "contents"; $param = new stdclass(); $param->path = ltrim($path, '/'); @@ -70,10 +70,10 @@ class gitea if(empty($commits)) continue; $commit = $commits[0]; - $info->revision = $commit->id; - $info->comment = $commit->message; - $info->account = $commit->committer_name; - $info->date = date('Y-m-d H:i:s', strtotime($commit->committed_date)); + $info->revision = $commit->sha; + $info->comment = $commit->commit->message; + $info->account = $commit->commit->committer->name; + $info->date = date('Y-m-d H:i:s', strtotime($commit->commit->committer->date)); $info->size = 0; } @@ -102,23 +102,23 @@ class gitea public function files($path, $ref = 'master') { $path = urlencode($path); - $api = "files/$path"; + $api = "contents/$path"; $param = new stdclass(); $param->ref = $ref; $file = $this->fetch($api, $param); - if(!isset($file->file_name)) return false; + if(!isset($file->name)) return false; $commits = $this->getCommitsByPath($path, '', '', 1); - $file->revision = $file->commit_id; + $file->revision = $file->sha; $file->size = $this->formatBytes($file->size); if(!empty($commits)) { $commit = $commits[0]; - $file->revision = $commit->id; - $file->committer = $commit->committer_name; - $file->comment = $commit->message; - $file->date = date('Y-m-d H:i:s', strtotime($commit->committed_date)); + $file->revision = $commit->sha; + $file->committer = $commit->commit->committer->name; + $file->comment = $commit->commit->message; + $file->date = date('Y-m-d H:i:s', strtotime($commit->commit->committer->date)); } return $file; @@ -145,7 +145,7 @@ class gitea { $params['page'] = $page; $list = $this->fetch($api, $params); - if(empty($list)) break; + if(empty($list) or $list == '[]') break; foreach($list as $tag) $tags[] = $tag->name; if(count($list) < $params['per_page']) break; @@ -177,7 +177,7 @@ class gitea foreach($branchList as $branch) { if(!isset($branch->name)) continue; - if($branch->default) + if($branch->name == 'main') { $default[$branch->name] = $branch->name; } @@ -232,7 +232,7 @@ class gitea $list = $this->getCommitsByPath($path, $fromRevision, $toRevision); foreach($list as $commit) { - if(isset($commit->id)) $commit->diffs = $this->getFilesByCommit($commit->id); + if(isset($commit->sha)) $commit->diffs = $this->getFilesByCommit($commit->sha); } return $this->parseLog($list); @@ -248,42 +248,7 @@ class gitea */ public function blame($path, $revision) { - if(!scm::checkRevision($revision)) return array(); - - $path = ltrim($path, DIRECTORY_SEPARATOR); - $path = urlencode($path); - $api = "files/$path/blame"; - $param = new stdclass; - $param->ref = ($revision and $revision != 'HEAD') ? $revision : $this->branch; - $results = $this->fetch($api, $param); - - $blames = array(); - $revLine = 0; - $revision = ''; - - $lineNumber = 1; - foreach($results as $blame) - { - $line = array(); - $line['revision'] = $blame->commit->id; - $line['committer'] = $blame->commit->committer_name; - $line['time'] = $blame->commit->committer_name; - $line['line'] = $lineNumber; - $line['lines'] = count($blame->lines); - $line['content'] = array_shift($blame->lines); - - $blames[$lineNumber] = $line; - - $lineNumber ++; - - foreach($blame->lines as $line) - { - $blames[$lineNumber] = array('line' => $lineNumber, 'content' => $line); - $lineNumber ++; - } - } - - return $blames; + return array(); } /** @@ -562,16 +527,16 @@ class gitea { $api .= '/' . $version; $commit = $this->fetch($api); - if(isset($commit->id)) + if(isset($commit->sha)) { $log = new stdclass; - $log->committer = $commit->committer_name; - $log->revision = $commit->id; - $log->comment = $commit->message; - $log->time = date('Y-m-d H:i:s', strtotime($commit->created_at)); + $log->committer = $commit->commit->committer->name; + $log->revision = $commit->sha; + $log->comment = $commit->commit->message; + $log->time = date('Y-m-d H:i:s', strtotime($commit->commit->committer->date)); - $commits[$commit->id] = $log; - $files[$commit->id] = $this->getFilesByCommit($log->revision); + $commits[$commit->sha] = $log; + $files[$commit->sha] = $this->getFilesByCommit($log->revision); return array('commits' => $commits, 'files' => $files); } @@ -608,16 +573,16 @@ class gitea foreach($list as $commit) { - if(!is_object($commit)) continue; + if(!is_object($commit->commit)) continue; $log = new stdclass; - $log->committer = $commit->committer_name; - $log->revision = $commit->id; - $log->comment = $commit->message; - $log->time = date('Y-m-d H:i:s', strtotime($commit->created_at)); + $log->committer = $commit->commit->committer->name; + $log->revision = $commit->sha; + $log->comment = $commit->commit->message; + $log->time = date('Y-m-d H:i:s', strtotime($commit->commit->committer->date)); - $commits[$commit->id] = $log; - $files[$commit->id] = $this->getFilesByCommit($log->revision); + $commits[$commit->sha] = $log; + $files[$commit->sha] = $this->getFilesByCommit($log->revision); } return array('commits' => $commits, 'files' => $files); @@ -694,33 +659,18 @@ class gitea public function getFilesByCommit($revision) { if(!scm::checkRevision($revision)) return array(); - $api = "commits/{$revision}/diff"; - $params = new stdclass; - $params->page = 1; - $params->per_page = 100; - - $allResults = array(); - while(true) - { - $results = $this->fetch($api, $params); - $params->page ++; - if(!is_array($results)) $results = array(); - $allResults = $allResults + $results; - if(count($results) < 100) break; - } + $api = "contents"; + $results = $this->fetch($api, array('ref' => $revision)); + if(empty($results)) return array(); $files = array(); - foreach($allResults as $row) + foreach($results as $row) { - $file = new stdclass(); + $file = new stdclass(); $file->revision = $revision; - $file->path = '/' . $row->new_path; - $file->type = 'file'; - - $file->action = 'M'; - if($row->new_file) $file->action = 'A'; - if($row->renamed_file) $file->action = 'R'; - if($row->deleted_file) $file->action = 'D'; + $file->action = 'A'; + $file->type = $row->type; + $file->path = '/' . trim($row->path); $files[] = $file; } @@ -737,7 +687,7 @@ class gitea */ public function tree($path, $recursive = 1) { - $api = "tree"; + $api = "contents"; $params = array(); $params['path'] = ltrim($path, '/'); @@ -756,8 +706,8 @@ class gitea public function fetch($api, $params = array(), $needToLoop = false) { $params = (array) $params; - $params['private_token'] = $this->token; - $params['per_page'] = isset($params['per_page']) ? $params['per_page'] : 100; + $params['token'] = $this->token; + $params['per_page'] = isset($params['per_page']) ? $params['per_page'] : 100; $api = ltrim($api, '/'); $api = $this->root . $api . '?' . http_build_query($params); @@ -783,7 +733,8 @@ class gitea return array(); } - return json_decode($response); + $res = json_decode($response); + return empty($res) ? trim($response) : $res; } } @@ -816,12 +767,12 @@ class gitea $i = 0; foreach($logs as $commit) { - if(!isset($commit->id)) continue; + if(!isset($commit->sha)) continue; $parsedLog = new stdclass(); - $parsedLog->revision = $commit->id; - $parsedLog->committer = $commit->committer_name; - $parsedLog->time = date('Y-m-d H:i:s', strtotime($commit->committed_date)); - $parsedLog->comment = $commit->message; + $parsedLog->revision = $commit->sha; + $parsedLog->committer = $commit->commit->committer->name; + $parsedLog->time = date('Y-m-d H:i:s', strtotime($commit->commit->committer->date)); + $parsedLog->comment = $commit->commit->message; $parsedLog->change = array(); foreach($commit->diffs as $diff) { @@ -834,4 +785,20 @@ class gitea return $parsedLogs; } + + /** + * Get download url. + * + * @param string $branch + * @param string $ext + * @access public + * @return string + */ + public function getDownloadUrl($branch = 'master', $ext = 'zip') + { + $params = (array) $params; + $params['token'] = $this->token; + + return "{$this->root}archive/{$branch}.{$ext}" . '?' . http_build_query($params); + } } diff --git a/lib/scm/gitlab.class.php b/lib/scm/gitlab.class.php index 324d03a9cc..583b0b1475 100644 --- a/lib/scm/gitlab.class.php +++ b/lib/scm/gitlab.class.php @@ -834,4 +834,21 @@ class gitlab return $parsedLogs; } + + /** + * Get download url. + * + * @param string $branch + * @param string $ext + * @access public + * @return string + */ + public function getDownloadUrl($branch = 'master', $ext = 'zip') + { + $params = (array) $params; + $params['private_token'] = $this->token; + $params['sha'] = $branch; + + return "{$this->root}archive.{$ext}" . '?' . http_build_query($params); + } } diff --git a/lib/scm/scm.class.php b/lib/scm/scm.class.php index 06648998c3..9cbd9cb79a 100644 --- a/lib/scm/scm.class.php +++ b/lib/scm/scm.class.php @@ -244,6 +244,19 @@ class scm if(preg_match('/[^a-z0-9\-_\.\^\w][\x{4e00}-\x{9fa5}]/ui', $revision)) return false; return true; } + + /** + * Get download url. + * + * @param string $branch + * @param string $ext + * @access public + * @return void + */ + public function getDownloadUrl($branch = '', $ext = 'zip') + { + return $this->engine->getDownloadUrl($branch, $ext); + } } /** diff --git a/module/repo/config.php b/module/repo/config.php index af7bfa2cd0..4bd26c4601 100644 --- a/module/repo/config.php +++ b/module/repo/config.php @@ -49,6 +49,9 @@ $config->repo->gitlab = new stdclass; $config->repo->gitlab->perPage = 300; $config->repo->gitlab->apiPath = "%s/api/v4/projects/%s/repository/"; +$config->repo->gitea = new stdclass; +$config->repo->gitea->apiPath = "%s/api/v1/repos/%s/"; + $config->repo->gitServiceList = array('gitlab', 'gitea'); $config->repo->rules['module']['task'] = 'Task'; diff --git a/module/repo/control.php b/module/repo/control.php index b1bcb3be74..7d31cc353b 100644 --- a/module/repo/control.php +++ b/module/repo/control.php @@ -1367,9 +1367,11 @@ class repo extends control } $repo = $this->repo->getRepoByID($repoID); - if($repo->SCM == 'Gitlab') + if(in_array($repo->SCM, array('Gitlab', 'Gitea'))) { - $url = $this->loadModel('gitlab')->downloadCode($repo->gitlab, $repo->project, $branch); + $this->scm = $this->app->loadClass('scm'); + $this->scm->setEngine($repo); + $url = $this->scm->getDownloadUrl($branch); } elseif($repo->SCM == 'Git') { diff --git a/module/repo/model.php b/module/repo/model.php index bdc537385e..9a32306156 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -2032,7 +2032,7 @@ class repoModel extends model $repo->gitService = $service ? $service->id : 0; $repo->project = $service ? $repo->path : ''; // The projectID in gitlab. - $repo->path = $service ? sprintf($this->config->repo->gitlab->apiPath, $service->url, $repo->path) : ''; + $repo->path = $service ? sprintf($this->config->repo->{$service->type}->apiPath, $service->url, $repo->path) : ''; $repo->client = $service ? $service->url : ''; $repo->password = $service ? $service->token : ''; return $repo;