Skip to content

Commit

Permalink
SUPESC-889 Adjusted PR creation so that error traces are shortened. (#…
Browse files Browse the repository at this point in the history
…326)

* SUPESC-889 Add trace truncation config and method

* SUPESC-889 Abort if first trace line is not found

* FRW-1877 Add test

* FRW-1877 Update test

* SUPESC-889 Move strings into constants

* SUPESC-889 Remove parenthesis

* SUPESC-889 Add line breaks

* FRW-889 Update src/Upgrade/Infrastructure/VersionControlSystem/SourceCodeProvider/GitHub/GitHubSourceCodeProvider.php

Co-authored-by: Olha Livitchuk <77281282+olhalivitchuk@users.noreply.github.com>

* SUPESC-889 Update test

---------

Co-authored-by: aleksandr-velikanov <aleksandr.velikanov>
Co-authored-by: Olha Livitchuk <77281282+olhalivitchuk@users.noreply.github.com>
  • Loading branch information
aleksandr-velikanov and olhalivitchuk authored Sep 20, 2024
1 parent 28c3534 commit 9b5540e
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,12 @@ public function getPullRequestReviewers(): array;
* @return bool
*/
public function isPhpStanOptimizationRun(): bool;

/**
* Specification:
* - Defines whether error traces in error messages should be truncated before adding them to a PR.
*
* @return bool
*/
public function isTruncateErrorTracesInPrsEnabled(): bool;
}
10 changes: 10 additions & 0 deletions src/Upgrade/Infrastructure/Configuration/ConfigurationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,14 @@ public function isPhpStanOptimizationRun(): bool
{
return EnvFetcher::getBool('PHPSTAN_OPTIMIZATION_RUN', false);
}

/**
* {@inheritDoc}
*
* @return bool
*/
public function isTruncateErrorTracesInPrsEnabled(): bool
{
return EnvFetcher::getBool('TRUNCATE_ERROR_TRACES_IN_PRS', true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class GitHubSourceCodeProvider implements SourceCodeProviderInterface
*/
protected const NUMBER_KEY = 'number';

/**
* @var string
*/
protected const STRING_STACK_TRACE = '[stacktrace]';

/**
* @var string
*/
protected const STRING_NUMBER_ONE = '#1';

/**
* @var string
*/
protected const STRING_TRACE_TRUNCATED = '[...trace truncated...]';

/**
* @var \Upgrade\Infrastructure\Configuration\ConfigurationProvider
*/
Expand Down Expand Up @@ -149,7 +164,35 @@ public function buildBlockerTextBlock(ValidatorViolationDto $blocker): string
'> [!IMPORTANT] %s> <b>%s.</b> %s',
PHP_EOL,
$blocker->getTitle(),
$blocker->getMessage(),
$this->buildMessageWithTruncatedTrace($blocker->getMessage()),
) . PHP_EOL;
}

/**
* @param string $message
*
* @return string
*/
protected function buildMessageWithTruncatedTrace(string $message): string
{
$messageArray = explode(self::STRING_STACK_TRACE, $message);

if (
!$this->configurationProvider->isTruncateErrorTracesInPrsEnabled()
|| !isset($messageArray[0])
|| !isset($messageArray[1])
) {
return $message;
}

$traceArray = explode(self::STRING_NUMBER_ONE, $messageArray[1]);

if (!isset($traceArray[0])) {
return $message;
}

return $messageArray[0]
. $traceArray[0]
. self::STRING_TRACE_TRUNCATED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,34 @@ public function testBuildBlockerTextBlock(string $title, string $message, string
$this->assertSame($expectedOutput, $result);
}

/**
* @dataProvider testBuildBlockerTextBlockTruncatesErrorTraceDataProvider
*
* @param string $title
* @param string $message
* @param string $expectedOutput
*
* @return void
*/
public function testBuildBlockerTextBlockTruncatesErrorTrace(string $title, string $message, string $expectedOutput): void
{
$configurationProviderMock = $this->createMock(ConfigurationProvider::class);
$configurationProviderMock
->method('isTruncateErrorTracesInPrsEnabled')
->willReturn(true);
$gitHubClientFactoryMock = $this->createMock(GitHubClientFactory::class);

$gitHubSourceCodeProvider = new GitHubSourceCodeProvider(
$configurationProviderMock,
$gitHubClientFactoryMock,
new OutputMessageBuilder(),
);

$result = $gitHubSourceCodeProvider->buildBlockerTextBlock(new ValidatorViolationDto($title, $message));

$this->assertSame($expectedOutput, $result);
}

/**
* @return array<array>
*/
Expand All @@ -167,4 +195,23 @@ public function buildBlockerTextBlockDataProvider(): array
['Another Title', 'Another Message', "> [!IMPORTANT] \n> <b>Another Title.</b> Another Message\n"],
];
}

/**
* @return array<array>
*/
public function testBuildBlockerTextBlockTruncatesErrorTraceDataProvider(): array
{
return [
[
'Title 1',
'<b>Test error message.</b>[stacktrace] #0 /first/row/of/trace/#1 /second/row/of/trace/ #3 /third/row/of/trace/',
"> [!IMPORTANT] \n> <b>Title 1.</b> <b>Test error message.</b> #0 /first/row/of/trace/[...trace truncated...]\n",
],
[
'Title 2',
'<u>Another test error message.</u>[stacktrace] #0 /first/row/of/trace/#1 /second/row/of/trace/ #3 /third/row/of/trace/',
"> [!IMPORTANT] \n> <b>Title 2.</b> <u>Another test error message.</u> #0 /first/row/of/trace/[...trace truncated...]\n",
],
];
}
}

0 comments on commit 9b5540e

Please sign in to comment.