From 074d13aa20a1df023c00f2a650160122b5278e57 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 3 Nov 2021 08:53:18 +0800 Subject: [PATCH 001/251] * Correct malformed codes and create sugon branch. --- lib/scm/gitlab.class.php | 16 ++++++++-------- module/gitlab/control.php | 4 ++-- module/mr/control.php | 6 +++--- module/mr/model.php | 10 ++++------ 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/scm/gitlab.class.php b/lib/scm/gitlab.class.php index 783d46df5e..ae1ea6d596 100644 --- a/lib/scm/gitlab.class.php +++ b/lib/scm/gitlab.class.php @@ -96,7 +96,7 @@ class gitlab * @param string $path * @param string $ref * @access public - * @return array + * @return object * @doc https://docs.gitlab.com/ee/api/repository_files.html */ public function files($path, $ref = 'master') @@ -369,7 +369,7 @@ class gitlab * * @param string $cmd * @access public - * @todo Exec commads by gitlab api. + * @todo Exec commands by gitlab api. * @return array */ public function exec($cmd) @@ -539,7 +539,7 @@ class gitlab if($version and $version != 'HEAD') { - $committedDate = $this->getCommitedDate($version); + $committedDate = $this->getCommittedDate($version); if(!$committedDate) return array('commits' => array(), 'files' => array()); $params['until'] = $committedDate; @@ -570,7 +570,7 @@ class gitlab * @access public * @return void */ - public function getCommitedDate($sha) + public function getCommittedDate($sha) { if(!scm::checkRevision($sha)) return null; @@ -598,8 +598,8 @@ class gitlab $param->path = urldecode($path); $param->ref_name = $this->branch; - $fromDate = $this->getCommitedDate($fromRevision); - $toDate = $this->getCommitedDate($toRevision); + $fromDate = $this->getCommittedDate($fromRevision); + $toDate = $this->getCommittedDate($toRevision); $since = ''; $until = ''; @@ -667,7 +667,7 @@ class gitlab * @param string $path * @param bool $recursive * @access public - * @return void + * @return mixed */ public function tree($path, $recursive = 1) { @@ -685,7 +685,7 @@ class gitlab * * @param string $api * @access public - * @return void + * @return mixed */ public function fetch($api, $params = array()) { diff --git a/module/gitlab/control.php b/module/gitlab/control.php index a8ff29062f..d2ef1b4cc0 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -214,9 +214,9 @@ class gitlab extends control * @access public * @return void */ - public function delete($id, $confim = 'no') + public function delete($id, $confirm = 'no') { - if($confim != 'yes') die(js::confirm($this->lang->gitlab->confirmDelete, inlink('delete', "id=$id&confirm=yes"))); + if($confirm != 'yes') die(js::confirm($this->lang->gitlab->confirmDelete, inlink('delete', "id=$id&confirm=yes"))); $oldGitLab = $this->gitlab->getByID($id); $this->loadModel('action'); diff --git a/module/mr/control.php b/module/mr/control.php index 6b05a0b1f3..0d5f0591d1 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -100,7 +100,7 @@ class mr extends control $this->view->title = $this->lang->mr->edit; $this->view->MR = $MR; $this->view->targetBranchList = $targetBranchList; - $this->view->users = array("" => "") + $users; + $this->view->users = array('' => '') + $users; $this->view->assignee = zget($gitlabUsers, $MR->assignee, ''); $this->view->reviewer = zget($gitlabUsers, $MR->reviewer, ''); @@ -114,9 +114,9 @@ class mr extends control * @access public * @return void */ - public function delete($id, $confim = 'no') + public function delete($id, $confirm = 'no') { - if($confim != 'yes') die(js::confirm($this->lang->mr->confirmDelete, inlink('delete', "id=$id&confirm=yes"))); + if($confirm != 'yes') die(js::confirm($this->lang->mr->confirmDelete, inlink('delete', "id=$id&confirm=yes"))); $MR = $this->mr->getByID($id); diff --git a/module/mr/model.php b/module/mr/model.php index 5d8b7e3718..5236b4f7d6 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -359,7 +359,6 @@ class mrModel extends model return rtrim($gitlab->url, '/')."/dashboard/todos?project_id=$projectID&type=MergeRequest"; } - /** * Create MR by API. * @@ -482,8 +481,7 @@ class mrModel extends model $scm = $this->app->loadClass('scm'); $scm->setEngine($repo); - $encoding = empty($encoding) ? $repo->encoding : $encoding; - $encoding = strtolower(str_replace('_', '-', $encoding)); + /* TODO fix this malformed function. */ return $scm->diff('', $MR->sourceBranch, $MR->targetBranch, $parse = true, $MR->sourceProject); } @@ -499,9 +497,9 @@ class mrModel extends model public function getSudoAccountPair($gitlabID, $projectID, $account) { $bindedUsers = $this->gitlab->getUserAccountIdPairs($gitlabID); - $accuntPair = array(); - if(isset($bindedUsers[$account])) $accuntPair[$account] = $bindedUsers[$account]; - return $accuntPair; + $accountPair = array(); + if(isset($bindedUsers[$account])) $accountPair[$account] = $bindedUsers[$account]; + return $accountPair; } /** From bdcf45423950a2a6c417f72ff08bcc0d1cc35754 Mon Sep 17 00:00:00 2001 From: dingguodong Date: Wed, 3 Nov 2021 17:53:14 +0800 Subject: [PATCH 002/251] * Implement view diffs in MR module. --- module/mr/control.php | 14 +++++--- module/mr/model.php | 63 +++++++++++++++++++++++++++++++++--- module/mr/view/diff.html.php | 21 ++---------- 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/module/mr/control.php b/module/mr/control.php index 0d5f0591d1..b6e953866a 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -258,10 +258,13 @@ class mr extends control * @access public * @return void */ - public function diff($MRID) + public function diff($MRID, $encoding= '') { + $encoding = empty($encoding) ? 'utf-8' : $encoding; + $encoding = strtolower(str_replace('_', '-', $encoding)); /* Revert $config->requestFix in $encoding. */ + $MR = $this->mr->getByID($MRID); - $diffs = $this->mr->getDiffs($MR); + $diffs = $this->mr->getDiffs($MR, $encoding = ''); $arrange = $this->cookie->arrange ? $this->cookie->arrange : 'inline'; if($this->server->request_method == 'POST') @@ -294,9 +297,10 @@ class mr extends control } } - $this->view->title = $this->lang->mr->viewDiff; - $this->view->diffs = $diffs; - $this->view->arrange = $arrange; + $this->view->title = $this->lang->mr->viewDiff; + $this->view->diffs = $diffs; + $this->view->encoding = $encoding; + $this->view->arrange = $arrange; $this->display(); } } diff --git a/module/mr/model.php b/module/mr/model.php index 5236b4f7d6..754d4eba14 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -462,11 +462,13 @@ class mrModel extends model * * @docs https://docs.gitlab.com/ee/api/merge_requests.html#get-mr-diff-versions * @param object $MR + * @param string $encoding * @access public * @return object */ - public function getDiffs($MR) + public function getDiffs($MR, $encoding = '') { + $diffVersions = $this->apiGetDiffVersions($MR->gitlabID, $MR->targetProject, $MR->mriid); $gitlab = $this->gitlab->getByID($MR->gitlabID); $this->loadModel('repo'); @@ -477,12 +479,32 @@ class mrModel extends model $repo->path = sprintf($this->config->repo->gitlab->apiPath, $gitlab->url, $MR->targetProject); $repo->client = $gitlab->url; $repo->password = $gitlab->token; + $repo->account = ''; + $repo->encoding = $encoding; + $lines = array(); + foreach ($diffVersions as $diffVersion) + { + $singleDiff = $this->apiGetSingleDiffVersion($MR->gitlabID, $MR->targetProject, $MR->mriid, $diffVersion->id); + if ($singleDiff->state == 'empty') continue; + $diffs = $singleDiff->diffs; + foreach ($diffs as $diff) + { + /* Here use $index to make sure $index is unique in $lines. */ + $index = sprintf("index %s ... %s %s ", $singleDiff->head_commit_sha, $singleDiff->base_commit_sha, $diff->b_mode); + if(in_array($index, $lines)) continue; + $lines[] = sprintf("diff --git a/%s b/%s", $diff->old_path, $diff->new_path); + $lines[] = $index; + $lines[] = sprintf("--a/%s", $diff->old_path); + $lines[] = sprintf("--b/%s", $diff->new_path); + $diffLines = explode("\n", $diff->diff); + foreach ($diffLines as $diffLine) $lines[] = $diffLine; + } + } $scm = $this->app->loadClass('scm'); $scm->setEngine($repo); - - /* TODO fix this malformed function. */ - return $scm->diff('', $MR->sourceBranch, $MR->targetBranch, $parse = true, $MR->sourceProject); + $diff = $scm->engine->parseDiff($lines); + return $diff; } /** @@ -540,4 +562,35 @@ class mrModel extends model $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/todo"); return json_decode(commonModel::http($url, $data = null, $options = array(CURLOPT_CUSTOMREQUEST => 'POST'))); } -} + + /** + * Get diff versions of MR from GitLab API. + * + * @param int $gitlabID + * @param int $projectID + * @param int $MRID + * @access public + * @return object + */ + public function apiGetDiffVersions($gitlabID, $projectID, $MRID) + { + $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/versions"); + return json_decode(commonModel::http($url)); + } + + /** + * Get a single diff version of MR from GitLab API. + * + * @param int $gitlabID + * @param int $projectID + * @param int $MRID + * @param int $versionID + * @access public + * @return object + */ + public function apiGetSingleDiffVersion($gitlabID, $projectID, $MRID, $versionID) + { + $url = sprintf($this->gitlab->getApiRoot($gitlabID), "/projects/$projectID/merge_requests/$MRID/versions/$versionID"); + return json_decode(commonModel::http($url)); + } +} \ No newline at end of file diff --git a/module/mr/view/diff.html.php b/module/mr/view/diff.html.php index a4ff5ab6f9..ac5361274a 100644 --- a/module/mr/view/diff.html.php +++ b/module/mr/view/diff.html.php @@ -17,7 +17,7 @@ $backURI = $this->session->mrView ? $this->session->mrView : $this->session->mrList; if($backURI) { - echo html::a($backURI, " " . $lang->goback, '', "class='btn btn-link' data-app='{$app->openApp}'"); + echo html::a($backURI, " " . $lang->goback, '', "class='btn btn-link' data-app='{$app->tab}'"); } else { @@ -47,7 +47,7 @@ - + @@ -76,7 +76,6 @@ type != 'new') echo $line->oldlc?> type != 'old') echo $line->newlc?> line = $repo->SCM == 'Subversion' ? htmlspecialchars($line->line) : $line->line; echo $line->type == 'old' ? preg_replace('/^\-/', '–', $line->line) : ($line->type == 'new' ? $line->line : ' ' . $line->line); ?> @@ -105,13 +104,11 @@ type?> type == 'custom') echo "line-old"?> code'>old[$oldlc])) $content->old[$oldlc] = ''; - $content->old[$oldlc] = $repo->SCM == 'Subversion' ? htmlspecialchars($content->old[$oldlc]) : $content->old[$oldlc]; if(!empty($oldlc)) echo $line->type != 'all' ? preg_replace('/^\-/', '–', $content->old[$oldlc]) : ' ' . $content->old[$oldlc]; ?> type?> type == 'custom') echo "line-new"?> code'>new[$newlc])) $content->new[$newlc] = ''; - $content->new[$newlc] = $repo->SCM == 'Subversion' ? htmlspecialchars($content->new[$newlc]) : $content->new[$newlc]; if(!empty($newlc)) echo $line->type != 'all' ? $content->new[$newlc] : ' ' . $content->new[$newlc]; ?> -