diff --git a/config/zentaopms.php b/config/zentaopms.php index 0efdf433a4..ea0d52e4b7 100644 --- a/config/zentaopms.php +++ b/config/zentaopms.php @@ -171,6 +171,8 @@ define('TABLE_NOTIFY', '`' . $config->db->prefix . 'notify`'); define('TABLE_OAUTH', '`' . $config->db->prefix . 'oauth`'); define('TABLE_CREDENTIAL', '`' . $config->db->prefix . 'credential`'); define('TABLE_JENKINS', '`' . $config->db->prefix . 'jenkins`'); +define('TABLE_CI_TASK', '`' . $config->db->prefix . 'citask`'); +define('TABLE_CI_BUILD', '`' . $config->db->prefix . 'cibuild`'); define('TABLE_REPO', '`' . $config->db->prefix . 'repo`'); define('TABLE_REPOHISTORY', '`' . $config->db->prefix . 'repohistory`'); diff --git a/module/ci/control.php b/module/ci/control.php index db290e1add..209ceb21d8 100644 --- a/module/ci/control.php +++ b/module/ci/control.php @@ -47,7 +47,7 @@ class ci extends control $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); - $this->view->title = $this->lang->credential->common . $this->lang->colon . $this->lang->credential->list; + $this->view->title = $this->lang->credential->common . $this->lang->colon . $this->lang->ci->list; $this->view->credentialList = $this->ci->listCredential($orderBy, $pager); $this->view->position[] = $this->lang->ci->common; @@ -144,7 +144,7 @@ class ci extends control $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); - $this->view->title = $this->lang->jenkins->common . $this->lang->colon . $this->lang->jenkins->list; + $this->view->title = $this->lang->jenkins->common . $this->lang->colon . $this->lang->ci->list; $this->view->jenkinsList = $this->ci->listJenkins($orderBy, $pager); $this->view->position[] = $this->lang->ci->common; @@ -229,6 +229,104 @@ class ci extends control $this->send(array('result' => 'success')); } + /** + * Browse ci task. + * + * @param string $orderBy + * @param int $recTotal + * @param int $recPerPage + * @param int $pageID + * @access public + * @return void + */ + public function browseCitask($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1) + { + $this->app->loadClass('pager', $static = true); + $pager = new pager($recTotal, $recPerPage, $pageID); + + $this->view->title = $this->lang->citask->common . $this->lang->colon . $this->lang->ci->list; + $this->view->citaskList = $this->ci->listCitask($orderBy, $pager); + + $this->view->position[] = $this->lang->ci->common; + $this->view->position[] = $this->lang->citask->common; + + $this->view->orderBy = $orderBy; + $this->view->pager = $pager; + $this->view->module = 'citask'; + $this->display(); + } + + /** + * Create a ci task. + * + * @access public + * @return void + */ + public function createCitask() + { + if($_POST) + { + $this->ci->createCitask(); + if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); + $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('browseCitask'))); + } + + $this->app->loadLang('action'); + $this->view->title = $this->lang->citask->create . $this->lang->colon . $this->lang->citask->common; + $this->view->position[] = $this->lang->ci->common; + $this->view->position[] = html::a(inlink('browseCitask'), $this->lang->citask->common); + $this->view->position[] = $this->lang->citask->create; + + $this->view->repoList = $this->ci->listRepoForSelection("true"); + $this->view->module = 'citask'; + + $this->display(); + } + + /** + * Edit a ci task. + * + * @param int $id + * @access public + * @return void + */ + public function editCitask($id) + { + $citask = $this->ci->getJenkinsByID($id); + if($_POST) + { + $this->ci->updateCitask($id); + if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); + $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('browseCitask'))); + } + + $this->app->loadLang('action'); + $this->view->title = $this->lang->citask->edit . $this->lang->colon . $citask->name; + + $this->view->position[] = $this->lang->ci->common; + $this->view->position[] = html::a(inlink('browseCitask'), $this->lang->citask->common); + $this->view->position[] = $this->lang->citask->edit; + + $this->view->citask = $citask; + + $this->view->module = 'jenkins'; + $this->display(); + } + + /** + * Delete a ci task. + * + * @param int $id + * @access public + * @return void + */ + public function deleteCitask($id) + { + $this->ci->delete(TABLE_CI_TASK, $id); + if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError())); + + $this->send(array('result' => 'success')); + } /** * Browse repo. @@ -245,7 +343,7 @@ class ci extends control $this->app->loadClass('pager', $static = true); $pager = new pager($recTotal, $recPerPage, $pageID); - $this->view->title = $this->lang->repo->common . $this->lang->colon . $this->lang->repo->list; + $this->view->title = $this->lang->repo->common . $this->lang->colon . $this->lang->ci->list; $this->view->position[] = $this->lang->ci->common; $this->view->position[] = $this->lang->repo->common; diff --git a/module/ci/js/createcitask.js b/module/ci/js/createcitask.js new file mode 100644 index 0000000000..ead3f6e651 --- /dev/null +++ b/module/ci/js/createcitask.js @@ -0,0 +1,47 @@ +$(function() +{ + triggerTypeChanged('tag'); +}); + +function triggerTypeChanged(type) { + if(type == 'tag') { + $('.tag-fields').removeClass('hidden'); + $('.comment-fields').addClass('hidden'); + + scheduleTypeChanged(); + } else if(type == 'commit') { + $('.tag-fields').addClass('hidden'); + $('.comment-fields').removeClass('hidden'); + + $('.corn-fields').addClass('hidden'); + $('.custom-fields').addClass('hidden'); + + scheduleTypeChanged(); + } else if(type == 'schedule') { + $('.tag-fields').addClass('hidden'); + $('.comment-fields').addClass('hidden'); + + var val = $("input[name='scheduleType']:checked").val(); + console.log(val); + scheduleTypeChanged(val? val: 'corn'); + } +} + +function scheduleTypeChanged(type) { + if(type == 'corn') { + $('.schedule-fields').removeClass('hidden'); + + $('.corn-fields').removeClass('hidden'); + $('.custom-fields').addClass('hidden'); + } else if(type == 'custom') { + $('.schedule-fields').removeClass('hidden'); + + $('.corn-fields').addClass('hidden'); + $('.custom-fields').removeClass('hidden'); + } else { + $('.schedule-fields').addClass('hidden'); + + $('.corn-fields').addClass('hidden'); + $('.custom-fields').addClass('hidden'); + } +} \ No newline at end of file diff --git a/module/ci/lang/zh-cn.php b/module/ci/lang/zh-cn.php index 9aa7eb0eb6..9ad760f7c9 100644 --- a/module/ci/lang/zh-cn.php +++ b/module/ci/lang/zh-cn.php @@ -8,6 +8,7 @@ $lang->ci->name = '名称'; $lang->ci->subModules['credential'] = '凭证'; $lang->ci->subModules['jenkins'] = 'Jenkins'; $lang->ci->subModules['repo'] = '代码库'; +$lang->ci->subModules['citask'] = '构建任务'; // credential $lang->credential->common = '凭证'; @@ -52,6 +53,39 @@ $lang->jenkins->desc = '描述'; $lang->jenkins->typeList['token'] = 'Token'; $lang->jenkins->typeList['credential'] = '凭证'; +// ci task +$lang->citask->common = '构建任务'; +$lang->citask->browse = '浏览构建任务'; +$lang->citask->create = '添加构建任务'; +$lang->citask->edit = '编辑构建任务'; +$lang->citask->delete = '删除构建任务'; +$lang->citask->confirmDelete = '确认删除该构建任务吗?'; + +$lang->citask->id = 'ID'; +$lang->citask->name = '名称'; +$lang->citask->repo = '代码库'; +$lang->citask->jenkins = 'Jenkins服务'; +$lang->citask->jenkinsTask = 'Jenkins任务名'; +$lang->citask->buildType = '构建类型'; +$lang->citask->triggerType = '触发方式'; +$lang->citask->scheduleType = '时间计划'; +$lang->citask->cornExpression = 'Corn表达式'; +$lang->citask->custom = '自定义'; + +$lang->citask->scheduleInterval = '间隔'; +$lang->citask->tagKeywords = '标签关键字'; +$lang->citask->commentKeywords = '注释关键字'; + +$lang->citask->everyDay = '每天'; +$lang->citask->workDay = '工作日'; +$lang->citask->at = '在'; +$lang->citask->de = '的'; +$lang->citask->lastExe = '最后执行'; + +$lang->citask->buildTypeList = array(build=>'仅构建', buildAndDeploy=>'构建部署', buildAndTest=>'构建测试'); +$lang->citask->triggerTypeList = array(tag=>'打标签', commit=>'代码提交注释', schedule=>'定时计划'); +$lang->citask->scheduleTypeList = array(corn=>'Corn表达式', custom=>'自定义'); + // repo $lang->repo->common = '代码库'; $lang->repo->browse = '浏览代码库'; diff --git a/module/ci/model.php b/module/ci/model.php index 5cb12c5f7a..723138af06 100644 --- a/module/ci/model.php +++ b/module/ci/model.php @@ -167,20 +167,81 @@ class ciModel extends model } /** - * list credential for repo and jenkins edit page + * Get a ci task by id. * - * @param $whr - * @return mixed + * @param int $id + * @access public + * @return object */ - public function listCredentialForSelection($whr) + public function getCitaskByID($id) { - $credentials = $this->dao->select('id, name')->from(TABLE_CREDENTIAL) + $jenkins = $this->dao->select('*')->from(TABLE_CI_TASK)->where('id')->eq($id)->fetch(); + return $jenkins; + } + + /** + * Get ci task list. + * + * @param string $orderBy + * @param object $pager + * @param bool $decode + * @access public + * @return array + */ + public function listCitask($orderBy = 'id_desc', $pager = null, $decode = true) + { + $jenkinsList = $this->dao->select('*')->from(TABLE_CI_TASK) ->where('deleted')->eq('0') - ->beginIF(!empty(whr))->andWhere($whr)->fi() - ->orderBy(id) - ->fetchPairs(); - $credentials[''] = ''; - return $credentials; + ->orderBy($orderBy) + ->page($pager) + ->fetchAll('id'); + return $jenkinsList; + } + + /** + * Create a ci task. + * + * @access public + * @return bool + */ + public function createCitask() + { + $jenkins = fixer::input('post') + ->add('createdBy', $this->app->user->account) + ->add('createdDate', helper::now()) + ->get(); + + $this->dao->insert(TABLE_CI_TASK)->data($jenkins) + ->batchCheck($this->config->jenkins->create->requiredFields, 'notempty') + ->batchCheck("serviceUrl", 'URL') + ->batchCheckIF($jenkins->type === 'credential', "credential", 'notempty') + ->autoCheck() + ->exec(); + return !dao::isError(); + } + + /** + * Update a ci task. + * + * @param int $id + * @access public + * @return bool + */ + public function updateCitask($id) + { + $jenkins = fixer::input('post') + ->add('editedBy', $this->app->user->account) + ->add('editedDate', helper::now()) + ->get(); + + $this->dao->update(TABLE_CI_TASK)->data($jenkins) + ->batchCheck($this->config->jenkins->edit->requiredFields, 'notempty') + ->batchCheck("serviceUrl", 'URL') + ->batchCheckIF($jenkins->type === 'credential', "credential", 'notempty') + ->autoCheck() + ->where('id')->eq($id) + ->exec(); + return !dao::isError(); } /** @@ -592,4 +653,38 @@ class ciModel extends model $link .= $pathParams; return $link; } + + /** + * list credential for repo and jenkins edit page + * + * @param $whr + * @return mixed + */ + public function listCredentialForSelection($whr) + { + $credentials = $this->dao->select('id, name')->from(TABLE_CREDENTIAL) + ->where('deleted')->eq('0') + ->beginIF(!empty(whr))->andWhere($whr)->fi() + ->orderBy(id) + ->fetchPairs(); + $credentials[''] = ''; + return $credentials; + } + + /** + * list repos for jenkins task edit + * + * @return mixed + */ + public function listRepoForSelection($whr) + { + $repos = $this->dao->select('id, name')->from(TABLE_REPO) + ->where('deleted')->eq('0') + ->beginIF(!empty(whr))->andWhere($whr)->fi() + ->orderBy(id) + ->fetchPairs(); + $repos[''] = ''; + return $repos; + } + } diff --git a/module/ci/view/browsecitask.html.php b/module/ci/view/browsecitask.html.php new file mode 100644 index 0000000000..ce53412ec7 --- /dev/null +++ b/module/ci/view/browsecitask.html.php @@ -0,0 +1,71 @@ + + * @package jenkins + * @version $Id$ + * @link http://www.zentao.net + */ +?> + +jenkins->confirmDelete); ?> + +
+ +
+
+ + + + recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}"; ?> + + + + + + + + + + + + + $task): ?> + + + + + + + + + + + + + +
citask->id); ?> + citask->name); ?> + citask->jenkins); ?>citask->jenkinsTask; ?>citask->buildType; ?>citask->triggerType; ?>citask->lastExe; ?>actions; ?>
name; ?>jenkinsServer; ?>jenkinsTask; ?> + citask->buildTypeList[$task->buildType]; ?> + citask->triggerTypeList[$task->triggerType]; ?>lastBuild; ?> + createLink('ci', 'deleteJenkins', "jenkinsID=$id&confirm=yes"); + echo html::a("javascript:ajaxDelete(\"$deleteURL\", \"jenkinsList\", confirmDelete)", '', '', "title='{$lang->jenkins->delete}' class='btn'"); + } + ?> +
+ + + +
+
+
+ diff --git a/module/ci/view/createcitask.html.php b/module/ci/view/createcitask.html.php new file mode 100644 index 0000000000..fa88942349 --- /dev/null +++ b/module/ci/view/createcitask.html.php @@ -0,0 +1,92 @@ + + * @package jenkins + * @version $Id$ + * @link http://www.zentao.net + */ +?> + + + +
+ +
+
+
+

citask->create; ?>

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
citask->name; ?>
citask->repo; ?>citask->buildType; ?>citask->buildTypeList, 'buildAndDeploy', "class='form-control chosen'"); ?>
citask->jenkins; ?>citask->jenkinsTask; ?>
citask->triggerType; ?>citask->triggerTypeList, '', + "onchange='triggerTypeChanged(this.value)' class='form-control chosen'"); ?>citask->scheduleType; ?>citask->scheduleTypeList, 'corn', + "onclick='scheduleTypeChanged(this.value)'");?>
citask->tagKeywords; ?>*build_*
citask->commentKeywords; ?>build_now
citask->cornExpression; ?>0 0 2 * * ?
citask->custom; ?>
jenkins->desc; ?>
+ + +
+
+
+
+
+ + diff --git a/module/ci/view/editcitask.html.php b/module/ci/view/editcitask.html.php new file mode 100644 index 0000000000..5b17717a0b --- /dev/null +++ b/module/ci/view/editcitask.html.php @@ -0,0 +1,61 @@ + + * @package jenkins + * @version $Id$ + * @link http://www.zentao.net + */ +?> + + + +
+ +
+
+
+

jenkins->edit; ?>

+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
jenkins->name; ?>name, "class='form-control'"); ?>
jenkins->serviceUrl; ?>serviceUrl, "class='form-control'"); ?>
credential->common; ?>credential, "class='form-control'"); ?>
jenkins->desc; ?>desc, "rows='3' class='form-control'"); ?>
+ + +
+
+
+
+
+