tao.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * The tao file of compile 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 XX <xx@easycorp.ltd>
  8. * @package compile
  9. * @link https://www.zentao.net
  10. */
  11. class compileTao extends compileModel
  12. {
  13. /**
  14. * 更新流水线最后同步时间。
  15. * Update job last sync date.
  16. *
  17. * @param int $jobID
  18. * @param string $date
  19. * @access protected
  20. * @return void
  21. */
  22. protected function updateJobLastSyncDate($jobID, $date)
  23. {
  24. $this->dao->update(TABLE_JOB)->set('lastSyncDate')->eq($date)->where('id')->eq($jobID)->exec();
  25. }
  26. /**
  27. * 获取jenkins api接口前缀地址。
  28. * Get jenkins api url prefix.
  29. *
  30. * @param string $url
  31. * @param string $pipeline
  32. * @access protected
  33. * @return string
  34. */
  35. protected function getJenkinsUrlPrefix($url, $pipeline)
  36. {
  37. if(strpos($pipeline, '/job/') !== false)
  38. {
  39. $urlPrefix = sprintf('%s%s', $url, $pipeline);
  40. }
  41. else
  42. {
  43. $urlPrefix = sprintf('%s/job/%s/', $url, $pipeline);
  44. }
  45. return rtrim($urlPrefix, '/') . '/';
  46. }
  47. /**
  48. * 根据构建信息创建compile。
  49. * Create compile by build info.
  50. *
  51. * @param string $name
  52. * @param int $jobID
  53. * @param object $build
  54. * @param string $buildType
  55. * @access protected
  56. * @return bool
  57. */
  58. protected function createByBuildInfo($name, $jobID, $build, $buildType)
  59. {
  60. if($buildType == 'jenkins' && empty($build->queueId)) return false;
  61. if($buildType == 'gitlab' && empty($build->id)) return false;
  62. $data = new stdclass();
  63. $data->name = $name;
  64. $data->job = $jobID;
  65. $data->createdBy = 'system';
  66. if($buildType == 'jenkins')
  67. {
  68. $data->queue = $build->queueId;
  69. $data->status = $build->result == 'SUCCESS' ? 'success' : 'running';
  70. if($build->result == 'FAILURE') $data->status = 'failure';
  71. $data->createdDate = date('Y-m-d H:i:s', (int)($build->timestamp / 1000));
  72. $data->updateDate = $data->createdDate;
  73. }
  74. else
  75. {
  76. if(empty($build->number) && empty($build->id)) return false;
  77. $data->queue = !empty($build->number) ? $build->number : $build->id;
  78. $data->status = isset($this->lang->compile->statusList[$build->status]) ? $build->status : 'failure';
  79. $createdDate = isset($build->created_at) ? strtotime($build->created_at) : time();
  80. if(isset($build->created)) $createdDate = $build->created;
  81. $data->createdDate = date('Y-m-d H:i:s', $createdDate);
  82. $updatedDate = isset($build->updated_at) ? strtotime($build->updated_at) : $createdDate;
  83. if(isset($build->updated))
  84. {
  85. if(strlen((string)$build->updated) > 10) $updatedDate = intval($build->updated / 1000);
  86. $updatedDate = $build->updated;
  87. }
  88. $data->updateDate = date('Y-m-d H:i:s', $updatedDate);
  89. $data->branch = !empty($build->branch) ? $build->branch : '';
  90. }
  91. $this->dao->insert(TABLE_COMPILE)->data($data)->exec();
  92. $this->dao->update(TABLE_JOB)->set('lastExec')->eq($data->createdDate)->set('lastStatus')->eq($data->status)->where('id')->eq($jobID)->exec();
  93. return !dao::isError();
  94. }
  95. }