diff --git a/module/gitlab/control.php b/module/gitlab/control.php index b559ae27fa..201f13f110 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -119,10 +119,10 @@ class gitlab extends control $user->providerType = 'gitlab'; $this->dao->delete() - ->from(TABLE_OAUTH) - ->where('providerType')->eq($user->providerType) - ->andWhere('providerID')->eq($user->providerID) - ->exec(); + ->from(TABLE_OAUTH) + ->where('providerType')->eq($user->providerType) + ->andWhere('providerID')->eq($user->providerID) + ->exec(); foreach($users as $openID => $account) { @@ -131,12 +131,12 @@ class gitlab extends control $user->openID = $openID; $this->dao->delete() - ->from(TABLE_OAUTH) - ->where('openID')->eq($user->openID) - ->andWhere('providerType')->eq($user->providerType) - ->andWhere('providerID')->eq($user->providerID) - ->andWhere('account')->eq($user->account) - ->exec(); + ->from(TABLE_OAUTH) + ->where('openID')->eq($user->openID) + ->andWhere('providerType')->eq($user->providerType) + ->andWhere('providerID')->eq($user->providerID) + ->andWhere('account')->eq($user->account) + ->exec(); $this->dao->insert(TABLE_OAUTH)->data($user)->exec(); } @@ -162,8 +162,8 @@ class gitlab extends control */ public function bindProduct($gitlabID) { - $this->view->projectPairs = $this->gitlab->getProjectPairs($gitlabID); - $this->view->title = $this->lang->gitlab->bindProduct; + $this->view->projectPairs = $this->gitlab->getProjectPairs($gitlabID); + $this->view->title = $this->lang->gitlab->bindProduct; $this->display(); } @@ -215,7 +215,7 @@ class gitlab extends control //$input = file_get_contents('php://input'); $requestBody = json_decode($input); $result = $this->gitlab->webhookParseBody($requestBody, $gitlab); - + a($result);exit; $logFile = $this->app->getLogRoot() . 'webhook.'. date('Ymd') . '.log.php'; if(!file_exists($logFile)) file_put_contents($logFile, ''); @@ -228,12 +228,7 @@ class gitlab extends control fclose($fh); } - switch($result->objectType) - { - case 'task': - $this->updateTaskFromIssue($request); - break; - } + if($result->action = 'updateissue') $this->gitlab->webhookSyncIssue($gitlab, $result); $this->view->result = 'success'; $this->view->status = 'ok'; @@ -248,7 +243,6 @@ class gitlab extends control $issue = $this->gitlab->taskToIssue($gitlabID, $projectID, $task); $issue = $this->gitlab->apiCreateIssue($gitlabID, $projectID, $issue); $this->gitlab->saveSyncedIssue('task', $task, $gitlabID, $issue); - exit; } } diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 8b3e690fcb..4732dabbaf 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -777,17 +777,18 @@ class gitlabModel extends model */ public function WebhookParseIssue($body, $gitlabID) { - $object = $this->parseObjectFromLabel($body->labels); + $object = $this->webhookParseObject($body->labels); if(empty($object)) return null; $issue = new stdclass; - $issue->action = $body->object_attributes->action . $body->object_kind; - $issue->issue = $body->object_attributes; - $issue->objectType = $object->type; - $issue->objectID = $object->id; + $issue->action = $body->object_attributes->action . $body->object_kind; + $issue->issue = $body->object_attributes; - if(!is_callable(array($this, "issueTo{$object->type}"))) return false; - $issue->object = call_user_func_array(array($this, "issueTo{$object->type}"), array('body' => $issue->issue, 'gitlab' => $gitlabID)); + $issue->issue->objectType = $object->type; + $issue->issue->objectID = $object->id; + + if(!isset($this->config->gitlab->maps->$object->type)) return false; + $issue->object = $this->issueToZentaoObject($issue->issue, $gitlabID); return $issue; } @@ -821,6 +822,22 @@ class gitlabModel extends model $request->url = $issue->url; } + /** + * Webhook sync issue. + * + * @param object $issue + * @param int $objectType + * @param int $objectID + * @access public + * @return void + */ + public function webhookSyncIssue($issue) + { + $tableName = zget($this->config->gitlab->objectTables, $issue->objectType, ''); + if($tableName) $this->dao->update($tableName)->data($issue->object)->where('id')->eq($issue->objectID)->exec(); + return !dao::isError(); + } + /** * Parse zentao object from labels. * @@ -828,7 +845,7 @@ class gitlabModel extends model * @access public * @return object|false */ - public function parseObjectFromLabel($labels) + public function webhookParseObject($labels) { $object = null; foreach($labels as $label) @@ -836,6 +853,7 @@ class gitlabModel extends model if(preg_match($this->config->gitlab->labelPattern->task, $label->title)) { list($prefix, $id) = explode('/', $label->title); + $object = new stdclass; $object->type = 'task'; $object->id = $id; @@ -845,71 +863,33 @@ class gitlabModel extends model } /** - * Parse issue from gitlab. - * - * @param object $issue - * @access public - * @return object - */ - public function parseIssue($issue) - { - $object = $this->parseObjectFromLabels($issue->labels); - if(!$object) return false; - if($object->type == 'task') $object->object = $this->issue2Task($issue); - if($object->type == 'story') $object->object = $this->issue2Story($issue); - if($object->type == 'bug') $object->object = $this->issue2Bug($issue); - return $object; - } - - /** - * Parse issue to task. + * Parse issue to zentao object. * * @param object $issue * @param int $gitlabID * @access public * @return object */ - public function issueToTask($issue, $gitlabID) + public function issueToZentaoObject($issue, $gitlabID) { - $task = new stdclass; - $maps = $this->config->gitlab->maps->task; + if(!isset($this->config->gitlab->maps->{$issue->objectType}) return null; + + $maps = $this->config->gitlab->maps->{$issue->objectType}; $gitlabUsers = $this->getUserAccountIdPairs($gitlabID); - foreach($maps as $taskField => $config) + $object = new stdclass; + $object->id = $issue->objectID; + foreach($maps as $zentaoField => $config) { $value = ''; - list($field, $optionType, $options) = explode('|', $config); - if($optionType == 'field') $value = $issue->$field; - if($optionType == 'userPairs') $value = zget($gitlabUsers, $issue->$field); - if($optionType == 'configItems' and isset($issue->$field)) $value = array_search($issue->$field, $this->config->gitlab->$options); - if($value) $task->$taskField = $value; + list($gitlabField, $optionType, $options) = explode('|', $config); + if($optionType == 'field') $value = $issue->$gitlabField; + if($optionType == 'field') $value = $issue->$gitlabField; + if($optionType == 'userPairs') $value = zget($gitlabUsers, $issue->$gitlabField); + if($optionType == 'configItems' and isset($issue->$gitlabField)) $value = array_search($issue->$gitlabField, $this->config->gitlab->$options); + if($value) $object->$zentaoField = $value; } - return $task; - } - - /** - * Parse issue to story. - * - * @param object $issue - * @param int $gitlabID - * @access public - * @return object - */ - public function issueToStory($issue, $gitlabID) - { - } - - /** - * Parse issue to task. - * - * @param object $issue - * @param int $gitlabID - * @access public - * @return object - */ - public function issueToBug($issue, $gitlabID) - { - + return $object; } /** @@ -918,7 +898,7 @@ class gitlabModel extends model * @param int $gitlabID * @param string $account * @access public - * @return void + * @return arary */ public function getGitlabUserID($gitlabID, $account) {