model.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * The model file of sonarqube 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.ltd>
  8. * @package sonarqube
  9. * @version $Id: $
  10. * @link https://www.zentao.net
  11. */
  12. class sonarqubeModel extends model
  13. {
  14. /**
  15. * 获取sonarqube api基础url和头。
  16. * Get sonarqube api base url and header by id.
  17. *
  18. * @param int $sonarqubeID
  19. * @access public
  20. * @return array
  21. */
  22. public function getApiBase($sonarqubeID)
  23. {
  24. $sonarqube = $this->loadModel('pipeline')->getByID($sonarqubeID);
  25. if(!$sonarqube) return array('', array());
  26. $url = rtrim($sonarqube->url, '/') . '/api/%s';
  27. $header[] = 'Authorization: Basic ' . $sonarqube->token;
  28. return array($url, $header);
  29. }
  30. /**
  31. * 检查sonarqube是否可用。
  32. * check sonarqube valid.
  33. *
  34. * @param string $host
  35. * @param string $token
  36. * @access public
  37. * @return array
  38. */
  39. public function apiValidate($host, $token)
  40. {
  41. $url = rtrim($host, '/') . "/api/authentication/validate";
  42. $header = array('Authorization: Basic ' . $token);
  43. $result = json_decode(commonModel::http($url, null, array(), $header));
  44. if(!isset($result->valid) or !$result->valid) return array('password' => array($this->lang->sonarqube->validError));
  45. $url = rtrim($host, '/') . "/api/user_groups/search";
  46. $adminer = json_decode(commonModel::http($url, null, array(), $header));
  47. if(empty($adminer) or isset($adminer->errors)) return array('account' => array($this->lang->sonarqube->notAdminer));
  48. return array();
  49. }
  50. /**
  51. * 获取sonarqube报告。
  52. * Get sonarqube report.
  53. *
  54. * @param int $sonarqubeID
  55. * @param string $projectKey
  56. * @param string $metricKeys
  57. * @access public
  58. * @return object|array|null
  59. */
  60. public function apiGetReport($sonarqubeID, $projectKey, $metricKeys = '')
  61. {
  62. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  63. if(!$apiRoot) return array();
  64. if(!$metricKeys) $metricKeys = 'bugs,coverage,vulnerabilities,duplicated_lines_density,code_smells,ncloc,security_hotspots_reviewed';
  65. $url = sprintf($apiRoot, "measures/component?component={$projectKey}&metricKeys={$metricKeys}");
  66. return json_decode(commonModel::http($url, null, array(), $header));
  67. }
  68. /**
  69. * 获取sonarqube项目质量门。
  70. * Get sonarqube qualitygate by project.
  71. *
  72. * @param int $sonarqubeID
  73. * @param string $projectKey
  74. * @access public
  75. * @return object|array|null
  76. */
  77. public function apiGetQualitygate($sonarqubeID, $projectKey)
  78. {
  79. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  80. if(!$apiRoot) return array();
  81. $url = sprintf($apiRoot, "qualitygates/project_status?projectKey={$projectKey}");
  82. return json_decode(commonModel::http($url, null, array(), $header));
  83. }
  84. /**
  85. * 获取sonarqube问题列表。
  86. * Get issues of one sonarqube.
  87. *
  88. * @param int $sonarqubeID
  89. * @param string $projectKey
  90. * @access public
  91. * @return array
  92. */
  93. public function apiGetIssues($sonarqubeID, $projectKey = '')
  94. {
  95. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  96. if(!$apiRoot) return array();
  97. $url = sprintf($apiRoot, "issues/search");
  98. $url .= "?ps=500";
  99. if($projectKey) $url .= "&componentKeys={$projectKey}";
  100. $allResults = array();
  101. for($page = 1; true; $page++)
  102. {
  103. $result = json_decode(commonModel::http($url . "&p={$page}", null, array(), $header));
  104. if(!isset($result->issues)) break;
  105. if(!empty($result->issues)) $allResults = array_merge($allResults, $result->issues);
  106. if(count($result->issues) < 500) break;
  107. }
  108. return $allResults;
  109. }
  110. /**
  111. * 获取sonarqube项目列表。
  112. * Get projects of one sonarqube.
  113. *
  114. * @param int $sonarqubeID
  115. * @param string $keyword
  116. * @access public
  117. * @return array
  118. * @param string $projectKey
  119. */
  120. public function apiGetProjects($sonarqubeID, $keyword = '', $projectKey = '')
  121. {
  122. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  123. if(!$apiRoot) return array();
  124. $url = sprintf($apiRoot, "projects/search");
  125. $url .= "?ps=500";
  126. if($keyword) $url .= "&q={$keyword}";
  127. if($projectKey) $url .= "&projects={$projectKey}";
  128. $allResults = array();
  129. for($page = 1; true; $page++)
  130. {
  131. $result = json_decode(commonModel::http($url . "&p={$page}", null, array(), $header));
  132. if(!isset($result->components)) break;
  133. if(!empty($result->components)) $allResults = array_merge($allResults, $result->components);
  134. if(count($result->components) < 500) break;
  135. }
  136. return $allResults;
  137. }
  138. /**
  139. * api创建sonarqube项目。
  140. * Create a sonarqube project by api.
  141. *
  142. * @param int $sonarqubeID
  143. * @param object $project
  144. * @access public
  145. * @return object|array|null|false
  146. */
  147. public function apiCreateProject($sonarqubeID, $project)
  148. {
  149. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  150. if(!$apiRoot) return false;
  151. $url = sprintf($apiRoot, "projects/create?name=" . urlencode($project->projectName) . "&project=" . urlencode($project->projectKey));
  152. return json_decode(commonModel::http($url, null, array(CURLOPT_CUSTOMREQUEST => 'POST'), $header));
  153. }
  154. /**
  155. * api删除sonarqube项目。
  156. * Delete sonarqube project by api.
  157. *
  158. * @param int $sonarqubeID
  159. * @param string $projectKey
  160. * @access public
  161. * @return object|array|null|false
  162. */
  163. public function apiDeleteProject($sonarqubeID, $projectKey)
  164. {
  165. list($apiRoot, $header) = $this->getApiBase($sonarqubeID);
  166. if(!$apiRoot) return false;
  167. $url = sprintf($apiRoot, "projects/delete?project=$projectKey");
  168. return json_decode(commonModel::http($url, null, array(CURLOPT_CUSTOMREQUEST => 'POST'), $header));
  169. }
  170. /**
  171. * 创建sonarqube项目。
  172. * Create a sonarqube project.
  173. *
  174. * @param int $sonarqubeID
  175. * @param object $project
  176. * @access public
  177. * @return bool
  178. */
  179. public function createProject($sonarqubeID, $project)
  180. {
  181. if(mb_strlen($project->projectName) > 255) dao::$errors['projectName'][] = sprintf($this->lang->sonarqube->lengthError, $this->lang->sonarqube->projectName, 255);
  182. if(mb_strlen($project->projectKey) > 400) dao::$errors['projectKey'][] = sprintf($this->lang->sonarqube->lengthError, $this->lang->sonarqube->projectKey, 400);
  183. if(dao::isError()) return false;
  184. $response = $this->apiCreateProject($sonarqubeID, $project);
  185. if(!empty($response->project->key))
  186. {
  187. $this->loadModel('action')->create('sonarqubeproject', 0, 'created', '', $response->project->name);
  188. return true;
  189. }
  190. if(!$response) return false;
  191. return $this->apiErrorHandling($response);
  192. }
  193. /**
  194. * api错误处理。
  195. * Api error handling.
  196. *
  197. * @param object $response
  198. * @access public
  199. * @return bool
  200. */
  201. public function apiErrorHandling($response)
  202. {
  203. if(!empty($response->errors))
  204. {
  205. foreach($response->errors as $error)
  206. {
  207. if(isset($error->msg)) dao::$errors['name'][] = $this->convertApiError($error->msg);
  208. }
  209. return false;
  210. }
  211. dao::$errors['name'][] = 'error';
  212. return false;
  213. }
  214. /**
  215. * api错误转换处理。
  216. * Convert API error.
  217. *
  218. * @param array|string $message
  219. * @access public
  220. * @return string
  221. */
  222. public function convertApiError($message)
  223. {
  224. if(is_array($message)) $message = $message[0];
  225. if(!is_string($message)) return $message;
  226. if(!isset($this->lang->sonarqube->apiErrorMap)) return $message;
  227. $this->app->loadLang('mr');
  228. foreach($this->lang->sonarqube->apiErrorMap as $key => $errorMsg)
  229. {
  230. if($message == $errorMsg)
  231. {
  232. $errorMessage = zget($this->lang->mr->errorLang, $key, $message);
  233. }
  234. elseif(strpos($errorMsg, '/') === 0)
  235. {
  236. $result = preg_match($errorMsg, $message, $matches);
  237. if($result) $errorMessage = sprintf(zget($this->lang->sonarqube->errorLang, $key), $matches[1]);
  238. }
  239. if(isset($errorMessage)) break;
  240. }
  241. return isset($errorMessage) ? $errorMessage : $message;
  242. }
  243. /**
  244. * 获取缓存文件。
  245. * Get cache file.
  246. *
  247. * @param int $sonarqubeID
  248. * @param string $projectKey
  249. * @access public
  250. * @return string|false
  251. */
  252. public function getCacheFile($sonarqubeID, $projectKey)
  253. {
  254. $cachePath = $this->app->getCacheRoot() . '/' . 'sonarqube';
  255. if(!is_dir($cachePath)) mkdir($cachePath, 0777, true);
  256. if(!is_writable($cachePath)) return false;
  257. return $cachePath . '/' . $sonarqubeID . '-' . md5($projectKey);
  258. }
  259. /**
  260. * 获取关联产品。
  261. * Get linked products.
  262. *
  263. * @param int $sonarqubeID
  264. * @param string $projectKey
  265. * @access public
  266. * @return string
  267. */
  268. public function getLinkedProducts($sonarqubeID, $projectKey)
  269. {
  270. return $this->dao->select('t2.product')->from(TABLE_JOB)->alias('t1')
  271. ->leftJoin(TABLE_REPO)->alias('t2')->on('t1.repo=t2.id')
  272. ->where('t1.frame')->eq('sonarqube')
  273. ->andWhere('sonarqubeServer')->eq($sonarqubeID)
  274. ->andWhere('projectKey')->eq($projectKey)
  275. ->fetch('product');
  276. }
  277. /**
  278. * 获取项目键值对。
  279. * Get project pairs.
  280. *
  281. * @param int $sonarqubeID
  282. * @param string $projectKey
  283. * @access public
  284. * @return array
  285. */
  286. public function getProjectPairs($sonarqubeID, $projectKey = '')
  287. {
  288. $jobPairs = $this->loadModel('job')->getJobBySonarqubeProject($sonarqubeID, array(), true, true);
  289. $existsProject = array_diff(array_keys($jobPairs), array($projectKey));
  290. $projectList = $this->apiGetProjects($sonarqubeID);
  291. $projectPairs = array();
  292. foreach($projectList as $project)
  293. {
  294. if(!empty($project) and !in_array($project->key, $existsProject)) $projectPairs[$project->key] = $project->name;
  295. }
  296. return $projectPairs;
  297. }
  298. /**
  299. * 判断按钮是否可点击。
  300. * Judge an action is clickable or not.
  301. *
  302. * @param object $sonarqube
  303. * @param string $action
  304. * @access public
  305. * @return bool
  306. */
  307. public static function isClickable($sonarqube, $action)
  308. {
  309. $action = strtolower($action);
  310. global $app;
  311. if($action == 'execjob') return $app->rawModule == 'repo' ? !$sonarqube->exec : !empty($sonarqube->jobID);
  312. if($action == 'reportview') return $app->rawModule == 'repo' ? !$sonarqube->report : !empty($sonarqube->reportView);
  313. return true;
  314. }
  315. /**
  316. * 判断按钮是否显示在列表页。
  317. * Judge an action is displayed in browse page.
  318. *
  319. * @param object $sonarqube
  320. * @param string $action
  321. * @access public
  322. * @return bool
  323. */
  324. public static function isDisplay($sonarqube, $action)
  325. {
  326. $action = strtolower($action);
  327. if(!commonModel::hasPriv('space', 'browse')) return false;
  328. if(in_array($action, array('browseproject', 'reportview', 'browseissue'))) return true;
  329. if(!commonModel::hasPriv('instance', 'manage', $sonarqube)) return false;
  330. return true;
  331. }
  332. }