Skip to content

Commit

Permalink
Changing comparison from latest commit on target branch to latest com…
Browse files Browse the repository at this point in the history
…mon ancestor commit
  • Loading branch information
moufmouf committed Apr 27, 2017
1 parent 7b4c02b commit efc861a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 27 deletions.
99 changes: 72 additions & 27 deletions src/Commands/RunCommand.php
Expand Up @@ -15,6 +15,7 @@
use TheCodingMachine\WashingMachine\Clover\CrapMethodMerger;
use TheCodingMachine\WashingMachine\Clover\DiffService;
use TheCodingMachine\WashingMachine\Clover\EmptyCloverFile;
use TheCodingMachine\WashingMachine\Git\GitRepository;
use TheCodingMachine\WashingMachine\Gitlab\BuildNotFoundException;
use TheCodingMachine\WashingMachine\Gitlab\BuildService;
use TheCodingMachine\WashingMachine\Gitlab\MergeRequestNotFoundException;
Expand Down Expand Up @@ -155,7 +156,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
$mergeRequest = $buildService->findMergeRequestByBuildRef($projectName, $buildRef);


list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromBranch($buildService, $mergeRequest['target_project_id'], $mergeRequest['target_branch'], $cloverFilePath, $crap4JFilePath);
$lastCommonCommit = $this->findMergeBase($mergeRequest['target_branch']);
list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromCommit($buildService, $mergeRequest['target_project_id'], $lastCommonCommit, $cloverFilePath, $crap4JFilePath);
//list($previousCodeCoverageProvider, $previousMethodsProvider) = $this->getMeasuresFromBranch($buildService, $mergeRequest['target_project_id'], $mergeRequest['target_branch'], $cloverFilePath, $crap4JFilePath);

$message = new Message();
if ($codeCoverageProvider !== null) {
Expand Down Expand Up @@ -262,39 +265,47 @@ public function getMeasuresFromBranch(BuildService $buildService, string $projec
if ($zipFile->open($tmpFile)!==true) {
throw new \RuntimeException('Invalid ZIP archive '.$tmpFile);
}
$cloverFileString = $zipFile->getFromName($cloverPath);

$cloverFile = null;
if ($cloverFileString !== false) {
$cloverFile = CloverFile::fromString($cloverFileString, getcwd());
return $this->getMeasuresFromZipFile($zipFile, $cloverPath, $crap4JPath);
} catch (\RuntimeException $e) {
if ($e->getCode() === 404) {
// We could not find a previous clover file in the master branch.
// Maybe this branch is the first to contain clover files?
// Let's deal with this by generating a fake "empty" clover file.
return [EmptyCloverFile::create(), EmptyCloverFile::create()];
} else {
throw $e;
}
}
}

$crap4JString = $zipFile->getFromName($crap4JPath);
/**
* Returns the last commit ID that is in common between current branch and passed branch.
*
* @param string $branchName
* @return string
*/
private function findMergeBase(string $branchName) : string
{
$repo = new GitRepository(getcwd());
$currentBranchName = $repo->getCurrentBranchName();
return $repo->getMergeBase($currentBranchName, $branchName);
}

$crap4JFile = null;
if ($crap4JString !== false) {
$crap4JFile = Crap4JFile::fromString($crap4JString);
}
public function getMeasuresFromCommit(BuildService $buildService, string $projectName, string $commitId, string $cloverPath, string $crap4JPath) : array
{
try {
$tmpFile = tempnam(sys_get_temp_dir(), 'art').'.zip';

$methodsProvider = null;
$codeCoverageProvider = null;

if ($cloverFile !== null && $crap4JFile !== null) {
$methodsProvider = new CrapMethodMerger($cloverFile, $crap4JFile);
$codeCoverageProvider = $cloverFile;
} elseif ($cloverFile !== null) {
$methodsProvider = $cloverFile;
$codeCoverageProvider = $cloverFile;
} elseif ($crap4JFile !== null) {
$methodsProvider = $crap4JFile;
} else {
return [EmptyCloverFile::create(), EmptyCloverFile::create()];
$buildRef = $this->getLatestBuildFromCommitId($projectName, $commitId);
$buildService->dumpArtifact($projectName, $buildRef, $tmpFile);
$zipFile = new \ZipArchive();
if ($zipFile->open($tmpFile)!==true) {
throw new \RuntimeException('Invalid ZIP archive '.$tmpFile);
}

return [$codeCoverageProvider, $methodsProvider];
return $this->getMeasuresFromZipFile($zipFile, $cloverPath, $crap4JPath);
} catch (\RuntimeException $e) {
if ($e->getCode() === 404) {
// We could not find a previous clover file in the master branch.
// We could not find a previous clover file in the give commit.
// Maybe this branch is the first to contain clover files?
// Let's deal with this by generating a fake "empty" clover file.
return [EmptyCloverFile::create(), EmptyCloverFile::create()];
Expand All @@ -304,6 +315,40 @@ public function getMeasuresFromBranch(BuildService $buildService, string $projec
}
}

private function getMeasuresFromZipFile(ZipArchive $zipFile, string $cloverPath, string $crap4JPath) : array
{
$cloverFileString = $zipFile->getFromName($cloverPath);

$cloverFile = null;
if ($cloverFileString !== false) {
$cloverFile = CloverFile::fromString($cloverFileString, getcwd());
}

$crap4JString = $zipFile->getFromName($crap4JPath);

$crap4JFile = null;
if ($crap4JString !== false) {
$crap4JFile = Crap4JFile::fromString($crap4JString);
}

$methodsProvider = null;
$codeCoverageProvider = null;

if ($cloverFile !== null && $crap4JFile !== null) {
$methodsProvider = new CrapMethodMerger($cloverFile, $crap4JFile);
$codeCoverageProvider = $cloverFile;
} elseif ($cloverFile !== null) {
$methodsProvider = $cloverFile;
$codeCoverageProvider = $cloverFile;
} elseif ($crap4JFile !== null) {
$methodsProvider = $crap4JFile;
} else {
return [EmptyCloverFile::create(), EmptyCloverFile::create()];
}

return [$codeCoverageProvider, $methodsProvider];
}

private function addFilesToMessage(Message $message, array $files, OutputInterface $output, Config $config) {
foreach ($files as $file) {
if (!file_exists($file)) {
Expand Down
15 changes: 15 additions & 0 deletions src/Git/GitRepository.php
@@ -0,0 +1,15 @@
<?php


namespace TheCodingMachine\WashingMachine\Git;


class GitRepository extends \Cz\Git\GitRepository
{
public function getMergeBase(string $commit1, string $commit2) : string
{
$results = $this->extractFromCommand('git merge-base' . escapeshellarg($commit1). ' '. escapeshellarg($commit2));

return $results[0];
}
}

0 comments on commit efc861a

Please sign in to comment.