From 14557c09db3c38730d9dac4f90d21457623f395e Mon Sep 17 00:00:00 2001
From: aaronchen2k <462826@qq.com>
Date: Fri, 17 Jan 2020 12:28:58 +0800
Subject: [PATCH] analyze new tags and launch ci task if needed
---
module/ci/config.php | 3 +-
module/ci/lang/zh-cn.php | 3 +
module/ci/view/createcitask.html.php | 10 +-
module/ci/view/editcitask.html.php | 10 +-
module/git/model.php | 141 +++++++++++++++++++++++++--
5 files changed, 148 insertions(+), 19 deletions(-)
diff --git a/module/ci/config.php b/module/ci/config.php
index e85ee96adf..ef1c6b446c 100644
--- a/module/ci/config.php
+++ b/module/ci/config.php
@@ -15,4 +15,5 @@ $config->repo->binary = '|pdf|';
$config->repo->requiredFields = 'name,repo,buildType,jenkins,jenkinsTask,triggerType';
-$config->repo->commitCommands = array( entity => '/\s*([a-z]+)\s+((?:build)|(?:story)|(?:task)|(?:bug))\s+#((?:\d|,)+)\s*/i' );
\ No newline at end of file
+$config->ci->commitCommandRegx = '/\s*([a-z]+)\s+((?:build)|(?:story)|(?:task)|(?:bug))\s+#((?:\d|,)+)\s*/i';
+$config->ci->tagCommandRegx = '/build[\-_]#((?:\d|,)+)/i';
\ No newline at end of file
diff --git a/module/ci/lang/zh-cn.php b/module/ci/lang/zh-cn.php
index 7314a012f1..4528e6719b 100644
--- a/module/ci/lang/zh-cn.php
+++ b/module/ci/lang/zh-cn.php
@@ -87,6 +87,9 @@ $lang->citask->day = '天';
$lang->citask->lastExe = '最后执行';
$lang->citask->scheduleTime = '时间';
+$lang->citask->example = '举例';
+$lang->citask->tagEx = 'build_#15,其中15为Jenkins任务编号';
+$lang->citask->commitEx = 'start build #15,其中15为Jenkins任务编号';
$lang->citask->cronSample = '如 0 0 2 * * 2-6/1 表示每个工作日凌晨2点';
$lang->citask->dayTypeList = array('workDay'=>'工作日', 'everyDay'=>'每天');
diff --git a/module/ci/view/createcitask.html.php b/module/ci/view/createcitask.html.php
index 905350f8be..ad3316dc45 100644
--- a/module/ci/view/createcitask.html.php
+++ b/module/ci/view/createcitask.html.php
@@ -52,14 +52,12 @@
"onclick='scheduleTypeChanged(this.value)'");?>
- | citask->tagKeywords; ?> |
- |
- *build_* |
+ citask->example; ?> |
+ citask->tagEx; ?> |
diff --git a/module/ci/view/editcitask.html.php b/module/ci/view/editcitask.html.php
index 5f29b17235..d1ea7cda2c 100644
--- a/module/ci/view/editcitask.html.php
+++ b/module/ci/view/editcitask.html.php
@@ -54,14 +54,12 @@
"onclick='scheduleTypeChanged(this.value)'");?>
- | citask->tagKeywords; ?> |
- tagKeywords, "class='form-control'"); ?> |
- *build_* |
+ citask->example; ?> |
+ citask->tagEx; ?> |
diff --git a/module/git/model.php b/module/git/model.php
index be26c91141..9731b127ff 100644
--- a/module/git/model.php
+++ b/module/git/model.php
@@ -122,7 +122,46 @@ class gitModel extends model
$this->deleteRestartFile();
$this->printLog("\n\nrepo ' . $repo->id . ': ' . $repo->path . ' finished");
- $this->printLog('extract ' . json_encode($allCommands));
+ $this->printLog('extract commands from logs' . json_encode($allCommands));
+
+ // exe ci task from logs
+ $citaskIDs = $allCommands['build']['start'];
+ foreach($citaskIDs as $id)
+ {
+ $this->loadModel('ci')->exeCitask($id);
+ }
+
+ // dealwith tag commands
+ $this->printLog("dealwith tag commands");
+ $savedTag = $this->getSavedTag();
+
+ $tags = $this->getRepoTags($repo);
+ if(empty($tags)) continue;
+
+ $arriveLastTag = false;
+ $taskToBuild = [];
+ foreach($tags as $tag)
+ {
+ if (!empty($savedTag) && $tag === $savedTag) {
+ $arriveLastTag = true;
+ continue;
+ }
+
+ if (!empty($savedTag) && !$arriveLastTag) {
+ continue;
+ }
+
+ $this->parseTag($tag, $taskToBuild);
+ }
+
+ $this->saveLastTag($tags[count($tags) - 1]);
+
+ $this->printLog('extract tasks to build: ' . json_encode($taskToBuild));
+
+ foreach($taskToBuild as $id)
+ {
+ $this->loadModel('ci')->exeCitask($id);
+ }
}
}
@@ -245,6 +284,7 @@ class gitModel extends model
if(empty($this->client)) return false;
$this->setLogFile($repo->id);
+ $this->setTagFile($repo->id);
$this->setRepoRoot($repo);
return true;
}
@@ -270,14 +310,26 @@ class gitModel extends model
/**
* Set the log file of a repo.
- *
- * @param string $repoName
+ *
+ * @param string $repoId
* @access public
* @return void
*/
- public function setLogFile($repoName)
+ public function setLogFile($repoId)
{
- $this->logFile = $this->logRoot . $repoName;
+ $this->logFile = $this->logRoot . $repoId . 'log';
+ }
+
+ /**
+ * Set the tag file of a repo.
+ *
+ * @param string $repoId
+ * @access public
+ * @return void
+ */
+ public function setTagFile($repoId)
+ {
+ $this->tagFile = $this->logRoot . $repoId . 'tag';
}
/**
@@ -292,6 +344,32 @@ class gitModel extends model
$this->repoRoot = $repo->path;
}
+ /**
+ * 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.
*
@@ -380,6 +458,32 @@ 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.
*
@@ -390,7 +494,7 @@ class gitModel extends model
*/
public function parseComment($comment, &$allCommands)
{
- $pattern = $this->config->repo->commitCommands['entity'];
+ $pattern = $this->config->ci->commitCommandRegx;
$matches = array();
preg_match_all($pattern, $comment,$matches);
@@ -771,6 +875,31 @@ class gitModel extends model
$ret = file_put_contents($this->logFile, $revision);
}
+ /**
+ * 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);
+ }
+
/**
* Pring log.
*