diff --git a/db/update12.0.1.sql b/db/update12.0.1.sql index 7653f5ced2..a08e055c32 100644 --- a/db/update12.0.1.sql +++ b/db/update12.0.1.sql @@ -85,3 +85,4 @@ ALTER TABLE `zt_jenkins` CHANGE `serviceUrl` `url` varchar(255) COLLATE 'utf8_ge ALTER TABLE `zt_jenkins` DROP `encrypt`; ALTER TABLE `zt_compile` ADD `atTime` varchar(10) COLLATE 'utf8_general_ci' NULL AFTER `logs`; +ALTER TABLE `zt_compile` ADD `tag` varchar(255) COLLATE 'utf8_general_ci' NULL AFTER `logs`; diff --git a/lib/scm/scm.class.php b/lib/scm/scm.class.php index 9e5c6c9ace..ed1ae252ce 100644 --- a/lib/scm/scm.class.php +++ b/lib/scm/scm.class.php @@ -40,7 +40,7 @@ class scm * @access public * @return array */ - public function tags($path, $revision, $onlyDir = true) + public function tags($path, $revision = 'HEAD', $onlyDir = true) { return $this->engine->tags($path, $revision, $onlyDir); } diff --git a/lib/scm/subversion.class.php b/lib/scm/subversion.class.php index aaa34a4f49..9ae31fd1f6 100644 --- a/lib/scm/subversion.class.php +++ b/lib/scm/subversion.class.php @@ -96,12 +96,23 @@ class Subversion public function tags($path, $revision = 'HEAD', $onlyDir = true) { $infos = $this->ls($path, $revision); - $tags = array(); + $dirs = array(); foreach($infos as $info) { if($onlyDir and $info->kind != 'dir') continue; - $dirPath = $path . '/' . $info->name; - $tags[$dirPath] = $info->name; + $dirs[$info->date][$info->name] = $info->name; + } + + ksort($dirs); + $tags = array(); + foreach($dirs as $dirNames) + { + ksort($dirNames); + foreach($dirNames as $dirName) + { + $dirPath = $path . '/' . $dirName; + $tags[$dirPath] = $dirName; + } } return $tags; diff --git a/module/ci/control.php b/module/ci/control.php index 1fadd377d4..e6b85da185 100644 --- a/module/ci/control.php +++ b/module/ci/control.php @@ -36,7 +36,7 @@ class ci extends control $this->loadModel('compile'); foreach($scheduleJobs as $job) { - if(strpos($job->atDay, $week) !== false) $this->compile->createByIntegration($job->id); + if(strpos($job->atDay, $week) !== false) $this->compile->createByIntegration($job->id, $job->atTime, 'atTime'); } echo 'success'; } @@ -50,53 +50,10 @@ class ci extends control public function exec() { $compiles = $this->loadModel('compile')->getUnexecutedList(); - foreach($compiles as $compile) $this->compile->execByCompile($compile); - - $integrations = $this->loadModel('integration')->getListByTriggerType('tag'); - - $repoIdList = array(); - $repos = array(); - foreach($integrations as $integration) $repoIdList[$integration->id] = $integration->id; - if($repoIdList) $repos = $this->loadModel('repo')->getByIdList($repoIdList); - - foreach($integrations as $integration) + foreach($compiles as $compile) { - $repo = zget($repos, $integration->repo, null); - if(empty($repo)) continue; - - $scm = $repo->SCM == 'Git' ? 'git' : 'svn'; - $savedTag = $this->loadModel($scm)->getSavedTag($repo->id); - - $tags = $this->$scm->getRepoTags($repo, $scm == 'svn' ? $integration->svnDir : ''); - if(!empty($tags)) - { - $arriveLastTag = false; - foreach($tags as $tag) - { - /* Get the last build tag position */ - if($scm == 'git') - { - if(!empty($savedTag) && !$arriveLastTag) continue; - if(!empty($savedTag) && $tag == $savedTag) - { - $arriveLastTag = true; - continue; - } - } - elseif($scm == 'svn') - { - if(isset($savedTag[$tag])) continue; - $tag = rtrim($repo->path , '/') . '/' . trim($integration->svnDir, '/') . '/' . $tag; - } - - $tagData = new stdclass(); - $tagData->PARAM_TAG = $tag; - $this->compile->execByIntegration($integration->id, $tagData); - } - - if($scm == 'svn') $tag = json_encode($tags); - $this->$scm->saveLastTag($tag, $repo->id); - } + if($compile->atTime and date('H:i') < $compile->atTime) continue; + $this->compile->execByCompile($compile); } echo 'success'; } diff --git a/module/compile/model.php b/module/compile/model.php index bedbfbeed9..f9dff5a455 100644 --- a/module/compile/model.php +++ b/module/compile/model.php @@ -61,16 +61,19 @@ class compileModel extends model * Save build by integration * * @param int $integrationID + * @param string $data + * @param string $type * @access public * @return void */ - public function createByIntegration($integrationID) + public function createByIntegration($integrationID, $data = '', $type = 'tag') { $integration = $this->dao->select('id,name')->from(TABLE_INTEGRATION)->where('id')->eq($integrationID)->fetch(); $build = new stdClass(); $build->integration = $integration->id; $build->name = $integration->name; + $build->$type = $data; $build->createdBy = $this->app->user->account; $build->createdDate = helper::now(); @@ -84,7 +87,7 @@ class compileModel extends model * @access public * @return bool */ - public function execByCompile($compile, $data = null) + public function execByCompile($compile) { $integration = $this->dao->select('t1.id,t1.name,t1.repo,t1.jkJob,t2.name as jenkinsName,t2.url,t2.account,t2.token,t2.password') ->from(TABLE_INTEGRATION)->alias('t1') @@ -94,45 +97,22 @@ class compileModel extends model if(!$integration) return false; + $tagData = ''; + if($compile->tag) + { + $tagData = new stdclass(); + $tagData->PARAM_TAG = $compile->tag; + } + $buildUrl = $this->getBuildUrl($integration); $build = new stdclass(); - $build->queue = $this->loadModel('ci')->sendRequest($buildUrl, $data); + $build->queue = $this->loadModel('ci')->sendRequest($buildUrl, $tagData); $build->status = $build->queue ? 'created' : 'create_fail'; $this->dao->update(TABLE_COMPILE)->data($build)->where('id')->eq($compile->id)->exec(); return !dao::isError(); } - /** - * Execute by integration. - * - * @param int $compile - * @access public - * @return bool - */ - public function execByIntegration($integrationID, $data = null) - { - $integration = $this->dao->select('t1.id,t1.name,t1.repo,t1.jkJob,t2.name as jenkinsName,t2.url,t2.account,t2.token,t2.password') - ->from(TABLE_INTEGRATION)->alias('t1') - ->leftJoin(TABLE_JENKINS)->alias('t2')->on('t1.jkHost=t2.id') - ->where('t1.id')->eq($integrationID) - ->fetch(); - - if(!$integration) return false; - - $buildUrl = $this->getBuildUrl($integration); - $build = new stdClass(); - $build->integration = $integration->id; - $build->name = $integration->name; - $build->queue = $this->loadModel('ci')->sendRequest($buildUrl, $data); - $build->status = $build->queue ? 'created' : 'create_fail'; - $build->createdBy = $this->app->user->account; - $build->createdDate = helper::now(); - $this->dao->insert(TABLE_COMPILE)->data($build)->exec(); - - return !dao::isError(); - } - /** * Get build url. * diff --git a/module/git/model.php b/module/git/model.php index f469d14cf6..fe44277d6d 100644 --- a/module/git/model.php +++ b/module/git/model.php @@ -141,6 +141,20 @@ class gitModel extends model $integrations = zget($objects, 'integrations', array()); $this->loadModel('compile'); foreach($integrations as $id) $this->compile->createByIntegration($id); + + // Create compile by tag. + $integrations = $this->dao->select('*')->from(TABLE_INTEGRATION)->where('triggerType')->eq('tag')->andWhere('repo')->eq($repo->id)->fetchAll('id'); + foreach($integrations as $integration) + { + $dirs = $this->getRepoTags($repo); + end($dirs); + $lastTag = current($dirs); + if($lastTag != $integration->lastTag) + { + $this->compile->createByIntegration($integration->id, $lastTag, 'tag'); + $this->dao->update(TABLE_INTEGRATION)->set('lastTag')->eq($lastTag)->where('id')->eq($integration->id)->exec(); + } + } } } diff --git a/module/integration/control.php b/module/integration/control.php index f563a83bf4..1e02daa540 100644 --- a/module/integration/control.php +++ b/module/integration/control.php @@ -78,6 +78,7 @@ class integration extends control $repoTypes = array(); foreach($repoList as $repo) { + if(empty($repo->synced)) continue; $repoPairs[$repo->id] = $repo->name; $repoTypes[$repo->id] = $repo->SCM; } @@ -117,6 +118,7 @@ class integration extends control $repoTypes[$repo->id] = $repo->SCM; foreach($repoList as $repo) { + if(empty($repo->synced)) continue; $repoPairs[$repo->id] = $repo->name; $repoTypes[$repo->id] = $repo->SCM; } diff --git a/module/integration/model.php b/module/integration/model.php index 6b5e54fbe3..1682f52fec 100644 --- a/module/integration/model.php +++ b/module/integration/model.php @@ -86,6 +86,24 @@ class integrationModel extends model $id = $this->dao->lastInsertId(); if($integration->triggerType == 'schedule' and strpos($integration->atDay, date('w')) !== false) $this->loadModel('compile')->createByIntegration($id); + if($integration->triggerType == 'tag') + { + $repo = $this->loadModel('repo')->getRepoByID($integration->repo); + $lastTag = ''; + if($this->post->repoType != 'Subversion') + { + $dirs = $this->loadModel('svn')->getRepoTags($repo, $integration->svnDir); + end($dirs); + $lastTag = current($dirs); + } + else + { + $tags = $this->loadModel('git')->getRepoTags($repo); + end($tags); + $lastTag = current($tags); + } + $this->dao->update(TABLE_INTEGRATION)->set('lastTag')->eq($lastTag)->where('id')->eq($id)->exec(); + } return true; } @@ -102,6 +120,10 @@ class integrationModel extends model $integration = fixer::input('post') ->setDefault('atDay', '') ->setIF($this->post->repoType != 'Subversion', 'svnDir', '') + ->setIF($this->post->triggerType != 'commit', 'comment', '') + ->setIF($this->post->triggerType != 'schedule', 'atDay', '') + ->setIF($this->post->triggerType != 'schedule', 'atTime', '') + ->setIF($this->post->triggerType != 'tag', 'lastTag', '') ->add('editedBy', $this->app->user->account) ->add('editedDate', helper::now()) ->remove('repoType') @@ -127,6 +149,27 @@ class integrationModel extends model if(strpos($integration->atDay, $week) !== false) $this->loadModel('compile')->createByIntegration($integration->id); } } + elseif($integration->triggerType == 'tag') + { + if($integration->triggerType != $oldIntegration->triggerType or $integration->repo != $oldIntegration->repo) + { + $repo = $this->loadModel('repo')->getRepoByID($integration->repo); + $lastTag = ''; + if($this->post->repoType == 'Subversion') + { + $dirs = $this->loadModel('svn')->getRepoTags($repo, $integration->svnDir); + end($dirs); + $lastTag = current($dirs); + } + else + { + $tags = $this->loadModel('git')->getRepoTags($repo); + end($tags); + $lastTag = current($tags); + } + $this->dao->update(TABLE_INTEGRATION)->set('lastTag')->eq($lastTag)->where('id')->eq($id)->exec(); + } + } return true; } } diff --git a/module/repo/control.php b/module/repo/control.php index 765cd5295c..3469ff3d3f 100644 --- a/module/repo/control.php +++ b/module/repo/control.php @@ -853,27 +853,8 @@ class repo extends control $dirs[$parent] = $this->repo->encodePath($parent); } - $parent = '/'; - if($repo->prefix) $parent = rtrim($repo->prefix, '/'); - if(trim($path, '/')) $parent = rtrim($repo->prefix, '/') . '/' . trim($path, '/'); - $stmt = $this->dao->select('t1.*,t2.revision as svnRevision,t2.time')->from(TABLE_REPOFILES)->alias('t1') - ->leftJoin(TABLE_REPOHISTORY)->alias('t2')->on('t1.revision = t2.id') - ->where('t1.repo')->eq($repoID) - ->andWhere('t1.type')->eq('dir') - ->andWhere('t1.parent')->eq($parent) - ->orderBy('path,svnRevision,time') - ->query(); - - while($row = $stmt->fetch()) - { - $path = $row->path; - if($repo->prefix) $path = str_replace($repo->prefix, '', $path); - if(empty($path)) $path = '/'; - - $dirs[$path] = $this->repo->encodePath($path); - if($row->action == 'D') unset($dirs[$path]); - } - + $tags = $this->loadModel('svn')->getRepoTags($repo, $path); + foreach($tags as $dirPath => $dirName) $dirs[$dirPath] = $this->repo->encodePath($dirPath); die(json_encode($dirs)); } } diff --git a/module/repo/model.php b/module/repo/model.php index e6829a7542..f5f6133960 100644 --- a/module/repo/model.php +++ b/module/repo/model.php @@ -167,6 +167,7 @@ class repoModel extends model { $repos = $this->dao->select('*')->from(TABLE_REPO)->where('deleted')->eq('0') ->andWhere('SCM')->eq($scm) + ->andWhere('synced')->eq(1) ->orderBy('id') ->fetchAll(); diff --git a/module/svn/model.php b/module/svn/model.php index 514410263d..551b93bbc6 100644 --- a/module/svn/model.php +++ b/module/svn/model.php @@ -142,6 +142,21 @@ class svnModel extends model $integrations = zget($objects, 'integrations', array()); $this->loadModel('compile'); foreach($integrations as $id) $this->compile->createByIntegration($id); + + /* Create compile by tag. */ + $integrations = $this->dao->select('*')->from(TABLE_INTEGRATION)->where('triggerType')->eq('tag')->andWhere('repo')->eq($repo->id)->fetchAll('id'); + foreach($integrations as $integration) + { + $dirs = $this->getRepoTags($repo, $integration->svnDir); + end($dirs); + $lastTag = current($dirs); + if($lastTag != $integration->lastTag) + { + $tag = rtrim($repo->path , '/') . '/' . trim($integration->svnDir, '/') . '/' . $lastTag; + $this->compile->createByIntegration($integration->id, $tag, 'tag'); + $this->dao->update(TABLE_INTEGRATION)->set('lastTag')->eq($lastTag)->where('id')->eq($integration->id)->exec(); + } + } } } @@ -316,9 +331,40 @@ class svnModel extends model */ public function getRepoTags($repo, $path) { - $scm = $this->app->loadClass('scm'); - $scm->setEngine($repo); - return $scm->tags($path); + $parent = '/'; + if($repo->prefix) $parent = rtrim($repo->prefix, '/'); + if(trim($path, '/')) $parent = rtrim($repo->prefix, '/') . '/' . trim($path, '/'); + $stmt = $this->dao->select('t1.*,t2.revision as svnRevision,t2.time')->from(TABLE_REPOFILES)->alias('t1') + ->leftJoin(TABLE_REPOHISTORY)->alias('t2')->on('t1.revision = t2.id') + ->where('t1.repo')->eq($repo->id) + ->andWhere('t1.type')->eq('dir') + ->andWhere('t1.parent')->eq($parent) + ->orderBy('path,svnRevision,time') + ->query(); + + $dirs = array(); + while($row = $stmt->fetch()) + { + $path = $row->path; + if($repo->prefix) $path = str_replace($repo->prefix, '', $path); + if(empty($path)) $path = '/'; + + $dirs[$path] = $row; + if($row->action == 'D') unset($dirs[$path]); + } + + $dirTime = array(); + foreach($dirs as $dirPath => $dir) $dirTime[$dir->time][$dirPath] = $dirPath; + + ksort($dirTime); + $dirPairs = array(); + foreach($dirTime as $time => $dirPaths) + { + ksort($dirPaths); + foreach($dirPaths as $dirPath) $dirPairs[$dirPath] = basename($dirPath); + } + + return $dirPairs; } /**