* Add for git unit.

This commit is contained in:
wangyidong
2022-05-06 17:33:43 +08:00
parent ec76ee1daf
commit 8d1f35aaf9
10 changed files with 342 additions and 101 deletions
+4 -101
View File
@@ -194,7 +194,6 @@ class gitModel extends model
$commits += count($logs);
}
}
}
$this->repo->updateCommitCount($repo->id, $commits);
@@ -296,6 +295,8 @@ class gitModel extends model
*/
public function getRepoTags($repo)
{
if(empty($repo->client) or empty($repo->path) or !isset($repo->account) or !isset($repo->password) or !isset($repo->encoding)) return false;
$scm = $this->app->loadClass('scm');
$scm->setEngine($repo);
return $scm->tags('');
@@ -311,6 +312,8 @@ class gitModel extends model
*/
public function getRepoLogs($repo, $fromRevision)
{
if(empty($repo->client) or empty($repo->path) or !isset($repo->account) or !isset($repo->password) or !isset($repo->encoding)) return false;
$scm = $this->app->loadClass('scm');
$scm->setEngine($repo);
$logs = $scm->log('', $fromRevision);
@@ -371,106 +374,6 @@ class gitModel extends model
return $parsedLog;
}
/**
* Diff a url.
*
* @param string $path
* @param int $revision
* @access public
* @return string|bool
*/
public function diff($path, $revision)
{
$repo = $this->getRepoByURL($path);
if(!$repo) return false;
$this->setClient($repo);
if(empty($this->client)) return false;
putenv('LC_CTYPE=en_US.UTF-8');
chdir($repo->path);
exec("{$this->client} config core.quotepath false");
$subPath = substr($path, strlen($repo->path));
if($subPath[0] == '/' or $subPath[0] == '\\') $subPath = substr($subPath, 1);
$encodings = explode(',', $this->config->git->encodings);
foreach($encodings as $encoding)
{
$encoding = trim($encoding);
if($encoding == 'utf-8') continue;
$subPath = helper::convertEncoding($subPath, 'utf-8', $encoding);
if($subPath) break;
}
exec("$this->client rev-list -n 2 $revision -- $subPath", $lists);
if(count($lists) == 2) list($nowRevision, $preRevision) = $lists;
$cmd = "$this->client diff $preRevision $nowRevision -- $subPath 2>&1";
$diff = `$cmd`;
$encoding = isset($repo->encoding) ? $repo->encoding : 'utf-8';
if($encoding and $encoding != 'utf-8') $diff = helper::convertEncoding($diff, $encoding);
return $diff;
}
/**
* Cat a url.
*
* @param string $path
* @param int $revision
* @access public
* @return string|bool
*/
public function cat($path, $revision)
{
$repo = $this->getRepoByURL($path);
if(!$repo) return false;
$this->setClient($repo);
if(empty($this->client)) return false;
putenv('LC_CTYPE=en_US.UTF-8');
$subPath = substr($path, strlen($repo->path));
if($subPath[0] == '/' or $subPath[0] == '\\') $subPath = substr($subPath, 1);
$encodings = explode(',', $this->config->git->encodings);
foreach($encodings as $encoding)
{
$encoding = trim($encoding);
if($encoding == 'utf-8') continue;
$subPath = helper::convertEncoding($subPath, 'utf-8', $encoding);
if($subPath) break;
}
chdir($repo->path);
exec("{$this->client} config core.quotepath false");
$cmd = "$this->client show $revision:$subPath 2>&1";
$code = `$cmd`;
$encoding = isset($repo->encoding) ? $repo->encoding : 'utf-8';
if($encoding and $encoding != 'utf-8') $code = helper::convertEncoding($code, $encoding);
return $code;
}
/**
* Get repo by url.
*
* @param string $url
* @access public
* @return object|bool
*/
public function getRepoByURL($url)
{
foreach($this->config->git->repos as $repo)
{
if(empty($repo['path'])) continue;
if(strpos($url, $repo['path']) !== false) return (object)$repo;
}
return false;
}
/**
* Pring log.
*
+131
View File
@@ -0,0 +1,131 @@
<?php
class gitTest
{
public $tester;
public function __construct()
{
global $tester;
$this->tester = $tester;
$this->gitModel = $this->tester->loadModel('git');
}
/**
* Run
*
* @access public
* @return void
*/
public function run()
{
return $this->gitModel->run();
}
/**
* Update commit.
*
* @param object $repo
* @param array $commentGroup
* @param bool $printLog
* @access public
* @return string
*/
public function updateCommit($repo)
{
$repoID = isset($repo->id) ? $repo->id : 0;
$oldLastSync = $this->tester->dao->select('lastSync')->from(TABLE_REPO)->where('id')->eq($repoID)->fetch('lastSync');
$result = $this->gitModel->updateCommit($repo, array(), false);
if(empty($repoID)) return $result;
$newLastSync = $this->tester->dao->select('lastSync')->from(TABLE_REPO)->where('id')->eq($repoID)->fetch('lastSync');
return $oldLastSync != $newLastSync;
}
/**
* Set the repos.
*
* @access public
* @return mixed
*/
public function setRepos()
{
$this->gitModel->setRepos();
$repo = array_shift($this->gitModel->repos);
return $repo;
}
/**
* Get repos.
*
* @access public
* @return array
*/
public function getRepos()
{
$pairs = $this->gitModel->getRepos();
return array_shift($pairs);
}
/**
* Set repo.
*
* @param object $repo
* @access public
* @return bool
*/
public function setRepo($repo)
{
$this->gitModel->client = '';
$this->gitModel->repoRoot = '';
$result = $this->gitModel->setRepo($repo);
$data = new stdclass();
$data->result = $result;
$data->client = !empty($this->gitModel->client);
$data->repoRoot = !empty($this->gitModel->repoRoot);
return $data;
}
/**
* get tags histories for repo.
*
* @param object $repo
* @access public
* @return mixed
*/
public function getRepoTags($repo)
{
$tags = $this->gitModel->getRepoTags($repo);
if(is_array($tags) and count($tags) >= 1) return 1;
return 0;
}
/**
* Get repo logs.
*
* @param object $repo
* @param int $fromRevision
* @access public
* @return array
*/
public function getRepoLogs($repo)
{
$logs = $this->gitModel->getRepoLogs($repo, '');
if(is_array($logs) and count($logs) >= 1) return 1;
return 0;
}
/**
* Convert log from xml format to object.
*
* @param object $log
* @access public
* @return object
*/
public function convertLog($log)
{
$parsedLogs = $this->gitModel->convertLog($log);
if(empty($parsedLogs->revision)) $parsedLogs->revision = 0;
return $parsedLogs;
}
}
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->convertLog();
cid=1
pid=1
使用正确格式的数据 >> e7699d04f1586d337f34496da932dde55db92616
使用空数据 >> 0
*/
$git = new gitTest();
$log[] = "commit e7699d04f1586d337f34496da932dde55db92616";
$log[] = "Author: zhengrunyu <zhenrunyu@easycorp.ltd>";
$log[] = "Date: Thu May 5 16:19:48 2022 +0800";
$log[] = " * Fix bug 22061.";
r($git->convertLog($log)) && p('revision') && e('e7699d04f1586d337f34496da932dde55db92616'); // 使用正确格式的数据
$log = array();
r($git->convertLog($log)) && p('revision') && e(0); // 使用空的数据
system("./ztest init");
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->getRepoLogs();
cid=1
pid=1
获取版本库的tags >> 1
使用空数据 >> 0
使用错误的版本库 >> 0
*/
$git = new gitTest();
/* Fix error Gitlab url. */
$tester->dao->update(TABLE_PIPELINE)->set('url')->eq('http://10.0.1.161:51080')->where('id')->eq(1)->exec();
$repo = $tester->dao->select('*')->from(TABLE_REPO)->limit(1)->fetch();
if(strtolower($repo->SCM) == 'gitlab') $repo = $tester->loadModel('repo')->processGitlab($repo);
r($git->getRepoLogs($repo)) && p() && e(1); // 获取版本库的tags
$repo = new stdclass();
r($git->getRepoLogs($repo)) && p() && e(0); // 使用空数据
$repo = new stdclass();
$repo->client = '';
$repo->path = '';
r($git->getRepoLogs($repo)) && p() && e(0); // 使用错误的版本库
system("./ztest init");
+25
View File
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->getRepos();
cid=1
pid=1
获取版本库 >> http://10.0.1.161:51080/api/v4/projects/42/repository/
*/
$git = new gitTest();
/* Fix error Gitlab url. */
$tester->dao->update(TABLE_PIPELINE)->set('url')->eq('http://10.0.1.161:51080')->where('id')->eq(1)->exec();
$tester->dao->update(TABLE_REPO)->set('synced')->eq(1)->where('id')->eq(1)->exec();
r($git->getRepos()) && p() && e('http://192.168.1.161:51080/api/v4/projects/42/repository/'); // 获取版本库
system("./ztest init");
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->getRepoTags();
cid=1
pid=1
获取版本库的tags >> 1
使用空数据 >> 0
使用错误的版本库 >> 0
*/
$git = new gitTest();
/* Fix error Gitlab url. */
$tester->dao->update(TABLE_PIPELINE)->set('url')->eq('http://10.0.1.161:51080')->where('id')->eq(1)->exec();
$repo = $tester->dao->select('*')->from(TABLE_REPO)->limit(1)->fetch();
if(strtolower($repo->SCM) == 'gitlab') $repo = $tester->loadModel('repo')->processGitlab($repo);
r($git->getRepoTags($repo)) && p() && e(1); // 获取版本库的tags
$repo = new stdclass();
r($git->getRepoTags($repo)) && p() && e(0); // 使用空数据
$repo = new stdclass();
$repo->client = '';
$repo->path = '';
r($git->getRepoTags($repo)) && p() && e(0); // 使用错误的版本库
system("./ztest init");
+27
View File
@@ -0,0 +1,27 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->setRepo();
cid=1
pid=1
设置错误版本号参数 >> ,,
设置正确版本号参数 >> 1,1,1
*/
$git = new gitTest();
$repo = new stdclass();
r($git->setRepo($repo)) && p("result,client,repoRoot") && e(",,"); // 设置错误版本库参数
$repo = $tester->dao->select('*')->from(TABLE_REPO)->limit(1)->fetch();
if(strtolower($repo->SCM) == 'gitlab') $repo = $tester->loadModel('repo')->processGitlab($repo);
r($git->setRepo($repo)) && p("result,client,repoRoot") && e("1,1,1"); // 设置正确版本库参数
system("./ztest init");
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->setRepos();
cid=1
pid=1
设置版本库 >> 1
*/
$git = new gitTest();
$tester->dao->update(TABLE_REPO)->set('synced')->eq(1)->where('id')->eq(1)->exec();
r($git->setRepos()) && p('id') && e(1); // 设置版本库
system("./ztest init");
+30
View File
@@ -0,0 +1,30 @@
#!/usr/bin/env php
<?php
include dirname(dirname(dirname(__FILE__))) . '/lib/init.php';
include dirname(dirname(dirname(__FILE__))) . '/class/git.class.php';
su('admin');
/**
title=测试gitModel->updatecommit();
cid=1
pid=1
测试正常的版本库 >> 1
测试空的版本库 >> 0
*/
$git = new gitTest();
/* Fix error Gitlab url. */
$tester->dao->update(TABLE_PIPELINE)->set('url')->eq('http://10.0.1.161:51080')->where('id')->eq(1)->exec();
$repo = $tester->dao->select('*')->from(TABLE_REPO)->limit(1)->fetch();
if(strtolower($repo->SCM) == 'gitlab') $repo = $tester->loadModel('repo')->processGitlab($repo);
r($git->updateCommit($repo)) && p() && e(1); // 测试正常的版本库
$repo = new stdclass();
r($git->updateCommit($repo)) && p() && e(0); // 测试空的版本库
system("./ztest init");
+3
View File
@@ -55,6 +55,9 @@ switch($argv[1])
case 'execution':
ztfRun('model/execution');
break;
case 'git':
ztfRun('model/git');
break;
case 'group':
ztfRun('model/group');
break;