zen.php 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. <?php
  2. /**
  3. * The zen file of repo module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
  6. * @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Zeng Gang<zenggang@easycorp.ltd>
  8. * @package repo
  9. * @link https://www.zentao.net
  10. */
  11. class repoZen extends repo
  12. {
  13. /**
  14. * 准备创建版本库的数据。
  15. * Prepare create repo data.
  16. *
  17. * @param form $formData
  18. * @param bool $isPipelineServer
  19. * @access protected
  20. * @return object|false
  21. */
  22. protected function prepareCreate($formData, $isPipelineServer)
  23. {
  24. if($this->config->inContainer || $this->config->inQuickon)
  25. {
  26. $formData->data->client = $this->post->client = $this->post->SCM == 'Subversion' ? 'svn' : 'git';
  27. }
  28. else
  29. {
  30. if(!$this->checkClient()) return false;
  31. }
  32. if(!$this->checkConnection()) return false;
  33. $repo = $formData
  34. ->setIf($isPipelineServer, 'password', $this->post->serviceToken)
  35. ->setIf($this->post->SCM == 'Gitlab', 'path', '')
  36. ->setIf($this->post->SCM == 'Gitlab', 'client', '')
  37. ->setIf($this->post->SCM == 'Gitlab', 'extra', $this->post->serviceProject)
  38. ->setIf($isPipelineServer, 'prefix', '')
  39. ->setIf($this->post->SCM == 'Git', 'account', '')
  40. ->setIf($this->post->SCM == 'Git', 'password', '')
  41. ->setIf(in_array($this->post->SCM, array('Gitea', 'Gogs')), 'path', $_POST['path'])
  42. ->setIf($this->post->encrypt == 'base64', 'password', base64_encode($this->post->password))
  43. ->skipSpecial('path,client,account,password,desc')
  44. ->setDefault('product', '')->join('product', ',')
  45. ->setDefault('projects', '')->join('projects', ',')
  46. ->get();
  47. if(strpos($repo->client, ' ')) $repo->client = "\"{$repo->client}\"";
  48. $acl = $this->checkACL();
  49. if(!$acl) return false;
  50. $repo->acl = json_encode($acl);
  51. if($repo->SCM == 'Subversion')
  52. {
  53. $scmRepo = clone $repo;
  54. if($this->post->encrypt == 'base64') $scmRepo->password = $this->post->password;
  55. $scm = $this->app->loadClass('scm');
  56. $scm->setEngine($scmRepo);
  57. $info = $scm->info('');
  58. $infoRoot = urldecode($info->root);
  59. $path = str_replace(array(':3690/',':80/'), '/', $repo->path);
  60. $prefix = str_replace('\\', '/', $path);
  61. $repo->prefix = empty($infoRoot) ? '' : trim(str_ireplace($infoRoot, '', $prefix), '/');
  62. if($repo->prefix) $repo->prefix = '/' . $repo->prefix;
  63. }
  64. if($isPipelineServer)
  65. {
  66. $serviceProject = $this->dao->select('*')->from(TABLE_REPO)
  67. ->where('`SCM`')->eq($repo->SCM)
  68. ->andWhere('`serviceHost`')->eq($repo->serviceHost)
  69. ->andWhere('`serviceProject`')->eq($repo->serviceProject)
  70. ->fetch();
  71. if($serviceProject)
  72. {
  73. dao::$errors['serviceProject'][] = $this->lang->repo->error->projectUnique;
  74. return false;
  75. }
  76. }
  77. return $repo;
  78. }
  79. /**
  80. * 准备创建版本库的数据。
  81. * Prepare create repo data.
  82. *
  83. * @param object $repo
  84. * @access protected
  85. * @return object|false
  86. */
  87. protected function prepareCreateRepo($repo)
  88. {
  89. $acl = $this->checkACL();
  90. if(!$acl) return false;
  91. $repo->acl = json_encode($acl);
  92. $group = $this->repo->getGroups($repo->serviceHost, $repo->namespace);
  93. $server = $this->loadModel('pipeline')->getByID($repo->serviceHost);
  94. $repo->path = "{$server->url}/{$group}/{$repo->name}";
  95. return $repo;
  96. }
  97. /**
  98. * 准备编辑版本库的数据。
  99. * Prepare edit repo data.
  100. *
  101. * @param form $formData
  102. * @param object $oldRepo
  103. * @param bool $isPipelineServer
  104. * @access protected
  105. * @return object|false
  106. */
  107. protected function prepareEdit($formData, $oldRepo, $isPipelineServer)
  108. {
  109. if($oldRepo->client != $this->post->client and !$this->checkClient()) return false;
  110. if(!$this->checkConnection()) return false;
  111. $repo = $formData
  112. ->setIf($isPipelineServer, 'password', $this->post->serviceToken)
  113. ->setDefault('client', 'svn')
  114. ->setIf($this->post->SCM == 'Gitlab', 'client', '')
  115. ->setIf($this->post->SCM == 'Gitlab', 'extra', $this->post->serviceProject)
  116. ->setIf($this->post->SCM == 'Gitlab', 'prefix', '')
  117. ->setDefault('product', '')
  118. ->skipSpecial('path,client,account,password,desc')
  119. ->join('product', ',')
  120. ->setDefault('projects', '')->join('projects', ',')
  121. ->get();
  122. if(strpos($repo->client, ' ')) $repo->client = "\"{$repo->client}\"";
  123. if($repo->path != $oldRepo->path) $repo->synced = 0;
  124. $acl = $this->checkACL();
  125. if(!$acl) return false;
  126. $repo->acl = json_encode($acl);
  127. if($repo->SCM == 'Subversion')
  128. {
  129. $scm = $this->app->loadClass('scm');
  130. $scm->setEngine($repo);
  131. $info = $scm->info('');
  132. $infoRoot = urldecode($info->root);
  133. $path = str_replace(array(':3690/',':80/'), '/', $repo->path);
  134. $prefix = str_replace('\\', '/', $path);
  135. $repo->prefix = empty($infoRoot) ? '' : trim(str_ireplace($infoRoot, '', $prefix), '/');
  136. if($repo->prefix) $repo->prefix = '/' . $repo->prefix;
  137. }
  138. elseif($repo->SCM != $oldRepo->SCM and $repo->SCM == 'Git')
  139. {
  140. $repo->prefix = '';
  141. }
  142. if($isPipelineServer)
  143. {
  144. $serviceProject = $this->dao->select('*')->from(TABLE_REPO)
  145. ->where('`SCM`')->eq($repo->SCM)
  146. ->andWhere('`serviceHost`')->eq($repo->serviceHost)
  147. ->andWhere('`serviceProject`')->eq($repo->serviceProject)
  148. ->andWhere('id')->ne($oldRepo->id)
  149. ->fetch();
  150. if($serviceProject)
  151. {
  152. dao::$errors['serviceProject'][] = $this->lang->repo->error->projectUnique;
  153. return false;
  154. }
  155. }
  156. return $repo;
  157. }
  158. /**
  159. * 检查权限数据。
  160. * Check acl.
  161. *
  162. * @access protected
  163. * @return array|false
  164. */
  165. protected function checkACL()
  166. {
  167. $acl = $this->post->acl;
  168. if($acl['acl'] == 'custom')
  169. {
  170. $aclGroups = array_filter($acl['groups']);
  171. $aclUsers = array_filter($acl['users']);
  172. if(empty($aclGroups) && empty($aclUsers))
  173. {
  174. $this->app->loadLang('product');
  175. dao::$errors['acl'] = sprintf($this->lang->error->notempty, $this->lang->product->whitelist);
  176. return false;
  177. }
  178. }
  179. return $acl;
  180. }
  181. /**
  182. * 检查svn、git客户端。
  183. * Check svn/git client.
  184. *
  185. * @access public
  186. * @return bool
  187. */
  188. protected function checkClient()
  189. {
  190. if(in_array($this->post->SCM, $this->config->repo->notSyncSCM)) return true;
  191. if(!$this->config->features->checkClient) return true;
  192. if(!$this->post->client)
  193. {
  194. dao::$errors['client'] = sprintf($this->lang->error->notempty, $this->lang->repo->client);
  195. return false;
  196. }
  197. $clientVersionFile = $this->session->clientVersionFile;
  198. if(empty($clientVersionFile))
  199. {
  200. $clientVersionFile = $this->app->getLogRoot() . uniqid('version_') . '.log';
  201. session_start();
  202. $this->session->set('clientVersionFile', $clientVersionFile);
  203. session_write_close();
  204. }
  205. if(file_exists($clientVersionFile)) return true;
  206. $cmd = $this->post->client . " --version > $clientVersionFile";
  207. dao::$errors['client'] = sprintf($this->lang->repo->error->safe, $clientVersionFile, $cmd);
  208. return false;
  209. }
  210. /**
  211. * 检查连接。
  212. * Check connection
  213. *
  214. * @access public
  215. * @return bool
  216. */
  217. protected function checkConnection()
  218. {
  219. if(empty($_POST)) return false;
  220. $scm = $this->post->SCM;
  221. $client = $this->post->client;
  222. $account = $this->post->account;
  223. $password = $this->post->password;
  224. $encoding = strtoupper($this->post->encoding ?: 'UTF-8');
  225. $path = $this->post->path;
  226. if($encoding != 'UTF8' and $encoding != 'UTF-8') $path = helper::convertEncoding($path, 'utf-8', $encoding);
  227. if($scm == 'Subversion')
  228. {
  229. /* Get svn version. */
  230. $versionCommand = "$client --version --quiet 2>&1";
  231. exec($versionCommand, $versionOutput, $versionResult);
  232. if($versionResult)
  233. {
  234. $message = sprintf($this->lang->repo->error->output, $versionCommand, $versionResult, implode("\n", $versionOutput));
  235. dao::$errors['client'] = $this->lang->repo->error->cmd . "\n" . $message;
  236. return false;
  237. }
  238. $svnVersion = end($versionOutput);
  239. $path = '"' . str_replace(array('%3A', '%2F', '+'), array(':', '/', ' '), urlencode($path)) . '"';
  240. if(stripos($path, 'https://') === 1 or stripos($path, 'svn://') === 1)
  241. {
  242. if(version_compare($svnVersion, '1.6', '<'))
  243. {
  244. dao::$errors['client'] = $this->lang->repo->error->version;
  245. return false;
  246. }
  247. $command = "$client info --username $account --password $password --non-interactive --trust-server-cert-failures=cn-mismatch --trust-server-cert --no-auth-cache $path 2>&1";
  248. if(version_compare($svnVersion, '1.9', '<')) $command = "$client info --username $account --password $password --non-interactive --trust-server-cert --no-auth-cache $path 2>&1";
  249. }
  250. elseif(stripos($path, 'file://') === 1)
  251. {
  252. $command = "$client info --non-interactive --no-auth-cache $path 2>&1";
  253. }
  254. else
  255. {
  256. $command = "$client info --username $account --password $password --non-interactive --no-auth-cache $path 2>&1";
  257. }
  258. exec($command, $output, $result);
  259. if($result)
  260. {
  261. $message = sprintf($this->lang->repo->error->output, $command, $result, implode("\n", $output));
  262. if(stripos($message, 'Expected FS format between') !== false and strpos($message, 'found format') !== false)
  263. {
  264. dao::$errors['client'] = $this->lang->repo->error->clientVersion;
  265. return false;
  266. }
  267. if(preg_match('/[^\:\/A-Za-z0-9_\-\'\"\.]/', $path))
  268. {
  269. dao::$errors['encoding'] = $this->lang->repo->error->encoding . "\n" . $message;
  270. return false;
  271. }
  272. dao::$errors['submit'] = $this->lang->repo->error->connect . "\n" . $message;
  273. return false;
  274. }
  275. }
  276. elseif(in_array($scm, array('Gitea', 'Gogs')))
  277. {
  278. if($this->post->name != '' and $this->post->serviceProject != '')
  279. {
  280. $module = strtolower($scm);
  281. $project = $this->loadModel($module)->apiGetSingleProject((int)$this->post->serviceHost, $this->post->serviceProject);
  282. if(isset($project->tokenCloneUrl))
  283. {
  284. $path = $this->app->getAppRoot() . 'www/data/repo/' . $this->post->name . '_' . $module;
  285. if(!realpath($path))
  286. {
  287. $cmd = 'git clone --progress -v "' . $project->tokenCloneUrl . '" "' . $path . '" > "' . $this->app->getTmpRoot() . "log/clone.progress.$module.{$this->post->name}.log\" 2>&1 &";
  288. if(PHP_OS == 'WINNT') $cmd = "start /b $cmd";
  289. exec($cmd);
  290. }
  291. $_POST['path'] = $path;
  292. }
  293. else
  294. {
  295. dao::$errors['serviceProject'] = $this->lang->repo->error->noCloneAddr;
  296. return false;
  297. }
  298. }
  299. }
  300. elseif($scm == 'Git')
  301. {
  302. if(!is_dir($path))
  303. {
  304. dao::$errors['path'] = sprintf($this->lang->repo->error->noFile, $path);
  305. return false;
  306. }
  307. if(!is_writable($path) || !is_executable($path))
  308. {
  309. dao::$errors['path'] = sprintf($this->lang->repo->error->noPriv, $path);
  310. return false;
  311. }
  312. if(!chdir($path))
  313. {
  314. dao::$errors['path'] = $this->lang->repo->error->path;
  315. return false;
  316. }
  317. $command = "$client tag 2>&1";
  318. exec($command, $output, $result);
  319. if($result)
  320. {
  321. dao::$errors['submit'] = $this->lang->repo->error->cmd . "\n" . sprintf($this->lang->repo->error->output, $command, $result, implode("\n", $output));
  322. return false;
  323. }
  324. }
  325. return true;
  326. }
  327. /**
  328. * 构建创建版本库页面数据。
  329. * Build form fields for create repo.
  330. *
  331. * @param int $objectID
  332. * @access protected
  333. * @return void
  334. */
  335. protected function buildCreateForm($objectID)
  336. {
  337. $this->repo->saveState(0, $objectID);
  338. $this->app->loadLang('action');
  339. $this->loadModel('product');
  340. if($this->app->tab == 'project' or $this->app->tab == 'execution')
  341. {
  342. $products = $this->loadModel('project')->getBranchesByProject($objectID);
  343. $products = $this->product->getProducts($objectID, 'all', '', false, array_keys($products));
  344. }
  345. else
  346. {
  347. $products = $this->product->getPairs('', 0, '', 'all');
  348. }
  349. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->create;
  350. $this->view->groups = $this->loadModel('group')->getPairs();
  351. $this->view->users = $this->loadModel('user')->getPairs('noletter|noempty|nodeleted|noclosed');
  352. $this->view->products = $products;
  353. $this->view->serviceHosts = $this->loadModel('pipeline')->getPairs(implode(',', $this->config->repo->notSyncSCM), true);
  354. $this->view->objectID = $objectID;
  355. $this->display();
  356. }
  357. /**
  358. * 构建创建版本库页面数据。
  359. * Build form fields for create repo.
  360. *
  361. * @param int $objectID
  362. * @access protected
  363. * @return void
  364. */
  365. protected function buildCreateRepoForm($objectID)
  366. {
  367. $this->repo->saveState(0, $objectID);
  368. $this->app->loadLang('action');
  369. if($this->app->tab == 'project' or $this->app->tab == 'execution')
  370. {
  371. $products = $this->loadModel('product')->getProductPairsByProject($objectID);
  372. }
  373. else
  374. {
  375. $products = $this->loadModel('product')->getPairs('', 0, '', 'all');
  376. }
  377. $repoGroups = array();
  378. $serviceHosts = $this->loadModel('pipeline')->getPairs(implode(',', $this->config->repo->notSyncSCM), true);
  379. if(!empty($serviceHosts))
  380. {
  381. $serverID = key($serviceHosts);
  382. $repoGroups = $this->repo->getGroups($serverID);
  383. }
  384. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->create;
  385. $this->view->groups = $this->loadModel('group')->getPairs();
  386. $this->view->users = $this->loadModel('user')->getPairs('noletter|noempty|nodeleted|noclosed');
  387. $this->view->products = $products;
  388. $this->view->serviceHosts = $serviceHosts;
  389. $this->view->repoGroups = $repoGroups;
  390. $this->view->objectID = $objectID;
  391. $this->display();
  392. }
  393. /**
  394. * 构建编辑版本库页面数据。
  395. * Build form fields for edit repo.
  396. *
  397. * @param int $objectID
  398. * @access protected
  399. * @return void
  400. * @param int $repoID
  401. */
  402. protected function buildEditForm($repoID, $objectID)
  403. {
  404. $repo = $this->repo->getByID($repoID);
  405. $repo->client = trim($repo->client, '"');
  406. $this->app->loadLang('action');
  407. $scm = strtolower($repo->SCM);
  408. if(in_array($scm, $this->config->repo->gitServiceList))
  409. {
  410. $serviceID = isset($repo->gitService) ? $repo->gitService : 0;
  411. $projectID = in_array($repo->SCM, $this->config->repo->notSyncSCM) ? (int)$repo->serviceProject : $repo->serviceProject;
  412. $project = $this->loadModel($scm)->apiGetSingleProject($serviceID, $projectID);
  413. $this->view->project = $project;
  414. }
  415. $products = $this->loadModel('product')->getPairs('', 0, '', 'all');
  416. $linkedProducts = $this->loadModel('product')->getByIdList(explode(',', $repo->product));
  417. $linkedProductPairs = array_combine(array_keys($linkedProducts), helper::arrayColumn($linkedProducts, 'name'));
  418. $products = $products + $linkedProductPairs;
  419. $this->view->title = $this->lang->repo->common . $this->lang->hyphen . $this->lang->repo->edit;
  420. $this->view->repo = $repo;
  421. $this->view->repoID = $repoID;
  422. $this->view->objectID = $objectID;
  423. $this->view->groups = $this->loadModel('group')->getPairs();
  424. $this->view->users = $this->loadModel('user')->getPairs('noletter|noempty|nodeleted|noclosed');
  425. $this->view->products = $products;
  426. $this->view->relatedProjects = $this->repo->filterProject(explode(',', $repo->product), explode(',', $repo->projects));
  427. $this->view->serviceHosts = $this->loadModel('pipeline')->getPairs($repo->SCM);
  428. $this->display();
  429. }
  430. /**
  431. * 获取还没存在禅道的项目列表。
  432. * Get not exist repos.
  433. *
  434. * @param object $gitlab
  435. * @access protected
  436. * @return array
  437. * @param object $server
  438. */
  439. protected function getNotExistRepos($server)
  440. {
  441. $repoList = array();
  442. if(!empty($server))
  443. {
  444. $repoList = $this->getGitlabProjectsByApi($server);
  445. $existRepoList = $this->dao->select('serviceProject,name')->from(TABLE_REPO)
  446. ->where('SCM')->eq('Gitlab')
  447. ->andWhere('serviceHost')->eq($server->id)
  448. ->fetchPairs();
  449. foreach($repoList as $key => $repo)
  450. {
  451. if(isset($existRepoList[$repo->id])) unset($repoList[$key]);
  452. }
  453. }
  454. return $repoList;
  455. }
  456. /**
  457. * 通过Graphql获取GitLab项目列表。
  458. * Get GitLab projects by Graphql.
  459. *
  460. * @param object $server
  461. * @access public
  462. * @return array
  463. */
  464. protected function getGitlabProjectsByApi($server)
  465. {
  466. $repoList = array();
  467. $endCursor = '';
  468. $hasNextPage = true;
  469. $url = rtrim($server->url, '/') . '/api/graphql' . "?private_token={$server->token}";
  470. while($hasNextPage)
  471. {
  472. $query = 'query { projects(after: "' . $endCursor . '") {pageInfo {endCursor hasNextPage} nodes {nameWithNamespace id name}}}';
  473. $response = json_decode(commonModel::http($url, array('query' => $query), array(CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1)));
  474. if(!$endCursor && !isset($response->data->projects->nodes)) return array();
  475. foreach($response->data->projects->nodes as $project)
  476. {
  477. preg_match('/\d+/', $project->id, $projectID);
  478. $project->id = $projectID ? $projectID[0] : $project->id;
  479. $project->name_with_namespace = $project->nameWithNamespace;
  480. $repoList[] = $project;
  481. }
  482. $hasNextPage = $response->data->projects->pageInfo->hasNextPage;
  483. $endCursor = $response->data->projects->pageInfo->endCursor;
  484. }
  485. return $repoList;
  486. }
  487. /**
  488. * 获取版本库文件列表信息。
  489. * Get repo files info.
  490. *
  491. * @param object $repo
  492. * @param string $path
  493. * @param string $branchID
  494. * @param string $base64BranchID
  495. * @param int $objectID
  496. * @access protected
  497. * @return array
  498. */
  499. protected function getFilesInfo($repo, $path, $branchID, $base64BranchID, $objectID)
  500. {
  501. $scm = $this->app->loadClass('scm');
  502. $scm->setEngine($repo);
  503. if($repo->SCM == 'Gitlab')
  504. {
  505. $_COOKIE['repoBranch'] = $branchID ? $branchID : $this->cookie->repoBranch;
  506. $infos = $this->repo->getGitlabFilesByPath($repo, $path, $branchID);
  507. foreach($infos as &$file)
  508. {
  509. $file->revision = '';
  510. $file->comment = '';
  511. $file->account = '';
  512. $file->date = '';
  513. }
  514. }
  515. else
  516. {
  517. $infos = $this->repo->getFileCommits($repo, $branchID, $path);
  518. }
  519. $filePath = $path;
  520. if($repo->SCM == 'Subversion')
  521. {
  522. $info = $scm->info('');
  523. if(!empty($info->root))
  524. {
  525. $prefixPath = str_replace($info->root . '/', '', $repo->path);
  526. $filePath = trim(str_replace($prefixPath, '', $path), '/');
  527. }
  528. }
  529. foreach($infos as $info)
  530. {
  531. $info->originalComment = $info->comment;
  532. $info->comment = $this->repo->replaceCommentLink($info->comment);
  533. if($repo->SCM != 'Subversion') $info->revision = substr($info->revision, 0, 10);
  534. $infoPath = trim(urldecode($path) . '/' . $info->name, '/');
  535. if($info->kind == 'dir')
  536. {
  537. $info->link = $this->repo->createLink('browse', "repoID={$repo->id}&branchID=$base64BranchID&objectID=$objectID&path=" . $this->repo->encodePath($infoPath));
  538. }
  539. else
  540. {
  541. if($repo->SCM == 'Subversion') $infoPath = $filePath . '/' . $info->name;
  542. $info->link = $this->repo->createLink('view', "repoID={$repo->id}&objectID=$objectID&entry=" . $this->repo->encodePath($infoPath));
  543. }
  544. }
  545. return $infos;
  546. }
  547. /**
  548. * 获取分支与tag下拉菜单组件配置。
  549. * Get items of branch and tags menu.
  550. *
  551. * @param object $repo
  552. * @param string $branchID
  553. * @access protected
  554. * @return array
  555. */
  556. protected function getBranchAndTagItems($repo, $branchID)
  557. {
  558. /* Set branch or tag for git. */
  559. $branches = $tags = array();
  560. if(!in_array($repo->SCM, $this->config->repo->gitTypeList)) return array();
  561. $scm = $this->app->loadClass('scm');
  562. $scm->setEngine($repo);
  563. $branches = $scm->branch();
  564. $initTags = $scm->tags('');
  565. foreach($initTags as $tag) $tags[$tag] = $tag;
  566. $selected = '';
  567. $branchMenus = array();
  568. $tagMenus = array();
  569. foreach($branches as $branchName)
  570. {
  571. $selected = ($branchName == $branchID) ? $branchName : $selected;
  572. $branchMenus[] = array(
  573. 'text' => $branchName,
  574. 'id' => $branchName,
  575. 'keys' => zget(common::convert2Pinyin(array($branchName)), $branchName, ''),
  576. 'url' => 'javascript:;',
  577. 'data-type' => 'branch',
  578. 'data-value' => $branchName,
  579. );
  580. }
  581. foreach($tags as $tagName)
  582. {
  583. $selected = ($tagName == $branchID) ? $tagName : $selected;
  584. $tagMenus[] = array(
  585. 'text' => $tagName,
  586. 'id' => $tagName,
  587. 'keys' => zget(common::convert2Pinyin(array($tagName)), $tagName, ''),
  588. 'url' => 'javascript:;',
  589. 'data-type' => 'tag',
  590. 'data-value' => $tagName,
  591. );
  592. }
  593. return array('branchMenus' => $branchMenus, 'tagMenus' => $tagMenus, 'selected' => $selected);
  594. }
  595. /**
  596. * 更新版本库最后提交时间。
  597. * Update repo last commited date.
  598. *
  599. * @param object $repo
  600. * @param object $lastRevision
  601. * @access protected
  602. * @return void
  603. */
  604. protected function updateLastCommit($repo, $lastRevision)
  605. {
  606. if(empty($lastRevision->committed_date)) return;
  607. $lastCommitDate = date('Y-m-d H:i:s', strtotime($lastRevision->committed_date));
  608. if(empty($repo->lastCommit) || $lastCommitDate > $repo->lastCommit) $this->dao->update(TABLE_REPO)->set('lastCommit')->eq($lastCommitDate)->where('id')->eq($repo->id)->exec();
  609. }
  610. /**
  611. * 获取browse方法项目、分支、tags信息。
  612. * Get project、branches、tags info for browse method.
  613. *
  614. * @param object $repo
  615. * @access protected
  616. * @return array
  617. */
  618. protected function getBrowseInfo($repo)
  619. {
  620. if($repo->SCM == 'Gitlab')
  621. {
  622. $scm = $this->app->loadClass('scm');
  623. $scm->setEngine($repo);
  624. $urls['project']['url'] = $scm->engine->getApiUrl("project");
  625. $urls['branches']['url'] = $scm->engine->getApiUrl('branches');
  626. $urls['tags']['url'] = $scm->engine->getApiUrl('tags');
  627. $this->app->loadClass('requests', true);
  628. $result = requests::request_multiple($urls);
  629. if($result['project']->status_code == 200)
  630. {
  631. $project = json_decode($result['project']->body);
  632. if(!is_object($project)) $project = new stdclass();
  633. $this->loadModel('gitlab')->setProject((int)$repo->gitService, (int)$repo->serviceProject, $project);
  634. }
  635. if(!empty($result['branches']->headers) && !is_null($result['branches']->headers->offsetGet('x-total')))
  636. {
  637. $branchList = json_decode($result['branches']->body);
  638. $totalPages = $result['branches']->headers->offsetGet('x-total-pages');
  639. if($totalPages > 1)
  640. {
  641. $requests = array();
  642. for($page = 2; $page <= $totalPages; $page++)
  643. {
  644. $requests[$page]['url'] = str_replace('page=1', "page={$page}", $urls['branches']['url']);
  645. }
  646. $reponses = requests::request_multiple($requests, array('timeout' => 10));
  647. foreach($reponses as $reponse)
  648. {
  649. $data = json_decode($reponse->body);
  650. if(!is_array($data)) continue;
  651. $branchList = array_merge($branchList, $data);
  652. }
  653. }
  654. $branches = array();
  655. $default = array();
  656. if(!empty($branchList) && is_array($branchList))
  657. {
  658. foreach($branchList as $branch)
  659. {
  660. if(!isset($branch->name)) continue;
  661. if($branch->default)
  662. {
  663. $default[$branch->name] = $branch->name;
  664. }
  665. else
  666. {
  667. $branches[$branch->name] = $branch->name;
  668. }
  669. }
  670. if(empty($branches) and empty($default)) $branches['master'] = 'master';
  671. asort($branches);
  672. $branches = $default + $branches;
  673. }
  674. }
  675. if(!empty($result['tags']->headers) && !is_null($result['tags']->headers->offsetGet('x-total')))
  676. {
  677. $tagList = json_decode($result['tags']->body);
  678. $totalPages = $result['tags']->headers->offsetGet('x-total-pages');
  679. if($totalPages > 1)
  680. {
  681. $requests = array();
  682. for($page = 2; $page <= $totalPages; $page++)
  683. {
  684. $requests[$page]['url'] = str_replace('page=1', "page={$page}", $urls['tags']['url']);
  685. }
  686. $reponses = requests::request_multiple($requests, array('timeout' => 10));
  687. foreach($reponses as $reponse)
  688. {
  689. $data = json_decode($reponse->body);
  690. if(!is_array($data)) continue;
  691. $tagList = array_merge($tagList, $data);
  692. }
  693. }
  694. $tags = array();
  695. if(!empty($tagList) && is_array($tagList))
  696. {
  697. foreach($tagList as $tag) $tags[] = $tag->name;
  698. }
  699. }
  700. return array(isset($branches) ? $branches : false, isset($tags) ? $tags : false);
  701. }
  702. }
  703. /**
  704. * 为git类型版本库设置分支和tag。
  705. * Set branch or tag for git.
  706. *
  707. * @param object $repo
  708. * @param string $branchID
  709. * @access protected
  710. * @return array
  711. */
  712. protected function setBranchTag($repo, $branchID)
  713. {
  714. if(in_array($repo->SCM, $this->config->repo->gitTypeList))
  715. {
  716. if($repo->SCM == 'Gitlab') list($branchInfo, $tagInfo) = $this->getBrowseInfo($repo);
  717. $scm = $this->app->loadClass('scm');
  718. $scm->setEngine($repo);
  719. $branches = isset($branchInfo) && $branchInfo !== false ? $branchInfo : $scm->branch();
  720. $initTags = isset($tagInfo) && $tagInfo !== false ? $tagInfo : $scm->tags('');
  721. $tags = array();
  722. foreach($initTags as $tag) $tags[$tag] = $tag;
  723. if(empty($branchID) and $this->cookie->repoBranch && $this->session->repoID == $repo->id) $branchID = $this->cookie->repoBranch;
  724. if(!isset($branches[$branchID]) && !isset($tags[$branchID])) $branchID = (string)key($branches);
  725. if($branchID) $this->setRepoBranch($branchID);
  726. return array($branchID, $branches, $tags);
  727. }
  728. else
  729. {
  730. $this->setRepoBranch('');
  731. return array($branchID, array(), array());
  732. }
  733. }
  734. /**
  735. * 获取commits列表
  736. * Get commits.
  737. *
  738. * @param object $repo
  739. * @param string $path
  740. * @param string $revision
  741. * @param string $type
  742. * @param object $pager
  743. * @param int $objectID
  744. * @access protected
  745. * @return array
  746. */
  747. protected function getCommits($repo, $path, $revision, $type, $pager, $objectID)
  748. {
  749. $revisions = $this->repo->getCommits($repo, $path, $revision, $type, $pager);
  750. $pathInfo = '&root=' . $this->repo->encodePath(empty($path) ? '/' : $path);
  751. foreach($revisions as $item)
  752. {
  753. $item->link = $this->repo->createLink('revision', "repoID={$repo->id}&objectID=$objectID&revision={$item->revision}" . $pathInfo);
  754. $item->revision = ($repo->SCM != 'Subversion' && $item->revision) ? substr($item->revision, 0, 10) : $item->revision;
  755. }
  756. return $revisions;
  757. }
  758. /**
  759. * 设置session信息。
  760. * Set session.
  761. *
  762. * @access protected
  763. * @return void
  764. */
  765. protected function setBrowseSession()
  766. {
  767. $this->setBackSession('list', true);
  768. session_start();
  769. $this->session->set('revisionList', $this->app->getURI(true));
  770. $this->session->set('gitlabBranchList', $this->app->getURI(true));
  771. session_write_close();
  772. }
  773. /**
  774. * 构建版本库搜索框。
  775. * Build repo search form.
  776. *
  777. * @param array $products
  778. * @param array $projects
  779. * @param int $objectID
  780. * @param string $orderBy
  781. * @param int $recPerPage
  782. * @param int $pageID
  783. * @param int $param
  784. * @access protected
  785. * @return void
  786. */
  787. protected function buildRepoSearchForm($products, $projects, $objectID, $orderBy, $recPerPage, $pageID, $param)
  788. {
  789. session_start();
  790. $this->config->repo->search['params']['product']['values'] = $products;
  791. $this->config->repo->search['params']['projects']['values'] = $projects;
  792. $this->config->repo->search['actionURL'] = $this->createLink('repo', 'maintain', "objectID={$objectID}&orderBy={$orderBy}&recPerPage={$recPerPage}&pageID={$pageID}&type=bySearch&param=myQueryID");
  793. $this->config->repo->search['queryID'] = $param;
  794. $this->config->repo->search['onMenuBar'] = 'yes';
  795. $this->loadModel('search')->setSearchParams($this->config->repo->search);
  796. session_write_close();
  797. }
  798. /**
  799. * 获取关联产品模块。
  800. * Get link product modules.
  801. *
  802. * @param array $products
  803. * @param string $type story|task|bug
  804. * @access protected
  805. * @return array
  806. */
  807. protected function getLinkModules($products, $type)
  808. {
  809. $modules = array();
  810. foreach($products as $productID => $product)
  811. {
  812. $productModules = $this->loadModel('tree')->getModulePairs($productID, $type);
  813. foreach($productModules as $key => $module) $modules[$key] = $product->name . ' / ' . $module;
  814. }
  815. return $modules;
  816. }
  817. /**
  818. * 获取关联产品分支。
  819. * Get link product branches.
  820. *
  821. * @param array $products
  822. * @access protected
  823. * @return array
  824. */
  825. protected function getLinkBranches($products)
  826. {
  827. $productBranches = array();
  828. foreach($products as $product)
  829. {
  830. if($product->type != 'normal')
  831. {
  832. $branches = $this->loadModel('branch')->getPairs($product->id, 'noempty');
  833. foreach($branches as $branchID => $branchName)
  834. {
  835. $branches[$branchID] = $product->name . ' / ' . $branchName;
  836. }
  837. $productBranches += $branches;
  838. }
  839. }
  840. return $productBranches;
  841. }
  842. /**
  843. * 获取产品关联执行列表。
  844. * Get link product executions.
  845. *
  846. * @param array $products
  847. * @access protected
  848. * @return array
  849. */
  850. protected function getLinkExecutions($products)
  851. {
  852. $executions = array();
  853. foreach($products as $product)
  854. {
  855. $productExecutions = $this->loadModel('product')->getExecutionPairsByProduct($product->id);
  856. $executions += $productExecutions;
  857. }
  858. return $executions;
  859. }
  860. /**
  861. * 构建需求搜索表格。
  862. * Build story search form.
  863. *
  864. * @param int $repoID
  865. * @param string $revision
  866. * @param int $queryID
  867. * @param array $products
  868. * @param array $modules
  869. * @access protected
  870. * @return void
  871. * @param string $browseType
  872. */
  873. protected function buildStorySearchForm($repoID, $revision, $browseType, $queryID, $products, $modules)
  874. {
  875. unset($this->lang->story->statusList['closed']);
  876. $storyStatusList = $this->lang->story->statusList;
  877. $this->config->product->search['actionURL'] = $this->createLink('repo', 'linkStory', "repoID=$repoID&revision=$revision&browseType=bySearch&queryID=myQueryID");
  878. $this->config->product->search['queryID'] = $queryID;
  879. $this->config->product->search['style'] = 'simple';
  880. $this->config->product->search['params']['plan']['values'] = $this->loadModel('productplan')->getForProducts(array_keys($products));
  881. $this->config->product->search['params']['module']['values'] = $modules;
  882. $this->config->product->search['params']['status'] = array('operator' => '=', 'control' => 'select', 'values' => $storyStatusList);
  883. $this->config->product->search['params']['product']['values'] = helper::arrayColumn($products, 'name', 'id');
  884. unset($this->config->product->search['fields']['roadmap']);
  885. unset($this->config->product->search['params']['roadmap']);
  886. unset($this->config->product->search['fields']['grade']);
  887. unset($this->config->product->search['params']['grade']);
  888. $productBranches = $this->getLinkBranches($products);
  889. if(empty($productBranches))
  890. {
  891. unset($this->config->product->search['fields']['branch']);
  892. unset($this->config->product->search['params']['branch']);
  893. }
  894. else
  895. {
  896. $this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName['branch']);
  897. $this->config->product->search['fields']['branch'] = sprintf($this->lang->product->branch, $this->lang->product->branchName['branch']);
  898. $this->config->product->search['params']['branch']['values'] = $productBranches;
  899. }
  900. session_start();
  901. $this->loadModel('search')->setSearchParams($this->config->product->search);
  902. session_write_close();
  903. }
  904. /**
  905. * 获取关联需求列表。
  906. * Get link stories list.
  907. *
  908. * @param int $repoID
  909. * @param string $revision
  910. * @param string $browseType
  911. * @param array $productIds
  912. * @param string $orderBy
  913. * @param object $pager
  914. * @param int $queryID
  915. * @access protected
  916. * @return array
  917. * @param mixed[] $products
  918. */
  919. protected function getLinkStories($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID)
  920. {
  921. $linkedStories = $this->repo->getRelationByCommit($repoID, $revision, 'story');
  922. $allStories = array();
  923. if($browseType == 'bySearch')
  924. {
  925. foreach($products as $productID => $product)
  926. {
  927. $productStories = $this->loadModel('story')->getBySearch($productID, 'all', $queryID, $orderBy, 0, 'story', array_keys($linkedStories));
  928. $allStories = array_merge($allStories, $productStories);
  929. }
  930. $allStories = array_filter($allStories, function($story) { return $story->isParent == '0'; });
  931. }
  932. else
  933. {
  934. foreach($products as $productID => $product)
  935. {
  936. $productStories = $this->loadModel('story')->getProductStories($productID, 'all', '0', 'draft,active,changed', 'story', $orderBy, true, array_keys($linkedStories));
  937. $allStories = array_merge($allStories, $productStories);
  938. }
  939. }
  940. return $this->getDataPager($allStories, $pager);
  941. }
  942. /**
  943. * 获取数据根据分页。
  944. * Get data by pager.
  945. *
  946. * @param array $data
  947. * @param object $pager
  948. * @access protected
  949. * @return array
  950. */
  951. protected function getDataPager($data, $pager)
  952. {
  953. $pager->setRecTotal(count($data));
  954. $pager->setPageTotal();
  955. $dataList = array_chunk($data, $pager->recPerPage);
  956. $pageData = empty($dataList) ? array() : $dataList[$pager->pageID - 1];
  957. return $pageData;
  958. }
  959. /**
  960. * 构建bug搜索表格。
  961. * Build bug search form.
  962. *
  963. * @param int $repoID
  964. * @param string $revision
  965. * @param string $browseType
  966. * @param int $queryID
  967. * @param array $products
  968. * @param array $modules
  969. * @access protected
  970. * @return void
  971. */
  972. protected function buildBugSearchForm($repoID, $revision, $browseType, $queryID, $products, $modules)
  973. {
  974. $productIds = array_keys($products);
  975. $this->config->bug->search['params']['status']['values'] = array_slice($this->lang->bug->statusList, 1, 1);
  976. $this->config->bug->search['actionURL'] = $this->createLink('repo', 'linkBug', "repoID=$repoID&revision=$revision&browseType=bySearch&queryID=myQueryID");
  977. $this->config->bug->search['queryID'] = $queryID;
  978. $this->config->bug->search['style'] = 'simple';
  979. $this->config->bug->search['params']['plan']['values'] = $this->loadModel('productplan')->getForProducts($productIds);
  980. $this->config->bug->search['params']['module']['values'] = $modules;
  981. $this->config->bug->search['params']['execution']['values'] = $this->getLinkExecutions($products);
  982. $this->config->bug->search['params']['openedBuild']['values'] = $this->loadModel('build')->getBuildPairs($productIds, 'all', '');
  983. $this->config->bug->search['params']['resolvedBuild']['values'] = $this->loadModel('build')->getBuildPairs($productIds, 'all', '');
  984. $this->config->bug->search['params']['product']['values'] = helper::arrayColumn($products, 'name', 'id');
  985. $productBranches = $this->getLinkBranches($products);
  986. if(empty($productBranches))
  987. {
  988. unset($this->config->bug->search['fields']['branch']);
  989. unset($this->config->bug->search['params']['branch']);
  990. }
  991. else
  992. {
  993. $this->lang->product->branch = sprintf($this->lang->product->branch, $this->lang->product->branchName['branch']);
  994. $this->config->bug->search['fields']['branch'] = sprintf($this->lang->product->branch, $this->lang->product->branchName['branch']);
  995. $this->config->bug->search['params']['branch']['values'] = $productBranches;
  996. }
  997. session_start();
  998. $this->loadModel('search')->setSearchParams($this->config->bug->search);
  999. session_write_close();
  1000. }
  1001. /**
  1002. * 获取关联bug列表。
  1003. * Get link bugs list.
  1004. *
  1005. * @param int $repoID
  1006. * @param string $revision
  1007. * @param string $browseType
  1008. * @param array $products
  1009. * @param string $orderBy
  1010. * @param object $pager
  1011. * @param int $queryID
  1012. * @access protected
  1013. * @return array
  1014. */
  1015. protected function getLinkBugs($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID)
  1016. {
  1017. $linkedBugs = $this->repo->getRelationByCommit($repoID, $revision, 'bug');
  1018. $allBugs = array();
  1019. if($browseType == 'bySearch')
  1020. {
  1021. $allBugs = $this->loadModel('bug')->getBySearch('bug', array_keys($products), 0, 0, 0, $queryID, implode(',', array_keys($linkedBugs)), $orderBy);
  1022. foreach($allBugs as $bugID => $bug)
  1023. {
  1024. if($bug->status != 'active') unset($allBugs[$bugID]);
  1025. }
  1026. }
  1027. else
  1028. {
  1029. foreach($products as $productID => $product)
  1030. {
  1031. $productBugs = $this->loadModel('bug')->getActiveBugs($product->id, 0, '0', array_keys($linkedBugs), null, $orderBy);
  1032. $allBugs = array_merge($allBugs, $productBugs);
  1033. }
  1034. }
  1035. $allBugs = $this->getDataPager($allBugs, $pager);
  1036. foreach($allBugs as $bug) $bug->statusText = $this->processStatus('bug', $bug);
  1037. return $allBugs;
  1038. }
  1039. /**
  1040. * 构建任务搜索表格。
  1041. * Build task search form.
  1042. *
  1043. * @param int $repoID
  1044. * @param string $revision
  1045. * @param string $browseType
  1046. * @param int $queryID
  1047. * @param array $modules
  1048. * @param array $executionPairs
  1049. * @access protected
  1050. * @return void
  1051. */
  1052. protected function buildTaskSearchForm($repoID, $revision, $browseType, $queryID, $modules, $executionPairs)
  1053. {
  1054. $this->config->execution->search['actionURL'] = $this->createLink('repo', 'linkTask', "repoID=$repoID&revision=$revision&browseType=bySearch&queryID=myQueryID", '', true);
  1055. $this->config->execution->search['queryID'] = $queryID;
  1056. $this->config->execution->search['style'] = 'simple';
  1057. $this->config->execution->search['params']['module']['values'] = $modules;
  1058. $this->config->execution->search['params']['execution']['values'] = array('' => '') + $executionPairs;
  1059. session_start();
  1060. $this->loadModel('search')->setSearchParams($this->config->execution->search);
  1061. session_write_close();
  1062. }
  1063. /**
  1064. * 获取关联任务列表。
  1065. * Get link tasks list.
  1066. *
  1067. * @param int $repoID
  1068. * @param string $revision
  1069. * @param string $browseType
  1070. * @param array $products
  1071. * @param string $orderBy
  1072. * @param object $pager
  1073. * @param int $queryID
  1074. * @param array $executionPairs
  1075. * @access protected
  1076. * @return array
  1077. */
  1078. protected function getLinkTasks($repoID, $revision, $browseType, $products, $orderBy, $pager, $queryID, $executionPairs)
  1079. {
  1080. $allTasks = array();
  1081. foreach($executionPairs as $executionID => $executionName)
  1082. {
  1083. $tasks = $this->loadModel('execution')->getTasks(0, $executionID, array(), $browseType, $queryID, 0, $orderBy, null);
  1084. $allTasks += $tasks;
  1085. }
  1086. if($browseType == 'bysearch')
  1087. {
  1088. foreach($allTasks as $key => $task)
  1089. {
  1090. if(!empty($task->children))
  1091. {
  1092. $allTasks = array_merge($task->children, $allTasks);
  1093. unset($task->children);
  1094. }
  1095. }
  1096. foreach($allTasks as $key => $task)
  1097. {
  1098. if($task->status == 'closed') unset($allTasks[$key]);
  1099. }
  1100. }
  1101. /* Filter linked tasks. */
  1102. $linkedTasks = $this->repo->getRelationByCommit($repoID, $revision, 'task');
  1103. $linkedTaskIDs = array_keys($linkedTasks);
  1104. foreach($allTasks as $key => $task)
  1105. {
  1106. if(in_array($task->id, $linkedTaskIDs)) unset($allTasks[$key]);
  1107. }
  1108. return $this->getDataPager($allTasks, $pager);
  1109. }
  1110. /**
  1111. * 关联对象。
  1112. * Link object.
  1113. *
  1114. * @param int $repoID
  1115. * @param string $revision
  1116. * @param string $type story|bug|task
  1117. * @access protected
  1118. * @return array
  1119. */
  1120. protected function linkObject($repoID, $revision, $type)
  1121. {
  1122. $this->repo->link($repoID, $revision, $type);
  1123. if(dao::isError()) return array('result' => 'fail', 'message' => dao::getError());
  1124. return array('result' => 'success', 'callback' => "$('.tab-content .active iframe')[0].contentWindow.getRelation('$revision')", 'closeModal' => true);
  1125. }
  1126. /**
  1127. * Get SCM by service host.
  1128. *
  1129. * @param int $serviceHost
  1130. * @access protected
  1131. * @return string
  1132. */
  1133. protected function getSCM($serviceHost)
  1134. {
  1135. $server = $this->loadModel('pipeline')->getByID($serviceHost);
  1136. foreach($this->lang->repo->scmList as $scmKey => $scmLang)
  1137. {
  1138. if($server->type == strtolower($scmKey)) return $scmKey;
  1139. }
  1140. return '';
  1141. }
  1142. /**
  1143. * 检查删除版本库的错误。
  1144. * Check repo delete error.
  1145. *
  1146. * @param int $repoID
  1147. * @access protected
  1148. * @return string
  1149. */
  1150. protected function checkDeleteError($repoID)
  1151. {
  1152. $relationIds = $this->dao->select('distinct AID as AID')->from(TABLE_RELATION)
  1153. ->where('extra')->eq($repoID)
  1154. ->andWhere('AType')->eq('design')
  1155. ->fetchAll();
  1156. $error = '';
  1157. if($relationIds)
  1158. {
  1159. $tmpDesignLinks = [];
  1160. foreach ($relationIds as $value)
  1161. {
  1162. array_push($tmpDesignLinks, html::a($this->createLink('design', 'view', 'designID=' . $value->AID), $value->AID, '_blank', '', false));
  1163. }
  1164. $error .= sprintf($this->lang->repo->error->deleted, implode(', ', $tmpDesignLinks));
  1165. }
  1166. $linkBranchs = $this->repo->getLinkedBranch(0, '', $repoID);
  1167. if(!empty($linkBranchs))
  1168. {
  1169. $tmpLinkBranchs = [];
  1170. foreach($linkBranchs as $value)
  1171. {
  1172. if(!array_key_exists($value->AType, $tmpLinkBranchs)) $tmpLinkBranchs[$value->AType] = [];
  1173. if(!in_array($value->BType, $tmpLinkBranchs[$value->AType])) array_push($tmpLinkBranchs[$value->AType], $value->BType);
  1174. }
  1175. foreach($tmpLinkBranchs as $type=>$value)
  1176. {
  1177. $error .= sprintf($this->lang->repo->error->linkedBranch, $this->lang->$type->common, html::a(
  1178. $this->createLink('repo', 'browse', 'repoID=' . $repoID),
  1179. implode(', ', $value), '_blank', '', false
  1180. ));
  1181. }
  1182. }
  1183. $jobs = $this->dao->select('*')->from(TABLE_JOB)->where('repo')->eq($repoID)->andWhere('deleted')->eq('0')->fetchAll();
  1184. if($jobs) $error .= sprintf($this->lang->repo->error->linkedJob, html::a($this->createLink('job', 'browse'), implode(', ', array_column($jobs, 'id')), '_blank', '', false));
  1185. return $error;
  1186. }
  1187. /**
  1188. * 跳转到版本库的diff页面。
  1189. * Redirect to diff page.
  1190. *
  1191. * @param int $repoID
  1192. * @param int $objectID
  1193. * @param string $arrange
  1194. * @param int $isBranchOrTag
  1195. * @param string $file
  1196. * @access protected
  1197. * @return void
  1198. */
  1199. protected function locateDiffPage($repoID, $objectID, $arrange, $isBranchOrTag, $file)
  1200. {
  1201. $oldRevision = isset($this->post->revision[1]) ? $this->post->revision[1] : '';
  1202. $newRevision = isset($this->post->revision[0]) ? $this->post->revision[0] : '';
  1203. if($this->post->encoding) $encoding = $this->post->encoding;
  1204. if($this->post->isBranchOrTag) $isBranchOrTag = (int)$this->post->isBranchOrTag;
  1205. if($this->post->arrange) $arrange = $this->post->arrange;
  1206. helper::setcookie('arrange', $arrange);
  1207. return $this->locate($this->repo->createLink('diff', "repoID={$repoID}&objectID={$objectID}&entry={$file}&oldrevision={$oldRevision}&newRevision={$newRevision}&showBug=0&encoding={$encoding}&isBranchOrTag={$isBranchOrTag}"));
  1208. }
  1209. /**
  1210. * 设置对比信息的编码格式。
  1211. * Set encoding for diff.
  1212. *
  1213. * @param array $diffs
  1214. * @param string $encoding
  1215. * @access protected
  1216. * @return array
  1217. */
  1218. protected function encodingDiff($diffs, $encoding)
  1219. {
  1220. foreach($diffs as $diff)
  1221. {
  1222. $diff->fileName = helper::convertEncoding($diff->fileName, $encoding);
  1223. if(empty($diff->contents)) continue;
  1224. foreach($diff->contents as $content)
  1225. {
  1226. if(empty($content->lines)) continue;
  1227. foreach($content->lines as $lines)
  1228. {
  1229. if(empty($lines->line)) continue;
  1230. $lines->line = helper::convertEncoding($lines->line, $encoding);
  1231. }
  1232. }
  1233. }
  1234. return $diffs;
  1235. }
  1236. /**
  1237. * 获取代码同步本地的日志。
  1238. * Get sync log.
  1239. *
  1240. * @param object $repo
  1241. * @access protected
  1242. * @return string
  1243. */
  1244. protected function syncLocalCommit($repo)
  1245. {
  1246. $logFile = realPath($this->app->getTmpRoot() . $this->config->repo->repoSyncLog->logFilePrefix . strtolower($repo->SCM) . ".{$repo->name}.log");
  1247. if($logFile && file_exists($logFile))
  1248. {
  1249. $content = file($logFile);
  1250. foreach($content as $line)
  1251. {
  1252. if($this->strposAry($line, $this->config->repo->repoSyncLog->fatal) !== false) return $line;
  1253. if($this->strposAry($line, $this->config->repo->repoSyncLog->failed) !== false) return $line;
  1254. }
  1255. $lastLine = $content[count($content) - 1];
  1256. if($this->strposAry($lastLine, $this->config->repo->repoSyncLog->done) === false)
  1257. {
  1258. if($this->strposAry($lastLine, $this->config->repo->repoSyncLog->emptyRepo) !== false)
  1259. {
  1260. @unlink($logFile);
  1261. }
  1262. elseif($this->strposAry($lastLine, $this->config->repo->repoSyncLog->total) !== false)
  1263. {
  1264. $logContent = file_get_contents($logFile);
  1265. if($this->strposAry($logContent, $this->config->repo->repoSyncLog->finishCount) !== false and $this->strposAry($logContent, $this->config->repo->repoSyncLog->finishCompress) !== false)
  1266. {
  1267. @unlink($logFile);
  1268. }
  1269. else
  1270. {
  1271. return $this->config->repo->repoSyncLog->one;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. return $this->config->repo->repoSyncLog->one;
  1277. }
  1278. }
  1279. else
  1280. {
  1281. @unlink($logFile);
  1282. }
  1283. }
  1284. return '';
  1285. }
  1286. /**
  1287. * 获取需要同步的分支。
  1288. * Get sync branches.
  1289. *
  1290. * @param object $repo
  1291. * @param string $branchID
  1292. * @access protected
  1293. * @return array
  1294. */
  1295. protected function getSyncBranches($repo, &$branchID = '')
  1296. {
  1297. $branches = array();
  1298. if(in_array($repo->SCM, $this->config->repo->gitTypeList))
  1299. {
  1300. $branches = $this->scm->branch();
  1301. if(empty($branches)) return array();
  1302. $tags = $this->scm->tags('');
  1303. foreach($tags as $tag) $branches[$tag] = $tag;
  1304. if($branches)
  1305. {
  1306. /* Init branchID. */
  1307. if($this->cookie->syncBranch) $branchID = $this->cookie->syncBranch;
  1308. if(!isset($branches[$branchID])) $branchID = '';
  1309. if(empty($branchID)) $branchID = key($branches);
  1310. /* Get unsynced branches. */
  1311. foreach($branches as $branch)
  1312. {
  1313. unset($branches[$branch]);
  1314. if($branch == $branchID) break;
  1315. }
  1316. $this->setRepoBranch($branchID);
  1317. helper::setcookie("syncBranch", $branchID, 0, $this->config->webRoot, '', $this->config->cookieSecure, true);
  1318. }
  1319. }
  1320. return $branches;
  1321. }
  1322. /**
  1323. * 获取同步结果。
  1324. * Get sync result.
  1325. *
  1326. * @param object $repo
  1327. * @param array $branches
  1328. * @param string $branchID
  1329. * @param int $commitCount
  1330. * @param string $type
  1331. * @access protected
  1332. * @return string|int
  1333. */
  1334. protected function checkSyncResult($repo, $branches, $branchID, $commitCount, $type)
  1335. {
  1336. if(empty($commitCount) && !$repo->synced)
  1337. {
  1338. if(in_array($repo->SCM, $this->config->repo->gitTypeList))
  1339. {
  1340. if($branchID) $this->repo->saveExistCommits4Branch($repo->id, $branchID);
  1341. if($branches)
  1342. {
  1343. $branchID = array_shift($branches);
  1344. helper::setcookie("syncBranch", $branchID);
  1345. }
  1346. else
  1347. {
  1348. $branchID = '';
  1349. }
  1350. if($branchID) $this->repo->fixCommit($repo->id);
  1351. }
  1352. if(empty($branchID) || in_array($repo->SCM, $this->config->repo->notSyncSCM))
  1353. {
  1354. helper::setcookie("syncBranch", '');
  1355. $this->repo->markSynced($repo->id);
  1356. return $this->config->repo->repoSyncLog->finish;
  1357. }
  1358. }
  1359. $this->dao->update(TABLE_REPO)->set('commits=commits + ' . $commitCount)->where('id')->eq($repo->id)->exec();
  1360. return $type == 'batch' ? $commitCount : $this->config->repo->repoSyncLog->finish;
  1361. }
  1362. /**
  1363. * 设置返回链接。
  1364. * Set back session.
  1365. *
  1366. * @param string $type
  1367. * @param bool $withOtherModule
  1368. * @access public
  1369. * @return void
  1370. */
  1371. public function setBackSession($type = 'list', $withOtherModule = false)
  1372. {
  1373. session_start();
  1374. $uri = $this->app->getURI(true);
  1375. if(!empty($_GET) and $this->config->requestType == 'PATH_INFO') $uri .= (strpos($uri, '?') === false ? '?' : '&') . http_build_query($_GET);
  1376. $backKey = 'repo' . ucfirst(strtolower($type));
  1377. $this->session->set($backKey, $uri);
  1378. if($type == 'list') unset($_SESSION['repoView']);
  1379. if($withOtherModule)
  1380. {
  1381. $this->session->set('bugList', $uri, 'qa');
  1382. $this->session->set('taskList', $uri, 'execution');
  1383. }
  1384. session_write_close();
  1385. }
  1386. /**
  1387. * 设置代码库分支。
  1388. * Set repo branch.
  1389. *
  1390. * @param string $branch
  1391. * @access public
  1392. * @return void
  1393. */
  1394. public function setRepoBranch($branch)
  1395. {
  1396. helper::setcookie("repoBranch", $branch, 0, $this->config->webRoot, '', $this->config->cookieSecure, false);
  1397. $_COOKIE['repoBranch'] = $branch;
  1398. }
  1399. /**
  1400. * 检查是否是二进制文件。
  1401. * Check content is binary.
  1402. *
  1403. * @param string $content
  1404. * @param string $suffix
  1405. * @access public
  1406. * @return bool
  1407. */
  1408. public function isBinary($content, $suffix = '')
  1409. {
  1410. if(strpos($this->config->repo->binary, "|$suffix|") !== false) return true;
  1411. $blk = substr($content, 0, 512);
  1412. return (
  1413. substr_count($blk, "^\r\n")/512 > 0.3 ||
  1414. substr_count($blk, "^ -~")/512 > 0.3 ||
  1415. substr_count($blk, "\x00") > 0
  1416. );
  1417. }
  1418. /**
  1419. * 检查字符串是否在数组元素中。
  1420. * Check str in array.
  1421. *
  1422. * @param string $str
  1423. * @param array $checkAry
  1424. * @access public
  1425. * @return bool
  1426. */
  1427. public function strposAry($str, $checkAry)
  1428. {
  1429. foreach($checkAry as $check)
  1430. {
  1431. if(mb_strpos($str, $check) !== false) return true;
  1432. }
  1433. return false;
  1434. }
  1435. /**
  1436. * 获取详情页面目录树。
  1437. * Get view tree.
  1438. *
  1439. * @param object $repo
  1440. * @param string $entry
  1441. * @param string $revision
  1442. * @access protected
  1443. * @return array
  1444. */
  1445. protected function getViewTree($repo, $entry, $revision)
  1446. {
  1447. if($repo->SCM == 'Gitlab') return $this->repo->getGitlabFilesByPath($repo, '', (string)$this->cookie->repoBranch);
  1448. if($repo->SCM != 'Subversion') return $this->repo->getFileTree($repo);
  1449. $scm = $this->app->loadClass('scm');
  1450. $scm->setEngine($repo);
  1451. $tree = $scm->ls($entry, (string)$revision);
  1452. foreach($tree as &$file)
  1453. {
  1454. $base64Name = $this->repo->encodePath($file->path);
  1455. $file->path = trim($file->path, '/');
  1456. if(!isset($file->id)) $file->id = $base64Name;
  1457. if(!isset($file->key)) $file->key = $base64Name;
  1458. if(!isset($file->text)) $file->text = trim($file->name, '/');
  1459. if($file->kind == 'dir') $file->items = array('url' => helper::createLink('repo', 'ajaxGetFiles', "repoID={$repo->id}&branch={$revision}&path=" . helper::safe64Encode($file->path)));
  1460. }
  1461. return $tree;
  1462. }
  1463. /**
  1464. * 检查代码库是否能正常访问。
  1465. * Check repo connected.
  1466. *
  1467. * @param object $repo
  1468. * @access protected
  1469. * @return bool
  1470. */
  1471. protected function checkRepoInternet($repo)
  1472. {
  1473. if(!$repo) return false;
  1474. $repoUrl = '';
  1475. if(empty($repoUrl) && isset($repo->path) && substr($repo->path, 0, 4) == 'http') $repoUrl = $repo->path;
  1476. if(empty($repoUrl) && isset($repo->client) && substr($repo->client, 0, 4) == 'http') $repoUrl = $repo->client;
  1477. if(empty($repoUrl) && isset($repo->apiPath) && substr($repo->apiPath, 0, 4) == 'http') $repoUrl = $repo->apiPath;
  1478. return $repoUrl && !$this->loadModel('admin')->checkInternet($repoUrl, 3);
  1479. }
  1480. /**
  1481. * 翻译API返回错误信息。
  1482. * Parse api log to client lang.
  1483. *
  1484. * @param string $message
  1485. * @access protected
  1486. * @return string
  1487. */
  1488. protected function parseErrorContent($message)
  1489. {
  1490. foreach($this->lang->repo->apiError as $key => $pattern)
  1491. {
  1492. if(preg_match("/$pattern/i", $message))
  1493. {
  1494. $message = zget($this->lang->repo->errorLang, $key);
  1495. break;
  1496. }
  1497. }
  1498. return $message;
  1499. }
  1500. /**
  1501. * 构建代码库路径。
  1502. * Build repo paths.
  1503. *
  1504. * @param array $repos
  1505. * @access protected
  1506. * @return array
  1507. */
  1508. protected function buildRepoPaths($repos)
  1509. {
  1510. $pathList = array();
  1511. foreach($repos as $repoID => $path)
  1512. {
  1513. $paths = explode('/', $path);
  1514. $parent = '';
  1515. foreach($paths as $path)
  1516. {
  1517. $path = trim($path);
  1518. if($path === '') continue;
  1519. $parentID = $parent == '' ? '0' : $pathList[$parent]['path'];
  1520. $parent .= $parent == '' ? $path : '/' . $path;
  1521. if(!isset($pathList[$parent]))
  1522. {
  1523. $pathList[$parent] = array(
  1524. 'value' => $repoID,
  1525. 'parent' => $parentID,
  1526. 'path' => $parent,
  1527. 'text' => $path,
  1528. );
  1529. }
  1530. }
  1531. }
  1532. ksort($pathList);
  1533. return $this->buildRepoTree($pathList, '0');
  1534. }
  1535. /**
  1536. * 组装代码库生成父子结构。
  1537. * Assemble repo path to generate parent-child structure.
  1538. *
  1539. * @param array $pathList
  1540. * @param string $parent
  1541. * @access protected
  1542. * @return array
  1543. */
  1544. protected function buildRepoTree($pathList = array(), $parent = '0')
  1545. {
  1546. $treeList = array();
  1547. $key = 0;
  1548. $pathName = array();
  1549. $repoName = array();
  1550. foreach($pathList as $path)
  1551. {
  1552. if ($path['parent'] == $parent)
  1553. {
  1554. $treeList[$key] = $path;
  1555. $repoName[$key] = $path['text'];
  1556. /* Default value is '~', because his ascii code is large in string. */
  1557. $pathName[$key] = '~';
  1558. $children = $this->buildRepoTree($pathList, $path['path']);
  1559. if($children)
  1560. {
  1561. unset($treeList[$key]['value']);
  1562. $treeList[$key]['disabled'] = true;
  1563. $treeList[$key]['items'] = $children;
  1564. $repoName[$key] = '';
  1565. $pathName[$key] = $path['path'];
  1566. }
  1567. }
  1568. $key++;
  1569. }
  1570. array_multisort($pathName, SORT_ASC, $repoName, SORT_ASC, $treeList);
  1571. return $treeList;
  1572. }
  1573. /**
  1574. * 获取分支和标签列表的picker数据。
  1575. * Get branch and tag picker data.
  1576. *
  1577. * @param object $scm
  1578. * @access protected
  1579. * @return array
  1580. */
  1581. protected function getBranchAndTagOptions($scm)
  1582. {
  1583. $options = array(
  1584. array('text' => $this->lang->repo->branch, 'items' => array(), 'disabled' => true),
  1585. array('text' => $this->lang->repo->tag, 'items' => array(), 'disabled' => true)
  1586. );
  1587. $branches = $scm->branch();
  1588. foreach($branches as $branch) $options[0]['items'][] = array('text' => $branch, 'value' => $branch, 'key' => $branch);
  1589. $tags = $scm->tags();
  1590. foreach($tags as $tag) $options[1]['items'][] = array('text' => $tag, 'value' => $tag, 'key' => $tag);
  1591. if(empty($tags)) unset($options[1]);
  1592. if(empty($branches)) unset($options[0]);
  1593. return $options;
  1594. }
  1595. /**
  1596. * 获取代码库ID,并且设置页面的代码库数据。
  1597. * Process repoID and set page repo data.
  1598. *
  1599. * @param int $repoID
  1600. * @param int $objectID
  1601. * @param array $scmList
  1602. * @access protected
  1603. * @return int
  1604. */
  1605. protected function processRepoID($repoID, $objectID, $scmList = array())
  1606. {
  1607. $hasSession = session_id() ? true : false;
  1608. if(!$hasSession) session_start();
  1609. if(!$repoID) $repoID = (int)$this->session->repoID;
  1610. $repoPairs = array();
  1611. if($this->app->tab == 'project' || $this->app->tab == 'execution')
  1612. {
  1613. if(!$scmList) $scmList = $this->config->repo->notSyncSCM;
  1614. $repoList = $this->repo->getList($objectID);
  1615. foreach($repoList as $repo)
  1616. {
  1617. if(!in_array($repo->SCM, $scmList)) continue;
  1618. $repoPairs[$repo->id] = $repo->name;
  1619. }
  1620. if(!isset($repoPairs[$repoID])) $this->locate(inLink('browse', "repoID=$repoID&objectID=$objectID"));
  1621. }
  1622. $this->view->repoID = $repoID;
  1623. $this->view->repoPairs = $repoPairs;
  1624. $repoID = $this->repo->saveState($repoID, $objectID);
  1625. if(!$hasSession) session_write_close();
  1626. return $repoID;
  1627. }
  1628. /**
  1629. * 构建提交页面搜索表单。
  1630. * Build commit search form.
  1631. *
  1632. * @param int $queryID
  1633. * @param string $actionURL
  1634. * @access protected
  1635. * @return void
  1636. */
  1637. protected function buildSearchForm($queryID, $actionURL)
  1638. {
  1639. session_start();
  1640. $this->config->repo->search = $this->config->repo->searchCommits;
  1641. $this->config->repo->search['actionURL'] = $actionURL;
  1642. $this->config->repo->search['queryID'] = $queryID;
  1643. $this->loadModel('search')->setSearchParams($this->config->repo->search);
  1644. session_write_close();
  1645. }
  1646. /**
  1647. * 获取搜索表单字段。
  1648. * Get search form field.
  1649. *
  1650. * @param int $queryID
  1651. * @param bool $getSql
  1652. * @access protected
  1653. * @return object|string
  1654. */
  1655. protected function getSearchForm($queryID = 0, $getSql = false)
  1656. {
  1657. session_start();
  1658. if($queryID)
  1659. {
  1660. $query = $this->loadModel('search')->getQuery($queryID);
  1661. if($query)
  1662. {
  1663. $this->session->set('repoCommitsQuery', $query->sql);
  1664. $this->session->set('repoCommitsForm', $query->form);
  1665. }
  1666. else
  1667. {
  1668. $this->session->set('repoCommitsQuery', ' 1 = 1');
  1669. }
  1670. }
  1671. if($getSql)
  1672. {
  1673. $query = $this->session->repoCommitsQuery;
  1674. $query = str_replace("`date`", 't1.`time`', $query);
  1675. $query = str_replace("`committer`", 't1.`committer`', $query);
  1676. $query = str_replace("`commit`", 't1.`revision`', $query);
  1677. }
  1678. else
  1679. {
  1680. $query = $this->getSearchFormQuery();
  1681. }
  1682. session_write_close();
  1683. return $query;
  1684. }
  1685. /**
  1686. * 获取搜索表单查询字段。
  1687. * Get search form query field.
  1688. *
  1689. * @access protected
  1690. * @return object
  1691. */
  1692. protected function getSearchFormQuery()
  1693. {
  1694. $query = new stdclass();
  1695. $query->begin = '';
  1696. $query->end = '';
  1697. $query->committer = '';
  1698. $query->commit = '';
  1699. if(!$this->session->repoCommitsForm) return $query;
  1700. $this->app->loadClass('date');
  1701. $lastWeek = date::getLastWeek();
  1702. $thisWeek = date::getThisWeek();
  1703. $lastMonth = date::getLastMonth();
  1704. $thisMonth = date::getThisMonth();
  1705. $yesterday = date::yesterday();
  1706. $today = date(DT_DATE1);
  1707. foreach($this->session->repoCommitsForm as $field)
  1708. {
  1709. if(empty($field['value'])) continue;
  1710. if(strpos($field['value'], '$') !== false)
  1711. {
  1712. $dateField = substr($field['value'], 1);
  1713. $query->begin = substr(${$dateField}['begin'], 0, 10) . ' 00:00:00';
  1714. $query->end = substr(${$dateField}['end'], 0, 10) . ' 23:59:59';
  1715. }
  1716. elseif($field['field'] == 'date')
  1717. {
  1718. if($field['operator'] == '>=' || $field['operator'] == '=') $query->begin = $field['value'];
  1719. if($field['operator'] == '>') $query->begin = date('Y-m-d', strtotime("{$field['value']} +1 day"));
  1720. if($field['operator'] == '<=') $query->end = $field['value'];
  1721. if($field['operator'] == '<') $query->end = date('Y-m-d', strtotime("{$field['value']} -1 day"));
  1722. if($field['operator'] == '=') $query->end = $field['value'] . ' 23:59:59';
  1723. }
  1724. elseif(in_array($field['field'], array('committer', 'commit')) && $field['value'])
  1725. {
  1726. $query->{$field['field']} = $field['value'];
  1727. }
  1728. }
  1729. return $query;
  1730. }
  1731. }