model.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * The model file of git module of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Yanyi Cao <caoyanyi@easycorp.com>
  8. * @package git
  9. * @link https://www.zentao.net
  10. */
  11. class gitModel extends model
  12. {
  13. /**
  14. * The git binary client.
  15. *
  16. * @var int
  17. * @access public
  18. */
  19. public $client;
  20. /**
  21. * Repos.
  22. *
  23. * @var array
  24. * @access public
  25. */
  26. public $repos = array();
  27. /**
  28. * The root path of a repo
  29. *
  30. * @var string
  31. * @access public
  32. */
  33. public $repoRoot = '';
  34. /**
  35. * Users
  36. *
  37. * @var array
  38. * @access public
  39. */
  40. public $users = array();
  41. /**
  42. * 执行定时任务同步提交信息。
  43. * Sync commit info by cron.
  44. *
  45. * @access public
  46. * @return bool
  47. */
  48. public function run()
  49. {
  50. /* Get repos and load module. */
  51. $this->setRepos();
  52. if(empty($this->repos)) return false;
  53. /* Get commit triggerType jobs by repoIdList. */
  54. $commentGroup = $this->loadModel('job')->getTriggerGroup('commit', array_keys($this->repos));
  55. /* Get tag triggerType jobs by repoIdList. */
  56. $tagGroup = $this->job->getTriggerGroup('tag', array_keys($this->repos));
  57. $this->loadModel('compile');
  58. $this->loadModel('gitlab');
  59. $this->loadModel('repo');
  60. foreach($this->repos as $repoID => $repo)
  61. {
  62. $this->updateCommit($repo, $commentGroup, true);
  63. if($repo->SCM == 'Gitlab')
  64. {
  65. $this->gitlab->updateCodePath((int)$repo->serviceHost, (int)$repo->serviceProject, (int)$repo->id);
  66. $this->repo->updateCommitDate((int)$repo->id);
  67. }
  68. /* Create compile by tag. */
  69. $jobs = zget($tagGroup, $repoID, array());
  70. foreach($jobs as $job)
  71. {
  72. $tags = $this->getRepoTags($repo);
  73. $isNew = empty($job->lastTag) ? true : false;
  74. $lastTag = '';
  75. foreach($tags as $tag)
  76. {
  77. if(empty($tag)) continue;
  78. if(!$isNew && $tag == $job->lastTag)
  79. {
  80. $isNew = true;
  81. continue;
  82. }
  83. if(!$isNew) continue;
  84. $lastTag = $tag;
  85. if($lastTag) $this->compile->createByJob($job->id, $lastTag, 'tag');
  86. }
  87. if($lastTag) $this->dao->update(TABLE_JOB)->set('lastTag')->eq($lastTag)->where('id')->eq($job->id)->exec();
  88. }
  89. }
  90. return !dao::isError();
  91. }
  92. /**
  93. * 保存提交信息及更新关联对象。
  94. * Save commit info and update related objects.
  95. *
  96. * @param object $repo
  97. * @param string $branch
  98. * @param array $logs
  99. * @param int $version
  100. * @param array $commentGroup
  101. * @param array $accountPairs
  102. * @param bool $printLog
  103. * @access public
  104. * @return int
  105. */
  106. public function saveCommits($repo, $branch, $logs, $version, $commentGroup, $accountPairs, $printLog)
  107. {
  108. if($printLog) $this->printLog("get " . count($logs) . " logs\n" . 'begin parsing logs');
  109. $this->loadModel('repo');
  110. foreach($logs as $log)
  111. {
  112. if($printLog) $this->printLog("parsing log {$log->revision}");
  113. if($printLog) $this->printLog("comment is\n----------\n" . trim($log->msg) . "\n----------");
  114. $objects = $this->repo->parseComment($log->msg);
  115. $lastVersion = $version;
  116. $version = $this->repo->saveOneCommit($repo->id, $log, $version, $branch);
  117. if($objects)
  118. {
  119. if($printLog) $this->printLog('extract' . ' story:' . join(' ', $objects['stories']) . ' task:' . join(' ', $objects['tasks']) . ' bug:' . join(',', $objects['bugs']) . ' design:' . join(',', $objects['designs']));
  120. if($lastVersion != $version)
  121. {
  122. $this->repo->saveAction2PMS($objects, $log, $this->repoRoot, $repo->encoding, 'git', $accountPairs);
  123. /* Objects link commit. */
  124. foreach($objects as $objectType => $objectIDs)
  125. {
  126. $objectTypeMap = array('stories' => 'story', 'bugs' => 'bug', 'tasks' => 'task');
  127. if(empty($objectIDs) || !isset($objectTypeMap[$objectType])) continue;
  128. $this->post->$objectType = $objectIDs;
  129. $this->repo->link($repo->id, $log->revision, $objectTypeMap[$objectType], 'commit');
  130. }
  131. }
  132. $this->linkCommit($objects['designs'], $repo->id, $log);
  133. }
  134. elseif($printLog)
  135. {
  136. $this->printLog('no objects found' . "\n");
  137. }
  138. /* Create compile by comment. */
  139. $jobs = zget($commentGroup, $repo->id, array());
  140. foreach($jobs as $job)
  141. {
  142. foreach(explode(',', $job->comment) as $comment)
  143. {
  144. if(strpos($log->msg, $comment) !== false)
  145. {
  146. $this->loadModel('job')->exec($job->id, array(), 'commit');
  147. continue 2;
  148. }
  149. }
  150. }
  151. }
  152. return count($logs);
  153. }
  154. /**
  155. * 更新提交信息。
  156. * Update commit.
  157. *
  158. * @param object $repo
  159. * @param array $commentGroup
  160. * @param bool $printLog
  161. * @access public
  162. * @return bool
  163. */
  164. public function updateCommit($repo, $commentGroup, $printLog = true)
  165. {
  166. $this->loadModel('repo');
  167. if(in_array($repo->SCM, $this->config->repo->notSyncSCM)) return false;
  168. /* Load module and print log. */
  169. if($printLog) $this->printLog("begin repo $repo->id");
  170. if(!$this->setRepo($repo)) return false;
  171. /* Get branches and commits. */
  172. $branches = $this->repo->getBranches($repo);
  173. $commits = $repo->commits;
  174. $accountPairs = array();
  175. if($repo->SCM != 'Git')
  176. {
  177. $scm = strtolower($repo->SCM);
  178. $userList = $this->loadModel($scm)->apiGetUsers((int)$repo->serviceHost);
  179. $accountIDPairs = $this->loadModel('pipeline')->getUserBindedPairs((int)$repo->serviceHost, $scm, 'openID,account');
  180. foreach($userList as $user) $accountPairs[$user->realname] = zget($accountIDPairs, $user->id, '');
  181. }
  182. /* Update code commit history. */
  183. foreach($branches as $branch)
  184. {
  185. if($printLog) $this->printLog("sync branch $branch logs.");
  186. $_COOKIE['repoBranch'] = $branch;
  187. if($printLog) $this->printLog("get this repo logs.");
  188. /* Ignore unsynced branch. */
  189. if($repo->synced != 1)
  190. {
  191. if($printLog) $this->printLog("Please init repo {$repo->name}");
  192. continue;
  193. }
  194. $logs = $this->repo->getUnsyncedCommits($repo);
  195. if(empty($logs)) continue;
  196. $lastInDB = $this->repo->getLatestCommit($repo->id);
  197. $version = isset($lastInDB->commit) ? (int)$lastInDB->commit + 1 : 1;
  198. $commits += $this->saveCommits($repo, $branch, $logs, $version, $commentGroup, $accountPairs, $printLog);
  199. }
  200. $this->repo->updateCommitCount($repo->id, $commits);
  201. $this->dao->update(TABLE_REPO)->set('lastSync')->eq(helper::now())->where('id')->eq($repo->id)->exec();
  202. if($printLog) $this->printLog("\n\nrepo #" . $repo->id . ': ' . $repo->path . " finished");
  203. return !dao::isError();
  204. }
  205. /**
  206. * 设置代码库列表。
  207. * Set the repos.
  208. *
  209. * @access public
  210. * @return bool
  211. */
  212. public function setRepos()
  213. {
  214. $repos = $this->loadModel('repo')->getListBySCM('Git,Gitlab,Gogs,Gitea');
  215. $gitRepos = array();
  216. $paths = array();
  217. foreach($repos as $repo)
  218. {
  219. if(!isset($paths[$repo->path]))
  220. {
  221. unset($repo->acl);
  222. unset($repo->desc);
  223. $gitRepos[$repo->id] = $repo;
  224. $paths[$repo->path] = $repo->path;
  225. }
  226. }
  227. if(empty($gitRepos)) echo "You must set one git repo.\n";
  228. $this->repos = $gitRepos;
  229. return true;
  230. }
  231. /**
  232. * 获取代码库列表。
  233. * Get repos.
  234. *
  235. * @access public
  236. * @return array
  237. */
  238. public function getRepos()
  239. {
  240. $this->setRepos();
  241. return helper::arrayColumn($this->repos, 'path');
  242. }
  243. /**
  244. * 设置仓库属性。
  245. * Set repo.
  246. *
  247. * @param object $repo
  248. * @access public
  249. * @return bool
  250. */
  251. public function setRepo($repo)
  252. {
  253. $this->setClient($repo);
  254. if(empty($this->client)) return false;
  255. $this->setRepoRoot($repo);
  256. return true;
  257. }
  258. /**
  259. * 设置仓库客户端。
  260. * Set client.
  261. *
  262. * @param object $repo
  263. * @access public
  264. * @return void
  265. */
  266. public function setClient($repo)
  267. {
  268. $this->client = $repo->client;
  269. }
  270. /**
  271. * 设置仓库根目录。
  272. * Set repo root.
  273. *
  274. * @param object $repo
  275. * @access public
  276. * @return void
  277. */
  278. public function setRepoRoot($repo)
  279. {
  280. $this->repoRoot = $repo->path;
  281. }
  282. /**
  283. * 获取仓库的分支列表。
  284. * get tags histories for repo.
  285. *
  286. * @param object $repo
  287. * @access public
  288. * @return array
  289. */
  290. public function getRepoTags($repo)
  291. {
  292. if(in_array(true, array(empty($repo->client), empty($repo->path), !isset($repo->account), !isset($repo->password), !isset($repo->encoding)))) return false;
  293. $scm = $this->app->loadClass('scm');
  294. $scm->setEngine($repo);
  295. return $scm->tags('');
  296. }
  297. /**
  298. * 获取代码提交记录。
  299. * Get repo logs.
  300. *
  301. * @param object $repo
  302. * @param string $fromRevision
  303. * @access public
  304. * @return array
  305. */
  306. public function getRepoLogs($repo, $fromRevision)
  307. {
  308. if(in_array(true, array(empty($repo->client), empty($repo->path), !isset($repo->account), !isset($repo->password), !isset($repo->encoding)))) return false;
  309. $scm = $this->app->loadClass('scm');
  310. $scm->setEngine($repo);
  311. $logs = $scm->log('', $fromRevision);
  312. if(empty($logs)) return array();
  313. foreach($logs as $log)
  314. {
  315. $log->author = $log->committer;
  316. $log->msg = $log->comment;
  317. $log->date = $log->time;
  318. /* Process files. */
  319. $log->files = array();
  320. foreach($log->change as $file => $info) $log->files[$info['action']][] = $file;
  321. }
  322. return $logs;
  323. }
  324. /**
  325. * 将日志从xml格式转换为对象。
  326. * Convert log from xml format to object.
  327. *
  328. * @param array $log
  329. * @access public
  330. * @return object|null
  331. */
  332. public function convertLog($log)
  333. {
  334. if(empty($log)) return null;
  335. list($hash, $account, $date) = $log;
  336. $account = preg_replace('/^Author:/', '', $account);
  337. $account = trim(preg_replace('/<[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9_\-\.]+>/', '', $account));
  338. $date = trim(preg_replace('/^Date:/', '', $date));
  339. $count = count($log);
  340. $comment = '';
  341. $files = array();
  342. for($i = 3; $i < $count; $i++)
  343. {
  344. $line = $log[$i];
  345. if(preg_match('/^\s{2,}/', $line))
  346. {
  347. $comment .= $line;
  348. }
  349. elseif(strpos($line, "\t") !== false)
  350. {
  351. list($action, $entry) = explode("\t", $line);
  352. $entry = '/' . trim($entry);
  353. $files[$action][] = $entry;
  354. }
  355. }
  356. $parsedLog = new stdClass();
  357. $parsedLog->author = $account;
  358. $parsedLog->revision = trim(preg_replace('/^commit/', '', $hash));
  359. $parsedLog->msg = trim($comment);
  360. $parsedLog->date = date('Y-m-d H:i:s', strtotime($date));
  361. $parsedLog->files = $files;
  362. return $parsedLog;
  363. }
  364. /**
  365. * 输出日志信息.
  366. * Print log.
  367. *
  368. * @param string $log
  369. * @access public
  370. * @return void
  371. */
  372. public function printLog($log)
  373. {
  374. echo helper::now() . " $log\n";
  375. }
  376. /**
  377. * Code Association of design through annotations.
  378. *
  379. * @param array $designs
  380. * @param int $repoID
  381. * @param object $log
  382. * @access public
  383. * @return void
  384. */
  385. public function linkCommit($designs, $repoID, $log)
  386. {
  387. $this->loadModel('repo');
  388. foreach($designs as $designID)
  389. {
  390. if(empty($designID)) continue;
  391. $this->dao->delete()->from(TABLE_RELATION)->where('AType')->eq('design')->andWhere('AID')->eq($designID)->andWhere('BType')->eq('commit')->andWhere('relation')->eq('completedin')->exec();
  392. $this->dao->delete()->from(TABLE_RELATION)->where('AType')->eq('commit')->andWhere('BID')->eq($designID)->andWhere('BType')->eq('design')->andWhere('relation')->eq('completedfrom')->exec();
  393. $revisionID = $this->dao->select('id')->from(TABLE_REPOHISTORY)->where('repo')->eq($repoID)->andWhere('revision')->eq($log->revision)->fetch('id');
  394. $program = $this->dao->select('id,project,product')->from(TABLE_DESIGN)->where('id')->eq($designID)->fetch();
  395. $data = new stdclass();
  396. $data->program = $program->project;
  397. $data->product = $program->product;
  398. $data->AType = 'design';
  399. $data->AID = $designID;
  400. $data->BType = 'commit';
  401. $data->BID = $revisionID;
  402. $data->relation = 'completedin';
  403. $data->extra = $repoID;
  404. $this->dao->replace(TABLE_RELATION)->data($data)->autoCheck()->exec();
  405. $this->repo->saveRelation($designID, 'design', $revisionID, 'commit', 'completedfrom');
  406. }
  407. }
  408. }