From b6c7f38e1aa06d60e3167a7e13891c19e64ee5be Mon Sep 17 00:00:00 2001 From: aaronchen2k <462826@qq.com> Date: Fri, 10 Jan 2020 18:16:26 +0800 Subject: [PATCH] exe git pull and retrive git log --- module/ci/control.php | 2 +- module/ci/model.php | 16 ++++++++++++++++ module/git/config.php | 4 ++-- module/git/model.php | 43 +++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/module/ci/control.php b/module/ci/control.php index c61dfb4798..ffdba75c1b 100644 --- a/module/ci/control.php +++ b/module/ci/control.php @@ -438,7 +438,7 @@ class ci extends control */ public function deleteRepo($id) { - $this->ci->delete(TABLE_JENKINS, $id); + $this->ci->delete(TABLE_REPO, $id); if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); $this->send(array('result' => 'success')); diff --git a/module/ci/model.php b/module/ci/model.php index 108eff981a..c07309f2e4 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -698,6 +698,22 @@ class ciModel extends model return $repos; } + /** + * list repos for jenkins task edit + * + * @return mixed + */ + public function listRepoForSync($whr) + { + $repos = $this->dao->select('*')->from(TABLE_REPO) + ->where('deleted')->eq('0') + ->beginIF(!empty(whr))->andWhere($whr)->fi() + ->orderBy(id) + ->fetchAll(); + $repos[''] = ''; + return $repos; + } + /** * list jenkins for ci task edit * diff --git a/module/git/config.php b/module/git/config.php index 8033ef700f..72f2ab11f8 100644 --- a/module/git/config.php +++ b/module/git/config.php @@ -15,10 +15,10 @@ */ $config->git = new stdClass(); $config->git->encodings = 'utf-8'; -$config->git->client = ''; +$config->git->client = '/usr/bin/git'; $i = 1; -$config->git->repos[$i]['path'] = ''; +$config->git->repos[$i]['path'] = '/Users/aaron/devops/project/devops_test'; $config->git->repos[$i]['encoding'] = 'utf-8'; /* diff --git a/module/git/model.php b/module/git/model.php index 844f2724ac..ea7cb14542 100644 --- a/module/git/model.php +++ b/module/git/model.php @@ -94,6 +94,8 @@ class gitModel extends model $repo->name = $name; if(!$this->setRepo($repo)) return false; + $this->pull(); + $savedRevision = $this->getSavedRevision(); $this->printLog("start from revision $savedRevision"); $logs = $this->getRepoLogs($repo, $savedRevision); @@ -135,6 +137,23 @@ class gitModel extends model } } + /** + * Pull codes from remote repo. + * + * @access public + * @return void + */ + public function pull() + { + chdir($this->repoRoot); + +// exec('sudo -u aaron whoami', $output, $return); + + $cmd = "{$this->client} pull 2>&1"; + exec($cmd, $output, $return); + $this->printLog("{$cmd}, {$output}, {$return}"); + } + /** * Set the log root. * @@ -177,13 +196,33 @@ class gitModel extends model */ public function setRepos() { - if(!$this->config->git->repos) + $ci = $this->loadModel('ci'); + $repoObjs = $ci->listRepoForSync("true"); + + $gitRepos = []; + $paths = []; + foreach($repoObjs as $repoInDb) + { + if(strtolower($repoInDb->SCM) === 'git' && !in_array($repoInDb->path, $gitRepos)) { + $gitRepos[] = array(path => $repoInDb->path, encoding => 'utf-8'); + $paths[] = $repoInDb->path; + } + } + + foreach($this->config->git->repos as $repoInConfig) + { + if(!empty($repoInConfig['path']) && !in_array($repoInConfig['path'], $paths)) { + $gitRepos[] = $repoInConfig; + } + } + + if(!$gitRepos) { echo "You must set one git repo.\n"; return false; } - $this->repos = $this->config->git->repos; + $this->repos = $gitRepos; return true; }