zen.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * The zen file of job module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Zenggang <zenggang@easycorp.ltd>
  8. * @package job
  9. * @link https://www.zentao.net
  10. * @property jobModel $job
  11. */
  12. class jobZen extends job
  13. {
  14. /**
  15. * 检测版本库数量。
  16. * Check repo empty.
  17. *
  18. * @access protected
  19. * @return void
  20. */
  21. protected function checkRepoEmpty()
  22. {
  23. $repos = $this->loadModel('repo')->getRepoPairs('devops');
  24. if(empty($repos)) $this->locate($this->repo->createLink('create'));
  25. }
  26. /**
  27. * 获取流水线列表。
  28. * Get job list.
  29. *
  30. * @param int $repoID
  31. * @param string $jobQuery
  32. * @param string $orderBy
  33. * @param object $pager
  34. * @access protected
  35. * @return array
  36. */
  37. protected function getJobList($repoID, $jobQuery, $orderBy, $pager)
  38. {
  39. $this->loadModel('gitlab');
  40. $products = $this->loadModel('product')->getPairs('nodeleted', 0, '', 'all');
  41. $jobList = $this->job->getList($repoID, $jobQuery, $orderBy, $pager);
  42. foreach($jobList as $job)
  43. {
  44. if($job->engine == 'jenkins')
  45. {
  46. if(strpos($job->pipeline, '/job/') === 0) $job->pipeline = trim(substr($job->pipeline, 5), '/');
  47. }
  48. else
  49. {
  50. $job->branch = empty($job->pipeline) ? '' : zget(json_decode($job->pipeline), 'reference', '');
  51. $job->pipeline = $job->repoName;
  52. }
  53. $job->lastExec = $job->lastExec ? $job->lastExec : '';
  54. $job->triggerType = $this->job->getTriggerConfig($job);
  55. $job->buildSpec = !empty($job->pipeline) ? urldecode($job->pipeline) . '@' . $job->jenkinsName : $job->jenkinsName;
  56. $job->engine = zget($this->lang->job->engineList, $job->engine);
  57. $job->frame = zget($this->lang->job->frameList, $job->frame);
  58. $job->productName = zget($products, $job->product, '');
  59. }
  60. return $jobList;
  61. }
  62. /**
  63. * 返回创建或者编辑的响应。
  64. * Return reponse after create or edit.
  65. *
  66. * @param int $repoID
  67. * @access protected
  68. * @return array
  69. */
  70. protected function reponseAfterCreateEdit($repoID = 0)
  71. {
  72. if(dao::isError())
  73. {
  74. $errors = dao::getError();
  75. if($this->post->engine == 'gitlab' and isset($errors['server']))
  76. {
  77. if(!isset($errors['repo'])) $errors['repo'][] = sprintf($this->lang->error->notempty, $this->lang->job->repoServer);
  78. unset($errors['server']);
  79. unset($errors['pipeline']);
  80. }
  81. elseif($this->post->engine == 'jenkins')
  82. {
  83. if(isset($errors['server']))
  84. {
  85. $errors['jkServer'] = $errors['server'];
  86. unset($errors['server']);
  87. }
  88. if(isset($errors['pipeline']))
  89. {
  90. $errors['jkTask'] = $errors['pipeline'];
  91. unset($errors['pipeline']);
  92. }
  93. }
  94. return array('result' => 'fail', 'message' => $errors);
  95. }
  96. return array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => inlink('browse', 'repoID=' . ($repoID ? $repoID : $this->post->repo)));
  97. }
  98. /**
  99. * 获取svn目录。
  100. * Get subversion dir.
  101. *
  102. * @param object $repo
  103. * @access protected
  104. * @return void
  105. */
  106. protected function getSubversionDir($repo)
  107. {
  108. if($repo->SCM == 'Subversion')
  109. {
  110. $dirs = array();
  111. $path = empty($repo->prefix) ? '/' : $this->loadModel('repo')->decodePath('');
  112. $tags = $this->loadModel('svn')->getRepoTags($repo, $path);
  113. if($tags)
  114. {
  115. $dirs['/'] = $path ? $path : '/';
  116. foreach($tags as $dirPath => $dirName) $dirs[$dirPath] = $dirPath;
  117. }
  118. $this->view->dirs = $dirs;
  119. foreach($this->lang->job->triggerTypeList as $type => $name)
  120. {
  121. if($type == 'tag') $this->lang->job->triggerTypeList[$type] = $this->lang->job->dirChange;
  122. }
  123. }
  124. }
  125. /**
  126. * 获取流水线执行数据。
  127. * Get job compile data.
  128. *
  129. * @param object $compile
  130. * @access protected
  131. * @return void
  132. */
  133. protected function getCompileData($compile)
  134. {
  135. $this->app->loadLang('project');
  136. $taskID = $compile->testtask;
  137. $task = $this->loadModel('testtask')->getById($taskID);
  138. $runs = $this->testtask->getRunsForUnitCases($taskID, 'id');
  139. $cases = array();
  140. $runs = $this->loadModel('testcase')->appendData($runs, 'testrun');
  141. foreach($runs as $run) $cases[$run->case] = $run;
  142. $results = $this->dao->select('*')->from(TABLE_TESTRESULT)->where('`case`')->in(array_keys($cases))->andWhere('run')->in(array_keys($runs))->fetchAll('run');
  143. foreach($results as $result)
  144. {
  145. $runs[$result->run]->caseResult = $result->caseResult;
  146. $runs[$result->run]->xml = $result->xml;
  147. $runs[$result->run]->duration = $result->duration;
  148. }
  149. $groupCases = $this->dao->select('*')->from(TABLE_SUITECASE)->where('`case`')->in(array_keys($cases))->orderBy('case')->fetchGroup('suite', 'case');
  150. $summary = array();
  151. if(empty($groupCases)) $groupCases[] = $cases;
  152. foreach($groupCases as $suiteID => $groupCase)
  153. {
  154. $caseCount = 0;
  155. $failCount = 0;
  156. $duration = 0;
  157. foreach($groupCase as $caseID => $suitecase)
  158. {
  159. $case = $cases[$caseID];
  160. $groupCases[$suiteID][$caseID] = $case;
  161. $duration += $case->duration;
  162. $caseCount ++;
  163. if($case->caseResult == 'fail') $failCount ++;
  164. }
  165. $summary[$suiteID] = sprintf($this->lang->testtask->summary, $caseCount, $failCount, $duration);
  166. }
  167. $suites = $this->loadModel('testsuite')->getUnitSuites($task->product);
  168. $this->view->groupCases = $groupCases;
  169. $this->view->suites = $suites;
  170. $this->view->summary = $summary;
  171. $this->view->taskID = $taskID;
  172. }
  173. /**
  174. * 构建搜索表单。
  175. * Build search form.
  176. *
  177. * @param array $searchConfig
  178. * @param string|int $queryID
  179. * @param string $actionURL
  180. * @access protected
  181. * @return void
  182. */
  183. protected function buildSearchForm($searchConfig, $queryID, $actionURL)
  184. {
  185. $searchConfig['queryID'] = (int)$queryID;
  186. $searchConfig['actionURL'] = $actionURL;
  187. if(isset($searchConfig['params']['repo'])) $searchConfig['params']['repo']['values'] = $this->loadModel('repo')->getRepoPairs('');
  188. $searchConfig['params']['product']['values'] = $this->loadModel('product')->getPairs('nodeleted', 0, '', 'all');
  189. $this->loadModel('search')->setSearchParams($searchConfig);
  190. }
  191. /**
  192. * 获取搜索条件。
  193. * Get search condition.
  194. *
  195. * @param int $queryID
  196. * @access public
  197. * @return string
  198. */
  199. public function getJobSearchQuery($queryID)
  200. {
  201. $queryName = 'jobQuery';
  202. if($queryID)
  203. {
  204. $query = $this->loadModel('search')->getQuery($queryID);
  205. if($query)
  206. {
  207. $this->session->set($queryName, $query->sql);
  208. $this->session->set('jobForm', $query->form);
  209. }
  210. }
  211. if($this->session->$queryName === false) $this->session->set($queryName, ' 1 = 1');
  212. $jobQuery = $this->session->$queryName;
  213. $jobQuery = preg_replace('/`(\w+)`/', 't1.`$1`', $jobQuery);
  214. return $jobQuery;
  215. }
  216. }