diff --git a/module/gitlab/config.php b/module/gitlab/config.php index a1cbc5ee08..19d3542e97 100644 --- a/module/gitlab/config.php +++ b/module/gitlab/config.php @@ -1,9 +1,11 @@ gitlab->create = new stdclass(); -$config->gitlab->edit = new stdclass(); - $config->gitlab->create->requiredFields = 'name,url,token'; -$config->gitlab->edit->requiredFields = 'name,url,token'; + +$config->gitlab->edit = new stdclass(); +$config->gitlab->edit->requiredFields = 'name,url,token'; + +$config->gitlab->zentaoLabel = 'Zentao'; $config->gitlab->taskLabel = new stdclass(); $config->gitlab->taskLabel->name = "zentao task"; @@ -25,5 +27,3 @@ $config->gitlab->storyLabel->priority = "0"; $config->gitlab->zentaoApiWebhookUrl = "%s/api.php?m=gitlab&f=webhook&product=%s&gitlab=%s"; $config->gitlab->zentaoApiWebhookToken = ""; - - diff --git a/module/gitlab/control.php b/module/gitlab/control.php index 2fd9c19f64..4ee4ac3b1d 100644 --- a/module/gitlab/control.php +++ b/module/gitlab/control.php @@ -211,8 +211,11 @@ class gitlab extends control $gitlab = $this->get->gitlab; $project = $this->get->project; - $requestBody = json_decode(file_get_contents('php://input')); + $json = '{"object_kind":"note","event_type":"note","user":{"id":1,"name":"Administrator","username":"root","avatar_url":"https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80\u0026d=identicon","email":"admin@example.com"},"project_id":36,"project":{"id":36,"name":"zenops202106dgd","description":"repo for dingguodong dev env","web_url":"http://192.168.1.161:51080/root/zenops202106dgd","avatar_url":null,"git_ssh_url":"ssh://git@192.168.1.161:51022/root/zenops202106dgd.git","git_http_url":"http://192.168.1.161:51080/root/zenops202106dgd.git","namespace":"Administrator","visibility_level":0,"path_with_namespace":"root/zenops202106dgd","default_branch":"master","ci_config_path":null,"homepage":"http://192.168.1.161:51080/root/zenops202106dgd","url":"ssh://git@192.168.1.161:51022/root/zenops202106dgd.git","ssh_url":"ssh://git@192.168.1.161:51022/root/zenops202106dgd.git","http_url":"http://192.168.1.161:51080/root/zenops202106dgd.git"},"object_attributes":{"attachment":null,"author_id":1,"change_position":null,"commit_id":null,"created_at":"2021-06-22 03:04:00 UTC","discussion_id":"1e66ea56b811ac13502252c62e1d22a6ddcf4340","id":705,"line_code":null,"note":"124ds q ew 1234 142dsf ssdfgsdg","noteable_id":32,"noteable_type":"Issue","original_position":null,"position":null,"project_id":36,"resolved_at":null,"resolved_by_id":null,"resolved_by_push":null,"st_diff":null,"system":false,"type":null,"updated_at":"2021-06-22 03:04:00 UTC","updated_by_id":null,"description":"124ds q ew 1234 142dsf ssdfgsdg","url":"http://192.168.1.161:51080/root/zenops202106dgd/-/issues/2#note_705"},"repository":{"name":"zenops202106dgd","url":"ssh://git@192.168.1.161:51022/root/zenops202106dgd.git","description":"repo for dingguodong dev env","homepage":"http://192.168.1.161:51080/root/zenops202106dgd"},"issue":{"author_id":1,"closed_at":null,"confidential":false,"created_at":"2021-06-16 10:52:00 UTC","description":"the issue description","discussion_locked":null,"due_date":null,"id":32,"iid":2,"last_edited_at":null,"last_edited_by_id":null,"milestone_id":null,"moved_to_id":null,"duplicated_to_id":null,"project_id":36,"relative_position":1026,"state_id":1,"time_estimate":0,"title":"test issue","updated_at":"2021-06-22 03:04:00 UTC","updated_by_id":1,"url":"http://192.168.1.161:51080/root/zenops202106dgd/-/issues/2","total_time_spent":0,"human_total_time_spent":null,"human_time_estimate":null,"assignee_ids":[],"assignee_id":null,"labels":[{"id":38,"title":"zentao task","color":"#0033CC","project_id":36,"created_at":"2021-06-16 12:04:23 UTC","updated_at":"2021-06-16 12:04:23 UTC","template":false,"description":"task label from zentao","type":"ProjectLabel","group_id":null},{"id":40,"title":"zentao task : 121","color":"#428BCA","project_id":36,"created_at":"2021-06-21 08:58:17 UTC","updated_at":"2021-06-21 08:58:17 UTC","template":false,"description":"121","type":"ProjectLabel","group_id":null}],"state":"opened"}}'; + //$json = file_get_contents('php://input'); + $requestBody = json_decode($json); $request = $this->gitlab->parseWebhookBody($requestBody); + exit; switch($request->type) { @@ -231,15 +234,15 @@ class gitlab extends control if($fh) { fwrite($fh, date('Ymd H:i:s') . ": " . $this->app->getURI() . "\n"); - fwrite($fh, "GET: " . var_export($_GET, true) . "\n"); - fwrite($fh, "Body: " . var_export($requestBody, true) . "\n"); - if(!empty($errors)) fwrite($fh, "errors: " . $errors . "\n"); + fwrite($fh, "JSON: " . $json . "\n"); + fwrite($fh, "GET: " . print_r($_GET, true) . "\n"); + fwrite($fh, "Body: " . var_export($requestBody, true) . "\n"); fclose($fh); } $this->view->result = 'success'; $this->view->status = 'ok'; - $this->view->data = 'ougiugjvh'; + $this->view->data = 'ougiugjvh'; $this->display(); } } diff --git a/module/gitlab/model.php b/module/gitlab/model.php index 36f220b035..1d8fa02159 100644 --- a/module/gitlab/model.php +++ b/module/gitlab/model.php @@ -135,7 +135,7 @@ class gitlabModel extends model if($gitlabUser->email == $zentaoUser->email) $matches->emails[$gitlabUser->email][] = $zentaoUser->account; } } - + $bindedUsers = $this->dao->select('openID,account') ->from(TABLE_OAUTH) ->where('providerType')->eq('gitlab') @@ -156,7 +156,7 @@ class gitlabModel extends model if(isset($matches->accounts[$gitlabUser->account])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->accounts[$gitlabUser->account]); if(isset($matches->emails[$gitlabUser->email])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->emails[$gitlabUser->email]); if(isset($matches->names[$gitlabUser->realname])) $matchedZentaoUsers = array_merge($matchedZentaoUsers, $matches->names[$gitlabUser->realname]); - + $matchedZentaoUsers = array_unique($matchedZentaoUsers); if(count($matchedZentaoUsers) == 1) { @@ -239,13 +239,13 @@ class gitlabModel extends model $projectID = array(); foreach($productIDs as $project) $projectID[$project->id] = $project->program; - $gitlabAssociat = new stdclass; - $gitlabAssociat->execution = 0; - $gitlabAssociat->AVersion = 0; - $gitlabAssociat->relation = 'interrated'; - $gitlabAssociat->BVersion = 0; - $gitlabAssociat->extra = 0; - $gitlabAssociat->BID = $gitlabID; + $gitlabAssociat = new stdclass; + $gitlabAssociat->execution = 0; + $gitlabAssociat->AVersion = 0; + $gitlabAssociat->relation = 'interrated'; + $gitlabAssociat->BVersion = 0; + $gitlabAssociat->extra = 0; + $gitlabAssociat->BID = $gitlabID; foreach($products as $index => $prodcut) { @@ -253,7 +253,7 @@ class gitlabModel extends model $gitlabAssociat->Project = $projectID[$prodcut]; $gitlabAssociat->Product = $prodcut; - $this->dao->replace(TABLE_RELATION)->data($gitlabAssociat)->exec(); + $this->dao->replace(TABLE_RELATION)->data($gitlabAssociat)->exec(); } return true; } @@ -269,7 +269,7 @@ class gitlabModel extends model public function createWebhook($products, $gitlabID, $projectID) { $urls = $this->getWebhookUrls($gitlabID, $projectID); - + foreach($products as $index => $product) { $url = sprintf($this->config->gitlab->zentaoApiWebhookUrl, commonModel::getSysURL(), $product, $gitlabID, $projectID); @@ -348,7 +348,7 @@ class gitlabModel extends model public function apiCreateHook($gitlabID, $projectID, $url, $token) { $apiRoot = $this->getApiRoot($gitlabID); - + $postData = new stdclass; $postData->enable_ssl_verification = "false"; $postData->issues_events = "true"; @@ -394,7 +394,7 @@ class gitlabModel extends model public function apiUpdateHook($gitlabID, $projectID, $hookID) { $apiRoot = $this->getApiRoot($gitlabID); - + $postData = new stdclass; $postData->enable_ssl_verification = "false"; $postData->issues_events = "true"; @@ -571,6 +571,18 @@ class gitlabModel extends model $request->type = $body->object_kind; $issue = $body->object_attributes; + $request->labels = $body->labels; + $request->project = $body->project->id; + } + + public function parseNoteWebhook($body) + { + $request = new stdclass; + $request->type = $body->object_kind; + if(isset($body->issue)) $body->issue = $this->parseIssue($body->issue); + + $request->labels = $body->labels; + $request->project = $body->project->id; $request->labels = $labels; $request->type = $labelType; $request->typeID = $labelTypeID; @@ -584,7 +596,56 @@ class gitlabModel extends model $request->assignees = $issue->assignee_id; $request->url = $issue->url; - a($request);exit(); } + /** + * Parse zentao object from labels. + * + * @param array $labels + * @access public + * @return object|false + */ + public function parseObjectFromLabels($labels) + { + foreach($body->labels as $label) + { + if($label->title == $config->gitlab->zentaoLabel) + { + $object = json_decode($label->description); + if(!$object or !isset($object->type)) continue; + return $object; + } + } + return false; + } + + /** + * 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; + } + + public function issue2Task($issue) + { + } + + public function issue2Story($issue) + { + } + + public function issue2Bug($issue) + { + + } }