control.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. /**
  3. * The control file of job of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yidong Wang <yidong@cnezsoft.com>
  8. * @package job
  9. * @version $Id$
  10. * @property jobZen $jobZen
  11. * @property jobModel $job
  12. * @link https://www.zentao.net
  13. */
  14. class job extends control
  15. {
  16. /**
  17. * Construct
  18. *
  19. * @param string $moduleName
  20. * @param string $methodName
  21. * @access public
  22. * @return void
  23. */
  24. public function __construct($moduleName = '', $methodName = '')
  25. {
  26. parent::__construct($moduleName, $methodName);
  27. if(in_array($this->app->methodName, array('create', 'edit', 'trigger')))
  28. {
  29. if($this->session->repoID) $this->loadModel('ci')->setMenu();
  30. }
  31. elseif($this->app->methodName != 'browse')
  32. {
  33. $this->loadModel('ci')->setMenu();
  34. }
  35. $this->projectID = isset($_GET['project']) ? $_GET['project'] : 0;
  36. }
  37. /**
  38. * 流水线列表。
  39. * Browse job.
  40. *
  41. * @param int $repoID
  42. * @param string $type
  43. * @param string $queryID
  44. * @param string $orderBy
  45. * @param int $recTotal
  46. * @param int $recPerPage
  47. * @param int $pageID
  48. * @access public
  49. * @return void
  50. */
  51. public function browse($repoID = 0, $type = '', $queryID = '', $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  52. {
  53. $this->loadModel('ci');
  54. $this->app->loadLang('compile');
  55. if($repoID)
  56. {
  57. $this->jobZen->checkRepoEmpty();
  58. $repoID = $this->loadModel('repo')->saveState($repoID);
  59. /* Set session. */
  60. $this->ci->setMenu($repoID);
  61. session_start();
  62. unset($this->config->job->search['fields']['repo']);
  63. unset($this->config->job->search['params']['repo']);
  64. }
  65. else
  66. {
  67. $this->session->set('repoID', '');
  68. }
  69. $this->app->loadClass('pager', true);
  70. $pager = new pager($recTotal, $recPerPage, $pageID);
  71. $queryID = $type == 'bySearch' ? $queryID : 0;
  72. $actionURL = $this->createLink('job', 'browse', "repoID={$repoID}&type=bySearch&queryID=myQueryID&orderBy={$orderBy}&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}");
  73. $this->jobZen->buildSearchForm($this->config->job->search, $queryID, $actionURL);
  74. $jobQuery = $type == 'bySearch' ? $this->jobZen->getJobSearchQuery((int)$queryID) : '';
  75. $jobList = $this->jobZen->getJobList($repoID, $jobQuery, $orderBy, $pager);
  76. $pipelines = $this->loadModel('pipeline')->getPairs('jenkins,gitlab');
  77. $this->view->title = $this->lang->ci->job . $this->lang->hyphen . $this->lang->job->browse;
  78. $this->view->repoID = $repoID;
  79. $this->view->jobList = $jobList;
  80. $this->view->orderBy = $orderBy;
  81. $this->view->type = $type;
  82. $this->view->queryID = $queryID;
  83. $this->view->pager = $pager;
  84. $this->view->hasJobServer = !empty($pipelines);
  85. $this->display();
  86. }
  87. /**
  88. * Create a job.
  89. *
  90. * @param int $repoID
  91. * @access public
  92. * @return void
  93. */
  94. public function create($repoID = 0)
  95. {
  96. if($_POST)
  97. {
  98. $job = form::data($this->config->job->form->create)
  99. ->setIF($this->post->frame != 'sonarqube', 'sonarqubeServer', 0)
  100. ->setIF($this->post->frame != 'sonarqube', 'projectKey', '')
  101. ->add('createdBy', $this->app->user->account)
  102. ->get();
  103. if($job->engine == 'gitlab' && $job->repo)
  104. {
  105. $repo = $this->loadModel('repo')->fetchByID($job->repo);
  106. $project = $this->loadModel('gitlab')->apiGetSingleProject((int)$repo->serviceHost, (int)$job->repo, false);
  107. $job->reference = zget($project, 'default_branch', 'master');
  108. }
  109. $jobID = $this->job->create($job);
  110. if(!dao::isError()) $this->loadModel('action')->create('job', $jobID, 'imported');
  111. return $this->send($this->jobZen->reponseAfterCreateEdit());
  112. }
  113. $this->loadModel('ci');
  114. $this->view->title = $this->lang->ci->job . $this->lang->hyphen . $this->lang->job->create;
  115. $this->view->repoList = $this->loadModel('repo')->getList($this->projectID);
  116. $this->view->repoID = $repoID;
  117. $this->view->repo = $repoID ? $this->repo->getByID($repoID) : null;
  118. $this->view->products = array(0 => '') + $this->loadModel('product')->getProductPairsByProject($this->projectID);
  119. $this->view->jenkinsServerList = $this->loadModel('pipeline')->getPairs('jenkins');
  120. $this->view->sonarqubeServerList = array('' => '') + $this->pipeline->getPairs('sonarqube');
  121. $this->display();
  122. }
  123. /**
  124. * Edit a job.
  125. *
  126. * @param int $jobID
  127. * @access public
  128. * @return void
  129. */
  130. public function edit($jobID)
  131. {
  132. $job = $this->job->getByID($jobID);
  133. if($_POST)
  134. {
  135. $newJob = form::data($this->config->job->form->edit)
  136. ->setIF(!$this->post->repo, 'repo', $job->repo)
  137. ->setIF($this->post->triggerType && !in_array('commit', $this->post->triggerType), 'comment', '')
  138. ->setIF($this->post->triggerType && !in_array('schedule', $this->post->triggerType), 'atDay', '')
  139. ->setIF($this->post->triggerType && !in_array('schedule', $this->post->triggerType), 'atTime', '')
  140. ->setIF($this->post->triggerType && !in_array('tag', $this->post->triggerType), 'lastTag', '')
  141. ->setIF($this->post->frame != 'sonarqube', 'sonarqubeServer', 0)
  142. ->setIF($this->post->frame != 'sonarqube', 'projectKey', '')
  143. ->add('editedBy', $this->app->user->account)
  144. ->get();
  145. $this->job->update($jobID, $newJob);
  146. if(!dao::isError()) $this->loadModel('action')->create('job', $jobID, 'edited');
  147. return $this->send($this->jobZen->reponseAfterCreateEdit($job->repo));
  148. }
  149. $repo = $this->loadModel('repo')->getByID($job->repo);
  150. if($repo->SCM == 'Gitlab') $this->view->refList = $this->loadModel('gitlab')->getReferenceOptions($repo->gitService, (int)$repo->serviceProject);
  151. if($repo->SCM != 'Gitlab') $this->view->refList = $this->repo->getBranches($repo, true);
  152. $this->jobZen->getSubversionDir($repo);
  153. $products = $this->repo->getProductsByRepo($job->repo);
  154. if(!isset($products[$job->product]))
  155. {
  156. $jobProduct = $this->loadModel('product')->getByID($job->product);
  157. if($jobProduct and $jobProduct->deleted == 0) $products += array($job->product => $jobProduct->name);
  158. }
  159. if($job->frame == 'sonarqube' && $job->sonarqubeServer && $job->projectKey)
  160. {
  161. $this->view->sonarqubeProjectPairs = $this->loadModel('sonarqube')->getProjectPairs($job->sonarqubeServer, $job->projectKey);
  162. }
  163. $this->view->title = $this->lang->job->pipeline . $this->lang->hyphen . $this->lang->job->edit;
  164. $this->view->repoList = $this->loadModel('repo')->getList($this->projectID);
  165. $this->view->job = $job;
  166. $this->view->repo = $repo;
  167. $this->view->products = $products;
  168. $this->view->jenkinsServerList = $this->loadModel('pipeline')->getPairs('jenkins');
  169. $this->view->sonarqubeServerList = array('' => '') + $this->pipeline->getPairs('sonarqube');
  170. $this->display();
  171. }
  172. /**
  173. * Delete a job.
  174. *
  175. * @param int $jobID
  176. * @access public
  177. * @return void
  178. */
  179. public function delete($jobID)
  180. {
  181. $this->job->delete(TABLE_JOB, $jobID);
  182. $response['load'] = true;
  183. $response['result'] = 'success';
  184. return $this->send($response);
  185. }
  186. /**
  187. * View job and compile.
  188. *
  189. * @param int $jobID
  190. * @param int $compileID
  191. * @access public
  192. * @return void
  193. */
  194. public function view($jobID, $compileID = 0)
  195. {
  196. $job = $this->job->getById($jobID);
  197. $this->loadModel('compile');
  198. if($compileID)
  199. {
  200. $compile = $this->compile->getById($compileID);
  201. }
  202. else
  203. {
  204. $compile = $this->compile->getLastResult($jobID);
  205. }
  206. if($compile && $compile->testtask) $this->jobZen->getCompileData($compile);
  207. $this->view->title = $this->lang->ci->job . $this->lang->hyphen . $this->lang->job->browse;
  208. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  209. $this->view->job = $job;
  210. $this->view->compile = $compile;
  211. $this->view->repo = $this->loadModel('repo')->getByID($job->repo);
  212. $this->view->jenkins = $this->loadModel('pipeline')->getById($job->server);
  213. $this->view->product = $this->loadModel('product')->getById($job->product);
  214. $this->display();
  215. }
  216. /**
  217. * 执行流水线。
  218. * Exec a job.
  219. *
  220. * @param int $jobID
  221. * @access public
  222. * @return void
  223. */
  224. public function exec($jobID)
  225. {
  226. $compile = $this->job->exec($jobID);
  227. if(dao::isError())
  228. {
  229. $errors = array();
  230. foreach(dao::getError() as $error)
  231. {
  232. if(is_array($error))
  233. {
  234. foreach($error as $val)
  235. {
  236. $errors[] = $val;
  237. }
  238. }
  239. else
  240. {
  241. $errors[] = $error;
  242. }
  243. }
  244. return $this->sendError(implode("\n", $errors));
  245. }
  246. $this->app->loadLang('compile');
  247. $this->loadModel('action')->create('job', $jobID, 'executed');
  248. $message = sprintf($this->lang->job->sendExec, zget($this->lang->compile->statusList, $compile->status));
  249. return $this->sendSuccess(array('message' => $message));
  250. }
  251. /**
  252. * ajax方式获取产品根据版本库。
  253. * AJAX: Get product by repo.
  254. *
  255. * @param int $repoID
  256. * @access public
  257. * @return string
  258. */
  259. public function ajaxGetProductByRepo($repoID)
  260. {
  261. $repo = $this->loadModel('repo')->getByID($repoID);
  262. if(empty($repo)) return print(json_encode(array(""=>"")));
  263. $product = $repo->product;
  264. if(strpos($product, ','))
  265. {
  266. /* Do not use `array_intersect()` here. */
  267. $productList = explode(',', $product);
  268. $matchedProducts = array();
  269. $productPair = $this->loadModel('product')->getPairs();
  270. foreach($productList as $productLeft)
  271. {
  272. foreach($productPair as $productRight => $productName)
  273. {
  274. if($productLeft == $productRight) $matchedProducts[$productName] = $productRight;
  275. }
  276. }
  277. return print(json_encode($matchedProducts));
  278. }
  279. $productName = $this->loadModel('product')->getByID($repo->product)->name;
  280. echo json_encode(array($productName => $repo->product));
  281. }
  282. /**
  283. * ajax方式获取版本库分支列表。
  284. * Ajax get reference list function.
  285. *
  286. * @param int $repoID
  287. * @access public
  288. * @return void
  289. */
  290. public function ajaxGetRefList($repoID)
  291. {
  292. $repo = $this->loadModel('repo')->getByID($repoID);
  293. if($repo->SCM == 'Gitlab') $refList = $this->loadModel('gitlab')->getReferenceOptions($repo->gitService, (int)$repo->serviceProject);
  294. if($repo->SCM != 'Gitlab') $refList = $this->repo->getBranches($repo, true);
  295. $options = array();
  296. foreach($refList as $branch => $branchName)
  297. {
  298. $options[] = array('text' => $branchName, 'value' => $branch);
  299. }
  300. $this->send(array('result' => 'success', 'refList' => $options));
  301. }
  302. /**
  303. * ajax方式获取版本库列表根据引擎。
  304. * Ajax get repo list.
  305. *
  306. * @param int $engine
  307. * @access public
  308. * @return void
  309. */
  310. public function ajaxGetRepoList($engine)
  311. {
  312. $repoList = $this->loadModel('repo')->getList($this->projectID);
  313. $repoPairs = array(0 => '');
  314. foreach($repoList as $repo)
  315. {
  316. if(empty($repo->synced)) continue;
  317. if($engine == 'gitlab')
  318. {
  319. if(strtolower($repo->SCM) == 'gitlab') $repoPairs[$repo->id] = $repo->name;
  320. }
  321. else
  322. {
  323. $repoPairs[$repo->id] = "[{$repo->SCM}] {$repo->name}";
  324. }
  325. }
  326. echo html::select('repo', $repoPairs, '', "class='form-control chosen'");
  327. }
  328. /**
  329. * ajax方式获取版本库类型。
  330. * Ajax get an repo type.
  331. *
  332. * @param int $repoID
  333. * @access public
  334. * @return void
  335. */
  336. public function ajaxGetRepoType($repoID)
  337. {
  338. $repo = $this->loadModel('repo')->getByID($repoID);
  339. $this->send(array('result' => 'success', 'type' => strtolower($repo->SCM), 'triggerByTag' => true));
  340. }
  341. /**
  342. * ajax检查该版本库是否已关联sonarqube。
  343. * Ajax check SonarQube linked by repoID.
  344. *
  345. * @param int $repoID
  346. * @access public
  347. * @return void
  348. * @param int $jobID
  349. */
  350. public function ajaxCheckSonarqubeLink($repoID, $jobID = 0)
  351. {
  352. $repo = $this->loadModel('job')->getSonarqubeByRepo(array($repoID), $jobID, true);
  353. foreach($repo as $linkRepo)
  354. {
  355. if($linkRepo->id == $jobID) $this->send(array('result' => 'success', 'message' => ''));
  356. }
  357. if(!empty($repo))
  358. {
  359. $message = $repo[$repoID]->deleted ? $this->lang->job->jobIsDeleted : sprintf($this->lang->job->repoExists, $repo[$repoID]->id . '-' . $repo[$repoID]->name);
  360. $this->send(array('result' => 'fail', 'message' => $message));
  361. }
  362. $this->send(array('result' => 'success', 'message' => ''));
  363. }
  364. /**
  365. * Ajax方式获取项目流水线信息。
  366. * Get pipelines by ajax.
  367. *
  368. * @param int $repoID
  369. * @param bool $isAjax
  370. * @access public
  371. * @return void
  372. */
  373. public function ajaxGetPipelines($repoID, $isAjax = true)
  374. {
  375. $repo = $this->loadModel('repo')->getByID($repoID);
  376. if(!$repo) return print(array());
  377. $scm = $this->app->loadClass('scm');
  378. $scm->setEngine($repo);
  379. $pipelines = $scm->pipelines();
  380. $options = array();
  381. foreach($pipelines as $pipeline) $options[] = array('text' => $pipeline->uid, 'value' => $pipeline->uid);
  382. if($isAjax) return $this->send(array('result' => 'success', 'data' => $options));
  383. return $options;
  384. }
  385. /**
  386. * AJAX: Import the pipeline from Pipeline Server into ZenTaoPMS
  387. *
  388. * @param string|int $repoID
  389. * @return void
  390. */
  391. public function ajaxImportJobs($repoID)
  392. {
  393. if($this->job->import($repoID)) $this->sendSuccess();
  394. $this->sendError(dao::isError() ? dao::getError() : 'fail');
  395. }
  396. /**
  397. * 流水线触发器。
  398. * Pipeline trigger.
  399. *
  400. * @param int $jobID
  401. * @access public
  402. * @return void
  403. */
  404. public function trigger($jobID)
  405. {
  406. $this->lang->job->edit = $this->lang->job->trigger;
  407. $this->edit($jobID);
  408. }
  409. }