add scmUtils class
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
class scmUtils
|
||||
{
|
||||
public $commitCommandRegx = '/\s*([a-z]+)\s+((?:build)|(?:story)|(?:task)|(?:bug))\s+#((?:\d|,)+)\s*/i';
|
||||
public $tagCommandRegx = '/build[\-_]#((?:\d|,)+)/i';
|
||||
|
||||
/**
|
||||
* Parse the comment of git, extract object id list from it.
|
||||
*
|
||||
* @param string $comment
|
||||
* @param array $allCommands
|
||||
* @access public
|
||||
*/
|
||||
public function parseComment($comment, &$allCommands)
|
||||
{
|
||||
$pattern = $this->commitCommandRegx;
|
||||
$matches = array();
|
||||
preg_match_all($pattern, $comment,$matches);
|
||||
|
||||
if(count($matches) > 1 && count($matches[1]) > 0)
|
||||
{
|
||||
$i = 0;
|
||||
foreach($matches[1] as $action)
|
||||
{
|
||||
$action = $matches[1][$i];
|
||||
$entityType = $matches[2][$i];
|
||||
$entityIds = $matches[3][$i];
|
||||
|
||||
$currArr = $allCommands[$entityType][$action];
|
||||
if (empty($currArr)) {
|
||||
$currArr = [];
|
||||
}
|
||||
$newArr = explode(",", $entityIds);
|
||||
|
||||
$allCommands[$entityType][$action] = array_keys(array_flip($currArr) + array_flip($newArr));
|
||||
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the tag, extract task list from it.
|
||||
*
|
||||
* @param string $comment
|
||||
* @param array $jobToBuild
|
||||
* @access public
|
||||
*/
|
||||
public function parseTag($comment, &$jobToBuild)
|
||||
{
|
||||
$pattern = $this->tagCommandRegx;
|
||||
$matches = array();
|
||||
preg_match($pattern, $comment,$matches);
|
||||
|
||||
if(count($matches) > 0)
|
||||
{
|
||||
$entityIds = $matches[1];
|
||||
|
||||
if (empty($taskToBuild)) {
|
||||
$taskToBuild = [];
|
||||
}
|
||||
$newArr = explode(",", $entityIds);
|
||||
$jobToBuild = array_keys(array_flip($taskToBuild) + array_flip($newArr));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,9 +21,6 @@ $i = 1;
|
||||
$config->git->repos[$i]['path'] = '/Users/aaron/devops/project/devops_test';
|
||||
$config->git->repos[$i]['encoding'] = 'utf-8';
|
||||
|
||||
$config->ci->commitCommandRegx = '/\s*([a-z]+)\s+((?:build)|(?:story)|(?:task)|(?:bug))\s+#((?:\d|,)+)\s*/i';
|
||||
$config->ci->tagCommandRegx = '/build[\-_]#((?:\d|,)+)/i';
|
||||
|
||||
/*
|
||||
$i ++;
|
||||
$config->git->repos[$i]['path'] = '';
|
||||
|
||||
+7
-93
@@ -92,13 +92,10 @@ class gitModel extends model
|
||||
$this->printLog("begin repo $repo->id");
|
||||
if(!$this->setRepo($repo)) return false;
|
||||
|
||||
// $this->pull();
|
||||
|
||||
$savedRevision = $this->getSavedRevision();
|
||||
$this->printLog("start from revision $savedRevision");
|
||||
$logs = $this->getRepoLogs($repo, $savedRevision);
|
||||
if(!empty($logs)) {
|
||||
|
||||
$this->printLog("get " . count($logs) . " logs");
|
||||
$this->printLog('begin parsing logs');
|
||||
$latestRevision = $logs[0]->revision;
|
||||
@@ -112,7 +109,7 @@ class gitModel extends model
|
||||
}
|
||||
|
||||
$this->printLog("comment is\n----------\n" . trim($log->msg) . "\n----------");
|
||||
$this->parseComment($log->msg, $allCommands);
|
||||
$this->app->loadClass('scmUtils')->parseComment($log->msg, $allCommands);
|
||||
}
|
||||
|
||||
$this->saveLastRevision($latestRevision);
|
||||
@@ -124,10 +121,10 @@ class gitModel extends model
|
||||
}
|
||||
|
||||
// exe ci task from logs
|
||||
$citaskIDs = $allCommands['build']['start'];
|
||||
foreach($citaskIDs as $id)
|
||||
$cijobIDs = $allCommands['build']['start'];
|
||||
foreach($cijobIDs as $id)
|
||||
{
|
||||
$this->loadModel('citask')->exe($id);
|
||||
$this->loadModel('ci')->exeJob($id);
|
||||
}
|
||||
|
||||
// dealwith tag commands
|
||||
@@ -148,7 +145,7 @@ class gitModel extends model
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->parseTag($tag, $taskToBuild);
|
||||
$this->app->loadClass('scmUtils')->parseTag($tag, $taskToBuild);
|
||||
}
|
||||
|
||||
$this->saveLastTag($tags[count($tags) - 1]);
|
||||
@@ -156,33 +153,12 @@ class gitModel extends model
|
||||
$this->printLog('extract tasks to build: ' . json_encode($taskToBuild));
|
||||
|
||||
foreach ($taskToBuild as $id) {
|
||||
$this->loadModel('citask')->exe($id);
|
||||
$this->loadModel('ci')->exeJob($id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * Pull codes from remote repo.
|
||||
// *
|
||||
// * @param $repo
|
||||
// */
|
||||
// public function pull($repo = '')
|
||||
// {
|
||||
// if (!empty($repo)) {
|
||||
// $repoRoot = $repo->path;
|
||||
// $this->setClient($repo);
|
||||
// } else {
|
||||
// $repoRoot = $this->repoRoot;
|
||||
// }
|
||||
//
|
||||
// chdir($repoRoot);
|
||||
//// exec('sudo -u aaron whoami', $output, $return);
|
||||
//
|
||||
// $cmd = "{$this->client} pull 2>&1";
|
||||
// exec($cmd, $output, $return);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Set the log root.
|
||||
*
|
||||
@@ -230,7 +206,7 @@ class gitModel extends model
|
||||
|
||||
$gitRepos = [];
|
||||
$paths = [];
|
||||
// 有了库管理,忽略配置里的库
|
||||
// ignore same repo in config.php
|
||||
foreach($repoObjs as $repoInDb)
|
||||
{
|
||||
if(strtolower($repoInDb->SCM) === 'git' && !in_array($repoInDb->path, $gitRepos)) {
|
||||
@@ -455,68 +431,6 @@ class gitModel extends model
|
||||
return $parsedLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the comment of git, extract object id list from it.
|
||||
*
|
||||
* @param string $comment
|
||||
* @param array $allCommands
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function parseTag($comment, &$taskToBuild)
|
||||
{
|
||||
$pattern = $this->config->ci->tagCommandRegx;
|
||||
$matches = array();
|
||||
preg_match($pattern, $comment,$matches);
|
||||
|
||||
if(count($matches) > 0)
|
||||
{
|
||||
$entityIds = $matches[1];
|
||||
|
||||
if (empty($taskToBuild)) {
|
||||
$taskToBuild = [];
|
||||
}
|
||||
$newArr = explode(",", $entityIds);
|
||||
$taskToBuild = array_keys(array_flip($taskToBuild) + array_flip($newArr));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the comment of git, extract object id list from it.
|
||||
*
|
||||
* @param string $comment
|
||||
* @param array $allCommands
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function parseComment($comment, &$allCommands)
|
||||
{
|
||||
$pattern = $this->config->ci->commitCommandRegx;
|
||||
$matches = array();
|
||||
preg_match_all($pattern, $comment,$matches);
|
||||
|
||||
if(count($matches) > 1 && count($matches[1]) > 0)
|
||||
{
|
||||
$i = 0;
|
||||
foreach($matches[1] as $action)
|
||||
{
|
||||
$action = $matches[1][$i];
|
||||
$entityType = $matches[2][$i];
|
||||
$entityIds = $matches[3][$i];
|
||||
|
||||
$currArr = $allCommands[$entityType][$action];
|
||||
if (empty($currArr)) {
|
||||
$currArr = [];
|
||||
}
|
||||
$newArr = explode(",", $entityIds);
|
||||
|
||||
$allCommands[$entityType][$action] = array_keys(array_flip($currArr) + array_flip($newArr));
|
||||
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the comment to uft-8.
|
||||
*
|
||||
|
||||
+120
-64
@@ -90,47 +90,73 @@ class svnModel extends model
|
||||
foreach($this->repos as $name => $repo)
|
||||
{
|
||||
$this->printLog("begin repo $name");
|
||||
$repo = (object)$repo;
|
||||
$repo->name = $name;
|
||||
if(!$this->setRepo($repo)) return false;
|
||||
|
||||
$savedRevision = $this->getSavedRevision();
|
||||
$this->printLog("start from revision $savedRevision");
|
||||
$logs = $this->getRepoLogs($repo, $savedRevision);
|
||||
if(empty($logs)) continue;
|
||||
|
||||
$this->printLog("get " . count($logs) . " logs");
|
||||
$this->printLog('begin parsing logs');
|
||||
foreach($logs as $log)
|
||||
{
|
||||
$this->printLog("parsing log {$log->revision}");
|
||||
if($log->revision == $savedRevision)
|
||||
{
|
||||
$this->printLog("{$log->revision} alread parsed, ommit it");
|
||||
continue;
|
||||
if(!empty($logs)) {
|
||||
$this->printLog("get " . count($logs) . " logs");
|
||||
$this->printLog('begin parsing logs');
|
||||
$latestRevision = $logs[0]->revision;
|
||||
|
||||
$allCommands = [];
|
||||
foreach ($logs as $log) {
|
||||
$this->printLog("parsing log {$log->revision}");
|
||||
if ($log->revision == $savedRevision) {
|
||||
$this->printLog("{$log->revision} alread parsed, commit it");
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->printLog("comment is\n----------\n" . trim($log->msg) . "\n----------");
|
||||
$this->app->loadClass('scmUtils')->parseComment($log->msg, $allCommands);
|
||||
}
|
||||
|
||||
$this->printLog("comment is\n----------\n" . trim($log->msg) . "\n----------");
|
||||
$objects = $this->parseComment($log->msg);
|
||||
if($objects)
|
||||
{
|
||||
$this->printLog('extract' .
|
||||
'story:' . join(' ', $objects['stories']) .
|
||||
' task:' . join(' ', $objects['tasks']) .
|
||||
' bug:' . join(',', $objects['bugs']));
|
||||
$this->saveLastRevision($latestRevision);
|
||||
$this->printLog("save revision $latestRevision");
|
||||
$this->deleteRestartFile();
|
||||
$this->printLog("\n\nrepo ' . $repo->id . ': ' . $repo->path . ' finished");
|
||||
|
||||
$this->saveAction2PMS($objects, $log);
|
||||
$this->printLog('extract commands from logs' . json_encode($allCommands));
|
||||
}
|
||||
|
||||
// exe ci task from logs
|
||||
$cijobIDs = $allCommands['build']['start'];
|
||||
foreach($cijobIDs as $id)
|
||||
{
|
||||
$this->loadModel('ci')->exeJob($id);
|
||||
}
|
||||
|
||||
// dealwith tag commands
|
||||
$this->printLog("dealwith tag commands");
|
||||
$savedTag = $this->getSavedTag();
|
||||
|
||||
$tags = $this->getRepoTags($repo);
|
||||
if(!empty($tags)) {
|
||||
$arriveLastTag = false;
|
||||
$taskToBuild = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (!empty($savedTag) && $tag === $savedTag) { // get the last build tag position
|
||||
$arriveLastTag = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!empty($savedTag) && !$arriveLastTag) { // not get
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->app->loadClass('scmUtils')->parseTag($tag, $taskToBuild);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->printLog('no objects found' . "\n");
|
||||
|
||||
$this->saveLastTag($tags[count($tags) - 1]);
|
||||
|
||||
$this->printLog('extract tasks to build: ' . json_encode($taskToBuild));
|
||||
|
||||
foreach ($taskToBuild as $id) {
|
||||
$this->loadModel('ci')->exeJob($id);
|
||||
}
|
||||
if($log->revision > $savedRevision) $savedRevision = $log->revision;
|
||||
}
|
||||
$this->saveLastRevision($savedRevision);
|
||||
$this->printLog("save revision $savedRevision");
|
||||
$this->deleteRestartFile();
|
||||
$this->printLog("\n\nrepo $name finished");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,13 +205,24 @@ class svnModel extends model
|
||||
$repo = $this->loadModel('repo');
|
||||
$repoObjs = $repo->listForSync("SCM='Subversion'");
|
||||
|
||||
if(!$this->config->svn->repos)
|
||||
$svnRepos = [];
|
||||
$paths = [];
|
||||
// ignore same repo in config.php
|
||||
foreach($repoObjs as $repoInDb)
|
||||
{
|
||||
echo "You must set one svn repo.\n";
|
||||
if(strtolower($repoInDb->SCM) === 'subversion' && !in_array($repoInDb->path, $svnRepos)) {
|
||||
$gitRepos[] = (object)array('id'=>$repoInDb->id, 'path' => $repoInDb->path, 'encoding' => 'utf-8');
|
||||
$paths[] = $repoInDb->path;
|
||||
}
|
||||
}
|
||||
|
||||
if(!$svnRepos)
|
||||
{
|
||||
echo "You must set one git repo.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->repos = $this->config->svn->repos;
|
||||
$this->repos = $svnRepos;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -282,6 +319,32 @@ class svnModel extends model
|
||||
$this->repoRoot = $repoRoot;
|
||||
}
|
||||
|
||||
/**
|
||||
* get tags histories for repo.
|
||||
*
|
||||
* @param object $repo
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function getRepoTags($repo)
|
||||
{
|
||||
$parsedTags = array();
|
||||
|
||||
/* The git tag command. */
|
||||
chdir($this->repoRoot);
|
||||
exec("{$this->client} config core.quotepath false");
|
||||
|
||||
$cmd = "$this->client for-each-ref --sort=taggerdate | grep refs/tags | grep -v commit";
|
||||
exec($cmd, $list, $return);
|
||||
foreach($list as $line)
|
||||
{
|
||||
$arr = explode('refs/tags/', $line);
|
||||
$parsedTags[] = $arr[count($arr) - 1];
|
||||
}
|
||||
|
||||
return $parsedTags;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get repo logs.
|
||||
*
|
||||
@@ -341,38 +404,6 @@ class svnModel extends model
|
||||
return $parsedLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the comment of svn, extract object id list from it.
|
||||
*
|
||||
* @param string $comment
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function parseComment($comment)
|
||||
{
|
||||
$stories = array();
|
||||
$tasks = array();
|
||||
$bugs = array();
|
||||
|
||||
// bug|story|task(case insensitive) + some space + #|:|:(Chinese) + id lists(maybe join with space or ,)
|
||||
// $comment = "bug # 1,2,3,4 Bug:1 2 3 4 5 story:9999,1234566 story:456,1234566";
|
||||
$commonReg = "(?:\s){0,}(?:#|:|:){0,}([0-9, ]{1,})";
|
||||
$taskReg = '/task' . $commonReg . '/i';
|
||||
$storyReg = '/story' . $commonReg . '/i';
|
||||
$bugReg = '/bug' . $commonReg . '/i';
|
||||
|
||||
if(preg_match_all($storyReg, $comment, $result)) $stories = join(' ', $result[1]);
|
||||
if(preg_match_all($taskReg, $comment, $result)) $tasks = join(' ', $result[1]);
|
||||
if(preg_match_all($bugReg, $comment, $result)) $bugs = join(' ', $result[1]);
|
||||
|
||||
if($stories) $stories = array_unique(explode(' ', str_replace(',', ' ', $stories)));
|
||||
if($tasks) $tasks = array_unique(explode(' ', str_replace(',', ' ', $tasks)));
|
||||
if($bugs) $bugs = array_unique(explode(' ', str_replace(',', ' ', $bugs)));
|
||||
|
||||
if(!$stories and !$tasks and !$bugs) return array();
|
||||
return array('stories' => $stories, 'tasks' => $tasks, 'bugs' => $bugs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the comment to uft-8.
|
||||
*
|
||||
@@ -735,4 +766,29 @@ class svnModel extends model
|
||||
|
||||
return $buildedURL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the saved tag.
|
||||
*
|
||||
* @access public
|
||||
* @return int
|
||||
*/
|
||||
public function getSavedTag()
|
||||
{
|
||||
if(!file_exists($this->tagFile)) return 0;
|
||||
if(file_exists($this->restartFile)) return 0;
|
||||
return trim(file_get_contents($this->tagFile));
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the last revision.
|
||||
*
|
||||
* @param int $tag
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function saveLastTag($tag)
|
||||
{
|
||||
$ret = file_put_contents($this->tagFile, $tag);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user