Permalink
Browse files

Added buttons to project page for removing all/old project builds. App

config option `php_censor.build.keep_builds` (int) is define quantity
of keeping builds. Issue #209.
  • Loading branch information...
corpsee committed Sep 15, 2018
1 parent 5791114 commit a816b666a8ab2c2fdad0a246b70e2c820f3af5f0
View
@@ -61,6 +61,7 @@ php-censor:
remove_builds: true # This option allow/deny build cleaning
writer_buffer_size: 500 # BuildErrorWriter buffer size (count of inserts in one SQL query)
allow_public_artifacts: false # This option allow/deny to generate public artifacts (PHPUnit code coverage html report, Pdepend html reports)
keep_builds: 100 # How much builds is keeping when cleaning old builds.
security:
disable_auth: false # This option allows/deny you to disable authentication for PHP Censor
default_user_id: 1 # Default user when authentication disabled
@@ -295,6 +295,7 @@ protected function getConfigInformation(InputInterface $input, OutputInterface $
'remove_builds' => true,
'writer_buffer_size' => 500,
'allow_public_artifacts' => true,
'keep_builds' => 100,
],
'security' => [
'disable_auth' => false,
@@ -136,7 +136,7 @@ public function view($buildId)
$actions = '';
if (!$project->getArchived()) {
if ($this->currentUserIsAdmin()) {
$actions .= "<a class=\"btn btn-danger\" href=\"{$rebuildLink}?debug=1\">{$rebuildDebug}</a> ";
$actions .= "<a class=\"btn btn-warning\" href=\"{$rebuildLink}?debug=1\">{$rebuildDebug}</a> ";
}
$actions .= "<a class=\"btn btn-success\" href=\"{$rebuildLink}\">{$rebuild}</a> ";
}
@@ -17,6 +17,7 @@
use PHPCensor\Store\Factory;
use PHPCensor\Model\Project;
use PHPCensor\WebController;
use PHPCensor\Config;
/**
* Project Controller - Allows users to create, edit and view projects.
@@ -240,8 +241,12 @@ public function build($projectId)
}
/**
* Delete a project.
*/
* @param int $projectId
*
* @return RedirectResponse
* @throws PHPCensor\Exception\HttpException
* @throws PHPCensor\Exception\HttpException\ForbiddenException
*/
public function delete($projectId)
{
$this->requireAdmin();
@@ -255,6 +260,44 @@ public function delete($projectId)
return $response;
}
/**
* @param int $projectId
*
* @return RedirectResponse
* @throws PHPCensor\Exception\HttpException
* @throws PHPCensor\Exception\HttpException\ForbiddenException
*/
public function deleteAllBuilds($projectId)
{
$this->requireAdmin();
$this->buildService->deleteAllByProject($projectId);
$response = new RedirectResponse();
$response->setHeader('Location', APP_URL . 'project/view/' . $projectId);
return $response;
}
/**
* @param int $projectId
*
* @return RedirectResponse
* @throws PHPCensor\Exception\HttpException
* @throws PHPCensor\Exception\HttpException\ForbiddenException
*/
public function deleteOldBuilds($projectId)
{
$this->requireAdmin();
$this->buildService->deleteOldByProject($projectId);
$response = new RedirectResponse();
$response->setHeader('Location', APP_URL . 'project/view/' . $projectId);
return $response;
}
/**
* Render latest builds for project as HTML table.
*
@@ -3,6 +3,7 @@
namespace PHPCensor\Controller;
use PHPCensor\Config;
use PHPCensor\Exception\HttpException\ForbiddenException;
use PHPCensor\Exception\HttpException\NotFoundException;
use PHPCensor\Form;
use PHPCensor\WebController;
@@ -65,6 +65,8 @@
'build_now_debug' => 'Build now with debug',
'edit_project' => 'Edit Project',
'delete_project' => 'Delete Project',
'delete_old_builds' => 'Delete old builds',
'delete_all_builds' => 'Delete all builds',
// Project Summary:
'no_builds_yet' => 'No builds yet!',
@@ -63,6 +63,8 @@
'build_now_debug' => 'Собрать в режиме отладки',
'edit_project' => 'Редактировать проект',
'delete_project' => 'Удалить проект',
'delete_old_builds' => 'Удалить старые сборки',
'delete_all_builds' => 'Удалить все сборки',
// Project Summary:
'no_builds_yet' => 'Нет сборок!',
@@ -9,6 +9,7 @@
use PHPCensor\Model\Build;
use PHPCensor\Model\Project;
use PHPCensor\Store\BuildStore;
use Symfony\Component\Filesystem\Filesystem;
/**
* The build service handles the creation, duplication and deletion of builds.
@@ -141,6 +142,52 @@ public function createDuplicateBuild(Build $copyFrom)
return $build;
}
/**
* @param int $projectId
*
* @throws \PHPCensor\Exception\HttpException
*/
public function deleteOldByProject($projectId)
{
$keepBuilds = (int)Config::getInstance()->get('php-censor.build.keep_builds', 100);
$builds = $this->buildStore->getOldByProject((int)$projectId, $keepBuilds);
/** @var Build $build */
foreach ($builds['items'] as $build) {
$build->removeBuildDirectory(true);
$this->buildStore->delete($build);
}
}
/**
* @param int $projectId
*/
public function deleteAllByProject($projectId)
{
$this->buildStore->deleteAllByProject((int)$projectId);
try {
$projectPaths = [
RUNTIME_DIR . 'builds/' . $projectId . '/',
PUBLIC_DIR . 'artifacts/pdepend/' . $projectId . '/',
PUBLIC_DIR . 'artifacts/phpunit/' . $projectId . '/',
];
$fileSystem = new Filesystem();
foreach ($projectPaths as $projectPath) {
if (is_link($projectPath)) {
// Remove the symlink without using recursive.
exec(sprintf('rm "%s"', $projectPath));
} else {
$fileSystem->remove($projectPath);
}
}
} catch (\Exception $e) {
}
}
/**
* Delete a given build.
*
View
@@ -494,9 +494,49 @@ public function updateStatusSync($build, $status)
$stmt->bindValue(':id', $build->getId(), \PDO::PARAM_INT);
$stmt->bindValue(':status_current', $build->getStatus(), \PDO::PARAM_INT);
$stmt->bindValue(':status_new', $status, \PDO::PARAM_INT);
return ($stmt->execute() && ($stmt->rowCount() == 1));
} catch (\Exception $e) {
return false;
}
}
public function deleteAllByProject($projectId)
{
$q = Database::getConnection('write')
->prepareCommon(
'DELETE FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id'
);
$q->bindValue(':project_id', $projectId);
$q->execute();
return $q->rowCount();
}
public function getOldByProject($projectId, $keep = 100)
{
if (is_null($projectId)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{' . $this->tableName . '}} WHERE {{project_id}} = :project_id ORDER BY {{create_date}} DESC OFFSET :keep';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':project_id', $projectId);
$stmt->bindValue(':keep', (int)$keep, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Build($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
}
@@ -11,7 +11,7 @@ use PHPCensor\Model\Build;
<?php if(empty($builds) || !count($builds)): ?>
<tr class="">
<td colspan="6"><?= Lang::get('no_builds_yet'); ?></td>
<td colspan="10"><?= Lang::get('no_builds_yet'); ?></td>
</tr>
<?php endif; ?>
@@ -21,26 +21,34 @@ use PHPCensor\Model\Project;
<?= Lang::get('edit_project'); ?>
</a>
<a class="btn btn-danger" id="delete-project">
<?= Lang::get('delete_project'); ?>
</a>
<?php if ($this->getUser()->getIsAdmin()): ?>
<a class="btn btn-danger" id="delete-project">
<?= Lang::get('delete_project'); ?>
</a>
<a class="btn btn-danger" id="delete-old-builds">
<?= Lang::get('delete_old_builds'); ?>
</a>
<a class="btn btn-danger" id="delete-all-builds">
<?= Lang::get('delete_all_builds'); ?>
</a>
<?php endif; ?>
<?php
$build_url = APP_URL . 'project/build/' . $project->getId();
?>
<?php $build_url = APP_URL . 'project/build/' . $project->getId(); ?>
<div class="pull-right btn-group">
<?php if (!$project->getArchived()): ?>
<?php if ($this->getUser()->getIsAdmin()): ?>
<?php if (!empty($environment)): ?>
<a class="btn btn-danger" href="<?= $build_url . '?' . http_build_query(['type' => 'environment', 'id' => $environment, 'debug' => 1]); ?>">
<a class="btn btn-warning" href="<?= $build_url . '?' . http_build_query(['type' => 'environment', 'id' => $environment, 'debug' => 1]); ?>">
<?= Lang::get('build_now_debug'); ?>
</a>
<?php elseif (!empty($branch)): ?>
<a class="btn btn-danger" href="<?= $build_url . '?' . http_build_query(['type' => 'branch', 'id' => $branch, 'debug' => 1]); ?>">
<a class="btn btn-warning" href="<?= $build_url . '?' . http_build_query(['type' => 'branch', 'id' => $branch, 'debug' => 1]); ?>">
<?= Lang::get('build_now_debug'); ?>
</a>
<?php else: ?>
<a class="btn btn-danger" href="<?= $build_url . '?' . http_build_query(['debug' => 1]); ?>">
<a class="btn btn-warning" href="<?= $build_url . '?' . http_build_query(['debug' => 1]); ?>">
<?= Lang::get('build_now_debug'); ?>
</a>
<?php endif; ?>
@@ -233,5 +241,17 @@ use PHPCensor\Model\Project;
confirmDelete("<?= APP_URL; ?>project/delete/<?= $project->getId(); ?>")
.onCloseConfirmed = function () {window.location = '/'};
});
$('#delete-old-builds').on('click', function (e) {
e.preventDefault();
confirmDelete("<?= APP_URL; ?>project/delete-old-builds/<?= $project->getId(); ?>")
.onCloseConfirmed = function () {window.location = '/'};
});
$('#delete-all-builds').on('click', function (e) {
e.preventDefault();
confirmDelete("<?= APP_URL; ?>project/delete-all-builds/<?= $project->getId(); ?>")
.onCloseConfirmed = function () {window.location = '/'};
});
})
</script>

0 comments on commit a816b66

Please sign in to comment.