From 0fc2a6667826e675ab76a09aea4727e3d17ca41b Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sun, 25 Feb 2018 18:14:54 +0700 Subject: [PATCH 1/2] Now builds artifacts remove when remove build and project. Issue #107. --- src/PHPCensor/Logging/BuildDBLogHandler.php | 2 +- src/PHPCensor/Model/Build.php | 84 ++++++++++++++++----- src/PHPCensor/Plugin/Pdepend.php | 55 ++++++++++---- src/PHPCensor/Plugin/PhpUnit.php | 42 ++++++++--- src/PHPCensor/Service/BuildService.php | 1 + src/PHPCensor/Service/ProjectService.php | 12 +++ 6 files changed, 150 insertions(+), 46 deletions(-) diff --git a/src/PHPCensor/Logging/BuildDBLogHandler.php b/src/PHPCensor/Logging/BuildDBLogHandler.php index 5cd34892..942b358e 100644 --- a/src/PHPCensor/Logging/BuildDBLogHandler.php +++ b/src/PHPCensor/Logging/BuildDBLogHandler.php @@ -70,7 +70,7 @@ class BuildDBLogHandler extends AbstractProcessingHandler protected function write(array $record) { $message = (string)$record['message']; - $message = str_replace($this->build->currentBuildPath, './', $message); + $message = str_replace($this->build->getBuildPath(), './', $message); $this->logValue .= $message . PHP_EOL; diff --git a/src/PHPCensor/Model/Build.php b/src/PHPCensor/Model/Build.php index 2a40d684..9ebeb453 100644 --- a/src/PHPCensor/Model/Build.php +++ b/src/PHPCensor/Model/Build.php @@ -4,6 +4,7 @@ namespace PHPCensor\Model; use PHPCensor\Builder; use PHPCensor\Store\BuildErrorStore; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Parser as YamlParser; use PHPCensor\Model; use b8\Store\Factory; @@ -44,6 +45,16 @@ class Build extends Model */ protected $newErrorsCount = null; + /** + * @var string + */ + protected $buildDirectory; + + /** + * @var string + */ + protected $buildBranchDirectory; + /** * @var array */ @@ -683,8 +694,6 @@ class Build extends Model return Factory::getStore('BuildMeta', 'PHPCensor')->getByBuildId($this->getId()); } - public $currentBuildPath; - /** * 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 */ public function getBuildPath() @@ -895,17 +938,7 @@ class Build extends Model return null; } - if (empty($this->currentBuildPath)) { - $buildDirectory = $this->getId() . '_' . substr(md5(microtime(true)), 0, 5); - $this->currentBuildPath = - RUNTIME_DIR . - 'builds' . - DIRECTORY_SEPARATOR . - $buildDirectory . - DIRECTORY_SEPARATOR; - } - - return $this->currentBuildPath; + return RUNTIME_DIR . 'builds/' . $this->getBuildDirectory() . '/'; } /** @@ -921,11 +954,22 @@ class Build extends Model return; } - if (is_link($buildPath)) { - // Remove the symlink without using recursive. - exec(sprintf('rm "%s"', $buildPath)); - } else { - exec(sprintf('rm -Rf "%s"', $buildPath)); + try { + $fileSystem = new Filesystem(); + + if (is_link($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) { + } } diff --git a/src/PHPCensor/Plugin/Pdepend.php b/src/PHPCensor/Plugin/Pdepend.php index dc02df9d..b9cc2735 100644 --- a/src/PHPCensor/Plugin/Pdepend.php +++ b/src/PHPCensor/Plugin/Pdepend.php @@ -5,6 +5,7 @@ namespace PHPCensor\Plugin; use PHPCensor\Builder; use PHPCensor\Model\Build; use PHPCensor\Plugin; +use Symfony\Component\Filesystem\Filesystem; /** * Pdepend Plugin - Allows Pdepend report @@ -20,6 +21,11 @@ class Pdepend extends Plugin */ protected $buildDirectory; + /** + * @var string + */ + protected $buildBranchDirectory; + /** * @var string Directory which needs to be scanned */ @@ -41,10 +47,14 @@ class Pdepend extends Plugin protected $pyramid; /** - * @var string Location on the server where the files are stored. Preferably in the webroot for inclusion - * in the readme.md of the repository + * @var string */ - protected $location; + protected $buildLocation; + + /** + * @var string + */ + protected $buildBranchLocation; /** * @return string @@ -69,8 +79,11 @@ class Pdepend extends Plugin $this->pyramid = 'pyramid.svg'; $this->chart = 'chart.svg'; - $this->buildDirectory = $build->getProjectId() . '/' . $build->getId(); - $this->location = PUBLIC_DIR . 'artifacts/pdepend/' . $this->buildDirectory; + $this->buildDirectory = $build->getBuildDirectory(); + $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() { - if (!file_exists($this->location)) { - mkdir($this->location, (0777 & ~umask()), true); + if (!file_exists($this->buildLocation)) { + mkdir($this->buildLocation, (0777 & ~umask()), true); } - if (!is_writable($this->location)) { - throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->location)); + if (!is_writable($this->buildLocation)) { + throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->buildLocation)); } $pdepend = $this->findBinary('pdepend'); @@ -100,22 +113,32 @@ class Pdepend extends Plugin $success = $this->builder->executeCommand( $cmd, - $this->location . DIRECTORY_SEPARATOR . $this->summary, - $this->location . DIRECTORY_SEPARATOR . $this->chart, - $this->location . DIRECTORY_SEPARATOR . $this->pyramid, + $this->buildLocation . DIRECTORY_SEPARATOR . $this->summary, + $this->buildLocation . DIRECTORY_SEPARATOR . $this->chart, + $this->buildLocation . DIRECTORY_SEPARATOR . $this->pyramid, $ignore, $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'); if ($success) { $this->builder->logSuccess( 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->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 foreach ([$this->summary, $this->chart, $this->pyramid] as $file) { - if (file_exists($this->location . DIRECTORY_SEPARATOR . $file)) { - unlink($this->location . DIRECTORY_SEPARATOR . $file); + if (file_exists($this->buildLocation . DIRECTORY_SEPARATOR . $file)) { + unlink($this->buildLocation . DIRECTORY_SEPARATOR . $file); } } } diff --git a/src/PHPCensor/Plugin/PhpUnit.php b/src/PHPCensor/Plugin/PhpUnit.php index 2cb0cd44..ca49afba 100644 --- a/src/PHPCensor/Plugin/PhpUnit.php +++ b/src/PHPCensor/Plugin/PhpUnit.php @@ -11,6 +11,7 @@ use PHPCensor\Plugin\Util\PhpUnitResultJson; use PHPCensor\Plugin\Util\PhpUnitResultJunit; use PHPCensor\Plugin; use PHPCensor\ZeroConfigPluginInterface; +use Symfony\Component\Filesystem\Filesystem; /** * PHP Unit Plugin - A rewrite of the original PHP Unit plugin @@ -28,9 +29,21 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface /** * @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 = []; /** @@ -57,10 +70,13 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface { parent::__construct($builder, $build, $options); - $this->buildDirectory = $this->build->getProjectId() . '/' . $this->build->getId(); - $this->location = PUBLIC_DIR . 'artifacts/phpunit/' . $this->buildDirectory . '/coverage'; + $this->buildDirectory = $build->getBuildDirectory(); + $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); } - if (!file_exists($this->location) && $options->getOption('coverage')) { - mkdir($this->location, (0777 & ~umask()), true); + $fileSystem = new Filesystem(); + + if (!$fileSystem->exists($this->buildLocation) && $options->getOption('coverage')) { + $fileSystem->mkdir($this->buildLocation, (0777 & ~umask())); } $arguments = $this->builder->interpolate($options->buildArgumentString()); @@ -154,6 +172,11 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $success = $this->builder->executeCommand($cmd, $arguments, $directory); $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); $config = $this->builder->getSystemConfig('php-censor'); @@ -173,8 +196,9 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $this->builder->logSuccess( sprintf( - "\nPHPUnit successful.\nYou can use coverage report: %s", - $config['url'] . '/artifacts/phpunit/' . $this->buildDirectory . '/coverage/index.html' + "\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 . '/index.html', + $config['url'] . '/artifacts/phpunit/' . $this->buildBranchDirectory . '/index.html' ) ); } diff --git a/src/PHPCensor/Service/BuildService.php b/src/PHPCensor/Service/BuildService.php index fa06c506..2aee59ee 100644 --- a/src/PHPCensor/Service/BuildService.php +++ b/src/PHPCensor/Service/BuildService.php @@ -151,6 +151,7 @@ class BuildService public function deleteBuild(Build $build) { $build->removeBuildDirectory(); + return $this->buildStore->delete($build); } diff --git a/src/PHPCensor/Service/ProjectService.php b/src/PHPCensor/Service/ProjectService.php index 5d54a079..7822f062 100644 --- a/src/PHPCensor/Service/ProjectService.php +++ b/src/PHPCensor/Service/ProjectService.php @@ -2,8 +2,10 @@ namespace PHPCensor\Service; +use PHPCensor\Model\Build; use PHPCensor\Model\Project; use PHPCensor\Store\ProjectStore; +use Symfony\Component\Filesystem\Filesystem; /** * The project service handles the creation, modification and deletion of projects. @@ -120,6 +122,16 @@ class ProjectService */ 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); } From 2a933a7ecbf34128ea24aefab79fcf92bc0f7411 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Thu, 8 Mar 2018 00:47:09 +0700 Subject: [PATCH 2/2] Added global application config (config.yml) option 'php-censor.build.allow_public_artifacts' for allow/deny to generate public artifacts (PHPUnit code coverage html report, Pdepend html reports). Issue #107. --- docs/en/configuring.md | 5 +- src/PHPCensor/Command/InstallCommand.php | 5 +- src/PHPCensor/Command/RunCommand.php | 2 +- src/PHPCensor/Model/Build.php | 12 +++-- .../Plugin/Option/PhpUnitOptions.php | 11 ++++- src/PHPCensor/Plugin/Pdepend.php | 45 +++++++++--------- src/PHPCensor/Plugin/PhpUnit.php | 47 ++++++++++++++----- src/PHPCensor/Service/BuildService.php | 2 +- 8 files changed, 82 insertions(+), 47 deletions(-) diff --git a/docs/en/configuring.md b/docs/en/configuring.md index 85833b44..641a4066 100644 --- a/docs/en/configuring.md +++ b/docs/en/configuring.md @@ -58,8 +58,9 @@ php-censor: status: commit: false # This option allow/deny to post status to Github commit build: - remove_builds: true # This option allow/deny build cleaning - writer_buffer_size: 500 # BuildErrorWriter buffer size (count of inserts in one SQL query) + 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) security: disable_auth: false # This option allows/deny you to disable authentication for PHP Censor default_user_id: 1 # Default user when authentication disabled diff --git a/src/PHPCensor/Command/InstallCommand.php b/src/PHPCensor/Command/InstallCommand.php index 70a8bc15..285749d3 100644 --- a/src/PHPCensor/Command/InstallCommand.php +++ b/src/PHPCensor/Command/InstallCommand.php @@ -291,8 +291,9 @@ class InstallCommand extends Command ], ], 'build' => [ - 'remove_builds' => true, - 'writer_buffer_size' => 500, + 'remove_builds' => true, + 'writer_buffer_size' => 500, + 'allow_public_artifacts' => true, ], 'security' => [ 'disable_auth' => false, diff --git a/src/PHPCensor/Command/RunCommand.php b/src/PHPCensor/Command/RunCommand.php index 71b32e2d..0f6c582a 100644 --- a/src/PHPCensor/Command/RunCommand.php +++ b/src/PHPCensor/Command/RunCommand.php @@ -163,7 +163,7 @@ class RunCommand extends Command $build->setStatus(Build::STATUS_FAILED); $build->setFinishDate(new \DateTime()); $store->save($build); - $build->removeBuildDirectory(); + $build->removeBuildDirectory(true); continue; } diff --git a/src/PHPCensor/Model/Build.php b/src/PHPCensor/Model/Build.php index 9ebeb453..6c4cec40 100644 --- a/src/PHPCensor/Model/Build.php +++ b/src/PHPCensor/Model/Build.php @@ -943,8 +943,10 @@ class Build extends Model /** * Removes the build directory. + * + * @param boolean $withArtifacts */ - public function removeBuildDirectory() + public function removeBuildDirectory($withArtifacts = false) { // Get the path and remove the trailing slash as this may prompt PHP // to see this as a directory even if it's a link. @@ -964,10 +966,12 @@ class Build extends Model $fileSystem->remove($buildPath); } - $buildDirectory = $this->getBuildDirectory(); + if ($withArtifacts) { + $buildDirectory = $this->getBuildDirectory(); - $fileSystem->remove(PUBLIC_DIR . 'artifacts/pdepend/' . $buildDirectory); - $fileSystem->remove(PUBLIC_DIR . 'artifacts/phpunit/' . $buildDirectory); + $fileSystem->remove(PUBLIC_DIR . 'artifacts/pdepend/' . $buildDirectory); + $fileSystem->remove(PUBLIC_DIR . 'artifacts/phpunit/' . $buildDirectory); + } } catch (\Exception $e) { } diff --git a/src/PHPCensor/Plugin/Option/PhpUnitOptions.php b/src/PHPCensor/Plugin/Option/PhpUnitOptions.php index 6a53599e..0a49869d 100644 --- a/src/PHPCensor/Plugin/Option/PhpUnitOptions.php +++ b/src/PHPCensor/Plugin/Option/PhpUnitOptions.php @@ -2,6 +2,8 @@ namespace PHPCensor\Plugin\Option; +use b8\Config; + /** * Class PhpUnitOptions validates and parse the option for the PhpUnitV2 plugin * @@ -120,7 +122,14 @@ class PhpUnitOptions * Handles command aliases outside of the args option */ if (isset($this->options['coverage']) && $this->options['coverage']) { - $this->addArgument('coverage-html', $this->location); + $allowPublicArtifacts = (bool)Config::getInstance()->get( + 'php-censor.build.allow_public_artifacts', + true + ); + + if ($allowPublicArtifacts) { + $this->addArgument('coverage-html', $this->location); + } $this->addArgument('coverage-text'); } diff --git a/src/PHPCensor/Plugin/Pdepend.php b/src/PHPCensor/Plugin/Pdepend.php index b9cc2735..39445e47 100644 --- a/src/PHPCensor/Plugin/Pdepend.php +++ b/src/PHPCensor/Plugin/Pdepend.php @@ -2,6 +2,7 @@ namespace PHPCensor\Plugin; +use b8\Config; use PHPCensor\Builder; use PHPCensor\Model\Build; use PHPCensor\Plugin; @@ -91,18 +92,26 @@ class Pdepend extends Plugin */ public function execute() { - if (!file_exists($this->buildLocation)) { - mkdir($this->buildLocation, (0777 & ~umask()), true); + $allowPublicArtifacts = (bool)Config::getInstance()->get( + 'php-censor.build.allow_public_artifacts', + true + ); + + $fileSystem = new Filesystem(); + + if (!$fileSystem->exists($this->buildLocation)) { + $fileSystem->mkdir($this->buildLocation, (0777 & ~umask())); } + if (!is_writable($this->buildLocation)) { - throw new \Exception(sprintf('The location %s is not writable or does not exist.', $this->buildLocation)); + throw new \Exception(sprintf( + 'The location %s is not writable or does not exist.', + $this->buildLocation + )); } $pdepend = $this->findBinary('pdepend'); - - $cmd = $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; - - $this->removeBuildArtifacts(); + $cmd = $pdepend . ' --summary-xml="%s" --jdepend-chart="%s" --overview-pyramid="%s" %s "%s"'; // If we need to ignore directories if (count($this->builder->ignore)) { @@ -120,16 +129,17 @@ class Pdepend extends Plugin $this->directory ); - $fileSystem = new Filesystem(); - - if (file_exists($this->buildLocation)) { + if (!$allowPublicArtifacts) { + $fileSystem->remove($this->buildLocation); + } + if ($allowPublicArtifacts && file_exists($this->buildLocation)) { $fileSystem->remove($this->buildBranchLocation); $fileSystem->mirror($this->buildLocation, $this->buildBranchLocation); } $config = $this->builder->getSystemConfig('php-censor'); - if ($success) { + if ($allowPublicArtifacts && $success) { $this->builder->logSuccess( sprintf( "\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", @@ -145,17 +155,4 @@ class Pdepend extends Plugin return $success; } - - /** - * Remove files created from previous builds - */ - protected function removeBuildArtifacts() - { - //Remove the created files first - foreach ([$this->summary, $this->chart, $this->pyramid] as $file) { - if (file_exists($this->buildLocation . DIRECTORY_SEPARATOR . $file)) { - unlink($this->buildLocation . DIRECTORY_SEPARATOR . $file); - } - } - } } diff --git a/src/PHPCensor/Plugin/PhpUnit.php b/src/PHPCensor/Plugin/PhpUnit.php index ca49afba..3ce1b052 100644 --- a/src/PHPCensor/Plugin/PhpUnit.php +++ b/src/PHPCensor/Plugin/PhpUnit.php @@ -2,6 +2,7 @@ namespace PHPCensor\Plugin; +use b8\Config; use PHPCensor; use PHPCensor\Builder; use PHPCensor\Model\Build; @@ -76,7 +77,7 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $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); + $this->options = new PhpUnitOptions($options, $this->buildLocation); } /** @@ -144,9 +145,18 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface * @param string $logFormat * * @return bool|mixed + * + * @throws \Exception */ protected function runConfig($directory, $configFile, $logFormat) { + $allowPublicArtifacts = (bool)Config::getInstance()->get( + 'php-censor.build.allow_public_artifacts', + true + ); + + $fileSystem = new Filesystem(); + $options = clone $this->options; $buildPath = $this->build->getBuildPath(); @@ -161,10 +171,17 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $options->addArgument('configuration', $buildPath . $configFile); } - $fileSystem = new Filesystem(); + if ($options->getOption('coverage') && $allowPublicArtifacts) { + if (!$fileSystem->exists($this->buildLocation)) { + $fileSystem->mkdir($this->buildLocation, (0777 & ~umask())); + } - if (!$fileSystem->exists($this->buildLocation) && $options->getOption('coverage')) { - $fileSystem->mkdir($this->buildLocation, (0777 & ~umask())); + if (!is_writable($this->buildLocation)) { + throw new \Exception(sprintf( + 'The location %s is not writable or does not exist.', + $this->buildLocation + )); + } } $arguments = $this->builder->interpolate($options->buildArgumentString()); @@ -172,7 +189,11 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface $success = $this->builder->executeCommand($cmd, $arguments, $directory); $output = $this->builder->getLastOutput(); - if ($fileSystem->exists($this->buildLocation) && $options->getOption('coverage')) { + if ( + $fileSystem->exists($this->buildLocation) && + $options->getOption('coverage') && + $allowPublicArtifacts + ) { $fileSystem->remove($this->buildBranchLocation); $fileSystem->mirror($this->buildLocation, $this->buildBranchLocation); } @@ -194,13 +215,15 @@ class PhpUnit extends Plugin implements ZeroConfigPluginInterface 'lines' => !empty($matches[3]) ? $matches[3] : '0.00', ]); - $this->builder->logSuccess( - sprintf( - "\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 . '/index.html', - $config['url'] . '/artifacts/phpunit/' . $this->buildBranchDirectory . '/index.html' - ) - ); + if ($allowPublicArtifacts) { + $this->builder->logSuccess( + sprintf( + "\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 . '/index.html', + $config['url'] . '/artifacts/phpunit/' . $this->buildBranchDirectory . '/index.html' + ) + ); + } } return $success; diff --git a/src/PHPCensor/Service/BuildService.php b/src/PHPCensor/Service/BuildService.php index 2aee59ee..36ba2958 100644 --- a/src/PHPCensor/Service/BuildService.php +++ b/src/PHPCensor/Service/BuildService.php @@ -150,7 +150,7 @@ class BuildService */ public function deleteBuild(Build $build) { - $build->removeBuildDirectory(); + $build->removeBuildDirectory(true); return $this->buildStore->delete($build); }