control.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * The control file of ci module 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 Chenqi <chenqi@cnezsoft.com>
  8. * @package product
  9. * @link https://www.zentao.net
  10. * @property ciModel $ci
  11. * @property compileModel $compile
  12. */
  13. class ci extends control
  14. {
  15. /**
  16. * ci constructor.
  17. * @param string $moduleName
  18. * @param string $methodName
  19. */
  20. public function __construct($moduleName = '', $methodName = '')
  21. {
  22. parent::__construct($moduleName, $methodName);
  23. $this->ci->setMenu();
  24. }
  25. /**
  26. * 初始化构建队列。
  27. * Init compile queue.
  28. *
  29. * @access public
  30. * @return void
  31. */
  32. public function initQueue()
  33. {
  34. $scheduleJobs = $this->loadModel('job')->getListByTriggerType('schedule');
  35. $week = date('w');
  36. $this->loadModel('compile');
  37. foreach($scheduleJobs as $job)
  38. {
  39. if(strpos($job->atDay, $week) !== false) $this->compile->createByJob($job->id, $job->atTime, 'atTime');
  40. }
  41. echo 'success';
  42. }
  43. /**
  44. * 执行构建。
  45. * Exec compile.
  46. *
  47. * @access public
  48. * @return void
  49. */
  50. public function exec()
  51. {
  52. $compiles = $this->loadModel('compile')->getUnexecutedList();
  53. foreach($compiles as $compile)
  54. {
  55. if($compile->atTime && date('H:i') < $compile->atTime) continue;
  56. $this->compile->exec($compile);
  57. }
  58. echo 'success';
  59. }
  60. /**
  61. * 向jenkins或gitlab发送请求以检查构建状态。
  62. * Send a request to jenkins or gitlab to check build status.
  63. *
  64. * @param int $compileID
  65. * @access public
  66. * @return void
  67. */
  68. public function checkCompileStatus($compileID = 0)
  69. {
  70. $this->ci->checkCompileStatus($compileID);
  71. if(dao::isError()) return $this->sendError(dao::getError());
  72. return $this->send(array('result' => 'success', 'load' => $this->createLink('compile', 'logs', "compileID={$compileID}")));
  73. }
  74. /**
  75. * 检查ztf的提交结果。
  76. * Commit result from ztf.
  77. *
  78. * @access public
  79. * @return void
  80. */
  81. public function commitResult()
  82. {
  83. /* Get post data. */
  84. $post = file_get_contents('php://input');
  85. $post = json_decode($post);
  86. $testType = $post->testType;
  87. $productID = zget($post, 'productId', 0);
  88. $taskID = zget($post, 'taskId', 0);
  89. $zentaoData = zget($post, 'zentaoData', '');
  90. /* Get compileID and jobID. */
  91. parse_str($zentaoData, $params);
  92. $compileID = zget($params, 'compile', 0);
  93. $jobID = 0;
  94. list($productID, $jobID) = $this->ciZen->getProductIdAndJobID($params, $post);
  95. if(empty($productID)) return print(json_encode(array('result' => 'fail', 'message' => 'productID is not found')));
  96. $linkProjects = $this->loadModel('product')->getProjectPairsByProduct($productID);
  97. if(empty($linkProjects)) return print(json_encode(array('result' => 'fail', 'message' => sprintf($this->lang->ci->errors->noProject, $productID))));
  98. $linkExecutions = $this->product->getExecutionPairsByProduct($productID);
  99. if(empty($linkExecutions)) return print(json_encode(array('result' => 'fail', 'message' => sprintf($this->lang->ci->errors->noExecution, $productID))));
  100. /* Get testtaskID or create testtask. */
  101. if(!$taskID) $taskID = $this->ci->saveTestTaskForZtf($testType, $productID, $compileID, $taskID, $post->name);
  102. $this->ciZen->parseZtfResult($post, $taskID, $productID, $jobID, $compileID);
  103. echo json_encode(array('result' => 'success'));
  104. }
  105. }