control.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * The control file of gitea 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 gitea
  9. * @link https://www.zentao.net
  10. */
  11. class gitea extends control
  12. {
  13. /**
  14. * The gitea constructor.
  15. * @param string $moduleName
  16. * @param string $methodName
  17. */
  18. public function __construct($moduleName = '', $methodName = '')
  19. {
  20. parent::__construct($moduleName, $methodName);
  21. if(!commonModel::hasPriv('instance', 'manage')) $this->loadModel('common')->deny('instance', 'manage', false);
  22. /* This is essential when changing tab(menu) from gitea to repo. */
  23. /* Optional: common::setMenuVars('devops', $this->session->repoID); */
  24. if($this->app->rawMethod != 'binduser') $this->loadModel('ci')->setMenu();
  25. }
  26. /**
  27. * Gitea服务器列表。
  28. * Browse gitea.
  29. *
  30. * @param string $orderBy
  31. * @param int $recTotal
  32. * @param int $recPerPage
  33. * @param int $pageID
  34. * @access public
  35. * @return void
  36. */
  37. public function browse($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  38. {
  39. $this->app->loadClass('pager', true);
  40. $pager = new pager($recTotal, $recPerPage, $pageID);
  41. /* Admin user don't need bind. */
  42. $giteaList = $this->loadModel('pipeline')->getList('gitea', $orderBy, $pager);
  43. $myGiteas = $this->gitea->getGiteaListByAccount();
  44. foreach($giteaList as $gitea)
  45. {
  46. $gitea->isBindUser = true;
  47. if(!$this->app->user->admin && !isset($myGiteas[$gitea->id])) $gitea->isBindUser = false;
  48. }
  49. $this->view->title = $this->lang->gitea->common . $this->lang->hyphen . $this->lang->gitea->browse;
  50. $this->view->giteaList = $giteaList;
  51. $this->view->orderBy = $orderBy;
  52. $this->view->pager = $pager;
  53. $this->display();
  54. }
  55. /**
  56. * 增加一个gitea服务器。
  57. * Create a gitea.
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. public function create()
  63. {
  64. if($_POST)
  65. {
  66. $gitea = form::data($this->config->gitea->form->create)
  67. ->add('createdBy', $this->app->user->account)
  68. ->get();
  69. $result = $this->checkToken($gitea);
  70. if(is_array($result)) return $this->send($result);
  71. $giteaID = $this->loadModel('pipeline')->create($gitea);
  72. if(dao::isError()) return $this->sendError(dao::getError());
  73. $this->loadModel('action')->create('gitea', $giteaID, 'created');
  74. return $this->sendSuccess(array('locate' => $this->createLink('space', 'browse')));
  75. }
  76. $this->view->title = $this->lang->gitea->common . $this->lang->hyphen . $this->lang->gitea->lblCreate;
  77. $this->display();
  78. }
  79. /**
  80. * 查看一个gitea服务器。
  81. * View a gitea.
  82. * @param int $giteaID
  83. * @access public
  84. * @return void
  85. */
  86. public function view($giteaID)
  87. {
  88. $gitea = $this->gitea->fetchByID($giteaID);
  89. $this->view->title = $this->lang->gitea->common . $this->lang->hyphen . $this->lang->gitea->view;
  90. $this->view->gitea = $gitea;
  91. $this->view->users = $this->loadModel('user')->getPairs('noclosed');
  92. $this->view->actions = $this->loadModel('action')->getList('gitea', $giteaID);
  93. $this->display();
  94. }
  95. /**
  96. * 编辑一个gitea服务器。
  97. * Edit a gitea.
  98. *
  99. * @param int $giteaID
  100. * @access public
  101. * @return void
  102. */
  103. public function edit($giteaID)
  104. {
  105. $oldGitea = $this->gitea->fetchByID($giteaID);
  106. if($_POST)
  107. {
  108. $gitea = form::data($this->config->gitea->form->edit)
  109. ->add('editedBy', $this->app->user->account)
  110. ->get();
  111. $result = $this->checkToken($gitea);
  112. if(is_array($result)) return $this->send($result);
  113. $this->loadModel('pipeline')->update($giteaID, $gitea);
  114. if(dao::isError()) return $this->sendError(dao::getError());
  115. $gitea = $this->gitea->fetchByID($giteaID);
  116. $actionID = $this->loadModel('action')->create('gitea', $giteaID, 'edited');
  117. $changes = common::createChanges($oldGitea, $gitea);
  118. $this->action->logHistory($actionID, $changes);
  119. return $this->sendSuccess(array('load' => true, 'closeModal' => true));
  120. }
  121. $this->view->title = $this->lang->gitea->common . $this->lang->hyphen . $this->lang->gitea->edit;
  122. $this->view->gitea = $oldGitea;
  123. $this->display();
  124. }
  125. /**
  126. * 删除一条gitea记录
  127. * Delete a gitea.
  128. *
  129. * @param int $giteaID
  130. * @access public
  131. * @return void
  132. */
  133. public function delete($giteaID)
  134. {
  135. $oldGitea = $this->loadModel('pipeline')->fetchByID($giteaID);
  136. $actionID = $this->pipeline->deleteByObject($giteaID, 'gitea');
  137. if(!$actionID)
  138. {
  139. $response['result'] = 'fail';
  140. $response['callback'] = sprintf('zui.Modal.alert("%s");', $this->lang->pipeline->delError);
  141. return $this->send($response);
  142. }
  143. $gitea = $this->pipeline->fetchByID($giteaID);
  144. $changes = common::createChanges($oldGitea, $gitea);
  145. $this->loadModel('action')->logHistory($actionID, $changes);
  146. $response['load'] = $this->createLink('space', 'browse');
  147. $response['message'] = zget($this->lang->instance->notices, 'uninstallSuccess');
  148. $response['result'] = 'success';
  149. return $this->send($response);
  150. }
  151. /**
  152. * 绑定gitea用户到禅道用户。
  153. * Bind gitea user to zentao users.
  154. *
  155. * @param int $giteaID
  156. * @param string $type
  157. * @access public
  158. * @return void
  159. */
  160. public function bindUser($giteaID, $type = 'all')
  161. {
  162. if($_POST)
  163. {
  164. $this->gitea->bindUser($giteaID, (array)$this->post->zentaoUsers, (array)$this->post->giteaUserNames);
  165. if(dao::isError()) return $this->sendError(dao::getError());
  166. return $this->sendSuccess(array('load' => helper::createLink('space', 'browse')));
  167. }
  168. $userList = array();
  169. $giteaUsers = $this->gitea->apiGetUsers($giteaID);
  170. $matchedResult = $this->giteaZen->getMatchedUsers($giteaID, $giteaUsers);
  171. $bindedUsers = $this->loadModel('pipeline')->getUserBindedPairs($giteaID, 'gitea');
  172. $zentaoUsers = $this->loadModel('user')->getRealNameAndEmails(helper::arrayColumn($matchedResult, 'zentaoAccount'));
  173. foreach($giteaUsers as $giteaUser)
  174. {
  175. $user = new stdclass();
  176. $user->email = '';
  177. $user->status = 'notBind';
  178. $user->giteaID = $giteaUser->id;
  179. $user->giteaEmail = $giteaUser->email;
  180. $user->giteaAccount = $giteaUser->account;
  181. $user->giteaUser = $giteaUser->realname . '@' . $giteaUser->account;
  182. $user->giteaUserAvatar = $giteaUser->avatar;
  183. $user->zentaoUsers = isset($matchedResult[$giteaUser->id]) ? $matchedResult[$giteaUser->id]->zentaoAccount : '';
  184. if($user->zentaoUsers)
  185. {
  186. if(isset($zentaoUsers[$user->zentaoUsers])) $user->email = $zentaoUsers[$user->zentaoUsers]->email;
  187. if(isset($bindedUsers[$user->zentaoUsers]) && $bindedUsers[$user->zentaoUsers] == $giteaUser->account) $user->status = 'binded';
  188. }
  189. if($type != 'all' && $user->status != $type) continue;
  190. $userList[] = $user;
  191. }
  192. $this->view->title = $this->lang->gitea->bindUser;
  193. $this->view->type = $type;
  194. $this->view->giteaID = $giteaID;
  195. $this->view->recTotal = count($userList);
  196. $this->view->userList = $userList;
  197. $this->view->userPairs = $this->user->getPairs('noclosed|noletter');
  198. $this->view->zentaoUsers = $zentaoUsers;
  199. $this->display();
  200. }
  201. /**
  202. * 获取分支列表。
  203. * Ajax get branches.
  204. *
  205. * @param int $giteaID
  206. * @param string $project
  207. * @access public
  208. * @return void
  209. */
  210. public function ajaxGetProjectBranches($giteaID, $project)
  211. {
  212. $options = array(array('text' => '', 'value' => ''));
  213. if(!$giteaID || !$project) return print(json_encode($options));
  214. $project = urldecode(base64_decode($project));
  215. $branches = $this->gitea->apiGetBranches($giteaID, $project);
  216. foreach($branches as $branch)
  217. {
  218. $options[] = array('text' => $branch->name, 'value' => $branch->name);
  219. }
  220. return print(json_encode($options));
  221. }
  222. }