From 9375676a666e8ca3be038515db327125e2747bb9 Mon Sep 17 00:00:00 2001 From: liugang Date: Wed, 25 Oct 2017 15:06:47 +0800 Subject: [PATCH] * Fix bugs. --- db/update9.5.1.sql | 2 + module/action/config.php | 2 + module/action/lang/zh-cn.php | 4 ++ module/entry/control.php | 2 +- module/entry/model.php | 16 +------- module/webhook/config.php | 4 +- module/webhook/control.php | 6 +-- module/webhook/lang/zh-cn.php | 5 ++- module/webhook/model.php | 63 +++++++++++++++++------------ module/webhook/view/create.html.php | 4 +- module/webhook/view/edit.html.php | 4 +- module/webhook/view/log.html.php | 6 +-- 12 files changed, 65 insertions(+), 53 deletions(-) diff --git a/db/update9.5.1.sql b/db/update9.5.1.sql index b5a1a27311..6274fc817b 100644 --- a/db/update9.5.1.sql +++ b/db/update9.5.1.sql @@ -10,6 +10,7 @@ CREATE TABLE IF NOT EXISTS `zt_entry` ( `createdDate` datetime NOT NULL, `editedBy` varchar(30) NOT NULL, `editedDate` datetime NOT NULL, + `deleted` enum('0', '1') NOT NULL DEFAULT '0', PRIMARY KEY `id` (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; @@ -30,6 +31,7 @@ CREATE TABLE IF NOT EXISTS `zt_webhook` ( `createdDate` datetime NOT NULL, `editedBy` varchar(30) NOT NULL, `editedDate` datetime NOT NULL, + `deleted` enum('0', '1') NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; diff --git a/module/action/config.php b/module/action/config.php index 99d917c4bb..06c1e081b9 100755 --- a/module/action/config.php +++ b/module/action/config.php @@ -19,5 +19,7 @@ $config->action->objectNameFields['module'] = 'name'; $config->action->objectNameFields['testsuite'] = 'name'; $config->action->objectNameFields['caselib'] = 'name'; $config->action->objectNameFields['testreport'] = 'title'; +$config->action->objectNameFields['entry'] = 'name'; +$config->action->objectNameFields['webhook'] = 'name'; $config->action->commonImgSize = 870; diff --git a/module/action/lang/zh-cn.php b/module/action/lang/zh-cn.php index 8e0bce22e6..5a4bd7f0b7 100755 --- a/module/action/lang/zh-cn.php +++ b/module/action/lang/zh-cn.php @@ -73,6 +73,8 @@ $lang->action->objectTypes['module'] = '模块'; $lang->action->objectTypes['testsuite'] = '套件'; $lang->action->objectTypes['caselib'] = '用例库'; $lang->action->objectTypes['testreport'] = '报告'; +$lang->action->objectTypes['entry'] = '应用'; +$lang->action->objectTypes['webhook'] = 'Webhook'; /* 用来描述操作历史记录。*/ $lang->action->desc = new stdclass(); @@ -197,6 +199,8 @@ $lang->action->label->doclib = '文档库|doc|browse|libID=%s'; $lang->action->label->doc = '文档|doc|view|docID=%s'; $lang->action->label->user = '用户|user|view|account=%s'; $lang->action->label->testreport = '报告|testreport|view|report=%s'; +$lang->action->label->entry = '应用|entry|browse|'; +$lang->action->label->webhook = 'Webhook|webhook|browse|'; $lang->action->label->space = ' '; /* Object type. */ diff --git a/module/entry/control.php b/module/entry/control.php index 2a4ba04628..06b94cf723 100644 --- a/module/entry/control.php +++ b/module/entry/control.php @@ -91,7 +91,7 @@ class entry extends control */ public function delete($entryID) { - $this->entry->delete($entryID); + $this->entry->delete(TABLE_ENTRY, $entryID); if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); $this->send(array('result' => 'success')); diff --git a/module/entry/model.php b/module/entry/model.php index 35693b499b..2fc73aeddc 100644 --- a/module/entry/model.php +++ b/module/entry/model.php @@ -33,7 +33,7 @@ class entryModel extends model */ public function getList($orderBy = 'id_desc', $pager = null) { - return $this->dao->select('*')->from(TABLE_ENTRY)->orderBy($orderBy)->page($pager)->fetchAll('id'); + return $this->dao->select('*')->from(TABLE_ENTRY)->where('deleted')->eq('0')->orderBy($orderBy)->page($pager)->fetchAll('id'); } /** @@ -93,18 +93,4 @@ class entryModel extends model return common::createChanges($oldEntry, $entry); } - - /** - * Delete an entry. - * - * @param int $entryID - * @param int $null - * @access public - * @return bool - */ - public function delete($entryID, $null = null) - { - $this->dao->delete()->from(TABLE_ENTRY)->where('id')->eq($entryID)->exec(); - return !dao::isError(); - } } diff --git a/module/webhook/config.php b/module/webhook/config.php index a237812206..f261ff89bb 100644 --- a/module/webhook/config.php +++ b/module/webhook/config.php @@ -13,12 +13,12 @@ $config->webhook->objectTypes['story'] = array('opened', 'edited', 'commen $config->webhook->objectTypes['productplan'] = array('opened', 'edited'); //$config->webhook->objectTypes['release'] = array('opened', 'edited', 'changestatus'); $config->webhook->objectTypes['project'] = array('opened', 'edited', 'started', 'delayed', 'suspended', 'closed', 'activated', 'undeleted'); -$config->webhook->objectTypes['task'] = array('opened', 'edited', 'commented', 'assigned', 'confirmed', 'started', 'finished', 'editestimate', 'deleteestimate', 'paused', 'canceled', 'restarted', 'closed', 'activated'); +$config->webhook->objectTypes['task'] = array('opened', 'edited', 'commented', 'assigned', 'confirmed', 'started', 'finished', 'editestimate', 'paused', 'canceled', 'restarted', 'closed', 'activated'); //$config->webhook->objectTypes['build'] = array('opened', 'edited'); $config->webhook->objectTypes['bug'] = array('opened', 'edited', 'commented', 'assigned', 'confirmed', 'bugconfirmed', 'resolved', 'closed', 'activated'); $config->webhook->objectTypes['case'] = array('opened', 'edited', 'commented', 'reviewed', 'confirmed'); $config->webhook->objectTypes['testtask'] = array('opened', 'edited', 'started', 'blocked', 'closed', 'activated'); -$config->webhook->objectTypes['todo'] = array('opened', 'edited', 'erased'); +$config->webhook->objectTypes['todo'] = array('opened', 'edited'); //$config->webhook->objectTypes['testsuite'] = array('opened', 'edited'); //$config->webhook->objectTypes['caselib'] = array('opened', 'edited'); //$config->webhook->objectTypes['testreport'] = array('opened', 'edited'); diff --git a/module/webhook/control.php b/module/webhook/control.php index f7970ddd0c..026df9fb09 100644 --- a/module/webhook/control.php +++ b/module/webhook/control.php @@ -97,7 +97,7 @@ class webhook extends control */ public function delete($id) { - $this->webhook->delete($id); + $this->webhook->delete(TABLE_WEBHOOK, $id); if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); $this->send(array('result' => 'success')); @@ -149,8 +149,8 @@ class webhook extends control if($webhook) { $contentType = zget($this->config->webhook->contentTypes, $webhook->contentType, 'application/json'); - $httpCode = $this->webhook->fetchHook($contentType, $webhook->url, $data->data); - $this->saveLog($data->webhook, $data->action, $webhook->url, $contentType, $data->data, $httpCode); + $result = $this->webhook->fetchHook($contentType, $webhook->url, $data->data); + $this->saveLog($data->webhook, $data->action, $webhook->url, $contentType, $data->data, $result); } if($httpCode == 200) $this->dao->update(TABLE_WEBHOOKDATA)->set('status')->eq('sended')->where('id')->eq($data->id)->exec(); diff --git a/module/webhook/lang/zh-cn.php b/module/webhook/lang/zh-cn.php index 2f1b5ee42b..1ebd344a02 100644 --- a/module/webhook/lang/zh-cn.php +++ b/module/webhook/lang/zh-cn.php @@ -44,4 +44,7 @@ $lang->webhook->confirmDelete = '您确认要删除该webhook吗?'; $lang->webhook->trimWords = '了'; $lang->webhook->note = new stdClass(); -$lang->webhook->note->async = '异步需要打开计划任务'; +$lang->webhook->note->async = '异步需要打开计划任务'; +$lang->webhook->note->dingding = '请在钉钉中添加一个自定义机器人,并将其webhook填写到此处。'; +$lang->webhook->note->product = '此项为空时所有产品的动作都会触发钩子,否则只有关联产品的动作才会触发。'; +$lang->webhook->note->project = '此项为空时所有项目的动作都会触发钩子,否则只有关联项目的动作才会触发。'; diff --git a/module/webhook/model.php b/module/webhook/model.php index bb0dbb994f..68ab0d86f5 100644 --- a/module/webhook/model.php +++ b/module/webhook/model.php @@ -37,7 +37,7 @@ class webhookModel extends model public function getList($type = '', $orderBy = 'id_desc', $pager = null) { $webhooks = $this->dao->select('*')->from(TABLE_WEBHOOK) - ->where(1) + ->where('deleted')->eq('0') ->beginIF($type)->andWhere('type')->eq($type)->fi() ->orderBy($orderBy) ->page($pager) @@ -63,7 +63,32 @@ class webhookModel extends model ->orderBy($orderBy) ->page($pager) ->fetchAll('id'); - foreach($logs as $log) $log->data = json_decode($log->data); + + $actions = array(); + foreach($logs as $log) $actions[] = $log->action; + + $this->loadModel('action'); + $actions = $this->dao->select('*')->from(TABLE_ACTION)->where('id')->in($actions)->fetchAll('id'); + + foreach($logs as $log) + { + if(!isset($actions[$log->action])) + { + $log->action = ''; + continue; + } + + $action = $actions[$log->action]; + + $text = $this->app->user->realname . $this->lang->action->label->{$action->action}. $this->lang->action->objectTypes[$action->objectType]; + $object = $this->dao->select('*')->from($this->config->objectTables[$action->objectType])->where('id')->eq($action->objectID)->fetch(); + $field = $this->config->action->objectNameFields[$action->objectType]; + $text .= "[#{$action->objectID}::{$object->$field}]"; + if($action->action == 'assigned') $text .= ' ' . $this->lang->webhook->assigned . ' ' . zget($users, $object->assignedTo); + + $log->action = $text; + $log->actionURL = $this->getViewLink($action->objectType, $action->objectID); + } return $logs; } @@ -167,21 +192,6 @@ class webhookModel extends model return !dao::isError(); } - /** - * Delete a webhook. - * - * @param int $id - * @param object $null - * @access public - * @return bool - */ - public function delete($id, $null = null) - { - $this->dao->delete()->from(TABLE_WEBHOOK)->where('id')->eq($id)->exec(); - $this->dao->delete()->from(TABLE_LOG)->where('objectType')->eq('webhook')->andWhere('objectID')->eq($id)->exec(); - return !dao::isError(); - } - /** * Send data. * @@ -201,7 +211,7 @@ class webhookModel extends model $snoopy = $this->app->loadClass('snoopy'); foreach($webhooks as $id => $webhook) { - if(!in_array($actionType, $webhook->actions->$objectType)) continue; + if(!isset($webhook->actions->$objectType) or !in_array($actionType, $webhook->actions->$objectType)) continue; $postData = $this->buildData($objectType, $objectID, $actionType, $actionID, $webhook); if(!$postData) continue; @@ -212,8 +222,8 @@ class webhookModel extends model } $contentType = zget($this->config->webhook->contentTypes, $webhook->contentType, 'application/json'); - $httpCode = $this->fetchHook($contentType, $webhook->url, $postData); - $this->saveLog($id, $actionID, $webhook->url, $contentType, $postData, $httpCode); + $result = $this->fetchHook($contentType, $webhook->url, $postData); + $this->saveLog($id, $actionID, $webhook->url, $contentType, $postData, $result); } return !dao::isError(); } @@ -275,7 +285,7 @@ class webhookModel extends model { if($param == 'text') { - $data->text = $this->app->user->realname . $this->lang->action->label->$actionType . $this->lang->action->objectTypes[$objectType] . ' ' . "[#{$objectID}::{$title}](" . $host . $viewLink . ")"; + $data->text = $this->app->user->realname . $this->lang->action->label->$actionType . $this->lang->action->objectTypes[$objectType] . ' ' . "[#{$objectID}::{$text}](" . $host . $viewLink . ")"; if($actionType == 'assigned') $data->text .= ' ' . $this->lang->webhook->assigned . ' ' . zget($users, $object->assignedTo); } else @@ -360,12 +370,13 @@ class webhookModel extends model curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); - curl_exec($ch); + $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); - if($error) return $error; + if($error) return $error; + if($result) return $result; return $httpCode; } @@ -399,11 +410,11 @@ class webhookModel extends model * @param string $url * @param string $contentType * @param string $data - * @param int $status + * @param string $result * @access public * @return bool */ - public function saveLog($webhookID, $actionID, $url, $contentType, $data, $status) + public function saveLog($webhookID, $actionID, $url, $contentType, $data, $result) { $log = new stdclass(); $log->objectType = 'webhook'; @@ -413,7 +424,7 @@ class webhookModel extends model $log->url = $url; $log->contentType = $contentType; $log->data = $data; - $log->status = $status; + $log->result = $result; $this->dao->insert(TABLE_LOG)->data($log)->exec(); return !dao::isError(); diff --git a/module/webhook/view/create.html.php b/module/webhook/view/create.html.php index 9a4c850467..ddd74bacf0 100644 --- a/module/webhook/view/create.html.php +++ b/module/webhook/view/create.html.php @@ -29,7 +29,7 @@ webhook->url;?> - + webhook->note->dingding;?> @@ -46,10 +46,12 @@ webhook->product;?> + webhook->note->product;?> webhook->project;?> + webhook->note->project;?> diff --git a/module/webhook/view/edit.html.php b/module/webhook/view/edit.html.php index bafc147912..8bc8f6b46b 100644 --- a/module/webhook/view/edit.html.php +++ b/module/webhook/view/edit.html.php @@ -29,7 +29,7 @@ webhook->url;?> url, "class='form-control'");?> - + type == 'dingding') echo $lang->webhook->note->dingding;?> type != 'dingding'):?> @@ -46,10 +46,12 @@ webhook->product;?> products, "class='form-control chosen' multiple");?> + webhook->note->product;?> webhook->project;?> projects, "class='form-control chosen' multiple");?> + webhook->note->project;?> type != 'dingding'):?> diff --git a/module/webhook/view/log.html.php b/module/webhook/view/log.html.php index 9335c419b3..813b6cbaa4 100644 --- a/module/webhook/view/log.html.php +++ b/module/webhook/view/log.html.php @@ -26,7 +26,7 @@ webhook->url);?> webhook->action);?> webhook->contentType);?> - webhook->status);?> + webhook->status);?> @@ -34,9 +34,9 @@ url;?> - data->text;?> + actionURL, $log->action);?> contentType;?> - status;?> + result;?>