diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 1a87c51110..2d25db6f52 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -387,6 +387,23 @@ class gitlabModel extends model return json_decode(commonModel::http($url, $data, $options)); } + /** + * Get a list of to-do items. + * + * @see https://docs.gitlab.com/ee/api/todos.html + * @param int $gitlabID + * @param int $projectID + * @access public + * @return object + */ + public function apiTodoList($gitlabID, $projectID) + { + $gitlab = $this->loadModel('gitlab')->getByID($gitlabID); + if(!$gitlab) return ''; + $url = rtrim($gitlab->url, '/')."/api/v4/todos?project_id=$projectID&type=MergeRequest&private_token={$gitlab->token}"; + return json_decode(commonModel::http($url)); + } + /** * Get current user. * diff --git a/module/mr/config.php b/module/mr/config.php index 35fdbbcb4a..c64305044e 100644 --- a/module/mr/config.php +++ b/module/mr/config.php @@ -6,11 +6,14 @@ $config->MR->create->skippedFields = 'projectID'; $config->MR->maps = new stdclass; $config->MR->maps->sync = array(); -$config->MR->maps->sync['title'] = 'title|field|'; -$config->MR->maps->sync['description'] = 'description|field|'; -$config->MR->maps->sync['assignee'] = 'assignees|userPairs|id'; -$config->MR->maps->sync['reviewer'] = 'reviewers|userPairs|id'; -$config->MR->maps->sync['targetBranch'] = 'target_branch|field|'; -$config->MR->maps->sync['status'] = 'state|field|'; -$config->MR->maps->sync['mergeStatus'] = 'merge_status|field|'; +$config->MR->maps->sync['title'] = 'title|field|'; +$config->MR->maps->sync['description'] = 'description|field|'; +$config->MR->maps->sync['assignee'] = 'assignees|userPairs|id'; +$config->MR->maps->sync['reviewer'] = 'reviewers|userPairs|id'; +$config->MR->maps->sync['targetBranch'] = 'target_branch|field|'; +$config->MR->maps->sync['sourceBranch'] = 'source_branch|field|'; +$config->MR->maps->sync['sourceProject'] = 'source_project_id|field|'; +$config->MR->maps->sync['targetProject'] = 'target_project_id|field|'; +$config->MR->maps->sync['status'] = 'state|field|'; +$config->MR->maps->sync['mergeStatus'] = 'merge_status|field|'; diff --git a/module/mr/control.php b/module/mr/control.php index ba743b6ba6..4c9450be56 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -15,13 +15,17 @@ class mr extends control public function browse($objectID = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1) { $this->app->loadClass('pager', $static = true); - $pager = new pager($recTotal, $recPerPage, $pageID); + $pager = new pager($recTotal, $recPerPage, $pageID); + $MRList = $this->mr->getList($orderBy, $pager); /* Save current URI to session. */ $this->session->set('mrList', $this->app->getURI(true), 'repo'); + /* Sync GitLab MR to ZenTao Database. */ + $this->mr->batchSyncMR($MRList); + $this->view->title = $this->lang->mr->common . $this->lang->colon . $this->lang->mr->browse; - $this->view->MRList = $this->mr->getList($orderBy, $pager); + $this->view->MRList = $MRList; $this->view->orderBy = $orderBy; $this->view->objectID = $objectID; $this->view->pager = $pager; @@ -148,7 +152,8 @@ class mr extends control */ public function syncMR() { - $this->mr->getList(); + $MRList = $this->mr->getList(); + $this->mr->batchSyncMR($MRList); if(dao::isError()) { diff --git a/module/mr/model.php b/module/mr/model.php index 0579eff71d..1f900b3b57 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -52,8 +52,6 @@ class mrModel extends model ->page($pager) ->fetchAll('id'); - foreach($MRList as $MR) $this->apiSyncMR($MR); - return $MRList; } @@ -191,7 +189,7 @@ class mrModel extends model { $rawMR = $this->apiGetSingleMR($MR->gitlabID, $MR->targetProject, $MR->mriid); - if(isset($rawMR->iid)) + if(isset($rawMR->iid) and $rawMR->state != 'merged') { $map = $this->config->MR->maps->sync; $gitlabUsers = $this->gitlab->getUserIdAccountPairs($MR->gitlabID); @@ -220,63 +218,98 @@ class mrModel extends model $this->dao->update(TABLE_MR)->data($newMR) ->where('id')->eq($MR->id) ->exec(); - - /*Sync GitLab Todo ZenTao Todo. */ - - $gitlabTodoList = $this->apiTodoList($MR->gitlabID, $MR->targetProject); - if($gitlabTodoList) - { - foreach($gitlabTodoList as $do) - { - $todoDesc = $this->dao->select('*') - ->from(TABLE_TODO) - ->where('idvalue') - ->eq($do->id) - ->fetch(); - if(empty($todoDesc)) - { - $todo = new stdClass; - $todo->account = $this->app->user->account; - $todo->assignedTo = $this->app->user->account; - $todo->assignedBy = $this->app->user->account; - $todo->date = $do->target->created_at; - $todo->assignedDate = $do->target->created_at; - $todo->begin = $do->target->created_at; - $todo->end = ''; - $todo->type = 'mrapprove'; - $todo->idvalue = $do->id; - $todo->pri = 1; - $todo->name = $do->target->title; - $todo->desc = $do->target->description . "
" . $this->todoDescriptionLink($MR->gitlabID, $MR->targetProject); - $todo->private = 0; - $todo->config = 0; - $todo->finishedBy = ''; - $todo->finishedDate = ''; - $todo->closedBy = ''; - $todo->closedDate = '0000-00-00 00:00:00'; - $this->dao->insert(TABLE_TODO)->data($todo)->exec(); - } - } - } } return $this->dao->findByID($MR->id)->from(TABLE_MR)->fetch(); } /** - * Get a list of to-do items. + * Batch Sync GitLab MR Database. + * + * @param object $MRList + * @access public + * @return void + */ + public function batchSyncMR($MRList) + { + if(!empty($MRList)) foreach($MRList as $MR) + { + + $rawMR = $this->apiGetSingleMR($MR->gitlabID, $MR->targetProject, $MR->mriid); + + if(isset($rawMR->iid) and $rawMR->state != 'merged') + { + /* create gitlab mr todo to zentao todo */ + $this->batchSyncTodo($MR->gitlabID, $MR->targetProject); + + $map = $this->config->MR->maps->sync; + $gitlabUsers = $this->gitlab->getUserIdAccountPairs($MR->gitlabID); + + $newMR = new stdclass; + foreach($map as $syncField => $config) + { + $value = ''; + list($field, $optionType, $options) = explode('|', $config); + + if($optionType == 'field') $value = $rawMR->$field; + if($optionType == 'userPairs') + { + $gitlabUserID = ''; + if(isset($rawMR->$field[0])) + { + $gitlabUserID = $rawMR->$field[0]->$options; + } + $value = zget($gitlabUsers, $gitlabUserID, ''); + } + + if($value) $newMR->$syncField = $value; + } + /* Update MR in Zentao database. */ + $this->dao->update(TABLE_MR)->data($newMR) + ->where('id')->eq($MR->id) + ->exec(); + } + } + } + + /** + * Sync GitLab Todo to ZenTao Todo. * - * @docs https://docs.gitlab.com/ee/api/todos.html * @param int $gitlabID * @param int $projectID * @access public - * @return object + * @return void */ - public function apiTodoList($gitlabID, $projectID) + public function batchSyncTodo($gitlabID, $projectID) { - $gitlab = $this->loadModel('gitlab')->getByID($gitlabID); - if(!$gitlab) return ''; - $url = rtrim($gitlab->url, '/')."/api/v4/todos?project_id=$projectID&type=MergeRequest&private_token={$gitlab->token}"; - return json_decode(commonModel::http($url)); + $todoList = $this->loadModel('gitlab')->apiTodoList($gitlabID, $projectID); + + if(!empty($todoList)) + { + foreach($todoList as $do) + { + $todoDesc = $this->dao->select('*') + ->from(TABLE_TODO) + ->where('idvalue')->eq($do->id) + ->fetch(); + if(empty($todoDesc)) + { + $todo = new stdClass; + $todo->account = $this->app->user->account; + $todo->assignedTo = $this->app->user->account; + $todo->assignedBy = $this->app->user->account; + $todo->date = $do->target->created_at; + $todo->assignedDate = $do->target->created_at; + $todo->begin = $do->target->created_at; + $todo->type = 'mrapprove'; + $todo->idvalue = $do->id; + $todo->pri = 1; + $todo->name = $do->target->title; + $todo->desc = $do->target->description . "
" . $this->todoDescriptionLink($gitlabID, $projectID); + $todo->finishedBy = $this->app->user->account; + $this->dao->insert(TABLE_TODO)->data($todo)->exec(); + } + } + } } /**