Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUPESC-889 Adjusted PR creation so that error traces are shortened. #326

Merged
merged 9 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
],
];
}
}
Loading