diff --git a/module/gitlab/config.php b/module/gitlab/config.php index 67dea62ab6..bc042904bb 100644 --- a/module/gitlab/config.php +++ b/module/gitlab/config.php @@ -31,23 +31,31 @@ $config->gitlab->storyLabel->priority = "0"; $config->gitlab->zentaoApiWebhookUrl = "%s/api.php?m=gitlab&f=webhook&product=%s&gitlab=%s"; $config->gitlab->zentaoApiWebhookToken = ""; +$config->gitlab->skippedFields = new stdclass; +$config->gitlab->skippedFields->issueCreate = array(); +$config->gitlab->skippedFields->issueCreate[] = ''; + $config->gitlab->maps = new stdclass; $config->gitlab->maps->task = array(); -$config->gitlab->maps->task['execution'] = 'execution'; -$config->gitlab->maps->task['name'] = 'title'; -$config->gitlab->maps->task['desc'] = 'description'; -$config->gitlab->maps->task['deadline'] = 'due_date'; +$config->gitlab->maps->task['name'] = 'title|field|'; +$config->gitlab->maps->task['desc'] = 'description|field|'; +$config->gitlab->maps->task['openedDate'] = 'created_at|field|'; +$config->gitlab->maps->task['assignedTo'] = 'assignee_id|userPairs|'; +$config->gitlab->maps->task['lastEditedDate'] = 'updated_at|field|'; +$config->gitlab->maps->task['deadline'] = 'due_date|field|'; +$config->gitlab->maps->task['status'] = 'state_event|configItems|taskStateMap'; +$config->gitlab->maps->task['pri'] = 'weight|configItems|taskWeightMap'; -$config->gitlab->maps->task['openedBy'] = 'due_date'; -$config->gitlab->maps->task['openedDate'] = 'created_at'; -$config->gitlab->maps->task['assignedTo'] = 'due_date'; -$config->gitlab->maps->task['closedBy'] = 'due_date'; -$config->gitlab->maps->task['closedDate'] = 'due_date'; -$config->gitlab->maps->task['finishedDate'] = 'closed_at'; -$config->gitlab->maps->task['lastEditedDate'] = 'last_edited_at'; -$config->gitlab->maps->task['deadline'] = 'due_date'; +$config->gitlab->taskWeightMap = array(); +$config->gitlab->taskWeightMap['1'] = '1'; +$config->gitlab->taskWeightMap['2'] = '2'; +$config->gitlab->taskWeightMap['3'] = '3'; -$config->gitlab->issueOptions = new stdclass; -$config->gitlab->issueOptions->task['type'] = 'devel'; -$config->gitlab->issueOptions->task['pri'] = '3'; -$config->gitlab->issueOptions->task['status'] = array(); +$config->gitlab->taskStateMap = array(); +$config->gitlab->taskStateMap['doing'] = 'reopen'; +$config->gitlab->taskStateMap['wait'] = 'reopen'; +$config->gitlab->taskStateMap['done'] = 'close'; +$config->gitlab->taskStateMap['cancel'] = 'close'; +$config->gitlab->taskStateMap['closed'] = 'close'; + +$config->gitlab->taskTypesToSync = 'design,devel,request,discuss,ui,affair,misc'; diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 546948c484..e61210e52a 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -246,4 +246,20 @@ class gitlab extends control $this->view->data = 'ougiugjvh'; $this->display(); } + + public function test($id = 11) + { + $gitlabID = 11; $projectID = 36; + $task = $this->loadModel('task')->getByID($id); + $issue = $this->gitlab->taskToIssue($gitlabID, $projectID, $task); + $issue = $this->gitlab->apiCreateIssue($gitlabID, $projectID, $issue); + a($issue); + $this->gitlab->saveSyncedIssue('task', $task, $gitlabID, $issue); + + exit; + } + + + + } diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 4429bf8e4d..543b6f65e7 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -56,7 +56,7 @@ class gitlabModel extends model */ public function getUserIdAccountPairs($gitlab) { - return $this->dao->select('id,account')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andwhere('providerID')->eq($gitlabID)->fetchPairs(); + return $this->dao->select('openID,account')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andwhere('providerID')->eq($gitlab)->fetchPairs(); } /** @@ -68,7 +68,7 @@ class gitlabModel extends model */ public function getUserAccountIdPairs($gitlab) { - return $this->dao->select('account,id')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andwhere('providerID')->eq($gitlabID)->fetchPairs(); + return $this->dao->select('account,openID')->from(TABLE_OAUTH)->where('providerType')->eq('gitlab')->andwhere('providerID')->eq($gitlab)->fetchPairs(); } /** @@ -554,38 +554,109 @@ class gitlabModel extends model } /** - * sync gitlab Issue. + * Sync task to gitlab issue. * - * @param int $gitlabID - * @param int $projectID + * @param int $taskID + * @param int $gitlab + * @param int $gitlabProject * @access public * @return void */ - public function syncGitlabIssue($taskID, $task, $gitlabID, $projectID) + public function syncTask($taskID, $gitlab, $gitlabProject) { - $issue = new stdClass(); - $issue->assignee_id = $taskID; - $issue->created_at = $task->create_at; - $issue->description = $task->description; - $issue->labelType = $task->labelType; - $issue->due_date = $task->data; - $issue->id = $task->project; - $issue->title = $task->title; - $issue->weight = $task->weight; - $issue->labels = $task->labels; - - $response = $this->loadModel('gitlab')->apiCreateIssue($gitlabID, $projectID, $issue); + $task = $this->loadModel('task')->getByID($taskID); + $syncedIssue = $this->getSyncedIssue($objectType = 'task', $objectID = $taskID, $gitlab); - return $response; + $issue = $this->taskToIssue($gitlab, $gitlabProject, $task); + if($syncedIssue) $issue = $this->apiUpdateIssue($gitlab, $gitlabProject, $syncedIssue, $issue); + $issue = $this->apiCreateIssue($gitlab, $gitlabProject, $issue); + + $this->saveSyncedIssue('task', $taskID, $gitlab, $issue); + } + + /** + * Get synced issue from relation table. + * + * @param string $objectType + * @param int $objectID + * @param int $gitlab + * @access public + * @return object + */ + public function getSyncedIssue($objectType, $objectID, $gitlab) + { + return $this->dao->select('*')->from(TABLE_RELATION) + ->where('AType')->eq($objectType) + ->andWhere('AID')->eq($objectID) + ->andWhere('extra')->eq($gitlab) + ->fetch(); + } + + /** + * Save synced issue to relation table. + * + * @param string $objectType + * @param object $object + * @param int $gitlab + * @param object $issue + * @access public + * @return void + */ + public function saveSyncedIssue($objectType, $object, $gitlab, $issue) + { + $relation = new stdclass; + $relation->execution = zget($object, 'execution', 0); + $relation->AType = $objectType; + $relation->AID = $object->id; + $relation->AVersion = ''; + $relation->relation = 'gitlab'; + $relation->BType = 'issue'; + $relation->BID = $issue->iid; + $relation->BVersion = $issue->project_id; + $relation->extra = $gitlab; + $this->dao->replace(TABLE_RELATION)->data($relation)->exec(); + } + + /** + * Parse task to issue. + * + * @param int $gitlabID + * @param int $gitlabProjectID + * @param object $task + * @access public + * @return object + */ + public function taskToIssue($gitlabID, $gitlabProjectID, $task) + { + $map = $this->config->gitlab->maps->task; + $issue = new stdclass; + $gitlabUsers = $this->getUserAccountIdPairs($gitlabID); + foreach($map as $taskField => $config) + { + $value = ''; + list($field, $optionType, $options) = explode('|', $config); + if($optionType == 'field') $value = $task->$taskField; + if($optionType == 'userPairs') $value = zget($gitlabUsers, $task->$taskField); + if($optionType == 'configItems') + { + $value = zget($this->config->gitlab->$options, $task->$taskField, ''); + } + if($value) $issue->$field = $value; + } + return $issue; } public function apiCreateIssue($gitlabID, $projectID, $issue) { $apiRoot = $this->getApiRoot($gitlabID); $apiPath = "/projects/{$projectID}/issues/"; + foreach($this->config->gitlab->skippedFields->issueCreate as $field) + { + if(isset($issue->$field)) unset($issue->$field); + } + $url = sprintf($apiRoot, $apiPath); - $response = json_decode(commonModel::http($url, $issue)); - return $response; + return json_decode(commonModel::http($url, $issue)); } public function pushTask($gitlabID, $projectID, $task) diff --git a/module/task/model.php b/module/task/model.php index d15807aa76..35c5b55716 100644 --- a/module/task/model.php +++ b/module/task/model.php @@ -120,8 +120,8 @@ class taskModel extends model $taskID = $this->dao->lastInsertID(); - /*sync gitlab Issue*/ - $this->loadModel('gitlab')->syncGitlabIssue($taskID, $gitlabID, $projectID); + /* Sync this task to gitlab issue. */ + $this->loadModel('gitlab')->syncTask($taskID, $this->post->gitlab, $this->post->gitlabProject); /* Mark design version.*/ if(isset($task->design) && !empty($task->design))