From 08aff43f08c95e3f31d66fd2cdfba3803be00d7f Mon Sep 17 00:00:00 2001 From: dingguodong Date: Fri, 20 Aug 2021 15:16:18 +0800 Subject: [PATCH] * Finish creating MR in model. --- module/mr/config.php | 5 +++++ module/mr/control.php | 18 ++++++++++++++--- module/mr/model.php | 45 +++++++++++++++++++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 module/mr/config.php diff --git a/module/mr/config.php b/module/mr/config.php new file mode 100644 index 0000000000..da874423db --- /dev/null +++ b/module/mr/config.php @@ -0,0 +1,5 @@ +MR = new stdclass(); +$config->MR->create = new stdclass(); +$config->MR->create->requiredFields = 'gitlabID,projectID,mrID,title'; +$config->MR->create->skippedFields = 'sourceProject,sourceBranch,targetProject,targetBranch'; diff --git a/module/mr/control.php b/module/mr/control.php index 6e74f224cc..8134df0fdf 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -35,7 +35,9 @@ class mr extends control { if($_POST) { - $this->mr->create(); + $rawMR = $this->mr->create(); + if(isset($rawMR->message)) return $this->send(array('result' => 'fail', 'message' => $rawMR->message)); + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('browse'))); } @@ -46,13 +48,23 @@ class mr extends control } /** - * update + * Update/Edit MR function. * * @access public * @return void */ - public function update() + public function update($MRID) { + if($_POST) + { + $this->mr->update(); + if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); + return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('browse'))); + } + + $this->view->MR = $this->mr->getByID($MRID); + $this->view->title = $this->lang->mr->update; + $this->display(); } /** diff --git a/module/mr/model.php b/module/mr/model.php index f2e1195416..30b5f20c55 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -32,7 +32,8 @@ class mrModel extends model */ public function getByID($id) { - return $this->dao->select('*')->from(TABLE_MR)->where('id')->eq($id)->fetch(); + $MR = $this->dao->select('*')->from(TABLE_MR)->where('id')->eq($id)->fetch(); + return $this->processMR($MR); } /** @@ -58,7 +59,7 @@ class mrModel extends model } /** - * ProcessMR info by api. + * Process MR info by api. * * @param object $MR * @access public @@ -66,7 +67,7 @@ class mrModel extends model */ public function processMR($MR) { - $rawMR = $this->apiGetSingleMR($MR->gitlabID, $MR->projectID, $MR->id); + $rawMR = $this->apiGetSingleMR($MR->gitlabID, $MR->projectID, $MR->mrID); $MR->name = $rawMR->title; $MR->targetBranch = $rawMR->target_branch; @@ -118,7 +119,7 @@ class mrModel extends model public function create() { $gitlabID = $this->post->gitlabID; - $projectID = $this->post->projectID; + $projectID = $this->post->sourceProject; $MR = new stdclass; $MR->target_project_id = $this->post->targetProject; @@ -130,6 +131,42 @@ class mrModel extends model $MR->reviewer_ids = $this->post->reviewer; $rawMR = $this->apiCreateMR($gitlabID, $projectID, $MR); + + /* Another open merge request already exists for this source branch. */ + if(isset($rawMR->message) and !isset($rawMR->iid)) return $rawMR; + + /* Create MR failed. */ + if(!isset($rawMR->iid)) return false; + + $MR = fixer::input('post') + ->add('repoID', 0) + ->add('gitlabID', $gitlabID) + ->add('projectID', $projectID) + ->add('mrID', $rawMR->iid) + ->add('createdBy', $this->app->user->account) + ->add('createdDate', helper::now()) + ->get(); + + /* Remove extra fields before inserting db table. */ + foreach(explode(',', $this->config->MR->create->skippedFields) as $field) unset($MR->$field); + + $this->dao->insert(TABLE_MR)->data($MR) + ->batchCheck($this->config->MR->create->requiredFields, 'notempty') + ->autoCheck() + ->exec(); + if(dao::isError()) return false; + + return $this->dao->lastInsertId(); + } + + /** + * Update MR function. + * + * @access public + * @return void + */ + public function update() + { } /**