model.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /**
  3. * The model file of pipeline 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 pipeline
  9. * @link https://www.zentao.net
  10. */
  11. class pipelineModel extends model
  12. {
  13. /**
  14. * 根据id获取一条服务器记录。
  15. * Get a pipeline by id.
  16. *
  17. * @param int $id
  18. * @access public
  19. * @return object|false
  20. */
  21. public function getByID($id)
  22. {
  23. $pipeline = $this->dao->select('*')->from(TABLE_PIPELINE)->where('id')->eq($id)->fetch();
  24. if($pipeline && !empty($pipeline->password)) $pipeline->password = base64_decode($pipeline->password);
  25. return $pipeline;
  26. }
  27. /**
  28. * 根据名称及类型获取一条流水线记录。
  29. * Get a pipeline by name and type.
  30. *
  31. * @param string $name
  32. * @param string $type
  33. * @access public
  34. * @return object|false
  35. */
  36. public function getByNameAndType($name, $type)
  37. {
  38. return $this->dao->select('id')->from(TABLE_PIPELINE)->where('name')->eq($name)->andWhere('type')->eq($type)->fetch();
  39. }
  40. /**
  41. * 根据url获取渠成创建的代码库。
  42. * Get a pipeline by url which created by quickon.
  43. *
  44. * @param string $url
  45. * @access public
  46. * @return object|false
  47. */
  48. public function getByUrl($url)
  49. {
  50. return $this->dao->select('id')->from(TABLE_PIPELINE)->where('url')->eq($url)->andWhere('createdBy')->eq('system')->fetch();
  51. }
  52. /**
  53. * 获取服务器列表。
  54. * Get pipeline list.
  55. *
  56. * @param string $type
  57. * @param string $orderBy
  58. * @param object $pager
  59. * @access public
  60. * @return array
  61. */
  62. public function getList($type = 'jenkins', $orderBy = 'id_desc', $pager = null)
  63. {
  64. $type = strtolower($type);
  65. return $this->dao->select('*')->from(TABLE_PIPELINE)
  66. ->where('deleted')->eq('0')
  67. ->beginIF($type)->AndWhere('type')->in($type)->fi()
  68. ->orderBy($orderBy)
  69. ->page($pager)
  70. ->fetchAll('id');
  71. }
  72. /**
  73. * 获取服务器列表。
  74. * Get pipeline pairs.
  75. *
  76. * @param string $type
  77. * @param bool $checkOnline
  78. * @access public
  79. * @return array
  80. */
  81. public function getPairs($type = '', $checkOnline = false)
  82. {
  83. $type = strtolower($type);
  84. if(!$this->config->inQuickon || !$checkOnline)
  85. {
  86. return $this->dao->select('id,name')->from(TABLE_PIPELINE)
  87. ->where('deleted')->eq('0')
  88. ->beginIF($type)->andWhere('type')->in($type)->fi()
  89. ->orderBy('id_desc')
  90. ->fetchPairs('id', 'name');
  91. }
  92. $serverList = $this->getList($type);
  93. $instanceList = $this->loadModel('instance')->getList();
  94. $statusList = $this->loadModel('cne')->batchQueryStatus($instanceList);
  95. $runningApps = array();
  96. foreach($instanceList as $instance)
  97. {
  98. $statusData = zget($statusList, $instance->k8name, '');
  99. if($statusData && $statusData->status == 'running') $runningApps[$instance->id] = $instance->domain;
  100. }
  101. $serverPairs = array();
  102. foreach($serverList as $server)
  103. {
  104. if($server->createdBy == 'system')
  105. {
  106. if($server->instanceID && isset($runningApps[$server->instanceID]))
  107. {
  108. $serverPairs[$server->id] = $server->name;
  109. continue;
  110. }
  111. foreach($runningApps as $domain)
  112. {
  113. if(strpos($server->url, $domain) !== false)
  114. {
  115. $serverPairs[$server->id] = $server->name;
  116. continue 2;
  117. }
  118. }
  119. continue;
  120. }
  121. $serverPairs[$server->id] = $server->name;
  122. }
  123. return $serverPairs;
  124. }
  125. /**
  126. * 创建服务器。
  127. * Create a server.
  128. *
  129. * @param object $server
  130. * @access public
  131. * @return int|false
  132. */
  133. public function create($server)
  134. {
  135. if(!empty($server->appType)) $server->type = $server->appType;
  136. unset($server->appType);
  137. $server->url = rtrim($server->url, '/');
  138. if(isset($server->password)) $server->password = base64_encode($server->password);
  139. $this->dao->insert(TABLE_PIPELINE)->data($server)
  140. ->batchCheck($this->config->pipeline->create->requiredFields, 'notempty')
  141. ->batchCheck("url", 'URL')
  142. ->check('name', 'unique', "`type` = '$server->type'")
  143. ->checkIF($server->type == 'jenkins', 'account', 'notempty')
  144. ->checkIF($server->type == 'jenkins' && !$server->token, 'password', 'notempty')
  145. ->checkIF($server->type == 'jenkins' && !$server->password, 'token', 'notempty')
  146. ->autoCheck()
  147. ->exec();
  148. if(dao::isError()) return false;
  149. return $this->dao->lastInsertId();
  150. }
  151. /**
  152. * 更新服务器。
  153. * Update a server.
  154. *
  155. * @param int $id
  156. * @param object $server
  157. * @access public
  158. * @return bool
  159. */
  160. public function update($id, $server)
  161. {
  162. $type = $this->dao->select('type')->from(TABLE_PIPELINE)->where('id')->eq($id)->fetch('type');
  163. $server->url = rtrim($server->url, '/');
  164. if(isset($server->password)) $server->password = base64_encode($server->password);
  165. $this->dao->update(TABLE_PIPELINE)->data($server)
  166. ->batchCheck($this->config->pipeline->edit->requiredFields, 'notempty')
  167. ->batchCheck("url", 'URL')
  168. ->check('name', 'unique', "`type` = '$type' and id <> $id")
  169. ->checkIF($type == 'jenkins', 'account', 'notempty')
  170. ->checkIF($type == 'jenkins' and !$server->token, 'password', 'notempty')
  171. ->checkIF($type == 'jenkins' and !$server->password, 'token', 'notempty')
  172. ->autoCheck()
  173. ->where('id')->eq($id)
  174. ->exec();
  175. return !dao::isError();
  176. }
  177. /**
  178. * 删除服务器。
  179. * Delete one record.
  180. *
  181. * @param int $id the id to be deleted
  182. * @param string $type the action object
  183. * @access public
  184. * @return int|bool
  185. */
  186. public function deleteByObject($id, $type = 'gitlab')
  187. {
  188. if(in_array($type, array('gitlab', 'gitea', 'gogs')))
  189. {
  190. $repo = $this->dao->select('*')->from(TABLE_REPO)
  191. ->where('deleted')->eq('0')
  192. ->andWhere('SCM')->eq(ucfirst($type))
  193. ->andWhere('serviceHost')->eq($id)
  194. ->fetch();
  195. if($repo) return false;
  196. }
  197. elseif($type == 'sonarqube')
  198. {
  199. $job = $this->dao->select('id,name,repo,deleted')->from(TABLE_JOB)
  200. ->where('frame')->eq('sonarqube')
  201. ->andWhere('server')->eq($id)
  202. ->andWhere('deleted')->eq('0')
  203. ->fetch();
  204. if($job) return false;
  205. }
  206. $server = $this->fetchByID($id);
  207. if(!$server) return false;
  208. $this->dao->update(TABLE_PIPELINE)->set('deleted')->eq(1)->where('id')->eq($id)->exec();
  209. $this->loadModel('action')->create($type, $id, 'deleted', '', $server->instanceID ? 0 : 1);
  210. return $this->dao->lastInsertID();
  211. }
  212. /**
  213. * 获取禅道用户绑定的第三方账号。
  214. * Get user binded third party accounts.
  215. *
  216. * @param int $providerID
  217. * @param string $providerType gitlab, gitea, gogs
  218. * @param string $fields key, value
  219. * @access public
  220. * @return array
  221. */
  222. public function getUserBindedPairs($providerID, $providerType, $fields = 'account,openID')
  223. {
  224. $providerType = strtolower($providerType);
  225. return $this->dao->select($fields)->from(TABLE_OAUTH)
  226. ->where('providerType')->eq($providerType)
  227. ->andWhere('providerID')->eq($providerID)
  228. ->fetchPairs();
  229. }
  230. /**
  231. * 根据服务器ID和禅道账号获取禅道用户绑定的第三方账号。
  232. * Get user binded third party accounts.
  233. *
  234. * @param int $providerID
  235. * @param string $providerType gitlab, gitea, gogs
  236. * @param string $zentaoAccount
  237. * @access public
  238. * @return string
  239. */
  240. public function getOpenIdByAccount($providerID, $providerType, $zentaoAccount)
  241. {
  242. $providerType = strtolower($providerType);
  243. return (string)$this->dao->select('openID')->from(TABLE_OAUTH)
  244. ->where('providerType')->eq($providerType)
  245. ->andWhere('providerID')->eq($providerID)
  246. ->andWhere('account')->eq($zentaoAccount)
  247. ->fetch('openID');
  248. }
  249. /**
  250. * 根据禅道账号获取禅道用户绑定的第三方服务器和账号信息。
  251. * Get user binded third party accounts.
  252. *
  253. * @param string $providerType gitlab, gitea, gogs
  254. * @param string $account
  255. * @access public
  256. * @return array
  257. */
  258. public function getProviderPairsByAccount($providerType, $account = '')
  259. {
  260. $providerType = strtolower($providerType);
  261. if(!$account) $account = $this->app->user->account;
  262. return $this->dao->select('providerID,openID')->from(TABLE_OAUTH)
  263. ->where('providerType')->eq($providerType)
  264. ->andWhere('account')->eq($account)
  265. ->fetchPairs('providerID');
  266. }
  267. }