model.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * The model file of jenkins 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 Yanyi Cao <caoyanyi@easycorp.com>
  8. * @package jenkins
  9. * @link https://www.zentao.net
  10. */
  11. class jenkinsModel extends model
  12. {
  13. /**
  14. * 获取流水线列表。
  15. * Get jenkins tasks.
  16. *
  17. * @param int $jenkinsID
  18. * @param int $depth
  19. * @access public
  20. * @return array
  21. */
  22. public function getTasks($jenkinsID, $depth = 0)
  23. {
  24. $jenkins = $this->loadModel('pipeline')->getByID($jenkinsID);
  25. if(!$jenkins) return array();
  26. $jenkinsServer = $jenkins->url;
  27. $jenkinsUser = $jenkins->account;
  28. $jenkinsPassword = $jenkins->token ? $jenkins->token : $jenkins->password;
  29. $userPWD = "$jenkinsUser:$jenkinsPassword";
  30. $response = common::http($jenkinsServer . '/api/json/items/list' . ($depth ? "?depth=1" : ''), '', array(CURLOPT_USERPWD => $userPWD));
  31. $response = json_decode($response);
  32. $tasks = array();
  33. if($depth)
  34. {
  35. /* Support up to 4 levels. */
  36. if(isset($response->jobs)) $tasks = $this->getDepthJobs($response->jobs, $userPWD, 1);
  37. }
  38. else
  39. {
  40. if(isset($response->jobs))
  41. {
  42. foreach($response->jobs as $job) $tasks[basename($job->url)] = $job->name;
  43. }
  44. }
  45. return $tasks;
  46. }
  47. /**
  48. * 根据深度获取流水线。
  49. * Get jobs by depth.
  50. *
  51. * @param mixed[] $jobs
  52. * @param string $userPWD
  53. * @param int $depth
  54. * @access protected
  55. * @return array
  56. */
  57. public function getDepthJobs($jobs, $userPWD, $depth = 1)
  58. {
  59. if($depth > 4) return array();
  60. $tasks = array();
  61. foreach($jobs as $job)
  62. {
  63. if(empty($job->url)) continue;
  64. $isJob = true;
  65. if(stripos($job->_class, '.multibranch') !== false || stripos($job->_class, '.folder') !== false || stripos($job->_class, '.OrganizationFolder') !== false) $isJob = false;
  66. if(!empty($job->buildable) && $job->buildable == true) $isJob = true;
  67. if($isJob)
  68. {
  69. $parms = parse_url($job->url);
  70. $tasks[$parms['path']] = $job->name;
  71. }
  72. else
  73. {
  74. if($depth > 1)
  75. {
  76. $response = common::http($job->url . 'api/json', '', array(CURLOPT_USERPWD => $userPWD));
  77. $job = json_decode($response);
  78. }
  79. $tasks[urldecode(basename($job->url))] = array();
  80. if(empty($job->jobs)) continue;
  81. $tasks[urldecode(basename($job->url))] = $this->getDepthJobs($job->jobs, $userPWD, $depth + 1);
  82. }
  83. }
  84. return $tasks;
  85. }
  86. /**
  87. * 获取Jenkins流水线。
  88. * Get jobs by jenkins.
  89. *
  90. * @param int $jenkinsID
  91. * @access public
  92. * @return array
  93. */
  94. public function getJobPairs($jenkinsID)
  95. {
  96. return $this->dao->select('id, name')->from(TABLE_JOB)
  97. ->where('server')->eq($jenkinsID)
  98. ->andWhere('engine')->eq('jenkins')
  99. ->andWhere('deleted')->eq('0')
  100. ->fetchPairs();
  101. }
  102. /**
  103. * 获取jenkins api 密码串。
  104. * Get jenkins api userpwd string.
  105. *
  106. * @param object $jenkins
  107. * @access public
  108. * @return string
  109. */
  110. public function getApiUserPWD($jenkins)
  111. {
  112. $jenkinsUser = $jenkins->account;
  113. $jenkinsPassword = $jenkins->token ? $jenkins->token : base64_decode($jenkins->password);
  114. $userPWD = "$jenkinsUser:$jenkinsPassword";
  115. return $userPWD;
  116. }
  117. }