control.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * The control 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 Chenqi <chenqi@cnezsoft.com>
  8. * @package product
  9. * @version $Id: ${FILE_NAME} 5144 2020/1/8 8:10 下午 chenqi@cnezsoft.com $
  10. * @link https://www.zentao.net
  11. */
  12. class jenkins extends control
  13. {
  14. /**
  15. * Jenkins 模块初始化。
  16. * jenkins constructor.
  17. *
  18. * @param string $moduleName
  19. * @param string $methodName
  20. */
  21. public function __construct($moduleName = '', $methodName = '')
  22. {
  23. parent::__construct($moduleName, $methodName);
  24. if(stripos($this->methodName, 'ajax') === false && !commonModel::hasPriv('space', 'browse')) $this->loadModel('common')->deny('space', 'browse', false);
  25. $this->loadModel('ci')->setMenu();
  26. }
  27. /**
  28. * 创建一个jenkins服务器。
  29. * Create a jenkins.
  30. *
  31. * @access public
  32. * @return void
  33. */
  34. public function create()
  35. {
  36. if($_POST)
  37. {
  38. $jenkins = form::data($this->config->jenkins->form->create)
  39. ->add('createdBy', $this->app->user->account)
  40. ->get();
  41. $this->jenkinsZen->checkTokenAccess($jenkins->url, $jenkins->account, $jenkins->password, $jenkins->token);
  42. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  43. $jenkinsID = $this->loadModel('pipeline')->create($jenkins);
  44. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  45. $this->loadModel('action')->create('jenkins', $jenkinsID, 'created');
  46. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('space', 'browse')));
  47. }
  48. }
  49. /**
  50. * 编辑一个Jenkins服务器。
  51. * Edit a jenkins.
  52. *
  53. * @param int $jenkinsID
  54. * @access public
  55. * @return void
  56. */
  57. public function edit($jenkinsID)
  58. {
  59. $jenkins = $this->loadModel('pipeline')->getByID($jenkinsID);
  60. if($_POST)
  61. {
  62. $jenkins = form::data($this->config->jenkins->form->edit)
  63. ->add('editedBy', $this->app->user->account)
  64. ->get();
  65. $this->jenkinsZen->checkTokenAccess($jenkins->url, $jenkins->account, $jenkins->password, $jenkins->token);
  66. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  67. $this->pipeline->update($jenkinsID, $jenkins);
  68. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  69. $newJenkins = $this->pipeline->getByID($jenkinsID);
  70. $actionID = $this->loadModel('action')->create('jenkins', $jenkinsID, 'edited');
  71. $changes = common::createChanges($jenkins, $newJenkins);
  72. $this->action->logHistory($actionID, $changes);
  73. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
  74. }
  75. $this->view->title = $this->lang->jenkins->common . $this->lang->hyphen . $this->lang->jenkins->edit;
  76. $this->view->jenkins = $jenkins;
  77. $this->display();
  78. }
  79. /**
  80. * 删除一条jenkins数据。
  81. * Delete a jenkins.
  82. *
  83. * @param int $jenkinsID
  84. * @access public
  85. * @return void
  86. */
  87. public function delete($jenkinsID)
  88. {
  89. $jobs = $this->jenkins->getJobPairs($jenkinsID);
  90. if(!empty($jobs)) return $this->sendError($this->lang->jenkins->error->linkedJob, true);
  91. $this->jenkins->delete(TABLE_PIPELINE, $jenkinsID);
  92. return $this->send(array('result' => 'success', 'load' => $this->createLink('space', 'browse')));
  93. }
  94. /**
  95. * 获取Jenkins任务列表。
  96. * AJAX: Get jenkins tasks.
  97. *
  98. * @param int $jenkinsID
  99. * @access public
  100. * @return void
  101. */
  102. public function ajaxGetJenkinsTasks($jenkinsID = 0)
  103. {
  104. $tasks = array();
  105. if($jenkinsID) $tasks = $this->jenkins->getTasks($jenkinsID, 3);
  106. $this->view->tasks = $this->jenkinsZen->buildTree($tasks);
  107. $this->display();
  108. }
  109. }