tao.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * The tao 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. */
  11. class jobTao extends jobModel
  12. {
  13. /**
  14. * 获取流水线实例的服务器和流水线信息。
  15. * Get job server and pipeline.
  16. *
  17. * @param object $job
  18. * @param object $repo
  19. * @access protected
  20. * @return object
  21. */
  22. protected function getServerAndPipeline($job, $repo)
  23. {
  24. $project = zget($repo, 'serviceProject', '');
  25. $host = (int)zget($repo, 'serviceHost', 0);
  26. if($job->engine == 'jenkins')
  27. {
  28. $job->server = (int)zget($job, 'jkServer', 0);
  29. $job->pipeline = zget($job, 'jkTask', '');
  30. }
  31. elseif($job->engine == 'gitlab')
  32. {
  33. if($job->repo && !empty($repo))
  34. {
  35. $pipeline = $this->loadModel('gitlab')->apiGetPipeline($host, (int)$project, '');
  36. if(!is_array($pipeline) or empty($pipeline))
  37. {
  38. dao::$errors['repo'][] = $this->lang->job->engineTips->error;
  39. return $job;
  40. }
  41. }
  42. $job->server = $host;
  43. $job->pipeline = json_encode(array('project' => $project, 'reference' => zget($job, 'reference', 'main')));
  44. }
  45. unset($job->reference);
  46. unset($job->jkServer);
  47. unset($job->jkTask);
  48. unset($job->gitlabRepo);
  49. return $job;
  50. }
  51. /**
  52. * 检查框架数据。
  53. * Check iframe data.
  54. *
  55. * @param object $job
  56. * @access protected
  57. * @return bool
  58. * @param int $jobID
  59. */
  60. protected function checkIframe($job, $jobID = 0)
  61. {
  62. /* SonarQube tool is only used if the engine is JenKins. */
  63. if($job->engine != 'jenkins' and $job->frame == 'sonarqube')
  64. {
  65. dao::$errors['frame'][] = $this->lang->job->mustUseJenkins;
  66. return false;
  67. }
  68. if($job->repo > 0 and $job->frame == 'sonarqube')
  69. {
  70. $sonarqubeJob = $this->getSonarqubeByRepo(array($job->repo), $jobID);
  71. if(!empty($sonarqubeJob))
  72. {
  73. $message = sprintf($this->lang->job->repoExists, $sonarqubeJob[$job->repo]->id . '-' . $sonarqubeJob[$job->repo]->name);
  74. dao::$errors['repo'][] = $message;
  75. return false;
  76. }
  77. }
  78. if(!empty($job->projectKey) and $job->frame == 'sonarqube')
  79. {
  80. $projectList = $this->getJobBySonarqubeProject($job->sonarqubeServer, array($job->projectKey));
  81. if(!empty($projectList) && $projectList[$job->projectKey] != $jobID)
  82. {
  83. $message = sprintf($this->lang->job->projectExists, $projectList[$job->projectKey]);
  84. dao::$errors['projectKey'][] = $message;
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * 获取svn目录信息。
  92. * Get svn dir.
  93. *
  94. * @param object $job
  95. * @param object $repo
  96. * @access protected
  97. * @return void
  98. */
  99. protected function getSvnDir(&$job, $repo)
  100. {
  101. $job->svnDir = '';
  102. if(strpos($job->triggerType, 'tag') !== false && $repo->SCM == 'Subversion')
  103. {
  104. $job->svnDir = array_pop($_POST['svnDir']);
  105. if($job->svnDir == '/' and $_POST['svnDir']) $job->svnDir = array_pop($_POST['svnDir']);
  106. }
  107. }
  108. /**
  109. * 获取流水线对象自定义参数。
  110. * Get job custom param.
  111. *
  112. * @param object $job
  113. * @access protected
  114. * @return bool
  115. */
  116. protected function getCustomParam(&$job)
  117. {
  118. $customParam = array();
  119. foreach($job->paramName as $key => $paramName)
  120. {
  121. $paramValue = zget($job->paramValue, $key, '');
  122. if(empty($paramName) and !empty($paramValue))
  123. {
  124. dao::$errors['paramName'][] = $this->lang->job->inputName;
  125. return false;
  126. }
  127. if(!empty($paramName) and !validater::checkREG($paramName, '/^\w+$/'))
  128. {
  129. dao::$errors['paramName'][] = $this->lang->job->invalidName;
  130. return false;
  131. }
  132. if(!empty($paramName)) $customParam[$paramName] = $paramValue;
  133. }
  134. unset($job->paramName);
  135. unset($job->paramValue);
  136. unset($job->custom);
  137. $job->customParam = json_encode($customParam);
  138. return true;
  139. }
  140. }