diff --git a/module/common/model.php b/module/common/model.php index 41dac7f075..1da0b66be8 100644 --- a/module/common/model.php +++ b/module/common/model.php @@ -1360,7 +1360,7 @@ class commonModel extends model * @access public * @return bool */ - public static function printLink($module, $method, $vars = '', $label, $target = '', $misc = '', $newline = true, $onlyBody = false, $object = null) + public static function printLink($module, $method, $vars = '', $label = '', $target = '', $misc = '', $newline = true, $onlyBody = false, $object = null) { /* Add data-app attribute. */ global $app; diff --git a/module/company/model.php b/module/company/model.php index a287493aa8..7cf87d3d11 100644 --- a/module/company/model.php +++ b/module/company/model.php @@ -15,7 +15,7 @@ class companyModel extends model { /** * Set menu. - * + * * @param int $dept * @access public * @return void @@ -31,7 +31,7 @@ class companyModel extends model /** * Get company list. - * + * * @access public * @return void */ @@ -42,7 +42,7 @@ class companyModel extends model /** * Get the first company. - * + * * @access public * @return void */ @@ -53,8 +53,8 @@ class companyModel extends model /** * Get company info by id. - * - * @param int $companyID + * + * @param int $companyID * @access public * @return object */ @@ -74,7 +74,7 @@ class companyModel extends model * @access public * @return array */ - public function getUsers($browseType = 'inside', $type, $queryID, $deptID, $sort, $pager) + public function getUsers($browseType = 'inside', $type = '', $queryID = 0, $deptID = 0, $sort = '', $pager = null) { /* Get users. */ if($type == 'bydept') @@ -131,7 +131,7 @@ class companyModel extends model /** * Update a company. - * + * * @access public * @return void */ diff --git a/module/mr/control.php b/module/mr/control.php index 8bb34c0d73..198e03cd04 100644 --- a/module/mr/control.php +++ b/module/mr/control.php @@ -950,7 +950,7 @@ class mr extends control $targetProject = $this->post->targetProject; $targetBranch = $this->post->targetBranch; - $result = $this->mr->hasSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); + $result = $this->mr->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); die(json_encode($result)); } } diff --git a/module/mr/model.php b/module/mr/model.php index 029f69e3ce..ef93ad079c 100644 --- a/module/mr/model.php +++ b/module/mr/model.php @@ -93,7 +93,7 @@ class mrModel extends model ->add('createdDate', helper::now()) ->get(); - $result = $this->hasSameOpened($MR->gitlabID, $MR->sourceProject, $MR->sourceBranch, $MR->targetProject, $MR->targetBranch); + $result = $this->checkSameOpened($MR->gitlabID, $MR->sourceProject, $MR->sourceBranch, $MR->targetProject, $MR->targetBranch); if($result['result'] == 'fail') return $result; /* Exec Job */ @@ -206,7 +206,7 @@ class mrModel extends model $MR->createdBy = $this->app->user->account; $MR->createdDate = date('Y-m-d H:i:s'); - $result = $this->hasSameOpened($MR->gitlabID, $MR->sourceProject, $MR->sourceBranch, $MR->targetProject, $MR->targetBranch); + $result = $this->checkSameOpened($MR->gitlabID, $MR->sourceProject, $MR->sourceBranch, $MR->targetProject, $MR->targetBranch); if($result['result'] == 'fail') { dao::$errors[] = $result['message']; @@ -547,13 +547,16 @@ class mrModel extends model */ public function apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch) { - $url = sprintf($this->loadModel('gitlab')->getApiRoot($gitlabID), "/merge_requests") . "&state=opened&source_branch={$sourceBranch}&target_branch={$targetBranch}&source_project_id={$sourceProject}&target_project_id={$targetProject}"; + if(empty($gitlabID) or empty($sourceProject) or empty($sourceBranch) or empty($targetProject) or empty($targetBranch)) return null; + + $url = sprintf($this->loadModel('gitlab')->getApiRoot((int)$gitlabID), "/merge_requests") . "&state=opened&source_branch={$sourceBranch}&target_branch={$targetBranch}&source_project_id={$sourceProject}&target_project_id={$targetProject}"; $response = json_decode(commonModel::http($url)); if($response) { foreach($response as $mr) { + if(empty($mr->source_project_id) or empty($mr->target_project_id)) return null; if($mr->source_project_id == $sourceProject and $mr->target_project_id == $targetProject) return $mr; } } @@ -1447,7 +1450,7 @@ class mrModel extends model } /** - * Has same opened mr for source branch. + * Check same opened mr for source branch. * * @param int $gitlabID * @param int $sourceProject @@ -1457,7 +1460,7 @@ class mrModel extends model * @access public * @return array */ - public function hasSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch) + public function checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch) { if(empty($sourceProject) or empty($sourceBranch) or empty($targetProject) or empty($targetBranch)) return array('result' => 'success'); @@ -1485,20 +1488,22 @@ class mrModel extends model */ public function convertApiError($message) { + if(is_array($message)) $message = $message[0]; + if(!is_string($message)) return $message; + foreach($this->lang->mr->apiErrorMap as $key => $errorMsg) { if(strpos($errorMsg, '/') === 0) { - $result = preg_match($errorMsg, $message[0], $matches); + $result = preg_match($errorMsg, $message, $matches); if($result) $errorMessage = sprintf(zget($this->lang->mr->errorLang, $key), $matches[1]); - break; } - else + elseif($message == $errorMsg) { - if($message[0] == $errorMsg) $errorMessage = zget($this->lang->mr->errorLang, $key, $message[0]); - break; + $errorMessage = zget($this->lang->mr->errorLang, $key, $message); } + if(isset($errorMessage)) break; } - return isset($errorMessage) ? $errorMessage : $message[0]; + return isset($errorMessage) ? $errorMessage : $message; } } diff --git a/test/model/mr/apigetsameopened.php b/test/model/mr/apigetsameopened.php new file mode 100644 index 0000000000..1d698a3e8a --- /dev/null +++ b/test/model/mr/apigetsameopened.php @@ -0,0 +1,123 @@ +#!/usr/bin/env php +> success +使用空的gitlabID >> null +使用空的sourceProject >> null +使用空的sourceBranch >> null +使用空的targetProject >> null +使用空的targetBranch >> null +使用错误的gitlabID >> null +使用错误的sourceProject >> null +使用错误的sourceBranch >> null +使用错误的targetProject >> null +使用错误的targetBranch >> null + +*/ +$mrModel = $tester->loadModel('mr'); + +$gitlabID = '2'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if(isset($result->iid)) $result = 'success'; +r($result) && p() && e('success'); //使用正确的gitlabID, sourceProject,sourceBranch,targetProject,targetBranch + +$gitlabID = ''; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用空的gitlabID + +$gitlabID = '1'; +$sourceProject = ''; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用空的sourceProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = ''; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用空的sourceBranch + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = ''; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用空的targetProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = ''; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用空的targetBranch + +$gitlabID = 'test'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用错误的gitlabID + +$gitlabID = '1'; +$sourceProject = 'test'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用错误的sourceProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用错误的sourceBranch + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = 'test'; +$targetBranch = 'master'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用错误的targetProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'main'; +$result = $mrModel->apiGetSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +if($result === null) $result = 'null'; +r($result) && p() && e('null'); //使用错误的targetBranch diff --git a/test/model/mr/checksameopened.php b/test/model/mr/checksameopened.php new file mode 100644 index 0000000000..4b8079ed39 --- /dev/null +++ b/test/model/mr/checksameopened.php @@ -0,0 +1,112 @@ +#!/usr/bin/env php +> fail +使用空的gitlabID >> success +使用空的sourceProject >> success +使用空的sourceBranch >> success +使用空的targetProject >> success +使用空的targetBranch >> success +使用错误的gitlabID >> success +使用错误的sourceProject >> success +使用错误的sourceBranch >> success +使用错误的targetProject >> success +使用错误的targetBranch >> success + +*/ +$mrModel = $tester->loadModel('mr'); + +$gitlabID = '2'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('fail'); //使用正确的gitlabID, sourceProject,sourceBranch,targetProject,targetBranch + +$gitlabID = ''; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用空的gitlabID + +$gitlabID = '1'; +$sourceProject = ''; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用空的sourceProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = ''; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用空的sourceBranch + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = ''; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用空的targetProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = ''; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用空的targetBranch + +$gitlabID = 'test'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用错误的gitlabID + +$gitlabID = '1'; +$sourceProject = 'test'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用错误的sourceProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch08'; +$targetProject = '42'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用错误的sourceBranch + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = 'test'; +$targetBranch = 'master'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用错误的targetProject + +$gitlabID = '1'; +$sourceProject = '42'; +$sourceBranch = 'branch-08'; +$targetProject = '42'; +$targetBranch = 'main'; +$result = $mrModel->checkSameOpened($gitlabID, $sourceProject, $sourceBranch, $targetProject, $targetBranch); +r($result) && p('result') && e('success'); //使用错误的targetBranch diff --git a/test/model/mr/convertapierror.php b/test/model/mr/convertapierror.php new file mode 100644 index 0000000000..d2034d01b5 --- /dev/null +++ b/test/model/mr/convertapierror.php @@ -0,0 +1,26 @@ +#!/usr/bin/env php +> success +使用空的message >> success + +*/ +$mrModel = $tester->loadModel('mr'); + +$message = array(); +$message[0] = 'Another open merge request already exists for this source branch: !11'; +$result = $mrModel->convertApiError($message); +if($result != $message[0]) $result = 'success'; +r($result) && p('') && e('success'); //使用正确的message + +$message = ''; +$result = $mrModel->convertApiError($message); +if(empty($result)) $result = 'success'; +r($result) && p('') && e('success'); //使用空的message