Now builds artifacts remove when remove build and project. Issue #107.

This commit is contained in:
Dmitry Khomutov 2018-02-25 18:14:54 +07:00
parent 3ec111da24
commit 0fc2a66678
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
6 changed files with 150 additions and 46 deletions

View file

@ -70,7 +70,7 @@ class BuildDBLogHandler extends AbstractProcessingHandler
protected function write(array $record) protected function write(array $record)
{ {
$message = (string)$record['message']; $message = (string)$record['message'];
$message = str_replace($this->build->currentBuildPath, './', $message); $message = str_replace($this->build->getBuildPath(), './', $message);
$this->logValue .= $message . PHP_EOL; $this->logValue .= $message . PHP_EOL;

View file

@ -4,6 +4,7 @@ namespace PHPCensor\Model;
use PHPCensor\Builder; use PHPCensor\Builder;
use PHPCensor\Store\BuildErrorStore; use PHPCensor\Store\BuildErrorStore;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Parser as YamlParser; use Symfony\Component\Yaml\Parser as YamlParser;
use PHPCensor\Model; use PHPCensor\Model;
use b8\Store\Factory; use b8\Store\Factory;
@ -44,6 +45,16 @@ class Build extends Model
*/ */
protected $newErrorsCount = null; protected $newErrorsCount = null;
/**
* @var string
*/
protected $buildDirectory;
/**
* @var string
*/
protected $buildBranchDirectory;
/** /**
* @var array * @var array
*/ */
@ -683,8 +694,6 @@ class Build extends Model
return Factory::getStore('BuildMeta', 'PHPCensor')->getByBuildId($this->getId()); return Factory::getStore('BuildMeta', 'PHPCensor')->getByBuildId($this->getId());
} }
public $currentBuildPath;
/** /**
* Get link to commit from another source (i.e. Github) * Get link to commit from another source (i.e. Github)
*/ */
@ -885,8 +894,42 @@ class Build extends Model
} }
/** /**
* Return the path to run this build into. * @return string|null
* */
public function getBuildDirectory()
{
if (!$this->getId()) {
return null;
}
if (empty($this->buildDirectory)) {
$this->buildDirectory = $this->getProjectId() . '/' . $this->getId() . '_' . substr(
md5(($this->getId() . '_' . $this->getCreateDate()->format('Y-m-d H:i:s'))
), 0, 8);
}
return $this->buildDirectory;
}
/**
* @return string|null
*/
public function getBuildBranchDirectory()
{
if (!$this->getId()) {
return null;
}
if (empty($this->buildBranchDirectory)) {
$this->buildBranchDirectory = $this->getProjectId() . '/' . $this->getBranch() . '_' . substr(
md5(($this->getBranch() . '_' . $this->getProject()->getCreateDate()->format('Y-m-d H:i:s'))
), 0, 8);
}
return $this->buildBranchDirectory;
}
/**
* @return string|null * @return string|null
*/ */
public function getBuildPath() public function getBuildPath()
@ -895,17 +938,7 @@ class Build extends Model
return null; return null;
} }
if (empty($this->currentBuildPath)) { return RUNTIME_DIR . 'builds/' . $this->getBuildDirectory() . '/';
$buildDirectory = $this->getId() . '_' . substr(md5(microtime(true)), 0, 5);
$this->currentBuildPath =
RUNTIME_DIR .
'builds' .
DIRECTORY_SEPARATOR .
$buildDirectory .
DIRECTORY_SEPARATOR;
}
return $this->currentBuildPath;
} }
/** /**
@ -921,11 +954,22 @@ class Build extends Model
return; return;
} }
if (is_link($buildPath)) { try {
// Remove the symlink without using recursive. $fileSystem = new Filesystem();
exec(sprintf('rm "%s"', $buildPath));
} else { if (is_link($buildPath)) {
exec(sprintf('rm -Rf "%s"', $buildPath)); // Remove the symlink without using recursive.
exec(sprintf('rm "%s"', $buildPath));
} else {
$fileSystem->remove($buildPath);
}
$buildDirectory = $this->getBuildDirectory();
$fileSystem->remove(PUBLIC_DIR . 'artifacts/pdepend/' . $buildDirectory);
$fileSystem->remove(PUBLIC_DIR . 'artifacts/phpunit/' . $buildDirectory);
} catch (\Exception $e) {
} }
} }

View file

@ -5,6 +5,7 @@ namespace PHPCensor\Plugin;
use PHPCensor\Builder; use PHPCensor\Builder;
use PHPCensor\Model\Build; use PHPCensor\Model\Build;
use PHPCensor\Plugin; use PHPCensor\Plugin;
use Symfony\Component\Filesystem\Filesystem;
/** /**
* Pdepend Plugin - Allows Pdepend report * Pdepend Plugin - Allows Pdepend report
@ -20,6 +21,11 @@ class Pdepend extends Plugin
*/ */
protected $buildDirectory; protected $buildDirectory;
/**
* @var string
*/
protected $buildBranchDirectory;
/** /**
* @var string Directory which needs to be scanned * @var string Directory which needs to be scanned
*/ */
@ -41,10 +47,14 @@ class Pdepend extends Plugin
protected $pyramid; protected $pyramid;
/** /**
* @var string Location on the server where the files are stored. Preferably in the webroot for inclusion * @var string
* in the readme.md of the repository
*/ */
protected $location; protected $buildLocation;
/**
* @var string
*/
protected $buildBranchLocation;
/** /**
* @return string * @return string
@ -69,8 +79,11 @@ class Pdepend extends Plugin
$this->pyramid = 'pyramid.svg'; $this->pyramid = 'pyramid.svg';
$this->chart = 'chart.svg'; $this->chart = 'chart.svg';
$this->buildDirectory = $build->getProjectId() . '/' . $build->getId(); $this->buildDirectory = $build->getBuildDirectory();
$this->location = PUBLIC_DIR . 'artifacts/pdepend/' . $this->buildDirectory; $this->buildBranchDirectory = $build->getBuildBranchDirectory();
$this->buildLocation = PUBLIC_DIR . 'artifacts/pdepend/' . $this->buildDirectory;
$this->buildBranchLocation = PUBLIC_DIR . 'artifacts/pdepend/' . $this->buildBranchDirectory;
} }
/** /**
@ -78,11 +91,11 @@ class Pdepend extends Plugin
*/ */
public function execute() public function execute()
{ {
if (!file_exists($this->location)) { if (!file_exists($this->buildLocation)) {
mkdir($this->location, (0777 & ~umask()), true); mkdir($this->buildLocation, (0777 & ~umask()), true);
} }
if (!is_writable($this->location)) { if (!is_writable($this->buildLocation)) {
throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->location)); throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->buildLocation));
} }
$pdepend = $this->findBinary('pdepend'); $pdepend = $this->findBinary('pdepend');
@ -100,22 +113,32 @@ class Pdepend extends Plugin
$success = $this->builder->executeCommand( $success = $this->builder->executeCommand(
$cmd, $cmd,
$this->location . DIRECTORY_SEPARATOR . $this->summary, $this->buildLocation . DIRECTORY_SEPARATOR . $this->summary,
$this->location . DIRECTORY_SEPARATOR . $this->chart, $this->buildLocation . DIRECTORY_SEPARATOR . $this->chart,
$this->location . DIRECTORY_SEPARATOR . $this->pyramid, $this->buildLocation . DIRECTORY_SEPARATOR . $this->pyramid,
$ignore, $ignore,
$this->directory $this->directory
); );
$fileSystem = new Filesystem();
if (file_exists($this->buildLocation)) {
$fileSystem->remove($this->buildBranchLocation);
$fileSystem->mirror($this->buildLocation, $this->buildBranchLocation);
}
$config = $this->builder->getSystemConfig('php-censor'); $config = $this->builder->getSystemConfig('php-censor');
if ($success) { if ($success) {
$this->builder->logSuccess( $this->builder->logSuccess(
sprintf( sprintf(
"\nPdepend successful.\nYou can use: %s,\n![Chart](%s \"Pdepend Chart\") and\n![Pyramid](%s \"Pdepend Pyramid\")\nfor inclusion in the readme.md file", "\nPdepend successful build report.\nYou can use report for this build for inclusion in the readme.md file:\n%s,\n![Chart](%s \"Pdepend Chart\") and\n![Pyramid](%s \"Pdepend Pyramid\")\n\nOr report for last build in the branch:\n%s,\n![Chart](%s \"Pdepend Chart\") and\n![Pyramid](%s \"Pdepend Pyramid\")\n",
$config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->summary, $config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->summary,
$config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->chart, $config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->chart,
$config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->pyramid $config['url'] . '/artifacts/pdepend/' . $this->buildDirectory . '/' . $this->pyramid,
$config['url'] . '/artifacts/pdepend/' . $this->buildBranchDirectory . '/' . $this->summary,
$config['url'] . '/artifacts/pdepend/' . $this->buildBranchDirectory . '/' . $this->chart,
$config['url'] . '/artifacts/pdepend/' . $this->buildBranchDirectory . '/' . $this->pyramid
) )
); );
} }
@ -130,8 +153,8 @@ class Pdepend extends Plugin
{ {
//Remove the created files first //Remove the created files first
foreach ([$this->summary, $this->chart, $this->pyramid] as $file) { foreach ([$this->summary, $this->chart, $this->pyramid] as $file) {
if (file_exists($this->location . DIRECTORY_SEPARATOR . $file)) { if (file_exists($this->buildLocation . DIRECTORY_SEPARATOR . $file)) {
unlink($this->location . DIRECTORY_SEPARATOR . $file); unlink($this->buildLocation . DIRECTORY_SEPARATOR . $file);
} }
} }
} }

View file

@ -11,6 +11,7 @@ use PHPCensor\Plugin\Util\PhpUnitResultJson;
use PHPCensor\Plugin\Util\PhpUnitResultJunit; use PHPCensor\Plugin\Util\PhpUnitResultJunit;
use PHPCensor\Plugin; use PHPCensor\Plugin;
use PHPCensor\ZeroConfigPluginInterface; use PHPCensor\ZeroConfigPluginInterface;
use Symfony\Component\Filesystem\Filesystem;
/** /**
* PHP Unit Plugin - A rewrite of the original PHP Unit plugin * PHP Unit Plugin - A rewrite of the original PHP Unit plugin
@ -28,9 +29,21 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface
/** /**
* @var string * @var string
*/ */
protected $location; protected $buildBranchDirectory;
/** @var string[] Raw options from the config file */ /**
* @var string
*/
protected $buildLocation;
/**
* @var string
*/
protected $buildBranchLocation;
/**
* @var string[] Raw options from the config file
*/
protected $options = []; protected $options = [];
/** /**
@ -57,10 +70,13 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface
{ {
parent::__construct($builder, $build, $options); parent::__construct($builder, $build, $options);
$this->buildDirectory = $this->build->getProjectId() . '/' . $this->build->getId(); $this->buildDirectory = $build->getBuildDirectory();
$this->location = PUBLIC_DIR . 'artifacts/phpunit/' . $this->buildDirectory . '/coverage'; $this->buildBranchDirectory = $build->getBuildBranchDirectory();
$this->options = new PhpUnitOptions($options, $this->location); $this->buildLocation = PUBLIC_DIR . 'artifacts/phpunit/' . $this->buildDirectory;
$this->buildBranchLocation = PUBLIC_DIR . 'artifacts/phpunit/' . $this->buildBranchDirectory;
$this->options = new PhpUnitOptions($options, $this->buildLocation, $this->buildBranchLocation);
} }
/** /**
@ -145,8 +161,10 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface
$options->addArgument('configuration', $buildPath . $configFile); $options->addArgument('configuration', $buildPath . $configFile);
} }
if (!file_exists($this->location) && $options->getOption('coverage')) { $fileSystem = new Filesystem();
mkdir($this->location, (0777 & ~umask()), true);
if (!$fileSystem->exists($this->buildLocation) && $options->getOption('coverage')) {
$fileSystem->mkdir($this->buildLocation, (0777 & ~umask()));
} }
$arguments = $this->builder->interpolate($options->buildArgumentString()); $arguments = $this->builder->interpolate($options->buildArgumentString());
@ -154,6 +172,11 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface
$success = $this->builder->executeCommand($cmd, $arguments, $directory); $success = $this->builder->executeCommand($cmd, $arguments, $directory);
$output = $this->builder->getLastOutput(); $output = $this->builder->getLastOutput();
if ($fileSystem->exists($this->buildLocation) && $options->getOption('coverage')) {
$fileSystem->remove($this->buildBranchLocation);
$fileSystem->mirror($this->buildLocation, $this->buildBranchLocation);
}
$this->processResults($logFile, $logFormat); $this->processResults($logFile, $logFormat);
$config = $this->builder->getSystemConfig('php-censor'); $config = $this->builder->getSystemConfig('php-censor');
@ -173,8 +196,9 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface
$this->builder->logSuccess( $this->builder->logSuccess(
sprintf( sprintf(
"\nPHPUnit successful.\nYou can use coverage report: %s", "\nPHPUnit successful build coverage report.\nYou can use coverage report for this build: %s\nOr coverage report for last build in the branch: %s",
$config['url'] . '/artifacts/phpunit/' . $this->buildDirectory . '/coverage/index.html' $config['url'] . '/artifacts/phpunit/' . $this->buildDirectory . '/index.html',
$config['url'] . '/artifacts/phpunit/' . $this->buildBranchDirectory . '/index.html'
) )
); );
} }

View file

@ -151,6 +151,7 @@ class BuildService
public function deleteBuild(Build $build) public function deleteBuild(Build $build)
{ {
$build->removeBuildDirectory(); $build->removeBuildDirectory();
return $this->buildStore->delete($build); return $this->buildStore->delete($build);
} }

View file

@ -2,8 +2,10 @@
namespace PHPCensor\Service; namespace PHPCensor\Service;
use PHPCensor\Model\Build;
use PHPCensor\Model\Project; use PHPCensor\Model\Project;
use PHPCensor\Store\ProjectStore; use PHPCensor\Store\ProjectStore;
use Symfony\Component\Filesystem\Filesystem;
/** /**
* The project service handles the creation, modification and deletion of projects. * The project service handles the creation, modification and deletion of projects.
@ -120,6 +122,16 @@ class ProjectService
*/ */
public function deleteProject(Project $project) public function deleteProject(Project $project)
{ {
try {
$fileSystem = new Filesystem();
$fileSystem->remove(RUNTIME_DIR . 'builds/' . $project->getId());
$fileSystem->remove(PUBLIC_DIR . 'artifacts/pdepend/' . $project->getId());
$fileSystem->remove(PUBLIC_DIR . 'artifacts/phpunit/' . $project->getId());
} catch (\Exception $e) {
}
return $this->projectStore->delete($project); return $this->projectStore->delete($project);
} }