From 422478e03fe2e29e9a32dbbdbd9fd5bb830ffc6b Mon Sep 17 00:00:00 2001 From: caoyanyi Date: Mon, 20 May 2024 10:39:55 +0800 Subject: [PATCH] + Add commit update tips for mr view. --- module/mr/control.php | 42 +++++++++++++++----------------------- module/mr/ui/view.html.php | 27 ++++++++++++++++-------- module/mr/zen.php | 27 ++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 35 deletions(-) diff --git a/module/mr/control.php b/module/mr/control.php index c1462472c7..dd2930cf95 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -303,16 +303,14 @@ class mr extends control */ public function view(int $MRID) { - $MR = $this->mr->fetchByID($MRID); - if(!$MR) return $this->locate($this->createLink('mr', 'browse')); + $oldMR = $this->mr->fetchByID($MRID); + if(!$oldMR) return $this->locate($this->createLink('mr', 'browse')); - if(isset($MR->hostID)) $rawMR = $this->mr->apiGetSingleMR($MR->repoID, $MR->mriid); - if($MR->synced && (!isset($rawMR->id) || empty($rawMR))) $this->sendError($this->lang->mr->apiError->emptyResponse, true); + if(isset($oldMR->hostID)) $rawMR = $this->mr->apiGetSingleMR($oldMR->repoID, $oldMR->mriid); + if($oldMR->synced && (!isset($rawMR->id) || empty($rawMR))) $this->sendError($this->lang->mr->apiError->emptyResponse, true); /* Sync MR from GitLab to ZenTaoPMS. */ - $oldMR = $MR; - $MR = $this->mr->apiSyncMR($MR); - + $MR = $this->mr->apiSyncMR($oldMR); $changes = common::createChanges($oldMR, $MR); if($changes) { @@ -321,9 +319,7 @@ class mr extends control } $host = $this->loadModel('pipeline')->getByID($MR->hostID); - - $projectOwner = false; - if(in_array(strtolower($host->type), array('gitlab', 'gitfox'))) + if(in_array($host->type, array('gitlab', 'gitfox'))) { $MR->sourceProject = (int)$MR->sourceProject; $MR->targetProject = (int)$MR->targetProject; @@ -331,11 +327,7 @@ class mr extends control $projectMethod = $host->type == 'gitfox' ? 'apiGetSingleRepo' : 'apiGetSingleProject'; $sourceProject = $this->loadModel($host->type)->$projectMethod($MR->hostID, $MR->sourceProject); - if(isset($MR->hostID) && !$this->app->user->admin) - { - $openID = $this->loadModel('pipeline')->getOpenIdByAccount($MR->hostID, $host->type, $this->app->user->account); - if(!$projectOwner && isset($sourceProject->owner->id) && $sourceProject->owner->id == $openID) $projectOwner = true; - } + $compile = $this->loadModel('compile')->getById($MR->compileID); $this->view->title = $this->lang->mr->view; $this->view->MR = $MR; @@ -343,10 +335,8 @@ class mr extends control $this->view->rawMR = isset($rawMR) ? $rawMR : false; $this->view->reviewer = $this->loadModel('user')->getById($MR->assignee); $this->view->actions = $this->loadModel('action')->getList('mr', $MRID); - $this->view->compile = $this->loadModel('compile')->getById($MR->compileID); - $this->view->compileJob = $MR->jobID ? $this->loadModel('job')->getById($MR->jobID) : false; - $this->view->projectOwner = $projectOwner; - $this->view->projectEdit = $this->mrZen->checkProjectEdit($host->type, $sourceProject, $MR); + $this->view->compile = $compile; + $this->view->hasNewCommit = $this->mrZen->checkNewCommit($host->type, $MR->hostID, (string)$MR->targetProject, $MR->mriid, $compile->createdDate); $this->view->sourceProject = $sourceProject; $this->view->targetProject = $this->{$host->type}->$projectMethod($MR->hostID, $MR->targetProject); $this->view->sourceBranch = $this->mrZen->getBranchUrl($host, $MR->sourceProject, $MR->sourceBranch); @@ -800,10 +790,11 @@ class mr extends control */ public function commitLogs(int $MRID) { - $MR = $this->mr->fetchByID($MRID); + $MR = $this->mr->fetchByID($MRID); + $compile = $this->loadModel('compile')->getById($MR->compileID); $this->view->title = $this->lang->mr->commitLogs; $this->view->MR = $MR; - $this->view->compile = $this->loadModel('compile')->getById($MR->compileID); + $this->view->compile = $compile; if($MR->synced) { $rawMR = $this->mr->apiGetSingleMR($MR->repoID, $MR->mriid); @@ -811,8 +802,7 @@ class mr extends control if(!isset($rawMR->id) || empty($rawMR)) return $this->display(); } - $repo = $this->loadModel('repo')->getByID($MR->repoID); - + $repo = $this->loadModel('repo')->getByID($MR->repoID); $commitLogs = $this->mr->apiGetMRCommits($MR->hostID, $MR->targetProject, $MR->mriid); foreach($commitLogs as $commitLog) { @@ -820,7 +810,7 @@ class mr extends control if(strtolower($repo->SCM) == 'gitfox') { $commitLog->id = $commitLog->sha; - $commitLog->committed_date = $commitLog->author->when; + $commitLog->committed_date = date('Y-m-d H:i:s', strtotime($commitLog->author->when)); $commitLog->committer_name = $commitLog->author->identity->name; $commitLog->committer_email = $commitLog->author->identity->email; } @@ -836,8 +826,8 @@ class mr extends control $commitLog->id = substr($commitLog->id, 0, 10); } - $this->view->commitLogs = $commitLogs; - $this->view->repo = $repo; + $this->view->commitLogs = $commitLogs; + $this->view->repo = $repo; $this->display(); } diff --git a/module/mr/ui/view.html.php b/module/mr/ui/view.html.php index 8a1c9c7075..1b67806a65 100644 --- a/module/mr/ui/view.html.php +++ b/module/mr/ui/view.html.php @@ -38,14 +38,14 @@ if($MR->compileID) set::name($lang->compile->atTime), $compile->createdDate ), - ($compileJob && !empty($compileJob->id)) ? item + !empty($MR->jobID) ? item ( set::name($lang->compile->result), zget($lang->compile->statusList, $compile->status), in_array($compile->status, array('success', 'failure')) ? h::a ( setClass('ml-1'), - set::href($this->createLink('job', 'view', "jobID={$compileJob->id}&compileID={$compile->id}")), + set::href($this->createLink('job', 'view', "jobID={$MR->jobID}&compileID={$compile->id}")), set('data-toggle', 'modal'), icon('search'), $lang->compile->logs @@ -274,15 +274,24 @@ panel set::align('baseline'), div ( - setClass('text-lg font-bold'), + setClass('text-lg font-bold flex justify-between'), $lang->mr->jobID, - !empty($compileJob->id) && hasPriv('job', 'exec') ? btn + div ( - setClass('btn ghost text-primary size-sm pull-right ajax-submit'), - set::url(helper::createLink('mr', 'ajaxExecJob', "MRID={$MR->id}&jobID={$compileJob->id}")), - set::hint($lang->mr->execJobTip), - $lang->mr->execJob - ) : null + setClass('text-base font-thin'), + $hasNewCommit ? span + ( + setClass('mr-2'), + $lang->mr->branchUpdateTip + ) : null, + !empty($MR->jobID) && hasPriv('job', 'exec') ? btn + ( + setClass('label primary size-sm ajax-submit'), + set::url(helper::createLink('mr', 'ajaxExecJob', "MRID={$MR->id}&jobID={$MR->jobID}")), + set::hint($lang->mr->execJobTip), + $lang->mr->execJob + ) : null + ) ), $job ) diff --git a/module/mr/zen.php b/module/mr/zen.php index d6d72e31b7..7fe2f844e9 100644 --- a/module/mr/zen.php +++ b/module/mr/zen.php @@ -282,4 +282,31 @@ class mrZen extends mr $branch = $this->loadModel($host->type)->apiGetSingleBranch($host->id, $projectID, $branch); return $branch ? zget($branch, 'web_url', '') : ''; } + + /** + * 检查是否有新的提交。 + * Check if there are new commits. + * + * @param string $hostType + * @param int $hostID + * @param string $projectID + * @param int $mriid + * @param string $lastTime + * @access protected + * @return bool + */ + protected function checkNewCommit(string $hostType, int $hostID, string $projectID, int $mriid, string $lastTime): bool + { + $commitLogs = $this->mr->apiGetMRCommits($hostID, $projectID, $mriid); + if($commitLogs) + { + $lastCommit = zget($commitLogs[0], 'committed_date', ''); + if($hostType == 'gitfox') $lastCommit = date('Y-m-d H:i:s', strtotime($commitLogs[0]->author->when)); + if(in_array($hostType, array('gitea', 'gogs'))) $lastCommit = $commitLogs[0]->author->committer->date; + + if($lastCommit > $lastTime) return true; + } + + return false; + } }