control.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * The control file of gogs 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 Liyuchun <liyuchun@easycorp.ltd>
  8. * @package gogs
  9. * @link https://www.zentao.net
  10. */
  11. class gogs extends control
  12. {
  13. /**
  14. * The gogs 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 gogs to repo. */
  23. /* Optional: common::setMenuVars('devops', $this->session->repoID); */
  24. if($this->app->rawMethod != 'binduser') $this->loadModel('ci')->setMenu();
  25. }
  26. /**
  27. * Gogs服务器列表。
  28. * Browse gogs list.
  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. $gogsList = $this->loadModel('pipeline')->getList('gogs', $orderBy, $pager);
  43. $myGogses = $this->gogs->getGogsListByAccount();
  44. foreach($gogsList as $gogs)
  45. {
  46. $gogs->isBindUser = true;
  47. if(!$this->app->user->admin && !isset($myGogses[$gogs->id])) $gogs->isBindUser = false;
  48. }
  49. $this->view->title = $this->lang->gogs->common . $this->lang->hyphen . $this->lang->gogs->browse;
  50. $this->view->gogsList = $gogsList;
  51. $this->view->orderBy = $orderBy;
  52. $this->view->pager = $pager;
  53. $this->display();
  54. }
  55. /**
  56. * 添加gogs服务器。
  57. * Create a gogs.
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. public function create()
  63. {
  64. if($_POST)
  65. {
  66. $gogs = form::data($this->config->gogs->form->create)
  67. ->add('createdBy', $this->app->user->account)
  68. ->get();
  69. $priv = $this->checkToken($gogs);
  70. if(is_array($priv)) return $this->send($priv);
  71. $gogsID = $this->loadModel('pipeline')->create($gogs);
  72. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  73. $this->loadModel('action')->create('gogs', $gogsID, 'created');
  74. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->createLink('space', 'browse')));
  75. }
  76. $this->view->title = $this->lang->gogs->common . $this->lang->hyphen . $this->lang->gogs->lblCreate;
  77. $this->display();
  78. }
  79. /**
  80. * 查看gogs服务器。
  81. * View a gogs.
  82. * @param int $gogsID
  83. * @access public
  84. * @return void
  85. */
  86. public function view($gogsID)
  87. {
  88. $gogs = $this->gogs->fetchByID($gogsID);
  89. $this->view->title = $this->lang->gogs->common . $this->lang->hyphen . $this->lang->gogs->view;
  90. $this->view->gogs = $gogs;
  91. $this->view->users = $this->loadModel('user')->getPairs('noclosed');
  92. $this->view->actions = $this->loadModel('action')->getList('gogs', $gogsID);
  93. $this->view->preAndNext = $this->loadModel('common')->getPreAndNextObject('pipeline', $gogsID);
  94. $this->display();
  95. }
  96. /**
  97. * 编辑gogs服务器。
  98. * Edit a gogs.
  99. *
  100. * @param int $gogsID
  101. * @access public
  102. * @return void
  103. */
  104. public function edit($gogsID)
  105. {
  106. $oldGogs = $this->gogs->fetchByID($gogsID);
  107. if($_POST)
  108. {
  109. $gogs = form::data($this->config->gogs->form->edit)
  110. ->add('editedBy', $this->app->user->account)
  111. ->get();
  112. $this->checkToken($gogs);
  113. $this->loadModel('pipeline')->update($gogsID, $gogs);
  114. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  115. $gogs = $this->gogs->fetchByID($gogsID);
  116. $actionID = $this->loadModel('action')->create('gogs', $gogsID, 'edited');
  117. $changes = common::createChanges($oldGogs, $gogs);
  118. $this->action->logHistory($actionID, $changes);
  119. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true, 'closeModal' => true));
  120. }
  121. $this->view->title = $this->lang->gogs->common . $this->lang->hyphen . $this->lang->gogs->edit;
  122. $this->view->gogs = $oldGogs;
  123. $this->display();
  124. }
  125. /**
  126. * 删除一条gogs数据。
  127. * Delete a gogs.
  128. *
  129. * @param int $gogsID
  130. * @access public
  131. * @return void
  132. */
  133. public function delete($gogsID)
  134. {
  135. $oldGogs = $this->loadModel('pipeline')->fetchByID($gogsID);
  136. $actionID = $this->pipeline->deleteByObject($gogsID, 'gogs');
  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. $gogs = $this->pipeline->fetchByID($gogsID);
  144. $changes = common::createChanges($oldGogs, $gogs);
  145. $this->loadModel('action')->logHistory($actionID, $changes);
  146. $response['load'] = $this->createLink('space', 'browse');
  147. $response['result'] = 'success';
  148. return $this->send($response);
  149. }
  150. /**
  151. * 绑定gogs用户到禅道用户。
  152. * Bind gogs user to zentao users.
  153. *
  154. * @param int $gogsID
  155. * @param string $type
  156. * @access public
  157. * @return void
  158. */
  159. public function bindUser($gogsID, $type = 'all')
  160. {
  161. if($_POST)
  162. {
  163. $this->gogs->bindUser($gogsID, (array)$this->post->zentaoUsers, (array)$this->post->gogsUserNames);
  164. if(dao::isError()) return $this->sendError(dao::getError());
  165. return $this->sendSuccess(array('message' => $this->lang->saveSuccess, 'load' => helper::createLink('space', 'browse')));
  166. }
  167. $userList = array();
  168. $gogsUsers = $this->gogs->apiGetUsers($gogsID);
  169. $bindedUsers = $this->loadModel('pipeline')->getUserBindedPairs($gogsID, 'gogs');
  170. $matchedResult = $this->gogsZen->getMatchedUsers($gogsID, $gogsUsers);
  171. $zentaoUsers = $this->loadModel('user')->getRealNameAndEmails(helper::arrayColumn($matchedResult, 'zentaoAccount'));
  172. foreach($gogsUsers as $gogsUser)
  173. {
  174. $user = new stdclass();
  175. $user->email = '';
  176. $user->status = 'notBind';
  177. $user->gogsID = $gogsUser->id;
  178. $user->gogsEmail = $gogsUser->email;
  179. $user->gogsAccount = $gogsUser->account;
  180. $user->gogsUser = $gogsUser->realname . '@' . $gogsUser->account;
  181. $user->gogsUserAvatar = $gogsUser->avatar;
  182. $user->zentaoUsers = isset($matchedResult[$gogsUser->id]) ? $matchedResult[$gogsUser->id]->zentaoAccount : '';
  183. if($user->zentaoUsers)
  184. {
  185. if(isset($zentaoUsers[$user->zentaoUsers])) $user->email = $zentaoUsers[$user->zentaoUsers]->email;
  186. if(isset($bindedUsers[$user->zentaoUsers]) && $bindedUsers[$user->zentaoUsers] == $gogsUser->account) $user->status = 'binded';
  187. }
  188. if($type != 'all' && $user->status != $type) continue;
  189. $userList[] = $user;
  190. }
  191. $this->view->title = $this->lang->gogs->bindUser;
  192. $this->view->type = $type;
  193. $this->view->gogsID = $gogsID;
  194. $this->view->recTotal = count($userList);
  195. $this->view->userList = $userList;
  196. $this->view->userPairs = $this->user->getPairs('noclosed|noletter');
  197. $this->view->zentaoUsers = $zentaoUsers;
  198. $this->display();
  199. }
  200. /**
  201. * 获取分支列表。
  202. * Ajax get branches.
  203. *
  204. * @param int $gogsID
  205. * @param string $project
  206. * @access public
  207. * @return void
  208. */
  209. public function ajaxGetProjectBranches($gogsID, $project)
  210. {
  211. $options = array(array('text' => '', 'value' => ''));
  212. if(!$gogsID || !$project) return print(json_encode($options));
  213. $project = urldecode(base64_decode($project));
  214. $branches = $this->gogs->apiGetBranches($gogsID, $project);
  215. foreach($branches as $branch)
  216. {
  217. $options[] = array('text' => $branch->name, 'value' => $branch->name);
  218. }
  219. return print(json_encode($options));
  220. }
  221. }