control.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014
  1. <?php
  2. /**
  3. * The control file of repo 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 Yidong Wang, Jinyong Zhu
  8. * @package repo
  9. * @link https://www.zentao.net
  10. * @property repoModel $repo
  11. * @property repoZen $repoZen
  12. */
  13. class repo extends control
  14. {
  15. /**
  16. * Construct.
  17. *
  18. * @access public
  19. * @return void
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->scm = $this->app->loadClass('scm');
  25. $disFuncs = str_replace(' ', '', ini_get('disable_functions'));
  26. if(stripos(",$disFuncs,", ',exec,') !== false or stripos(",$disFuncs,", ',shell_exec,') !== false)
  27. {
  28. return $this->sendError($this->lang->repo->error->useless, true);
  29. }
  30. $this->projectID = $this->session->project ? $this->session->project : 0;
  31. /* Unlock session for wait to get data of repo. */
  32. session_write_close();
  33. }
  34. /**
  35. * Common actions.
  36. *
  37. * @param int $repoID
  38. * @param int $objectID projectID|executionID
  39. * @access public
  40. * @return void
  41. */
  42. public function commonAction($repoID = 0, $objectID = 0)
  43. {
  44. $fromModal = in_array($this->app->rawModule, array('git', 'svn'));
  45. $tab = $fromModal ? '' :$this->app->tab;
  46. $this->repos = $this->repo->getRepoPairs($tab, $objectID);
  47. if($tab == 'project')
  48. {
  49. $projects = $this->loadModel('project')->getPairsByProgram();
  50. $objectID = $this->project->checkAccess($objectID, $projects);
  51. $project = $this->project->getByID($objectID);
  52. if($project && $project->model === 'kanban') return $this->locate($this->createLink('project', 'index', "projectID=$objectID"));
  53. $this->loadModel('project')->setMenu($objectID);
  54. $this->view->projectID = $objectID;
  55. }
  56. elseif($tab == 'execution')
  57. {
  58. $executions = $this->loadModel('execution')->getPairs(0, 'all', "nocode,noprefix,multiple");
  59. $objectID = $this->execution->checkAccess($objectID, $executions);
  60. $execution = $this->execution->getByID($objectID);
  61. if($execution && $execution->type === 'kanban') return $this->locate($this->createLink('execution', 'kanban', "executionID=$objectID"));
  62. if($execution)
  63. {
  64. $features = $this->execution->getExecutionFeatures($execution);
  65. if(!$features['devops']) return print($this->locate($this->createLink('execution', 'task', "executionID=$objectID")));
  66. }
  67. $this->loadModel('execution')->setMenu($objectID);
  68. $this->view->executionID = $objectID;
  69. }
  70. elseif($tab != 'admin' && !$fromModal)
  71. {
  72. $this->repo->setMenu($this->repos, $repoID);
  73. }
  74. if(empty($this->repos) && !in_array(strtolower($this->methodName), array('create', 'setrules', 'createrepo', 'import', 'maintain')))
  75. {
  76. $method = $this->app->tab == 'devops' ? 'maintain' : 'create';
  77. if($this->config->inCompose && $method == 'create') $method = 'createRepo';
  78. return $this->locate(inLink($method, "objectID=$objectID"));
  79. }
  80. $this->view->fromModal = $fromModal;
  81. }
  82. /**
  83. * 版本库列表。
  84. * List all repo.
  85. *
  86. * @param int $objectID
  87. * @param string $orderBy
  88. * @param int $recPerPage
  89. * @param int $pageID
  90. * @param string $type
  91. * @param int $param
  92. * @access public
  93. * @return void
  94. */
  95. public function maintain($objectID = 0, $orderBy = 'id_desc', $recPerPage = 20, $pageID = 1, $type = '', $param = 0)
  96. {
  97. $repoID = $this->repo->saveState(0, $objectID);
  98. if($this->viewType !== 'json') $this->commonAction($repoID, $objectID);
  99. $repoList = $this->repo->getList(0, '', $orderBy, null, false, true, $type, $param);
  100. /* Pager. */
  101. $this->app->loadClass('pager', true);
  102. $recTotal = count($repoList);
  103. $pager = new pager($recTotal, $recPerPage, $pageID);
  104. $repoList = array_chunk($repoList, $pager->recPerPage);
  105. if($repoList && !isset($repoList[$pageID - 1])) $pageID = 1;
  106. $repoList = empty($repoList) ? array() : $repoList[$pageID - 1];
  107. /* Get success jobs of sonarqube.*/
  108. $sonarRepoList = $this->loadModel('job')->getSonarqubeByRepo(helper::arrayColumn($repoList, 'id'));
  109. $successJobs = $this->loadModel('compile')->getSuccessJobs(helper::arrayColumn($sonarRepoList, 'id'));
  110. $products = $this->loadModel('product')->getPairs('all', 0, '', 'all');
  111. $projects = $this->loadModel('project')->getPairs();
  112. $this->repoZen->buildRepoSearchForm($products, $projects, $objectID, $orderBy, $recPerPage, $pageID, $param);
  113. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->browse;
  114. $this->view->serverPairs = $this->loadModel('pipeline')->getPairs('gitlab');
  115. $this->view->type = $type;
  116. $this->view->orderBy = $orderBy;
  117. $this->view->objectID = $objectID;
  118. $this->view->pager = $pager;
  119. $this->view->repoList = $repoList;
  120. $this->view->products = $products;
  121. $this->view->projects = $projects;
  122. $this->view->sonarRepoList = $sonarRepoList;
  123. $this->view->successJobs = $successJobs;
  124. $this->view->repoServers = $this->pipeline->getPairs($this->config->pipeline->checkRepoServers);
  125. $this->display();
  126. }
  127. /**
  128. * 创建版本库(关联代码库)。
  129. * Create a repo(Associate with an existing repo).
  130. *
  131. * @param int $objectID projectID|executionID
  132. * @access public
  133. * @return void
  134. */
  135. public function create($objectID = 0)
  136. {
  137. if($_POST)
  138. {
  139. /* Prepare data. */
  140. $formData = form::data($this->config->repo->form->create);
  141. $isPipelineServer = in_array(strtolower($this->post->SCM), $this->config->repo->gitServiceList) ? true : false;
  142. $repo = $this->repoZen->prepareCreate($formData, $isPipelineServer);
  143. /* Create a repo. */
  144. if($repo) $repoID = $this->repo->create($repo, $isPipelineServer);
  145. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  146. if(in_array($this->post->SCM, $this->config->repo->notSyncSCM))
  147. {
  148. /* Add webhook. */
  149. $repo = $this->repo->getByID($repoID);
  150. $this->loadModel($this->post->SCM)->updateCodePath($repo->serviceHost, (int)$repo->serviceProject, (int)$repo->id);
  151. $this->repo->updateCommitDate($repoID);
  152. }
  153. $this->loadModel('action')->create('repo', $repoID, 'created');
  154. if($this->viewType == 'json') return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'id' => $repoID));
  155. $link = $this->repo->createLink('showSyncCommit', "repoID=$repoID&objectID=$objectID", '', false);
  156. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $link, 'callback' => "importJob($repoID)"));
  157. }
  158. $this->commonAction(0, $objectID);
  159. $this->repoZen->buildCreateForm($objectID);
  160. }
  161. /**
  162. * 创建版本库,同步创建远程版本库。
  163. * Create a repo.
  164. *
  165. * @param int $objectID projectID|executionID
  166. * @access public
  167. * @return void
  168. */
  169. public function createRepo($objectID = 0)
  170. {
  171. if($_POST)
  172. {
  173. /* Prepare data. */
  174. $formData = form::data($this->config->repo->form->createRepo)->get();
  175. $repo = $this->repoZen->prepareCreateRepo($formData);
  176. /* Create a repo. */
  177. if($repo) $repoID = $this->repo->createRepo($repo);
  178. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  179. if(in_array($formData->SCM, $this->config->repo->notSyncSCM))
  180. {
  181. /* Add webhook. */
  182. $repo = $this->repo->getByID($repoID);
  183. $this->loadModel($formData->SCM)->updateCodePath($repo->serviceHost, (int)$repo->serviceProject, (int)$repo->id);
  184. $this->repo->updateCommitDate($repoID);
  185. }
  186. $this->loadModel('action')->create('repo', $repoID, 'created');
  187. $link = $this->repo->createLink('showSyncCommit', "repoID=$repoID&objectID=$objectID");
  188. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => $link));
  189. }
  190. $this->commonAction(0, $objectID);
  191. $this->repoZen->buildCreateRepoForm($objectID);
  192. }
  193. /**
  194. * 根据任务和执行创建分支。
  195. * Create a branch by task and execution.
  196. *
  197. * @param int $objectID
  198. * @param int $repoID
  199. * @access public
  200. * @return void
  201. */
  202. public function createBranch($objectID, $repoID = 0)
  203. {
  204. $objectType = $this->app->rawModule;
  205. $object = $this->loadModel($objectType)->fetchByID($objectID);
  206. $productIds = array(zget($object, 'product', 0));
  207. if($objectType == 'task') $productIds = $this->loadModel('product')->getProductIDByProject($object->execution, false);
  208. $repoList = $this->repo->getListBySCM(implode(',', $this->config->repo->gitServiceTypeList), 'haspriv');
  209. $repoPairs = array();
  210. foreach($repoList as $repo)
  211. {
  212. $linkedProducts = explode(',', $repo->product);
  213. foreach($productIds as $productID)
  214. {
  215. if(in_array($productID, $linkedProducts)) $repoPairs[$repo->id] = $repo->name;
  216. }
  217. }
  218. if(!$repoPairs) return $this->send(array('result' => 'fail', 'message' => $this->lang->repo->error->noFound));
  219. if(!empty($_POST)) $repoID = (int)$this->post->codeRepo;
  220. if(!$repoID || !isset($repoPairs[$repoID])) $repoID = key($repoPairs);
  221. $this->scm->setEngine($repoList[$repoID]);
  222. if(!empty($_POST))
  223. {
  224. $branch = form::data($this->config->repo->form->createBranch)->get();
  225. $result = $this->scm->createBranch($branch->branchName, $branch->branchFrom);
  226. if($result['result'] == 'fail') return $this->sendError($this->lang->repo->error->createdFail . ': ' . $this->repoZen->parseErrorContent($result['message']));
  227. $this->repo->saveRelation($repoID, $branch->branchName, $objectID, $objectType);
  228. $this->loadModel('action')->create($objectType, $objectID, 'createRepoBranch', '', $branch->branchName);
  229. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'callback' => array('name' => 'loadModal', array($this->createLink($objectType, 'createBranch', "objectID={$objectID}")))));
  230. }
  231. $canCreate = $object->status == 'active';
  232. if($objectType == 'task') $canCreate = $object->status == 'wait' || $object->status == 'doing';
  233. $this->view->linkedBranches = $this->repo->getLinkedBranch($objectID, $objectType);
  234. $this->view->repoPairs = $repoPairs;
  235. $this->view->allRepos = $this->repo->getRepoPairs('repo', 0, false);
  236. $this->view->repoID = $repoID;
  237. $this->view->objectID = $objectID;
  238. $this->view->fromList = $this->repoZen->getBranchAndTagOptions($this->scm);
  239. $this->view->objectType = $objectType;
  240. $this->view->canCreate = $canCreate;
  241. $this->display();
  242. }
  243. /**
  244. * 取消代码分支的关联。
  245. * Unlink code branch.
  246. *
  247. * @access public
  248. * @return void
  249. */
  250. public function unlinkBranch()
  251. {
  252. $objectType = $this->app->rawModule;
  253. $branch = (string)$this->post->branch;
  254. $objectID = (int)$this->post->objectID;
  255. $repoID = (int)$this->post->repoID;
  256. $this->repo->unlinkObjectBranch($objectID, $objectType, $repoID, $branch);
  257. if(dao::isError()) return $this->sendError(dao::getError());
  258. $this->loadModel('action')->create($objectType, $objectID, 'unlinkRepoBranch', '', $branch);
  259. $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'callback' => 'loadModal("' . $this->createLink($objectType, 'createBranch', "objectID={$objectID}") . '")'));
  260. }
  261. /**
  262. * Edit a repo.
  263. *
  264. * @param int $repoID
  265. * @param int $objectID
  266. * @access public
  267. * @return void
  268. */
  269. public function edit($repoID, $objectID = 0)
  270. {
  271. $this->commonAction($repoID, $objectID);
  272. if($_POST)
  273. {
  274. $repo = $this->repo->getByID($repoID);
  275. /* Prepare data. */
  276. $formData = form::data($this->config->repo->form->edit);
  277. $isPipelineServer = in_array(strtolower($this->post->SCM), $this->config->repo->gitServiceList) ? true : false;
  278. $editData = $this->repoZen->prepareEdit($formData, $repo, $isPipelineServer);
  279. if($editData) $noNeedSync = $this->repo->update($editData, $repo, $isPipelineServer);
  280. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  281. $newRepo = $this->repo->getByID($repoID);
  282. $actionID = $this->loadModel('action')->create('repo', $repoID, 'edited');
  283. $changes = common::createChanges($repo, $newRepo);
  284. $this->action->logHistory($actionID, $changes);
  285. if(!$noNeedSync)
  286. {
  287. $link = $this->repo->createLink('showSyncCommit', "repoID=$repoID");
  288. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $link));
  289. }
  290. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('maintain')));
  291. }
  292. $this->repoZen->buildEditForm($repoID, $objectID);
  293. }
  294. /**
  295. * 删除代码库。
  296. * Delete repo.
  297. *
  298. * @param int $repoID
  299. * @access public
  300. * @return void
  301. */
  302. public function delete($repoID)
  303. {
  304. $error = $this->repoZen->checkDeleteError($repoID);
  305. if($error) return $this->send(array('result' => 'fail', 'callback' => 'zui.Modal.alert({content: {html: "' . $error . '"}})'));
  306. $this->repo->deleteRepo($repoID);
  307. if(dao::isError()) return $this->sendError(dao::getError());
  308. return $this->send(array('result' => 'success', 'load' => true));
  309. }
  310. /**
  311. * 用编辑器查看代码库文件。
  312. * View repo file with monaco editor.
  313. *
  314. * @param int $repoID
  315. * @param int $objectID
  316. * @param string $entry
  317. * @param string $revision
  318. * @param int $showBug
  319. * @param string $encoding
  320. * @access public
  321. * @return void
  322. */
  323. public function monaco($repoID, $objectID = 0, $entry = '', $revision = 'HEAD', $showBug = 0, $encoding = '')
  324. {
  325. $this->commonAction($repoID, $objectID);
  326. $file = $entry;
  327. $entry = $this->repo->decodePath($entry);
  328. $lines = '';
  329. if(strpos($entry, '#'))
  330. {
  331. $bugData = explode('#', $entry);
  332. $entry = $bugData[0];
  333. $lines = $bugData[1];
  334. }
  335. $entry = urldecode($entry);
  336. $pathInfo = helper::mbPathinfo($entry);
  337. $repo = $this->repo->getByID($repoID);
  338. if($repo->SCM == 'Gitlab') $repo = $this->repo->processGitService($repo, true);
  339. $dropMenus = array();
  340. if(in_array($repo->SCM, $this->config->repo->gitTypeList)) $dropMenus = $this->repoZen->getBranchAndTagItems($repo, $this->cookie->repoBranch);
  341. if($this->app->tab == 'execution') $this->view->executionID = $objectID;
  342. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->view;
  343. $this->view->dropMenus = $dropMenus;
  344. $this->view->type = 'view';
  345. $this->view->branchID = $this->cookie->repoBranch;
  346. $this->view->showBug = $showBug;
  347. $this->view->encoding = $encoding;
  348. $this->view->repoID = $repoID;
  349. $this->view->objectID = $objectID;
  350. $this->view->repo = $repo;
  351. $this->view->revision = $revision;
  352. $this->view->file = $file;
  353. $this->view->lines = $lines;
  354. $this->view->entry = $entry;
  355. $this->view->pathInfo = $pathInfo;
  356. $this->view->tree = $this->repoZen->getViewTree($repo, '', $revision);
  357. $this->display();
  358. }
  359. /**
  360. * 查看代码文件。
  361. * View repo file.
  362. *
  363. * @param int $repoID
  364. * @param int $objectID
  365. * @param string $entry
  366. * @param string $revision
  367. * @param int $showBug
  368. * @param string $encoding
  369. * @access public
  370. * @return void
  371. */
  372. public function view($repoID, $objectID = 0, $entry = '', $revision = 'HEAD', $showBug = 0, $encoding = '')
  373. {
  374. set_time_limit(0);
  375. if($this->get->repoPath) $entry = $this->get->repoPath;
  376. if($repoID == 0) $repoID = $this->session->repoID;
  377. if($revision != 'HEAD')
  378. {
  379. helper::setCookie("repoBranch", $revision, $this->config->cookieLife, $this->config->webRoot, '', false, false);
  380. $this->cookie->set('repoBranch', $revision);
  381. }
  382. $this->commonAction($repoID, $objectID);
  383. $this->repoZen->setBackSession('view', true);
  384. $this->session->set('storyList', inlink('view', "repoID=$repoID&objectID=$objectID&entry=$entry&revision=$revision&showBug=$showBug&encoding=$encoding"), 'product');
  385. return print($this->fetch('repo', 'monaco', "repoID=$repoID&objectID=$objectID&entry=$entry&revision=$revision&showBug=$showBug&encoding=$encoding"));
  386. }
  387. /**
  388. * 代码库目录树及提交信息页面。
  389. * Browse repo.
  390. *
  391. * @param int $repoID
  392. * @param string $branchID
  393. * @param int $objectID
  394. * @param string $path
  395. * @param string $revision
  396. * @param int $refresh
  397. * @param string $branchOrTag branch|tag
  398. * @param string $type dir|file
  399. * @param int $recTotal
  400. * @param int $recPerPage
  401. * @param int $pageID
  402. * @access public
  403. * @return void
  404. */
  405. public function browse($repoID = 0, $branchID = '', $objectID = 0, $path = '', $revision = 'HEAD', $refresh = 0, $branchOrTag = 'branch', $type = 'dir', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  406. {
  407. $repoID = $this->repo->saveState($repoID, $objectID);
  408. /* Get path. */
  409. if($this->get->repoPath) $path = $this->get->repoPath;
  410. $path = $this->repo->decodePath($path);
  411. if($_POST)
  412. {
  413. $oldRevision = isset($this->post->revision[1]) ? $this->post->revision[1] : '';
  414. $newRevision = isset($this->post->revision[0]) ? $this->post->revision[0] : '';
  415. return $this->locate($this->repo->createLink('diff', "repoID=$repoID&objectID=$objectID&entry=" . $this->repo->encodePath($path) . "&oldrevision=$oldRevision&newRevision=$newRevision"));
  416. }
  417. /* Set menu and session. */
  418. $this->commonAction($repoID, $objectID);
  419. $this->repoZen->setBrowseSession();
  420. /* Get repo and synchronous commit. */
  421. $repo = $this->repo->getByID($repoID);
  422. if($repo->SCM == 'Git' && !is_dir($repo->path)) return $this->sendError(sprintf($this->lang->repo->error->notFound, $repo->name, $repo->path), $this->repo->createLink('maintain'));
  423. if($this->repoZen->checkRepoInternet($repo)) return $this->sendError($this->lang->repo->error->connect, true);
  424. if(!$repo->synced) return $this->locate($this->repo->createLink('showSyncCommit', "repoID=$repoID&objectID=$objectID"));
  425. /* Set branch or tag for git. */
  426. $branchID = $branchID ? base64_decode(helper::safe64Decode($branchID)) : '';
  427. list($branchID, $branches, $tags) = $this->repoZen->setBranchTag($repo, $branchID);
  428. if($this->app->tab == 'devops' && $repo->SCM != 'Subversion' && empty($branches)) return $this->sendError($this->lang->repo->error->empty, true);
  429. /* Refresh repo. */
  430. $refresh = $refresh || $this->cookie->repoRefresh;
  431. if($refresh)
  432. {
  433. helper::setcookie('repoRefresh', 0);
  434. $this->repo->updateCommit($repoID, $objectID, $branchID);
  435. if($repo->SCM == 'Gitlab') $this->repo->checkDeletedBranches($repoID, $branches);
  436. }
  437. /* Get revisions. */
  438. $this->app->loadClass('pager', true);
  439. $pager = new pager($recTotal, $recPerPage, $pageID);
  440. $revisions = $this->repoZen->getCommits($repo, $path, $revision, $type, $pager, $objectID);
  441. $lastRevision = empty($revisions) ? new stdclass() : current($revisions);
  442. if($path == '') $this->repoZen->updateLastCommit($repo, $lastRevision);
  443. /* Get files info. */
  444. $base64BranchID = helper::safe64Encode(base64_encode($branchID));
  445. $infos = $this->repoZen->getFilesInfo($repo, $path, $branchID, $base64BranchID, $objectID);
  446. /* Synchronous commit only in root path. */
  447. if(in_array($repo->SCM, $this->config->repo->gitTypeList) && $repo->SCM != 'Gitlab' && empty($path) && $infos && empty($revisions)) $this->locate($this->repo->createLink('showSyncCommit', "repoID=$repoID&objectID=$objectID&branch=" . helper::safe64Encode(base64_encode($this->cookie->repoBranch))));
  448. $this->view->title = $this->lang->repo->common;
  449. $this->view->repo = $repo;
  450. $this->view->revisions = $revisions;
  451. $this->view->revision = $revision;
  452. $this->view->lastRevision = $lastRevision;
  453. $this->view->infos = $infos;
  454. $this->view->repoID = $repoID;
  455. $this->view->branches = $branches;
  456. $this->view->tags = $tags;
  457. $this->view->branchID = $branchID;
  458. $this->view->base64BranchID = $base64BranchID;
  459. $this->view->objectID = $objectID;
  460. $this->view->pager = $pager;
  461. $this->view->path = urldecode($path);
  462. $this->view->logType = $type;
  463. $this->view->cloneUrl = $this->repo->getCloneUrl($repo);
  464. $this->view->repoPairs = $this->repo->getRepoPairs($this->app->tab, $objectID);
  465. $this->view->branchOrTag = $branchOrTag;
  466. $this->display();
  467. }
  468. /**
  469. * 代码提交记录列表。
  470. * show repo log.
  471. *
  472. * @param int $repoID
  473. * @param string $branchID
  474. * @param int $objectID
  475. * @param string $entry
  476. * @param string $source
  477. * @param string $browseType
  478. * @param int $param
  479. * @param int $recTotal
  480. * @param int $recPerPage
  481. * @param int $pageID
  482. * @access public
  483. * @return void
  484. */
  485. public function log($repoID = 0, $branchID = '', $objectID = 0, $entry = '', $source = 'log', $browseType = 'list', $param = 0, $recTotal = 0, $recPerPage = 50, $pageID = 1)
  486. {
  487. $repoID = $this->repo->saveState($repoID, $objectID);
  488. $this->commonAction($repoID, $objectID);
  489. if($this->get->repoPath) $entry = $this->get->repoPath;
  490. $this->repoZen->setBackSession('log', true);
  491. if($repoID == 0) $repoID = $this->session->repoID;
  492. $repo = $this->repo->getByID($repoID);
  493. $file = $entry;
  494. $entry = $this->repo->decodePath($entry);
  495. $this->app->loadClass('pager', true);
  496. $pager = new pager($recTotal, $recPerPage, $pageID);
  497. $pager->recPerPage = $recPerPage;
  498. if($_POST)
  499. {
  500. $oldRevision = isset($this->post->revision[1]) ? $this->post->revision[1] : '';
  501. $newRevision = isset($this->post->revision[0]) ? $this->post->revision[0] : '';
  502. $this->locate($this->repo->createLink('diff', "repoID=$repoID&objectID=$objectID&entry=" . $this->repo->encodePath($file) . "&oldrevision=$oldRevision&newRevision=$newRevision"));
  503. }
  504. /* Set branch or tag for git. */
  505. $branchID = $branchID ? base64_decode(helper::safe64Decode($branchID)) : '';
  506. list($branchID, $branches, $tags) = $this->repoZen->setBranchTag($repo, $branchID);
  507. if($this->app->tab == 'devops' && $repo->SCM != 'Subversion' && empty($branches)) return $this->sendError($this->lang->repo->error->empty, true);
  508. /* Build the search form. */
  509. $browseType = strtolower($browseType);
  510. $queryID = $browseType == 'bysearch' ? $param : 0;
  511. $branchID = helper::safe64Encode(base64_encode($branchID));
  512. $actionURL = $this->createLink('repo', 'log', "repoID={$repoID}&branchID={$branchID}&objectID={$objectID}&entry=&source={$source}&browseType=bySearch&param=myQueryID");
  513. $this->repoZen->buildSearchForm($queryID, $actionURL);
  514. $this->commonAction($repoID, $objectID);
  515. $query = $browseType == 'bysearch' ? $this->repoZen->getSearchForm($queryID, !in_array($repo->SCM, $this->config->repo->notSyncSCM)) : null;
  516. $logs = $this->repo->getCommits($repo, $entry, $branchID, 'dir', $pager, '', '', $query);
  517. if(count($logs) == 0 && $pageID != 1) $this->locate(inLink('log', "repoID=$repoID&branchID=$branchID&objectID=$objectID&entry=$entry&source=$source&browseType=$browseType&param=$param&recTotal=0&recPerPage=$recPerPage&pageID=1"));
  518. $revisionIds = array_column($logs, 'revision');
  519. $modelCommits = new stdClass();
  520. $modelCommits->stories = $this->loadModel('story')->getLinkedCommits($repoID, $revisionIds);
  521. $modelCommits->designs = $this->loadModel('design')->getLinkedCommits($repoID, $revisionIds);
  522. $modelCommits->tasks = $this->loadModel('task')->getLinkedCommits($repoID, $revisionIds);
  523. $modelCommits->bugs = $this->loadModel('bug')->getLinkedCommits($repoID, $revisionIds);
  524. /* Set tips and buttons for different relations. */
  525. foreach($logs as $logItem)
  526. {
  527. $logItem->relationFieldTips = '';
  528. foreach(array('designs' => 'design', 'stories' => 'story', 'tasks' => 'task' , 'bugs' => 'bug') as $fieldType => $moduleName)
  529. {
  530. if(!empty($modelCommits->{$fieldType}[$logItem->revision]))
  531. {
  532. $fieldCommits = $modelCommits->{$fieldType}[$logItem->revision];
  533. $logItem->relationFieldTips .= ' ' . $this->lang->repo->{$moduleName};
  534. foreach($fieldCommits as $item) $item->url = !empty($item->id) ? $this->createLink($moduleName, 'view', "{$moduleName}ID=" . $item->id) : '';
  535. $logItem->relationFieldTips .= ' #'.implode(' #', array_column($fieldCommits, 'id'));
  536. $logItem->relationField[$fieldType] = $fieldCommits;
  537. }
  538. }
  539. }
  540. $this->view->repo = $repo;
  541. $this->view->title = $this->lang->repo->common;
  542. $this->view->logs = $logs;
  543. $this->view->repoID = $repoID;
  544. $this->view->objectID = $objectID;
  545. $this->view->branchID = $this->cookie->repoBranch;
  546. $this->view->entry = urldecode($entry);
  547. $this->view->path = urldecode($entry);
  548. $this->view->file = urldecode($file);
  549. $this->view->pager = $pager;
  550. $this->view->repoPairs = $this->repo->getRepoPairs($this->app->tab, $objectID);
  551. $this->view->branches = $branches;
  552. $this->view->tags = $tags;
  553. $this->view->source = $source;
  554. $this->view->browseType = $browseType;
  555. $this->view->param = $param;
  556. $this->display();
  557. }
  558. /**
  559. * 单个代码提交记录。
  560. * Show repo revision.
  561. *
  562. * @param int $repoID
  563. * @param int $objectID
  564. * @param string $revision
  565. * @access public
  566. * @return void
  567. */
  568. public function revision($repoID, $objectID = 0, $revision = '')
  569. {
  570. if($repoID == 0) $repoID = $this->session->repoID;
  571. $repo = $this->repo->getByID($repoID);
  572. $this->scm->setEngine($repo);
  573. $log = $this->scm->log('', $revision, $revision);
  574. $revision = !empty($log[0]) ? $this->repo->getHistoryRevision($repoID, (string)$log[0]->revision) : '';
  575. if($revision)
  576. {
  577. if(in_array($repo->SCM, $this->config->repo->gitTypeList))
  578. {
  579. $thisAndPrevRevisions = $this->scm->exec("rev-list -n 2 {$revision} --");
  580. array_shift($thisAndPrevRevisions);
  581. if($thisAndPrevRevisions) $oldRevision = array_shift($thisAndPrevRevisions);
  582. }
  583. else
  584. {
  585. $oldRevision = $this->repo->getHistoryRevision($repoID, $revision, false, 'lt');
  586. }
  587. }
  588. if(empty($oldRevision))
  589. {
  590. $oldRevision = '^';
  591. if($revision && in_array($repo->SCM, $this->config->repo->gitTypeList)) $oldRevision = "{$revision}^";
  592. }
  593. $this->locate($this->repo->createLink('diff', "repoID=$repoID&objectID=$objectID&entry=&oldrevision=$oldRevision&newRevision={$log[0]->revision}"));
  594. }
  595. /**
  596. * 代码blame信息。
  597. * Blame repo file.
  598. *
  599. * @param int $repoID
  600. * @param int $objectID
  601. * @param string $entry
  602. * @param string $revision
  603. * @param string $encoding
  604. * @access public
  605. * @return void
  606. */
  607. public function blame($repoID, $objectID = 0, $entry = '', $revision = 'HEAD', $encoding = '')
  608. {
  609. $this->commonAction($repoID, $objectID);
  610. if($this->get->repoPath) $entry = $this->get->repoPath;
  611. if($repoID == 0) $repoID = $this->session->repoID;
  612. $repo = $this->repo->getByID($repoID);
  613. $file = $entry;
  614. $entry = $this->repo->decodePath($entry);
  615. $this->scm->setEngine($repo);
  616. $encoding = empty($encoding) ? $repo->encoding : $encoding;
  617. $encoding = strtolower(str_replace('_', '-', $encoding));
  618. $blames = $this->scm->blame($entry, $revision);
  619. $revisions = array();
  620. foreach($blames as $i => $blame)
  621. {
  622. if(isset($blame['revision'])) $revisions[$blame['revision']] = $blame['revision'];
  623. if($encoding != 'utf-8') $blames[$i]['content'] = helper::convertEncoding($blame['content'], $encoding);
  624. }
  625. $log = in_array($repo->SCM, $this->config->repo->gitTypeList) ? $this->repo->getHistoryRevision($repo->id, $revision, true) : '';
  626. $this->view->title = $this->lang->repo->common;
  627. $this->view->repoID = $repoID;
  628. $this->view->branchID = (string)$this->cookie->repoBranch;
  629. $this->view->objectID = $objectID;
  630. $this->view->repo = $repo;
  631. $this->view->revision = $revision;
  632. $this->view->entry = $entry;
  633. $this->view->file = $file;
  634. $this->view->encoding = str_replace('-', '_', $encoding);
  635. $this->view->revisionName = ($log && in_array($repo->SCM, $this->config->repo->gitTypeList)) ? $this->repo->getGitRevisionName($log->revision, $log->commit) : $revision;
  636. $this->view->blames = $blames;
  637. $this->display();
  638. }
  639. /**
  640. * 代码diff信息。
  641. * Show repo diff.
  642. *
  643. * @param int $repoID
  644. * @param int $objectID
  645. * @param string $entry
  646. * @param string $oldRevision
  647. * @param string $newRevision
  648. * @param int $showBug
  649. * @param string $encoding
  650. * @param int $isBranchOrTag
  651. * @access public
  652. * @return void
  653. */
  654. public function diff($repoID, $objectID = 0, $entry = '', $oldRevision = '', $newRevision = '', $showBug = 0, $encoding = '', $isBranchOrTag = 0)
  655. {
  656. $newRevision = strtr($newRevision, '*', '-');
  657. $oldRevision = strtr($oldRevision, '*', '-');
  658. $oldRevision = urldecode(urldecode($oldRevision)); //Fix error.
  659. if($isBranchOrTag)
  660. {
  661. $oldRevision = urldecode(helper::safe64Decode($oldRevision));
  662. $newRevision = urldecode(helper::safe64Decode($newRevision));
  663. }
  664. $this->commonAction($repoID, $objectID);
  665. $repo = $this->repo->getByID($repoID);
  666. if($this->get->repoPath) $entry = $this->get->repoPath;
  667. $file = $entry;
  668. $entry = $this->repo->decodePath($entry);
  669. $lines = '';
  670. if(strpos($entry, '#'))
  671. {
  672. $bugData = explode('#', $entry);
  673. $entry = $bugData[0];
  674. $lines = $bugData[1];
  675. }
  676. $this->config->repo->notSyncSCM[] = 'Subversion';
  677. if(!in_array($repo->SCM, $this->config->repo->notSyncSCM) && !is_dir($repo->path)) return $this->sendError(sprintf($this->lang->repo->error->notFound, $repo->name, $repo->path), $this->repo->createLink('maintain'));
  678. $arrange = $this->cookie->arrange ? $this->cookie->arrange : 'inline';
  679. if($this->server->request_method == 'POST') return $this->repoZen->locateDiffPage($repoID, $objectID, $arrange, $isBranchOrTag, $file);
  680. $diffs = array();
  681. $encoding = empty($encoding) ? $repo->encoding : $encoding;
  682. $encoding = strtolower(str_replace('_', '-', $encoding));
  683. if($oldRevision !== '')
  684. {
  685. $this->scm->setEngine($repo);
  686. $diffs = $this->scm->diff($entry, $oldRevision, $newRevision, 'yes', $isBranchOrTag ? 'isBranchOrTag': '');
  687. }
  688. if($encoding != 'utf-8') $diffs = $this->repoZen->encodingDiff($diffs, $encoding);
  689. if($arrange == 'appose') $diffs = $this->repo->getApposeDiff($diffs);
  690. $this->view->entry = urldecode($entry);
  691. $this->view->encoding = str_replace('-', '_', $encoding);
  692. $this->view->file = $file;
  693. $this->view->lines = $lines;
  694. $this->view->repoID = $repoID;
  695. $this->view->branchID = (string) $this->cookie->repoBranch;
  696. $this->view->objectID = $objectID;
  697. $this->view->repo = $repo;
  698. $this->view->diffs = $diffs;
  699. $this->view->newRevision = $newRevision;
  700. $this->view->oldRevision = $oldRevision;
  701. $this->view->isBranchOrTag = $isBranchOrTag;
  702. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->diff;
  703. $this->display();
  704. }
  705. /**
  706. * 代码下载。
  707. * Download repo file.
  708. *
  709. * @param int $repoID
  710. * @param string $path
  711. * @param string $fromRevision
  712. * @param string $toRevision
  713. * @param string $type
  714. * @param int $isBranchOrTag
  715. * @access public
  716. * @return void
  717. */
  718. public function download($repoID, $path, $fromRevision = 'HEAD', $toRevision = '', $type = 'file', $isBranchOrTag = 0)
  719. {
  720. if($this->get->repoPath) $path = $this->get->repoPath;
  721. $entry = $this->repo->decodePath($path);
  722. $repo = $this->repo->getByID($repoID);
  723. if($isBranchOrTag)
  724. {
  725. $fromRevision = urldecode(helper::safe64Decode($fromRevision));
  726. $toRevision = urldecode(helper::safe64Decode($toRevision));
  727. }
  728. $this->commonAction($repoID);
  729. $this->scm->setEngine($repo);
  730. if($type === 'file')
  731. {
  732. $content = $this->scm->cat($entry, $fromRevision);
  733. }
  734. else
  735. {
  736. $content = $this->scm->diff($entry, $fromRevision, $toRevision, 'patch', $isBranchOrTag ? 'isBranchOrTag': '');
  737. }
  738. $fileName = basename(urldecode($entry));
  739. if($type != 'file') $fileName .= "r$fromRevision--r$toRevision.patch";
  740. $extension = strpos($fileName, '.') ? strrchr($fileName, '.') : '';
  741. $extension = ltrim($extension, '.');
  742. $this->fetch('file', 'sendDownHeader', array("fileName" => $fileName, "fileType" => $extension, "content" => $content));
  743. }
  744. /**
  745. * 设置DevOps指令.
  746. * Set Rules.
  747. *
  748. * @access public
  749. * @return void
  750. */
  751. public function setRules()
  752. {
  753. if($_POST)
  754. {
  755. $this->loadModel('setting')->setItem('system.repo.rules', json_encode($this->post->rules));
  756. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'load' => true));
  757. }
  758. $this->app->loadLang('task');
  759. $this->app->loadLang('bug');
  760. $this->app->loadLang('story');
  761. if(is_string($this->config->repo->rules)) $this->config->repo->rules = json_decode($this->config->repo->rules, true);
  762. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->setRules;
  763. $this->display();
  764. }
  765. /**
  766. * 显示提交同步进度。
  767. * Show sync commit.
  768. *
  769. * @param int $repoID
  770. * @param int $objectID projectID|executionID
  771. * @param string $branch
  772. * @access public
  773. * @return void
  774. */
  775. public function showSyncCommit($repoID = 0, $objectID = 0, $branch = '')
  776. {
  777. $this->commonAction($repoID, $objectID);
  778. if($repoID == 0) $repoID = $this->session->repoID;
  779. if($branch) $branch = base64_decode(helper::safe64Decode($branch));
  780. $latestInDB = $this->repo->getLatestCommit($repoID);
  781. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->showSyncCommit;
  782. $this->view->version = $latestInDB ? (int)$latestInDB->commit : 1;
  783. $this->view->repoID = $repoID;
  784. $this->view->repo = $this->repo->getByID($repoID);
  785. $this->view->objectID = $objectID;
  786. $this->view->branch = $branch;
  787. $this->view->browseLink = $this->repo->createLink('browse', "repoID=" . ($this->app->tab == 'devops' ? $repoID : '') . "&branchID=" . helper::safe64Encode(base64_encode($branch)) . "&objectID=$objectID", '', false) . "#app={$this->app->tab}";
  788. $this->display();
  789. }
  790. /**
  791. * 根据提交信息关联需求。
  792. * Link story to commit.
  793. *
  794. * @param int $repoID
  795. * @param string $revision
  796. * @param string $browseType
  797. * @param int $param
  798. * @param string $orderBy
  799. * @param int $recTotal
  800. * @param int $recPerPage
  801. * @param int $pageID
  802. * @access public
  803. * @return void
  804. */
  805. public function linkStory($repoID, $revision, $browseType = '', $param = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 10, $pageID = 1)
  806. {
  807. if(!empty($_POST['stories'])) return $this->send($this->repoZen->linkObject($repoID, $revision, 'story'));
  808. $this->loadModel('story');
  809. $this->loadModel('release');
  810. $this->app->loadLang('productplan');
  811. $repo = $this->repo->getByID($repoID);
  812. $productIds = explode(',', $repo->product);
  813. $products = $this->loadModel('product')->getByIdList($productIds);
  814. $modules = $this->repoZen->getLinkModules($products, 'story');
  815. $queryID = $browseType == 'bySearch' ? (int)$param : 0;
  816. /* Load pager. */
  817. $this->app->loadClass('pager', true);
  818. $pager = new pager($recTotal, $recPerPage, $pageID);
  819. /* Build search form. */
  820. $this->repoZen->buildStorySearchForm($repoID, $revision, $browseType, $queryID, $products, $modules);
  821. $this->view->modules = $modules;
  822. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  823. $this->view->allStories = $this->repoZen->getLinkStories($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID);
  824. $this->view->repoID = $repoID;
  825. $this->view->revision = $revision;
  826. $this->view->browseType = $browseType;
  827. $this->view->param = $param;
  828. $this->view->orderBy = $orderBy;
  829. $this->view->pager = $pager;
  830. $this->display();
  831. }
  832. /**
  833. * 根据提交信息关联Bug。
  834. * Link bug to commit.
  835. *
  836. * @param int $repoID
  837. * @param string $revision
  838. * @param string $browseType
  839. * @param int $param
  840. * @param string $orderBy
  841. * @param int $recTotal
  842. * @param int $recPerPage
  843. * @param int $pageID
  844. * @access public
  845. * @return void
  846. */
  847. public function linkBug($repoID, $revision = '', $browseType = '', $param = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 10, $pageID = 1)
  848. {
  849. if(!empty($_POST['bugs'])) return $this->send($this->repoZen->linkObject($repoID, $revision, 'bug'));
  850. $this->loadModel('bug');
  851. $this->loadModel('release');
  852. $this->app->loadLang('productplan');
  853. $repo = $this->repo->getByID($repoID);
  854. $productIds = explode(',', $repo->product);
  855. $products = $this->loadModel('product')->getByIdList($productIds);
  856. $modules = $this->repoZen->getLinkModules($products, 'bug');
  857. $queryID = ($browseType == 'bysearch') ? (int)$param : 0;
  858. /* Load pager. */
  859. $this->app->loadClass('pager', true);
  860. $pager = new pager($recTotal, $recPerPage, $pageID);
  861. /* Build search form. */
  862. $this->repoZen->buildBugSearchForm($repoID, $revision, $browseType, $queryID, $products, $modules);
  863. $this->view->modules = $modules;
  864. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  865. $this->view->allBugs = $this->repoZen->getLinkBugs($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID);
  866. $this->view->repoID = $repoID;
  867. $this->view->revision = $revision;
  868. $this->view->browseType = $browseType;
  869. $this->view->param = $param;
  870. $this->view->orderBy = $orderBy;
  871. $this->view->pager = $pager;
  872. $this->display();
  873. }
  874. /**
  875. * 根据提交信息关联任务。
  876. * Link task to commit.
  877. *
  878. * @param int $repoID
  879. * @param string $revision
  880. * @param string $browseType
  881. * @param int $param
  882. * @param string $orderBy
  883. * @param int $recTotal
  884. * @param int $recPerPage
  885. * @param int $pageID
  886. * @access public
  887. * @return void
  888. */
  889. public function linkTask($repoID, $revision = '', $browseType = 'unclosed', $param = 0, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 10, $pageID = 1)
  890. {
  891. if(!empty($_POST['tasks'])) return $this->send($this->repoZen->linkObject($repoID, $revision, 'task'));
  892. $this->loadModel('execution');
  893. $this->loadModel('product');
  894. $this->app->loadLang('task');
  895. /* Set browse type. */
  896. $browseType = strtolower($browseType);
  897. $repo = $this->repo->getByID($repoID);
  898. $products = $this->loadModel('product')->getByIdList(explode(',', $repo->product));
  899. $modules = $this->repoZen->getLinkModules($products, 'task');
  900. $queryID = ($browseType == 'bysearch') ? (int)$param : 0;
  901. /* Load pager. */
  902. $this->app->loadClass('pager', true);
  903. $pager = new pager($recTotal, $recPerPage, $pageID);
  904. /* Get executions by product. */
  905. $executionPairs = $this->repoZen->getLinkExecutions($products);
  906. /* Build search form. */
  907. $this->repoZen->buildTaskSearchForm($repoID, $revision, $browseType, $queryID, $modules, $executionPairs);
  908. $this->view->modules = $modules;
  909. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  910. $this->view->allTasks = $this->repoZen->getLinkTasks($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID, $executionPairs);
  911. $this->view->repoID = $repoID;
  912. $this->view->revision = $revision;
  913. $this->view->browseType = $browseType;
  914. $this->view->param = $param;
  915. $this->view->orderBy = $orderBy;
  916. $this->view->pager = $pager;
  917. $this->display();
  918. }
  919. /**
  920. * 取消提交信息的关联记录。
  921. * Unlink object and commit revision.
  922. *
  923. * @param int $repoID
  924. * @param string $revision
  925. * @param string $objectType story|task|bug
  926. * @param int $objectID
  927. * @access public
  928. * @return void
  929. */
  930. public function unlink($repoID, $revision, $objectType, $objectID)
  931. {
  932. $this->repo->unlink($repoID, $revision, $objectType, $objectID);
  933. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  934. return $this->send(array('result' => 'success', 'revision' => $revision));
  935. }
  936. /**
  937. * 导入版本库。
  938. * Import repos.
  939. *
  940. * @param int $serverID
  941. * @access public
  942. * @return void
  943. */
  944. public function import($serverID = 0)
  945. {
  946. if($this->viewType !== 'json') $this->commonAction();
  947. $serverList = $this->loadModel('pipeline')->getPairs(implode(',', $this->config->repo->notSyncSCM), true);
  948. if(!$serverID) $serverID = key($serverList);
  949. if($_POST)
  950. {
  951. if($this->post->product)
  952. {
  953. $repos = form::batchData($this->config->repo->form->import)->get();
  954. if($repos) $this->repo->batchCreate($repos, $serverID, (string)$this->post->serverType);
  955. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  956. }
  957. return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => $this->repo->createLink('maintain')));
  958. }
  959. $server = $this->pipeline->getByID($serverID);
  960. $hiddenRepos = $this->loadModel('setting')->getItem('owner=system&module=repo&section=hiddenRepo&key=' . $serverID);
  961. $linkUser = $server ? $this->pipeline->getOpenIdByAccount($server->id, $server->type, $this->app->user->account) : array();
  962. $repoList = $server && ($linkUser || $this->app->user->admin) ? $this->repoZen->getNotExistRepos($server) : array();
  963. $products = $this->loadModel('product')->getPairs('', 0, '', 'all');
  964. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->importAction;
  965. $this->view->servers = $serverList;
  966. $this->view->products = $products;
  967. $this->view->server = $server;
  968. $this->view->repoList = array_values($repoList);
  969. $this->view->hiddenRepos = explode(',', $hiddenRepos);
  970. $this->display();
  971. }
  972. /**
  973. * 获取代码对比编辑器内容。
  974. * Get diff editor content by ajax.
  975. *
  976. * @param int $repoID
  977. * @param int $objectID
  978. * @param string $entry
  979. * @param string $oldRevision
  980. * @param string $newRevision
  981. * @param int $showBug // Used for biz.
  982. * @param string $encoding
  983. * @access public
  984. * @return void
  985. */
  986. public function ajaxGetDiffEditorContent($repoID, $objectID = 0, $entry = '', $oldRevision = '', $newRevision = '', $showBug = 0, $encoding = '')
  987. {
  988. if(!$entry) $entry = (string) $this->cookie->repoCodePath;
  989. $file = $entry;
  990. $repo = $this->repo->getByID($repoID);
  991. $entry = urldecode($this->repo->decodePath($entry));
  992. $revision = str_replace('*', '-', $oldRevision);
  993. $nRevision = str_replace('*', '-', $newRevision);
  994. $entry = urldecode($entry);
  995. $pathInfo = pathinfo($entry);
  996. $encoding = empty($encoding) ? $repo->encoding : $encoding;
  997. $encoding = strtolower(str_replace('_', '-', $encoding));
  998. $lines = '';
  999. if(strpos($entry, '#'))
  1000. {
  1001. $bugData = explode('#', $entry);
  1002. $entry = $bugData[0];
  1003. $lines = $bugData[1];
  1004. }
  1005. $this->scm->setEngine($repo);
  1006. $info = $this->scm->info($entry, $nRevision);
  1007. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->diff;
  1008. $this->view->type = 'diff';
  1009. $this->view->encoding = str_replace('-', '_', $encoding);
  1010. $this->view->repoID = $repoID;
  1011. $this->view->objectID = $objectID;
  1012. $this->view->repo = $repo;
  1013. $this->view->revision = $nRevision;
  1014. $this->view->oldRevision = $revision;
  1015. $this->view->file = $file;
  1016. $this->view->lines = $lines;
  1017. $this->view->entry = $entry;
  1018. $this->view->info = $info;
  1019. $this->view->content = '';
  1020. $this->view->pathInfo = $pathInfo;
  1021. $this->view->suffix = 'c';
  1022. $this->view->blames = array();
  1023. $this->view->showEditor = true;
  1024. $this->display('repo', 'ajaxgeteditorcontent');
  1025. }
  1026. /**
  1027. * 获取代码详情的编辑器内容。
  1028. * Get editor content by ajax.
  1029. *
  1030. * @param int $repoID
  1031. * @param int $objectID
  1032. * @param string $entry
  1033. * @param string $revision
  1034. * @param int $showBug
  1035. * @param string $encoding
  1036. * @access public
  1037. * @return void
  1038. */
  1039. public function ajaxGetEditorContent($repoID, $objectID = 0, $entry = '', $revision = 'HEAD', $showBug = 0, $encoding = '')
  1040. {
  1041. if(!$entry) $entry = (string) $this->cookie->repoCodePath;
  1042. $file = $entry;
  1043. $repo = $this->repo->getByID($repoID);
  1044. $entry = urldecode($this->repo->decodePath($entry));
  1045. $revision = str_replace('*', '-', $revision);
  1046. $lines = '';
  1047. if(strpos($entry, '#'))
  1048. {
  1049. $bugData = explode('#', $entry);
  1050. $entry = $bugData[0];
  1051. $lines = $bugData[1];
  1052. $file = $this->repo->encodePath($entry);
  1053. }
  1054. $this->scm->setEngine($repo);
  1055. $info = $this->scm->info($entry, $revision);
  1056. $path = $entry ? $info->path : '';
  1057. if($info->kind == 'dir') $this->locate($this->repo->createLink('browse', "repoID=$repoID&branchID=&objectID=$objectID&path=" . $this->repo->encodePath($path) . "&revision=$revision"));
  1058. $content = $this->scm->cat($entry, $revision);
  1059. $entry = urldecode($entry);
  1060. $pathInfo = pathinfo($entry);
  1061. $encoding = empty($encoding) ? $repo->encoding : $encoding;
  1062. $encoding = strtolower(str_replace('_', '-', $encoding));
  1063. $suffix = '';
  1064. if(isset($pathInfo["extension"])) $suffix = strtolower($pathInfo["extension"]);
  1065. if(!$suffix or (!array_key_exists($suffix, $this->config->program->suffix) and strpos($this->config->repo->images, "|$suffix|") === false)) $suffix = $this->repoZen->isBinary($content, $suffix) ? 'binary' : 'c';
  1066. if(strpos($this->config->repo->images, "|$suffix|") !== false)
  1067. {
  1068. $content = base64_encode($content);
  1069. }
  1070. elseif($encoding != 'utf-8')
  1071. {
  1072. $content = helper::convertEncoding($content, $encoding);
  1073. }
  1074. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->view;
  1075. $this->view->type = 'view';
  1076. $this->view->showBug = $showBug;
  1077. $this->view->repoID = $repoID;
  1078. $this->view->repo = $repo;
  1079. $this->view->revision = $revision;
  1080. $this->view->oldRevision = '';
  1081. $this->view->file = $file;
  1082. $this->view->lines = $lines;
  1083. $this->view->entry = $entry;
  1084. $this->view->suffix = $suffix;
  1085. $this->view->content = $content ? $content : '';
  1086. $this->view->pathInfo = $pathInfo;
  1087. $this->view->objectID = $objectID;
  1088. $this->view->showEditor = (strpos($this->config->repo->images, "|$suffix|") === false and $suffix != 'binary') ? true : false;
  1089. $this->display();
  1090. }
  1091. /**
  1092. * 异步同步代码提交记录。
  1093. * Ajax sync comment.
  1094. *
  1095. * @param int $repoID
  1096. * @param string $type
  1097. * @access public
  1098. * @return void
  1099. */
  1100. public function ajaxSyncCommit($repoID = 0, $type = 'batch')
  1101. {
  1102. set_time_limit(0);
  1103. $repo = $this->repo->getByID($repoID);
  1104. if(empty($repo)) return print($this->config->repo->repoSyncLog->finish);
  1105. if($repo->synced) return print($this->config->repo->repoSyncLog->finish);
  1106. if(in_array($repo->SCM, array('Gitea', 'Gogs')))
  1107. {
  1108. $syncLog = $this->repoZen->syncLocalCommit($repo);
  1109. if($syncLog) return print(trim($syncLog));
  1110. }
  1111. $this->commonAction($repoID);
  1112. $this->scm->setEngine($repo);
  1113. $branchID = $repo->SCM == 'Subversion' ? '' : (string)$this->cookie->syncBranch;
  1114. if(in_array($repo->SCM, $this->config->repo->gitTypeList) && !$this->cookie->syncBranch)
  1115. {
  1116. $branches = $this->scm->branch();
  1117. if(empty($branches)) return print($this->lang->repo->error->empty);
  1118. $branchID = current($branches);
  1119. }
  1120. $branches = $this->repoZen->getSyncBranches($repo, $branchID);
  1121. $logs = array();
  1122. $version = 1;
  1123. if(!in_array($repo->SCM, $this->config->repo->notSyncSCM))
  1124. {
  1125. $latestInDB = $this->repo->getLatestCommit($repoID, false);
  1126. $version = empty($latestInDB) ? 1 : $latestInDB->commit + 1;
  1127. if(in_array($repo->SCM, array('Git', 'Gitea', 'Gogs')))
  1128. {
  1129. $revision = $version == 1 ? 'HEAD' : $latestInDB->commit;
  1130. }
  1131. else
  1132. {
  1133. $revision = $version == 1 ? '0' : $latestInDB->revision;
  1134. }
  1135. $batchNum = $type == 'batch' ? $this->config->repo->batchNum : 0;
  1136. $logs = $this->scm->getCommits($revision, $batchNum, $branchID);
  1137. }
  1138. $commitCount = $this->repo->saveCommit($repoID, $logs, $version, $branchID);
  1139. echo $this->repoZen->checkSyncResult($repo, $branches, $branchID, $commitCount, $type);
  1140. }
  1141. /**
  1142. * 异步同步代码分支提交记录。
  1143. * Ajax sync git branch comment.
  1144. *
  1145. * @param int $repoID
  1146. * @param string $branch
  1147. * @access public
  1148. * @return void
  1149. */
  1150. public function ajaxSyncBranchCommit($repoID = 0, $branch = '')
  1151. {
  1152. set_time_limit(0);
  1153. $repo = $this->repo->getByID($repoID);
  1154. if(empty($repo)) return;
  1155. if(!in_array($repo->SCM, $this->config->repo->gitTypeList)) return print('finish');
  1156. if($branch) $branch = base64_decode(helper::safe64Decode($branch));
  1157. $this->scm->setEngine($repo);
  1158. $this->repoZen->setRepoBranch($branch);
  1159. helper::setcookie("syncBranch", $branch);
  1160. $latestInDB = $this->dao->select('t1.*')->from(TABLE_REPOHISTORY)->alias('t1')
  1161. ->leftJoin(TABLE_REPOBRANCH)->alias('t2')->on('t1.id=t2.revision')
  1162. ->where('t1.repo')->eq($repoID)
  1163. ->beginIF(in_array($repo->SCM, $this->config->repo->gitTypeList) and $this->cookie->repoBranch)->andWhere('t2.branch')->eq($this->cookie->repoBranch)->fi()
  1164. ->orderBy('t1.time')
  1165. ->limit(1)
  1166. ->fetch();
  1167. $version = empty($latestInDB) ? 1 : $latestInDB->commit + 1;
  1168. $logs = array();
  1169. $revision = $version == 1 ? 'HEAD' : $latestInDB->commit;
  1170. if($repo->SCM == 'Gitlab' and $version > 1) $revision = $latestInDB->revision;
  1171. $logs = $this->scm->getCommits($revision, $this->config->repo->batchNum, $branch);
  1172. $commitCount = $this->repo->saveCommit($repoID, $logs, $version, $branch);
  1173. if(empty($commitCount))
  1174. {
  1175. if($branch) $this->repo->saveExistCommits4Branch($repo->id, $branch);
  1176. helper::setcookie("syncBranch", $branch, 0, $this->config->webRoot, '', $this->config->cookieSecure, true);
  1177. $this->repo->markSynced($repoID);
  1178. return print('finish');
  1179. }
  1180. $this->dao->update(TABLE_REPO)->set('commits=commits + ' . $commitCount)->where('id')->eq($repoID)->exec();
  1181. echo $commitCount;
  1182. }
  1183. /**
  1184. * 获取SVN目录。
  1185. * Ajax get svn dir.
  1186. *
  1187. * @param int $repoID
  1188. * @param string $path
  1189. * @access public
  1190. * @return void
  1191. */
  1192. public function ajaxGetSVNDirs($repoID, $path = '')
  1193. {
  1194. $repo = $this->repo->getByID($repoID);
  1195. if($repo->SCM != 'Subversion') return print(json_encode(array()));
  1196. $path = $this->repo->decodePath($path);
  1197. $dirs = array();
  1198. if(empty($path))
  1199. {
  1200. $dirs['/'] = '';
  1201. if(empty($repo->prefix)) $path = '/';
  1202. }
  1203. $tags = $this->loadModel('svn')->getRepoTags($repo, $path);
  1204. if($tags)
  1205. {
  1206. $dirs['/'] = $this->repo->encodePath($path);
  1207. foreach($tags as $dirPath => $dirName) $dirs[$dirPath] = $this->repo->encodePath($dirPath);
  1208. }
  1209. echo json_encode($dirs);
  1210. }
  1211. /**
  1212. * 获取1.5级导航数据。
  1213. * Ajax get drop menu.
  1214. *
  1215. * @param int $repoID
  1216. * @param string $type
  1217. * @param string $method
  1218. * @param int $projectID
  1219. * @access public
  1220. * @return void
  1221. * @param string $module
  1222. */
  1223. public function ajaxGetDropMenu($repoID, $module = 'repo', $method = 'browse', $projectID = 0)
  1224. {
  1225. if($module == 'repo' and !in_array($method, array('review', 'diff', 'browsetag', 'browsebranch', 'log'))) $method = 'browse';
  1226. if($module == 'mr' && $method != 'create') $method = 'browse';
  1227. if($module == 'job') $method = 'browse';
  1228. if($module == 'compile' and $method == 'logs') $method = 'browse';
  1229. if($module == 'bug' and $method == 'view')
  1230. {
  1231. $module = 'repo';
  1232. $method = 'review';
  1233. }
  1234. $params = '';
  1235. if($projectID)
  1236. {
  1237. if($method == 'browse' || $method == 'log') $params = "&branchID=&objectID=$projectID";
  1238. if(in_array($method, array('browsetag', 'browsebranch'))) $params = "&objectID=$projectID";
  1239. }
  1240. /* Get repo group by type. */
  1241. $repoGroup = $this->repo->getRepoGroup('project', $projectID);
  1242. $this->view->repoID = $repoID;
  1243. $this->view->repoGroup = $repoGroup;
  1244. $this->view->link = $this->createLink($module, $method, "repoID=%s" . $params);
  1245. $this->display();
  1246. }
  1247. /**
  1248. * 根据产品ID获取项目列表。
  1249. * Get projects list by product id list by ajax.
  1250. *
  1251. * @access public
  1252. * @return void
  1253. */
  1254. public function ajaxProjectsOfProducts()
  1255. {
  1256. $productIds = $this->post->products ? explode(',', $this->post->products) : array();
  1257. if(empty($productIds))
  1258. {
  1259. $products = $this->loadModel('product')->getPairs('', 0, '', 'all');
  1260. $productIds = array_keys($products);
  1261. }
  1262. /* Get all projects that can be accessed. */
  1263. $accessProjects = $this->loadModel('product')->getProjectPairsByProductIDList($productIds);
  1264. $options = array();
  1265. foreach($accessProjects as $projectID => $project)
  1266. {
  1267. $options[] = array('text' => $project, 'value' => $projectID);
  1268. }
  1269. return print(json_encode($options));
  1270. }
  1271. /**
  1272. * 获取服务器下拉列表数据。
  1273. * Ajax get hosts.
  1274. *
  1275. * @param string $scm
  1276. * @access public
  1277. * @return void
  1278. */
  1279. public function ajaxGetHosts($scm)
  1280. {
  1281. $hosts = $this->loadModel('pipeline')->getPairs($scm, true);
  1282. $options = array();
  1283. foreach($hosts as $hostID => $host)
  1284. {
  1285. $options[] = array('text' => $host, 'value' => $hostID);
  1286. }
  1287. return print(json_encode($options));
  1288. }
  1289. /**
  1290. * 获取各个服务器下的项目。
  1291. * Ajax get projects by server.
  1292. *
  1293. * @param int $serverID
  1294. * @access public
  1295. * @return void
  1296. */
  1297. public function ajaxGetProjects($serverID)
  1298. {
  1299. $server = $this->loadModel('pipeline')->getByID($serverID);
  1300. $getProjectFunc = 'ajaxGet' . $server->type . 'Projects';
  1301. $repos = $this->$getProjectFunc($serverID);
  1302. return print(json_encode($this->repoZen->buildRepoPaths(array_column($repos, 'text', 'value'))));
  1303. }
  1304. /**
  1305. * 获取Gitea项目。
  1306. * Ajax get gitea projects.
  1307. *
  1308. * @param int $giteaID
  1309. * @access public
  1310. * @return array
  1311. */
  1312. public function ajaxGetGiteaProjects($giteaID)
  1313. {
  1314. $projects = $this->loadModel('gitea')->apiGetProjects($giteaID);
  1315. $importedProjects = $this->repo->getImportedProjects($giteaID);
  1316. $options = array();
  1317. $options[] = array('text' => '', 'value' => '');;
  1318. foreach($projects as $project)
  1319. {
  1320. if(in_array($project->full_name, $importedProjects)) continue;
  1321. $options[] = array('text' => $project->full_name, 'value' => $project->full_name);
  1322. }
  1323. return $options;
  1324. }
  1325. /**
  1326. * 获取Gogs项目。
  1327. * Ajax get gogs projects.
  1328. *
  1329. * @param int $gogsID
  1330. * @access public
  1331. * @return array
  1332. */
  1333. public function ajaxGetGogsProjects($gogsID)
  1334. {
  1335. $projects = $this->loadModel('gogs')->apiGetProjects($gogsID);
  1336. $importedProjects = $this->repo->getImportedProjects($gogsID);
  1337. $options = array();
  1338. $options[] = array('text' => '', 'value' => '');;
  1339. foreach($projects as $project)
  1340. {
  1341. if(in_array($project->full_name, $importedProjects)) continue;
  1342. $options[] = array('text' => $project->full_name, 'value' => $project->full_name);
  1343. }
  1344. return $options;
  1345. }
  1346. /**
  1347. * 获取Gitlab项目。
  1348. * Ajax get gitlab projects.
  1349. *
  1350. * @param int $gitlabID
  1351. * @param string $projectIdList
  1352. * @param string $filter
  1353. * @access public
  1354. * @return array
  1355. */
  1356. public function ajaxGetGitlabProjects($gitlabID, $projectIdList = '', $filter = '')
  1357. {
  1358. $projects = $this->repo->getGitlabProjects($gitlabID, $filter);
  1359. if(!$projects) return array();
  1360. $projectIdList = $projectIdList ? explode(',', $projectIdList) : null;
  1361. $options = array();
  1362. $options[] = array('text' => '', 'value' => '');;
  1363. foreach($projects as $project)
  1364. {
  1365. if(!empty($projectIdList) and $project and !in_array($project->id, $projectIdList)) continue;
  1366. $options[] = array('text' => $project->name_with_namespace, 'value' => $project->id);
  1367. }
  1368. return $options;
  1369. }
  1370. /**
  1371. * 根据服务器ID获取分组。
  1372. * Ajax get groups by server.
  1373. *
  1374. * @param int $serverID
  1375. * @access public
  1376. * @return void
  1377. */
  1378. public function ajaxGetGroups($serverID)
  1379. {
  1380. $options = $this->repo->getGroups($serverID);
  1381. $server = $this->loadModel('pipeline')->getByID($serverID);
  1382. $result = new stdclass();
  1383. $result->options = $options;
  1384. $result->server = $server;
  1385. return print(json_encode($result));
  1386. }
  1387. /**
  1388. * 根据代码库ID获取产品列表。
  1389. * Ajax:: Load product by repoID.
  1390. *
  1391. * @param int $repoID
  1392. * @access public
  1393. * @return void
  1394. */
  1395. public function ajaxLoadProducts($repoID)
  1396. {
  1397. $productPairs = $this->repo->getProductsByRepo($repoID);
  1398. $options = array();
  1399. $options[] = array('text' => '', 'value' => '');;
  1400. foreach($productPairs as $productID => $productName)
  1401. {
  1402. $options[] = array('text' => $productName, 'value' => $productID);
  1403. }
  1404. return print(json_encode($options));
  1405. }
  1406. /**
  1407. * 根据Url获取代码库信息。
  1408. * API: get repo by url.
  1409. *
  1410. * @access public
  1411. * @return void
  1412. */
  1413. public function apiGetRepoByUrl()
  1414. {
  1415. $url = urldecode($this->post->repoUrl);
  1416. $result = $this->repo->getRepoByUrl($url);
  1417. if($result['result'] == 'fail') return $this->send($result);
  1418. $repo = $result['data'];
  1419. $fileServer = new stdclass();
  1420. $fileServer->fileServerUrl = $repo->fileServerUrl;
  1421. $fileServer->fileServerAccount = $repo->fileServerAccount;
  1422. $fileServer->fileServerPassword = $repo->fileServerPassword;
  1423. return $this->send($fileServer);
  1424. }
  1425. /**
  1426. * 获取DevOps指令配置。
  1427. * API: get rules.
  1428. *
  1429. * @access public
  1430. * @return void
  1431. */
  1432. public function ajaxGetRules()
  1433. {
  1434. return $this->send(array('status' => 'success', 'rules' => $this->config->repo->rules));
  1435. }
  1436. /**
  1437. * Ajax get executions.
  1438. *
  1439. * @param int $productID
  1440. * @param int $branch
  1441. * @access public
  1442. * @return void
  1443. */
  1444. public function ajaxGetExecutions($productID, $branch = 0)
  1445. {
  1446. $executions = $this->repo->getExecutionPairs($productID, $branch);
  1447. $options = array();
  1448. foreach($executions as $executionID => $executionName)
  1449. {
  1450. $options[] = array('text' => $executionName, 'value' => $executionID);
  1451. }
  1452. return print(json_encode($options));
  1453. }
  1454. /**
  1455. * 下载代码。
  1456. * Download zip code.
  1457. *
  1458. * @param int $repoID
  1459. * @param string $branch
  1460. * @access public
  1461. * @return void
  1462. */
  1463. public function downloadCode($repoID, $branch = '')
  1464. {
  1465. $savePath = $this->app->getDataRoot() . 'repo';
  1466. if(!is_dir($savePath))
  1467. {
  1468. if(!is_writable($this->app->getDataRoot())) return $this->sendError(sprintf($this->lang->repo->error->noWritable, dirname($savePath)), true);
  1469. mkdir($savePath, 0777, true);
  1470. }
  1471. $repo = $this->repo->getByID($repoID);
  1472. $this->scm = $this->app->loadClass('scm');
  1473. $this->scm->setEngine($repo);
  1474. $url = $this->scm->getDownloadUrl($branch, $savePath);
  1475. return $this->send(array('result' => 'success', 'callback' => "window.open('{$url}')"));
  1476. }
  1477. /**
  1478. * 根据代码库和提交获取关联信息的标题列表。
  1479. * Get relation by commit.
  1480. *
  1481. * @param int $repoID
  1482. * @param string $commit
  1483. * @access public
  1484. * @return void
  1485. */
  1486. public function ajaxGetCommitRelation($repoID, $commit)
  1487. {
  1488. $titleList = $this->repo->getRelationByCommit($repoID, $commit);
  1489. return $this->send(array('titleList' => $titleList));
  1490. }
  1491. /**
  1492. * 根据对象ID和对象类型获取关联信息。
  1493. * Get relation story, task, bug info.
  1494. *
  1495. * @param int $objectID
  1496. * @param string $objectType story|task|bug
  1497. * @access public
  1498. * @return void
  1499. */
  1500. public function ajaxGetRelationInfo($objectID, $objectType = 'story')
  1501. {
  1502. $this->app->loadLang('release');
  1503. $this->view->object = $this->loadModel($objectType)->getById($objectID);
  1504. $this->view->users = $this->loadModel('user')->getPairs('noletter');
  1505. $this->view->actions = $this->loadModel('action')->getList($objectType, $objectID);
  1506. $this->view->objectID = $objectID;
  1507. $this->view->objectType = $objectType;
  1508. $this->display();
  1509. }
  1510. /**
  1511. * 通过行号和版本获取代码库的提交信息。
  1512. * Ajax get commit info.
  1513. *
  1514. * @access public
  1515. * @return void
  1516. */
  1517. public function ajaxGetCommitInfo()
  1518. {
  1519. $repo = $this->repo->getByID((int)$this->post->repoID);
  1520. $entry = $this->repo->decodePath($this->post->entry);
  1521. $revision = helper::safe64Decode(urldecode($this->post->revision));
  1522. $sourceRevision = helper::safe64Decode(urldecode($this->post->sourceRevision));
  1523. $this->scm->setEngine($repo);
  1524. $blames = $this->scm->blame($entry, $revision);
  1525. if(!$blames) $blames =$this->scm->blame($entry, $sourceRevision);
  1526. return $this->send(array('result' => 'success', 'blames' => $blames));
  1527. }
  1528. /**
  1529. * 获取Gitlab的文件信息。
  1530. * Get gitlab files.
  1531. *
  1532. * @param int $repoID
  1533. * @param string $branch
  1534. * @param string $path
  1535. * @access public
  1536. * @return void
  1537. */
  1538. public function ajaxGetFiles($repoID, $branch = '', $path = '')
  1539. {
  1540. $repo = $this->repo->getByID($repoID);
  1541. if($path) $path = helper::safe64Decode($path);
  1542. if($repo->SCM == 'Gitlab') return print(json_encode($this->repo->getGitlabFilesByPath($repo, $path, $branch)));
  1543. return print(json_encode($this->repoZen->getViewTree($repo, $path, $branch)));
  1544. }
  1545. /**
  1546. * 获取文件最后一次提交信息。
  1547. * Get file last commit info.
  1548. *
  1549. * @access public
  1550. * @return void
  1551. */
  1552. public function ajaxGetFileCommitInfo()
  1553. {
  1554. $repo = $this->repo->getByID((int)$this->post->repoID);
  1555. $commit = $this->loadModel('gitlab')->getFileLastCommit($repo, (string)$this->post->path, (string)$this->post->branch);
  1556. $commit->comment = $this->repo->replaceCommentLink($commit->message);
  1557. echo json_encode($commit);
  1558. }
  1559. /**
  1560. * 在批量导入代码库页面隐藏代码库。
  1561. * Hidden repo in import page.
  1562. *
  1563. * @access public
  1564. * @return void
  1565. */
  1566. public function ajaxHiddenRepo()
  1567. {
  1568. $repoID = $this->post->repoID;
  1569. $serverID = $this->post->serverID;
  1570. $reposID = $this->loadModel('setting')->getItem('owner=system&module=repo&section=hiddenRepo&key=' . $serverID);
  1571. if(!$reposID) $reposID = $repoID;
  1572. $repoIDList = explode(',', $reposID);
  1573. if(!in_array($repoID, $repoIDList)) $reposID .= ",{$repoID}";
  1574. $this->setting->setItem('system.repo.hiddenRepo.' . $serverID, $reposID);
  1575. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1576. return $this->send(array('result' => 'success'));
  1577. }
  1578. /**
  1579. * 在批量导入代码库页面显示代码库。
  1580. * Show repo in import page.
  1581. *
  1582. * @access public
  1583. * @return void
  1584. */
  1585. public function ajaxShowRepo()
  1586. {
  1587. $repoID = $this->post->repoID;
  1588. $serverID = $this->post->serverID;
  1589. $reposID = $this->loadModel('setting')->getItem('owner=system&module=repo&section=hiddenRepo&key=' . $serverID);
  1590. $reposID = str_replace(",{$repoID},", "", ",{$reposID},");
  1591. $this->setting->setItem('system.repo.hiddenRepo.' . $serverID, trim($reposID, ','));
  1592. if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
  1593. return $this->send(array('result' => 'success'));
  1594. }
  1595. /**
  1596. * 通过ajax获取代码库的分支和标签列表。
  1597. * Ajax: Get branches and tags.
  1598. *
  1599. * @param int $repoID
  1600. * @access public
  1601. * @return void
  1602. */
  1603. public function ajaxGetBranchesAndTags($repoID)
  1604. {
  1605. $repo = $this->repo->getByID($repoID);
  1606. $scm = $this->app->loadClass('scm');
  1607. $scm->setEngine($repo);
  1608. $branches = $scm->branch();
  1609. $tagList = $scm->tags();
  1610. $tags = array();
  1611. foreach($tagList as $tag) $tags[$tag] = $tag;
  1612. echo json_encode(array('branches' => $branches, 'tags' => $tags));
  1613. }
  1614. /**
  1615. * 浏览Tag列表。
  1616. * Browse tag list.
  1617. *
  1618. * @param int $repoID
  1619. * @param int $objectID
  1620. * @param string $keyword
  1621. * @param string $orderBy
  1622. * @param int $recTotal
  1623. * @param int $recPerPage
  1624. * @param int $pageID
  1625. * @access public
  1626. * @return void
  1627. */
  1628. public function browseTag($repoID, $objectID = 0, $keyword = '', $orderBy = 'date_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  1629. {
  1630. $repoID = $this->repoZen->processRepoID($repoID, $objectID);
  1631. $this->commonAction($repoID, $objectID);
  1632. $keyword = str_replace(' ', '+', urldecode($keyword));
  1633. $keyword = htmlspecialchars(base64_decode($keyword));
  1634. $repo = $this->repo->getByID($repoID);
  1635. if(!in_array($repo->SCM, $this->config->repo->notSyncSCM)) $this->locate(inLink('browse', "repoID=$repoID&objectID=$objectID"));
  1636. $this->scm->setEngine($repo);
  1637. $tagList = $this->scm->tags($keyword ? $keyword : 'all', 'HEAD', true, $orderBy, $recPerPage, $pageID);
  1638. if(count($tagList) == 0 && $pageID != 1) $this->locate(inLink('browseTag', "repoID=$repoID&objectID=$objectID&keyword=$keyword&orderBy=$orderBy&recTotal=0&recPerPage=$recPerPage&pageID=1"));
  1639. $this->app->loadClass('pager', true);
  1640. $pager = new pager($recTotal, $recPerPage, $pageID);
  1641. $pager->recPerPage = $recPerPage;
  1642. $pager->recTotal = count($tagList) < $pager->recPerPage ? $pager->recPerPage * $pager->pageID : $pager->recPerPage * ($pager->pageID + 1);
  1643. $committers = $this->loadModel('user')->getCommiters('account');
  1644. $showCreatedDate = false;
  1645. foreach($tagList as &$tag)
  1646. {
  1647. $tag->repoID = $repoID;
  1648. $tag->tagName = urlencode(helper::safe64Encode($tag->name));
  1649. $tag->objectID = $objectID;
  1650. $tag->commitID = isset($tag->commit->id) ? $tag->commit->id : '';
  1651. if(isset($tag->commit->sha)) $tag->commitID = $tag->commit->sha;
  1652. $tag->commitID = substr($tag->commitID, 0, 10);
  1653. $tag->committer = isset($tag->commit->author_name) ? $tag->commit->author_name : '';
  1654. if(isset($tag->commit->author->identity->name)) $tag->committer = $tag->commit->author->identity->name;
  1655. $tag->committer = zget($committers, $tag->committer);
  1656. $tag->createdBy = isset($tag->tagger->identity->name) ? zget($committers, $tag->tagger->identity->name) : '';
  1657. $tag->createdDate = isset($tag->tagger->when) ? date('Y-m-d H:i:s', strtotime($tag->tagger->when)) : '';
  1658. if(isset($tag->created_at)) $tag->createdDate = date('Y-m-d H:i:s', strtotime($tag->created_at));
  1659. if($tag->createdDate) $showCreatedDate = true;
  1660. $tag->date = isset($tag->commit->committed_date) ? date('Y-m-d H:i:s', strtotime($tag->commit->committed_date)) : '';
  1661. if(isset($tag->commit->committer->when)) $tag->date = date('Y-m-d H:i:s', strtotime($tag->commit->committer->when));
  1662. }
  1663. if(!$showCreatedDate) unset($this->config->repo->dtable->tag->fieldList['createdDate']);
  1664. $this->view->title = $this->lang->repo->browseTag;
  1665. $this->view->repoID = $repoID;
  1666. $this->view->objectID = $objectID;
  1667. $this->view->repo = $repo;
  1668. $this->view->pager = $pager;
  1669. $this->view->tagList = $tagList;
  1670. $this->view->orderBy = $orderBy;
  1671. $this->view->keyword = base64_encode($keyword);
  1672. $this->view->users = $this->user->getPairs('noletter');
  1673. $this->display();
  1674. }
  1675. /**
  1676. * 浏览分支列表。
  1677. * Browse branch list.
  1678. *
  1679. * @param int $repoID
  1680. * @param int $objectID
  1681. * @param string $label
  1682. * @param int $showArchived
  1683. * @param string $keyword
  1684. * @param string $orderBy
  1685. * @param int $recTotal
  1686. * @param int $recPerPage
  1687. * @param int $pageID
  1688. * @access public
  1689. * @return void
  1690. */
  1691. public function browseBranch($repoID, $objectID = 0, $label = 'all', $showArchived = 0, $keyword = '', $orderBy = 'date_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
  1692. {
  1693. $repoID = $this->repoZen->processRepoID($repoID, $objectID);
  1694. $this->commonAction($repoID, $objectID);
  1695. $repo = $this->repo->getByID($repoID);
  1696. if(!in_array($repo->SCM, $this->config->repo->notSyncSCM)) $this->locate(inLink('browse', "repoID=$repoID&objectID=$objectID"));
  1697. $keyword = str_replace(' ', '+', urldecode($keyword));
  1698. $keyword = htmlspecialchars(base64_decode($keyword));
  1699. $this->scm->setEngine($repo);
  1700. $branchList = $this->scm->branch($keyword ? $keyword : 'all', $orderBy, $recPerPage, $pageID, $label, $showArchived);
  1701. if(count($branchList) == 0 && $pageID != 1) $this->locate(inLink('browseBranch', "repoID=$repoID&objectID=$objectID&label=$label&showArchived=$showArchived&keyword=$keyword&orderBy=$orderBy&recTotal=0&recPerPage=$recPerPage&pageID=1"));
  1702. $this->app->loadClass('pager', true);
  1703. $pager = new pager($recTotal, $recPerPage, $pageID);
  1704. $pager->recPerPage = $recPerPage;
  1705. $pager->recTotal = count($branchList) < $pager->recPerPage ? $pager->recPerPage * $pager->pageID : $pager->recPerPage * ($pager->pageID + 1);
  1706. $committers = $this->loadModel('user')->getCommiters('account');
  1707. foreach($branchList as &$branch)
  1708. {
  1709. $branch->repoID = $repoID;
  1710. $branch->branchName = urlencode(helper::safe64Encode($branch->name));
  1711. $branch->objectID = $objectID;
  1712. $branch->commitID = isset($branch->commit->id) ? $branch->commit->id : '';
  1713. if(isset($branch->commit->sha)) $branch->commitID = $branch->commit->sha;
  1714. $branch->commitID = substr($branch->commitID, 0, 10);
  1715. $branch->committer = isset($branch->commit->author_name) ? $branch->commit->author_name : '';
  1716. if(isset($branch->commit->author->identity->name)) $branch->committer = $branch->commit->author->identity->name;
  1717. $branch->committer = zget($committers, $branch->committer);
  1718. $branch->commitDate = isset($branch->commit->committed_date) ? date('Y-m-d H:i:s', strtotime($branch->commit->committed_date)) : '';
  1719. if(isset($branch->commit->author->when)) $branch->commitDate = date('Y-m-d H:i:s', strtotime($branch->commit->author->when));
  1720. $branch->ahead = isset($branch->divergence->ahead) ? $branch->divergence->ahead : 0;
  1721. $branch->behind = isset($branch->divergence->behind) ? $branch->divergence->behind : 0;
  1722. }
  1723. $this->view->title = $this->lang->repo->browseBranch;
  1724. $this->view->repoID = $repoID;
  1725. $this->view->objectID = $objectID;
  1726. $this->view->repo = $repo;
  1727. $this->view->pager = $pager;
  1728. $this->view->orderBy = $orderBy;
  1729. $this->view->branchList = $branchList;
  1730. $this->view->keyword = base64_encode($keyword);
  1731. $this->view->users = $this->user->getPairs('noletter');
  1732. $this->view->label = $label;
  1733. $this->view->showArchived = $showArchived;
  1734. $this->display();
  1735. }
  1736. /**
  1737. * 通过ajax获取代码库的分支。
  1738. * Ajax: Get branches.
  1739. *
  1740. * @param int $repoID
  1741. * @access public
  1742. * @return void
  1743. */
  1744. public function ajaxGetBranchOptions($repoID)
  1745. {
  1746. $repo = $this->repo->getByID($repoID);
  1747. $scm = $this->app->loadClass('scm');
  1748. $scm->setEngine($repo);
  1749. $options = $this->getBranchAndTagOptions($this->scm);
  1750. if(!empty($options[0]['items']))
  1751. {
  1752. return print(json_encode($options[0]['items']));
  1753. }
  1754. return print(json_encode(array()));
  1755. }
  1756. }