-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add recoverable review diff service (#988)
* Add RecoverableReviewDiffService.php * Add RecoverableReviewDiffService.php * Add RecoverableReviewDiffService.php * Add coverage
- Loading branch information
1 parent
265a426
commit 446942a
Showing
3 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Service/Git/Review/ReviewDiffService/RecoverableReviewDiffService.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DR\Review\Service\Git\Review\ReviewDiffService; | ||
|
||
use DR\Review\Entity\Repository\Repository; | ||
use DR\Review\Entity\Review\CodeReview; | ||
use DR\Review\Service\Git\Review\FileDiffOptions; | ||
use Psr\Log\LoggerAwareInterface; | ||
use Psr\Log\LoggerAwareTrait; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
class RecoverableReviewDiffService implements ReviewDiffServiceInterface, LoggerAwareInterface | ||
{ | ||
use LoggerAwareTrait; | ||
|
||
public function __construct(private readonly ReviewDiffServiceInterface $diffService) | ||
{ | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function getDiffForRevisions(Repository $repository, array $revisions, ?FileDiffOptions $options = null): array | ||
{ | ||
return $this->diffService->getDiffForRevisions($repository, $revisions, $options); | ||
} | ||
|
||
/** | ||
* The target branch of the review might be stale, try to reset it to the main branch if the diff fails. | ||
* @inheritDoc | ||
*/ | ||
public function getDiffForBranch(CodeReview $review, array $revisions, string $branchName, ?FileDiffOptions $options = null): array | ||
{ | ||
try { | ||
return $this->diffService->getDiffForBranch($review, $revisions, $branchName, $options); | ||
} catch (ProcessFailedException $exception) { | ||
if ($review->getTargetBranch() === $review->getRepository()->getMainBranchName()) { | ||
throw $exception; | ||
} | ||
|
||
$this->logger?->notice('Failed to get diff for branch, trying to reset target branch', ['exception' => $exception]); | ||
$review->setTargetBranch($review->getRepository()->getMainBranchName()); | ||
} | ||
|
||
return $this->diffService->getDiffForBranch($review, $revisions, $branchName, $options); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
tests/Unit/Service/Git/Review/ReviewDiffService/RecoverableReviewDiffServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace DR\Review\Tests\Unit\Service\Git\Review\ReviewDiffService; | ||
|
||
use DR\Review\Entity\Git\Diff\DiffComparePolicy; | ||
use DR\Review\Entity\Repository\Repository; | ||
use DR\Review\Entity\Review\CodeReview; | ||
use DR\Review\Entity\Revision\Revision; | ||
use DR\Review\Service\Git\Review\FileDiffOptions; | ||
use DR\Review\Service\Git\Review\ReviewDiffService\RecoverableReviewDiffService; | ||
use DR\Review\Service\Git\Review\ReviewDiffService\ReviewDiffServiceInterface; | ||
use DR\Review\Tests\AbstractTestCase; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use stdClass; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Throwable; | ||
|
||
#[CoversClass(RecoverableReviewDiffService::class)] | ||
class RecoverableReviewDiffServiceTest extends AbstractTestCase | ||
{ | ||
private ReviewDiffServiceInterface&MockObject $diffService; | ||
private RecoverableReviewDiffService $service; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->diffService = $this->createMock(ReviewDiffServiceInterface::class); | ||
$this->service = new RecoverableReviewDiffService($this->diffService); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function testGetDiffForRevisions(): void | ||
{ | ||
$repository = new Repository(); | ||
$revisions = [new Revision()]; | ||
$options = new FileDiffOptions(5, DiffComparePolicy::ALL); | ||
|
||
$this->diffService->expects(self::once())->method('getDiffForRevisions')->with($repository, $revisions, $options); | ||
|
||
$this->service->getDiffForRevisions($repository, $revisions, $options); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function testGetDiffForBranchExpectTwoAttempts(): void | ||
{ | ||
$callCount = new stdClass(); | ||
$callCount->count = 0; | ||
$repository = (new Repository())->setMainBranchName('master'); | ||
$review = (new CodeReview())->setTargetBranch('foobar')->setRepository($repository); | ||
$revisions = [new Revision()]; | ||
$options = new FileDiffOptions(5, DiffComparePolicy::ALL); | ||
$branchName = 'branch'; | ||
|
||
$exception = $this->createMock(ProcessFailedException::class); | ||
|
||
$this->diffService->expects(self::exactly(2))->method('getDiffForBranch') | ||
->with($review, $revisions, $branchName, $options) | ||
->willThrowException($exception); | ||
|
||
$this->expectException(ProcessFailedException::class); | ||
$this->service->getDiffForBranch($review, $revisions, $branchName, $options); | ||
} | ||
|
||
/** | ||
* @throws Throwable | ||
*/ | ||
public function testGetDiffForBranchExpectOneAttemptOnMasterBranch(): void | ||
{ | ||
$callCount = new stdClass(); | ||
$callCount->count = 0; | ||
$repository = (new Repository())->setMainBranchName('master'); | ||
$review = (new CodeReview())->setTargetBranch('master')->setRepository($repository); | ||
$revisions = [new Revision()]; | ||
$options = new FileDiffOptions(5, DiffComparePolicy::ALL); | ||
$branchName = 'branch'; | ||
|
||
$exception = $this->createMock(ProcessFailedException::class); | ||
|
||
$this->diffService->expects(self::once())->method('getDiffForBranch') | ||
->with($review, $revisions, $branchName, $options) | ||
->willThrowException($exception); | ||
|
||
$this->expectException(ProcessFailedException::class); | ||
$this->service->getDiffForBranch($review, $revisions, $branchName, $options); | ||
} | ||
} |