* Refactor sonaqube module.

This commit is contained in:
zhaoke
2023-08-02 14:02:37 +08:00
parent 5379b95a20
commit 4c881641b4
10 changed files with 291 additions and 32 deletions
+52
View File
@@ -0,0 +1,52 @@
<?php
global $lang, $app;
$app->loadLang('sonarqube');
$config->sonarqube->dtable = new stdclass();
$config->sonarqube->dtable->fieldList['id']['title'] = 'ID';
$config->sonarqube->dtable->fieldList['id']['name'] = 'id';
$config->sonarqube->dtable->fieldList['id']['type'] = 'number';
$config->sonarqube->dtable->fieldList['id']['sortType'] = 'desc';
$config->sonarqube->dtable->fieldList['id']['checkbox'] = false;
$config->sonarqube->dtable->fieldList['id']['width'] = '80';
$config->sonarqube->dtable->fieldList['name']['title'] = $lang->sonarqube->name;
$config->sonarqube->dtable->fieldList['name']['name'] = 'name';
$config->sonarqube->dtable->fieldList['name']['type'] = 'desc';
$config->sonarqube->dtable->fieldList['name']['sortType'] = true;
$config->sonarqube->dtable->fieldList['name']['hint'] = true;
$config->sonarqube->dtable->fieldList['name']['minWidth'] = '356';
$config->sonarqube->dtable->fieldList['url']['title'] = $lang->sonarqube->url;
$config->sonarqube->dtable->fieldList['url']['name'] = 'url';
$config->sonarqube->dtable->fieldList['url']['type'] = 'desc';
$config->sonarqube->dtable->fieldList['url']['sortType'] = true;
$config->sonarqube->dtable->fieldList['url']['hint'] = true;
$config->sonarqube->dtable->fieldList['url']['minWidth'] = '356';
$config->sonarqube->actionList = array();
$config->sonarqube->actionList['list']['icon'] = 'list';
$config->sonarqube->actionList['list']['text'] = $lang->sonarqube->browseProject;
$config->sonarqube->actionList['list']['hint'] = $lang->sonarqube->browseProject;
$config->sonarqube->actionList['list']['url'] = helper::createLink('sonarqube', 'browseProject',"sonarqubeID={id}");
$config->sonarqube->actionList['edit']['icon'] = 'edit';
$config->sonarqube->actionList['edit']['text'] = $lang->sonarqube->edit;
$config->sonarqube->actionList['edit']['hint'] = $lang->sonarqube->edit;
$config->sonarqube->actionList['edit']['url'] = helper::createLink('sonarqube', 'edit',"sonarqubeID={id}");
$config->sonarqube->actionList['delete']['icon'] = 'trash';
$config->sonarqube->actionList['delete']['text'] = $lang->sonarqube->delete;
$config->sonarqube->actionList['delete']['hint'] = $lang->sonarqube->delete;
$config->sonarqube->actionList['delete']['ajaxSubmit'] = true;
$config->sonarqube->actionList['delete']['url'] = helper::createLink('sonarqube', 'delete',"sonarqubeID={id}");
$config->sonarqube->dtable->fieldList['actions']['name'] = 'actions';
$config->sonarqube->dtable->fieldList['actions']['title'] = $lang->actions;
$config->sonarqube->dtable->fieldList['actions']['type'] = 'actions';
$config->sonarqube->dtable->fieldList['actions']['width'] = '160';
$config->sonarqube->dtable->fieldList['actions']['sortType'] = false;
$config->sonarqube->dtable->fieldList['actions']['fixed'] = 'right';
$config->sonarqube->dtable->fieldList['actions']['menu'] = array('list', 'edit', 'delete');
$config->sonarqube->dtable->fieldList['actions']['list'] = $config->sonarqube->actionList;
+11
View File
@@ -0,0 +1,11 @@
<?php
$config->sonarqube->form = new stdclass();
$config->sonarqube->form->create = common::formConfig('sonarqube', 'create');
$config->sonarqube->form->create['type'] = array('type' => 'string', 'required' => true, 'default' => 'sonarqube');
$config->sonarqube->form->create['name'] = array('type' => 'string', 'required' => true, 'default' => '');
$config->sonarqube->form->create['url'] = array('type' => 'string', 'required' => true, 'default' => '');
$config->sonarqube->form->create['account'] = array('type' => 'string', 'required' => true, 'default' => '');
$config->sonarqube->form->create['password'] = array('type' => 'string', 'required' => true, 'default' => '');
+49 -29
View File
@@ -37,7 +37,7 @@ class sonarqube extends control
*/
public function browse($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$this->app->loadClass('pager', $static = true);
$this->app->loadClass('pager', true);
$pager = new pager($recTotal, $recPerPage, $pageID);
$sonarqubeList = $this->loadModel('pipeline')->getList('sonarqube', $orderBy, $pager);
@@ -106,13 +106,19 @@ class sonarqube extends control
$projectList = $this->sonarqube->apiGetProjects($sonarqubeID);
$projectPairs = array('' => '');
$projectPairs = array();
foreach($projectList as $project)
{
if(!empty($project) and !in_array($project->key, $existsProject)) $projectPairs[$project->key] = $project->name;
}
echo html::select('projectKey', $projectPairs, str_replace('*', '-', $projectKey), "class='form-control chosen'");
$options = array();
foreach($projectPairs as $productKey => $projectName)
{
$options[] = array('text' => $projectName, 'value' => $productKey);
}
return print(json_encode($options));
//echo html::select('projectKey', $projectPairs, str_replace('*', '-', $projectKey), "class='form-control chosen'");
}
/**
@@ -125,11 +131,20 @@ class sonarqube extends control
{
if($_POST)
{
$this->checkToken();
$sonarqubeID = $this->loadModel('pipeline')->create('sonarqube');
$sonarqube = form::data($this->config->sonarqube->form->create)
->add('type', 'sonarqube')
->add('private',md5(rand(10,113450)))
->add('createdBy', $this->app->user->account)
->add('createdDate', helper::now())
->trim('url,account,password')
->skipSpecial('url,token,account,password')
->remove('token,appType')
->get();
$this->checkToken($sonarqube);
$sonarqubeID = $this->loadModel('pipeline')->create($sonarqube);
if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError()));
$actionID = $this->loadModel('action')->create('sonarqube', $sonarqubeID, 'created');
$this->loadModel('action')->create('sonarqube', $sonarqubeID, 'created');
return $this->send(array('result' => 'success', 'message' => $this->lang->saveSuccess, 'locate' => inlink('browse')));
}
@@ -141,11 +156,11 @@ class sonarqube extends control
/**
* Check post info.
*
* @param int $sonarqubeID
* @param object $sonarqube
* @access protected
* @return void
*/
protected function checkToken($sonarqubeID = 0)
protected function checkToken(object $sonarqube)
{
$sonarqube = fixer::input('post')->trim('url,token,account,password')->get();
$this->dao->update('sonarqube')->data($sonarqube)
@@ -167,7 +182,7 @@ class sonarqube extends control
$result = $this->sonarqube->apiValidate($sonarqube->url, $token);
if(!empty($result)) return $this->send(array('result' => 'fail', 'message' => $result));
$this->post->set('token', $token);
$sonarqube->token = $token;
}
/**
@@ -208,19 +223,27 @@ class sonarqube extends control
* @access public
* @return void
*/
public function delete($sonarqubeID, $confirm = 'no')
public function delete($sonarqubeID)
{
if($confirm != 'yes') return print(js::confirm($this->lang->sonarqube->confirmDelete, inlink('delete', "sonarqubeID=$sonarqubeID&confirm=yes")));
$oldSonarQube = $this->loadModel('pipeline')->getByID($sonarqubeID);
$this->loadModel('action');
$actionID = $this->pipeline->delete($sonarqubeID, 'sonarqube');
if($actionID) return print(js::error($this->lang->sonarqube->delError));
if($actionID)
{
$response['result'] = 'fail';
$response['callback'] = sprintf('zui.Modal.alert("%s");', $this->lang->sonarqube->delError);
return $this->send($response);
}
$sonarQube = $this->pipeline->getByID($sonarqubeID);
$changes = common::createChanges($oldSonarQube, $sonarQube);
$this->action->logHistory($actionID, $changes);
echo js::reload('parent');
$response['load'] = true;
$response['result'] = 'success';
return $this->send($response);
}
/**
@@ -246,14 +269,14 @@ class sonarqube extends control
* @access public
* @return void
*/
public function browseProject($sonarqubeID, $orderBy = 'name_desc', $recTotal = 0, $recPerPage = 15, $pageID = 1)
public function browseProject($sonarqubeID, $orderBy = 'name_desc', $recPerPage = 15, $pageID = 1)
{
$this->app->loadClass('pager', $static = true);
$this->app->loadClass('pager', true);
$keyword = fixer::input('post')->setDefault('keyword', '')->get('keyword');
$sonarqubeProjectList = $this->sonarqube->apiGetProjects($sonarqubeID, $keyword);
$projectKeyList = array();
foreach($sonarqubeProjectList as $key => $sonarqubeProject)
foreach($sonarqubeProjectList as $sonarqubeProject)
{
if(!isset($sonarqubeProject->lastAnalysisDate)) $sonarqubeProject->lastAnalysisDate = '';
$projectKeyList[] = $sonarqubeProject->key;
@@ -266,7 +289,7 @@ class sonarqube extends control
array_multisort($orderList, $sort == 'desc' ? SORT_DESC : SORT_ASC, $sonarqubeProjectList);
/* Pager. */
$this->app->loadClass('pager', $static = true);
$this->app->loadClass('pager', true);
$recTotal = count($sonarqubeProjectList);
$pager = new pager($recTotal, $recPerPage, $pageID);
$sonarqubeProjectList = array_chunk($sonarqubeProjectList, $pager->recPerPage);
@@ -345,7 +368,7 @@ class sonarqube extends control
* @access public
* @return void
*/
public function browseIssue($sonarqubeID, $projectKey = '', $search = false, $orderBy = 'severity_desc', $recTotal = 0, $recPerPage = 100, $pageID = 1)
public function browseIssue($sonarqubeID, $projectKey = '', $search = false, $orderBy = 'severity_desc', $recPerPage = 100, $pageID = 1)
{
if(isset($_POST['keyword']))
{
@@ -357,7 +380,7 @@ class sonarqube extends control
else
{
$keyword = '';
if($search == true) $keyword = $this->session->sonarqubeIssueKeyword;
if($search) $keyword = $this->session->sonarqubeIssueKeyword;
}
ini_set('memory_limit', '1024M');
@@ -373,18 +396,15 @@ class sonarqube extends control
$sonarqubeIssue->message = htmlspecialchars($sonarqubeIssue->message);
$sonarqubeIssue->creationDate = date('Y-m-d H:i:s', strtotime($sonarqubeIssue->creationDate));
list($project, $file) = explode(':', $sonarqubeIssue->component);
list(, $file) = explode(':', $sonarqubeIssue->component);
$sonarqubeIssue->file = $file;
}
if($cacheFile)
if($cacheFile && !file_exists($cacheFile . '.lock'))
{
if(!file_exists($cacheFile . '.lock'))
{
touch($cacheFile . '.lock');
file_put_contents($cacheFile, serialize($sonarqubeIssueList));
unlink($cacheFile . '.lock');
}
touch($cacheFile . '.lock');
file_put_contents($cacheFile, serialize($sonarqubeIssueList));
unlink($cacheFile . '.lock');
}
}
else
@@ -413,7 +433,7 @@ class sonarqube extends control
$productID = current(explode(',', $products));
/* Pager. */
$this->app->loadClass('pager', $static = true);
$this->app->loadClass('pager', true);
$recTotal = count($sonarqubeIssueList);
$pager = new pager($recTotal, $recPerPage, $pageID);
$sonarqubeIssueList = array_chunk($sonarqubeIssueList, $pager->recPerPage);
+19
View File
@@ -0,0 +1,19 @@
window.createSortLink = function(col)
{
var sort = col.name + '_asc';
if(sort == orderBy) sort = col.name + '_desc';
return sortLink.replace('{orderBy}', sort);
}
window.renderCell = function(result, {col, row})
{
if(col.name === 'url')
{
result[0] = {html:'<a href="' + row.data.url + '" target="_blank">' + row.data.url + '</a>', style:{flexDirection:"column"}};
return result;
}
return result;
};
+1 -1
View File
@@ -19,7 +19,7 @@ $lang->sonarqube->createBug = "Create Bug";
$lang->sonarqube->delError = "There are bound jobs under this server, please delete the association and then operate";
$lang->sonarqube->id = 'ID';
$lang->sonarqube->name = "Server Name";
$lang->sonarqube->name = "Application Name";
$lang->sonarqube->url = 'Server Address';
$lang->sonarqube->account = 'Username';
$lang->sonarqube->password = 'Password';
+1 -1
View File
@@ -19,7 +19,7 @@ $lang->sonarqube->createBug = "转bug";
$lang->sonarqube->delError = "该服务器下有绑定的构建,请删除关联之后操作";
$lang->sonarqube->id = 'ID';
$lang->sonarqube->name = "服务器名称";
$lang->sonarqube->name = "应用名称";
$lang->sonarqube->url = '服务器地址';
$lang->sonarqube->account = '用户名';
$lang->sonarqube->password = '密码';
+1 -1
View File
@@ -57,7 +57,7 @@ class sonarqubeModel extends model
* Get sonarqube report.
*
* @param int $sonarqubeID
* @param stirng $projectKey
* @param string $projectKey
* @param string $metricKeys
* @access public
* @return object
+43
View File
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
/**
* The browse view file of sonarqube module of ZenTaoPMS.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Ke Zhao<zhaoke@easycorp.ltd>
* @package sonarqube
* @link http://www.zentao.net
*/
namespace zin;
featureBar();
/* zin: Define the toolbar on main menu. */
$canCreate = hasPriv('sonarqube', 'create');
$createLink = $this->createLink('sonarqube', 'create');
$createItem = array('text' => $lang->sonarqube->create, 'url' => $createLink, 'class' => 'primary', 'icon' => 'plus');
$tableData = initTableData($sonarqubeList, $config->sonarqube->dtable->fieldList, $this->sonarqube);
toolbar
(
$canCreate ? item(set($createItem)) : null,
);
jsVar('confirmDelete', $lang->sonarqube->confirmDelete);
jsVar('orderBy', $orderBy);
jsVar('canBrowseProject', common::hasPriv('sonarqube', 'browseProject'));
jsVar('sortLink', helper::createLink('sonarqube', 'browse', "orderBy={orderBy}&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}"));
dtable
(
set::cols(array_values($config->sonarqube->dtable->fieldList)),
set::data($tableData),
set::sortLink(jsRaw('createSortLink')),
set::onRenderCell(jsRaw('window.renderCell')),
set::footPager(usePager()),
);
render();
+57
View File
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/**
* The create view file of sonarqube module of ZenTaoPMS.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Ke Zhao<zhaoke@easycorp.ltd>
* @package sonarqube
* @link https://www.zentao.net
*/
namespace zin;
formPanel
(
set::id('sonarqubeCreateForm'),
set::title($lang->sonarqube->createServer),
formRow
(
formGroup
(
set::name('name'),
set::label($lang->sonarqube->name),
set::value($sonarqube->name),
)
),
formRow
(
formGroup
(
set::name('url'),
set::label($lang->sonarqube->url),
set::value($sonarqube->url),
set::placeholder($lang->sonarqube->placeholder->url)
)
),
formRow
(
formGroup
(
set::name('account'),
set::label($lang->sonarqube->account),
set::value($sonarqube->account),
set::placeholder($lang->sonarqube->placeholder->account)
)
),
formRow
(
formGroup
(
set::name('password'),
set::label($lang->sonarqube->password),
set::value($sonarqube->password),
)
),
);
+57
View File
@@ -0,0 +1,57 @@
<?php
declare(strict_types=1);
/**
* The edit view file of sonarqube module of ZenTaoPMS.
*
* @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.zentao.net)
* @license ZPL(https://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
* @author Ke Zhao<zhaoke@easycorp.ltd>
* @package sonarqube
* @link https://www.zentao.net
*/
namespace zin;
formPanel
(
set::id('sonarqubeCreateForm'),
set::title($lang->sonarqube->editServer),
formRow
(
formGroup
(
set::name('name'),
set::label($lang->sonarqube->name),
set::value($sonarqube->name),
)
),
formRow
(
formGroup
(
set::name('url'),
set::label($lang->sonarqube->url),
set::value($sonarqube->url),
set::placeholder($lang->sonarqube->placeholder->url)
)
),
formRow
(
formGroup
(
set::name('account'),
set::label($lang->sonarqube->account),
set::value($sonarqube->account),
set::placeholder($lang->sonarqube->placeholder->account)
)
),
formRow
(
formGroup
(
set::name('password'),
set::label($lang->sonarqube->password),
set::value($sonarqube->password),
)
),
);