control.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <?php
  2. /**
  3. * The control 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: ${FILE_NAME} 5144 2022/1/11 8:55 上午 caoyanyi@easycorp.ltd $
  10. * @link https://www.zentao.net
  11. */
  12. class sonarqube extends control
  13. {
  14. /**
  15. * The mr constructor.
  16. * @param string $moduleName
  17. * @param string $methodName
  18. */
  19. public function __construct($moduleName = '', $methodName = '')
  20. {
  21. parent::__construct($moduleName, $methodName);
  22. if(stripos($this->methodName, 'ajax') === false)
  23. {
  24. if(!commonModel::hasPriv('space', 'browse')) $this->loadModel('common')->deny('space', 'browse', false);
  25. if(!in_array(strtolower(strtolower($this->methodName)), array('browseproject', 'reportview', 'browseissue')))
  26. {
  27. if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
  28. }
  29. }
  30. /* This is essential when changing tab(menu) from gitlab to repo. */
  31. /* Optional: common::setMenuVars('devops', $this->session->repoID); */
  32. $this->loadModel('ci')->setMenu();
  33. }
  34. /**
  35. * sonarqube 列表。
  36. * Browse sonarqube.
  37. *
  38. * @param string $orderBy
  39. * @param int $recTotal
  40. * @param int $recPerPage
  41. * @param int $pageID
  42. * @access public
  43. * @return void
  44. */
  45. public function browse($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  46. {
  47. $this->app->loadClass('pager', true);
  48. $pager = new pager($recTotal, $recPerPage, $pageID);
  49. $sonarqubeList = $this->loadModel('pipeline')->getList('sonarqube', $orderBy, $pager);
  50. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->browse;
  51. $this->view->sonarqubeList = $sonarqubeList;
  52. $this->view->orderBy = $orderBy;
  53. $this->view->pager = $pager;
  54. $this->display();
  55. }
  56. /**
  57. * 展示sonarqube报告。
  58. * Show sonarqube report.
  59. *
  60. * @param int $jobID
  61. * @access public
  62. * @return void
  63. */
  64. public function reportView($jobID)
  65. {
  66. $job = $this->loadModel('job')->getByID($jobID);
  67. $qualitygate = $this->sonarqube->apiGetQualitygate($job->sonarqubeServer, $job->projectKey);
  68. $report = $this->sonarqube->apiGetReport($job->sonarqubeServer, $job->projectKey);
  69. $measures = array();
  70. if(isset($report->component->measures))
  71. {
  72. foreach($report->component->measures as $measure)
  73. {
  74. if(in_array($measure->metric, array('security_hotspots_reviewed', 'coverage', 'duplicated_lines_density')))
  75. {
  76. $measures[$measure->metric] = $measure->value . '%';
  77. }
  78. else
  79. {
  80. $measures[$measure->metric] = $measure->value;
  81. if($measure->value > 1000) $measures[$measure->metric] = round($measure->value / 1000, 1) . 'K';
  82. }
  83. }
  84. }
  85. $projectName = $job->projectKey;
  86. $projects = $this->sonarqube->apiGetProjects($job->sonarqubeServer, '', $job->projectKey);
  87. if(isset($projects[0]->name)) $projectName = $projects[0]->name;
  88. $this->view->measures = $measures;
  89. $this->view->qualitygate = $qualitygate;
  90. $this->view->projectName = $projectName;
  91. $this->view->projectKey = $job->projectKey;
  92. $this->view->sonarqubeID = $job->sonarqubeServer;
  93. $this->display();
  94. }
  95. /**
  96. * ajax方式获取项目下拉。
  97. * Ajax get project select.
  98. *
  99. * @param int $sonarqubeID
  100. * @param string $projectKey
  101. * @access public
  102. * @return void
  103. */
  104. public function ajaxGetProjectList($sonarqubeID, $projectKey = '')
  105. {
  106. $projectPairs = $this->sonarqube->getProjectPairs($sonarqubeID, $projectKey);
  107. $options = array();
  108. foreach($projectPairs as $productKey => $projectName)
  109. {
  110. $options[] = array('text' => $projectName, 'value' => $productKey);
  111. }
  112. return print(json_encode($options));
  113. }
  114. /**
  115. * 创建sonarqube服务器。
  116. * create a sonarqube.
  117. *
  118. * @access public
  119. * @return void
  120. */
  121. public function create()
  122. {
  123. if($_POST)
  124. {
  125. $sonarqube = form::data($this->config->sonarqube->form->create)
  126. ->add('createdBy', $this->app->user->account)
  127. ->get();
  128. $this->checkToken($sonarqube, 0);
  129. $sonarqubeID = $this->loadModel('pipeline')->create($sonarqube);
  130. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  131. $this->loadModel('action')->create('sonarqube', $sonarqubeID, 'created');
  132. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('space', 'browse')));
  133. }
  134. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->createServer;
  135. $this->display();
  136. }
  137. /**
  138. * 检查post数据。
  139. * Check post info.
  140. *
  141. * @param int $sonarqubeID
  142. * @access protected
  143. * @return void
  144. * @param object $sonarqube
  145. */
  146. protected function checkToken($sonarqube, $sonarqubeID = 0)
  147. {
  148. $this->sonarqubeZen->checkTokenRequire($sonarqube);
  149. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  150. /* Check name and url unique. */
  151. $existSonarQube = $this->dao->select('*')->from(TABLE_PIPELINE)
  152. ->where("type='sonarqube' and (name='{$sonarqube->name}' or url='{$sonarqube->url}')")
  153. ->andWhere('deleted')->eq('0')
  154. ->beginIF(!empty($sonarqubeID))->andWhere('id')->ne($sonarqubeID)->fi()
  155. ->fetch();
  156. if(isset($existSonarQube->name) and $existSonarQube->name == $sonarqube->name) return $this->send(array('result' => 'fail', 'message' => $this->lang->sonarqube->nameRepeatError));
  157. if(isset($existSonarQube->url) and $existSonarQube->url == $sonarqube->url) return $this->send(array('result' => 'fail', 'message' => $this->lang->sonarqube->urlRepeatError));
  158. $token = base64_encode("{$sonarqube->account}:{$sonarqube->password}");
  159. $result = $this->sonarqube->apiValidate($sonarqube->url, $token);
  160. if(!empty($result)) return $this->send(array('result' => 'fail', 'message' => $result));
  161. $sonarqube->token = $token;
  162. }
  163. /**
  164. * 编辑sonarqube服务器。
  165. * Edit a sonarqube.
  166. *
  167. * @param int $sonarqubeID
  168. * @access public
  169. * @return void
  170. */
  171. public function edit($sonarqubeID)
  172. {
  173. $oldSonarQube = $this->loadModel('pipeline')->getByID($sonarqubeID);
  174. if($_POST)
  175. {
  176. $oldSonarQube->url = $this->post->url;
  177. $oldSonarQube->account = $this->post->account;
  178. $oldSonarQube->password = $this->post->password;
  179. $this->checkToken($oldSonarQube, $sonarqubeID);
  180. $sonarqube = form::data($this->config->sonarqube->form->edit)
  181. ->add('editedBy', $this->app->user->account)
  182. ->get();
  183. $sonarqube->token = $oldSonarQube->token;
  184. $this->pipeline->update($sonarqubeID, $sonarqube);
  185. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  186. $sonarqube = $this->pipeline->getByID($sonarqubeID);
  187. $actionID = $this->loadModel('action')->create('sonarqube', $sonarqubeID, 'edited');
  188. $changes = common::createChanges($oldSonarQube, $sonarqube);
  189. $this->action->logHistory($actionID, $changes);
  190. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
  191. }
  192. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->editServer;
  193. $this->view->sonarqube = $oldSonarQube;
  194. $this->display();
  195. }
  196. /**
  197. * 删除sonarqube服务器。
  198. * Delete a sonarqube.
  199. *
  200. * @param int $sonarqubeID
  201. * @access public
  202. * @return void
  203. */
  204. public function delete($sonarqubeID)
  205. {
  206. $oldSonarQube = $this->loadModel('pipeline')->getByID($sonarqubeID);
  207. $this->loadModel('action');
  208. $actionID = $this->pipeline->deleteByObject($sonarqubeID, 'sonarqube');
  209. if(!$actionID)
  210. {
  211. $response['result'] = 'fail';
  212. $response['callback'] = sprintf('zui.Modal.alert("%s");', $this->lang->sonarqube->delError);
  213. return $this->send($response);
  214. }
  215. $sonarQube = $this->pipeline->getByID($sonarqubeID);
  216. $changes = common::createChanges($oldSonarQube, $sonarQube);
  217. $this->action->logHistory($actionID, $changes);
  218. $response['load'] = $this->createLink('space', 'browse');
  219. $response['result'] = 'success';
  220. return $this->send($response);
  221. }
  222. /**
  223. * 执行流水线。
  224. * Exec job.
  225. *
  226. * @param int $jobID
  227. * @access public
  228. * @return void
  229. */
  230. public function execJob($jobID)
  231. {
  232. echo $this->fetch('job', 'exec', "jobID=$jobID");
  233. }
  234. /**
  235. * soanrqube 项目列表。
  236. * Browse sonarqube project.
  237. *
  238. * @param int $sonarqubeID
  239. * @param string $orderBy
  240. * @param int $recTotal
  241. * @param int $recPerPage
  242. * @param int $pageID
  243. * @access public
  244. * @return void
  245. */
  246. public function browseProject($sonarqubeID, $orderBy = 'name_desc', $recPerPage = 15, $pageID = 1)
  247. {
  248. $keyword = fixer::input('post')->setDefault('keyword', '')->get('keyword');
  249. $sonarqubeProjectList = $this->sonarqube->apiGetProjects($sonarqubeID, $keyword);
  250. $projectKeyList = array();
  251. foreach($sonarqubeProjectList as $sonarqubeProject)
  252. {
  253. if(!isset($sonarqubeProject->lastAnalysisDate)) $sonarqubeProject->lastAnalysisDate = '';
  254. $projectKeyList[] = $sonarqubeProject->key;
  255. }
  256. $sonarqubeProjectList = $this->sonarqubeZen->sortAndPage($sonarqubeProjectList, $orderBy, $recPerPage, $pageID);
  257. /* Get success jobs of sonarqube.*/
  258. $projectJobPairs = $this->loadModel('job')->getJobBySonarqubeProject($sonarqubeID, $projectKeyList);
  259. $successJobs = $this->loadModel('compile')->getSuccessJobs($projectJobPairs);
  260. $sonarqube = $this->loadModel('pipeline')->getByID($sonarqubeID);
  261. $instance = $this->loadModel('instance')->getByUrl($sonarqube->url);
  262. $sonarqube->instanceID = $sonarqube->id;
  263. $sonarqube->type = 'external';
  264. if(!empty($instance->id))
  265. {
  266. $sonarqube->instanceID = $instance->id;
  267. $sonarqube->type = 'store';
  268. }
  269. $this->view->sonarqube = $sonarqube;
  270. $this->view->keyword = urldecode(urldecode($keyword));
  271. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->browseProject;
  272. $this->view->sonarqubeID = $sonarqubeID;
  273. $this->view->sonarqubeProjectList = (empty($sonarqubeProjectList) or empty($sonarqubeProjectList[$pageID - 1])) ? array() : $sonarqubeProjectList[$pageID - 1];
  274. $this->view->projectJobPairs = $projectJobPairs;
  275. $this->view->orderBy = $orderBy;
  276. $this->view->successJobs = $successJobs;
  277. $this->display();
  278. }
  279. /**
  280. * 创建一个sonarqube 项目。
  281. * Creat a sonarqube project.
  282. *
  283. * @param int $sonarqubeID
  284. * @access public
  285. * @return void
  286. */
  287. public function createProject($sonarqubeID)
  288. {
  289. if($_POST)
  290. {
  291. $project = form::data($this->config->sonarqube->form->createProject)->get();
  292. $this->sonarqube->createProject($sonarqubeID, $project);
  293. if(dao::isError()) return $this->sendError(dao::getError());
  294. return $this->sendSuccess(array('load' => inlink('browseProject', "sonarqubeID=$sonarqubeID"), 'closeModal' => true));
  295. }
  296. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->createProject;
  297. $this->view->sonarqubeID = $sonarqubeID;
  298. $this->display();
  299. }
  300. /**
  301. * 删除一个sonarqube项目。
  302. * Delete project.
  303. *
  304. * @param int $sonarqubeID
  305. * @param string $projectKey
  306. * @access public
  307. * @return void
  308. */
  309. public function deleteProject($sonarqubeID, $projectKey)
  310. {
  311. /* Fix error when request type is PATH_INFO and the tag name contains '-'.*/
  312. $projectKey = str_replace('*', '-', $projectKey);
  313. $reponse = $this->sonarqube->apiDeleteProject($sonarqubeID, $projectKey);
  314. if(isset($reponse->errors)) return $this->sendError($reponse->errors[0]->msg);
  315. $this->loadModel('action')->create('sonarqubeproject', 0, 'deleted', '', $projectKey);
  316. return $this->sendSuccess(array('load' => true));
  317. }
  318. /**
  319. * soanrqueb 项目问题列表。
  320. * Browse sonarqube issue.
  321. *
  322. * @param int $sonarqubeID
  323. * @param string $projectKey
  324. * @param bool $search
  325. * @param string $orderBy
  326. * @param int $recTotal
  327. * @param int $recPerPage
  328. * @param int $pageID
  329. * @access public
  330. * @return void
  331. */
  332. public function browseIssue($sonarqubeID, $projectKey = '', $search = false, $orderBy = 'severity_desc', $recPerPage = 100, $pageID = 1)
  333. {
  334. $projectKey = helper::safe64Decode(urldecode($projectKey));
  335. if(isset($_POST['keyword']))
  336. {
  337. $keyword = htmlspecialchars(trim($_POST['keyword']));
  338. $search = true;
  339. $pageID = 1;
  340. $this->session->set('sonarqubeIssueKeyword', $keyword);
  341. }
  342. else
  343. {
  344. $keyword = '';
  345. if($search) $keyword = $this->session->sonarqubeIssueKeyword;
  346. }
  347. $sonarqubeIssueList = $this->sonarqubeZen->getIssueList($sonarqubeID, $projectKey);
  348. /* Data search. */
  349. if($keyword)
  350. {
  351. foreach($sonarqubeIssueList as $key => $sonarqubeIssue)
  352. {
  353. if(strpos($sonarqubeIssue->message, $keyword) === false and strpos($sonarqubeIssue->file, $keyword) === false) unset($sonarqubeIssueList[$key]);
  354. }
  355. $sonarqubeIssueList = array_values($sonarqubeIssueList);
  356. }
  357. $sonarqubeIssueList = $this->sonarqubeZen->sortAndPage($sonarqubeIssueList, $orderBy, $recPerPage, $pageID);
  358. /* Get product. */
  359. $products = $this->sonarqube->getLinkedProducts($sonarqubeID, $projectKey);
  360. $productID = current(explode(',', $products));
  361. $this->view->projectKey = $projectKey;
  362. $this->view->search = $search;
  363. $this->view->keyword = $keyword;
  364. $this->view->title = $this->lang->sonarqube->common . $this->lang->hyphen . $this->lang->sonarqube->browseIssue;
  365. $this->view->sonarqubeID = $sonarqubeID;
  366. $this->view->sonarqube = $this->loadModel('pipeline')->getByID($sonarqubeID);
  367. $this->view->sonarqubeIssueList = (empty($sonarqubeIssueList) or empty($sonarqubeIssueList[$pageID - 1])) ? array() : $sonarqubeIssueList[$pageID - 1];
  368. $this->view->orderBy = $orderBy;
  369. $this->view->productID = $productID;
  370. $this->view->bugs = $this->loadModel('bug')->getBySonarqubeID($sonarqubeID);
  371. $this->display();
  372. }
  373. }