Skip to content

Commit

Permalink
Bump changelog generator (#3250)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 24, 2022
1 parent f8814b1 commit bebea61
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"nette/utils": "^3.2.8",
"nikic/php-parser": "^4.15.2",
"ondram/ci-detector": "^4.1",
"phpstan/phpdoc-parser": "^1.15.2",
"phpstan/phpdoc-parser": "^1.15.3",
"phpstan/phpstan": "^1.9.4",
"phpstan/phpstan-phpunit": "^1.3.2",
"react/event-loop": "^1.3",
Expand Down Expand Up @@ -89,6 +89,7 @@
"packages-tests",
"rules-tests"
],
"Rector\\Utils\\Tests\\": "utils-tests",
"Rector\\Core\\Tests\\": "tests",
"Rector\\RuleDocGenerator\\": "utils/RuleDocGenerator/src",
"E2e\\Parallel\\Reflection\\Resolver\\": [
Expand Down
1 change: 1 addition & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
__DIR__ . '/rules-tests',
__DIR__ . '/tests',
__DIR__ . '/utils',
__DIR__ . '/utils-tests',
__DIR__ . '/config',
__DIR__ . '/ecs.php',
__DIR__ . '/easy-ci.php',
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ parameters:
- tests
- rules-tests
- utils
- utils-tests
# this cannot be put it, because it wipes PHPStan cache on each run :( - must run in separate
#- config

Expand Down Expand Up @@ -873,3 +874,6 @@ parameters:
path: packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php

- '#Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\service not found#'
-
message: '#"Tests" namespace cannot be used outside of "tests" directory#'
path: utils-tests
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<directory>tests</directory>
<directory>rules-tests</directory>
<directory>packages-tests</directory>
<directory>utils-tests</directory>
</testsuite>
</testsuites>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Utils\Tests\ChangelogGenerator\Changelog;

use PHPUnit\Framework\TestCase;
use Rector\Utils\ChangelogGenerator\Changelog\ChangelogContentsFactory;

final class ChangelogContentsFactoryTest extends TestCase
{
private ChangelogContentsFactory $changelogContentsFactory;

protected function setUp(): void
{
$this->changelogContentsFactory = new ChangelogContentsFactory();
}

public function test(): void
{
$changelogLines = [
'* Add new rule',
'* Fix bug',
'* Fixed another bug',
'* Enable PHPStan on tests as well + add "unused public" ([#3238](https://github.com/rectorphp/rector-src/pull/3238))',
];

$generatedChangelogContents = $this->changelogContentsFactory->create($changelogLines);

$this->assertStringEqualsFile(__DIR__ . '/Fixture/generated_changelog.md', $generatedChangelogContents);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## New Features :partying_face:

* Add new rule

<br>

## Bugfixes :bug:

* Fix bug
* Fixed another bug
68 changes: 53 additions & 15 deletions utils/ChangelogGenerator/Changelog/ChangelogContentsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,53 @@
namespace Rector\Utils\ChangelogGenerator\Changelog;

use Rector\Utils\ChangelogGenerator\Enum\ChangelogCategory;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Utils\Tests\ChangelogGenerator\Changelog\ChangelogContentsFactoryTest
*/
final class ChangelogContentsFactory
{
/**
* @var array<string, string[]>
*/
private const FILTER_KEYWORDS_BY_CATEGORY = [
ChangelogCategory::NEW_FEATURES => ['Add support', 'Add'],
ChangelogCategory::SKIPPED => ['Fix wrong reference'],
ChangelogCategory::BUGFIXES => ['Fixed', 'Fix'],
ChangelogCategory::SKIPPED => [
'fix wrong reference',
'enable phpstan',
'bump to phpstan',
'bump composer',
'cleanup phpstan',
'compatibility with betterreflection',
'update to',
],
ChangelogCategory::NEW_FEATURES => ['add', 'added', 'improve'],
ChangelogCategory::BUGFIXES => ['fixed', 'fix'],
ChangelogCategory::REMOVED => ['removed', 'deleted', 'remove deprecated', 'remove'],
];

/**
* @param string[] $changelogLines
*/
public function create(array $changelogLines): string
{
Assert::allString($changelogLines);

// summarize into "Added Features" and "Bugfixes" groups
$linesByCategory = [];

// @todo test this one
foreach ($changelogLines as $changelogLine) {
foreach (self::FILTER_KEYWORDS_BY_CATEGORY as $category => $filterKeywords) {
foreach ($filterKeywords as $filterKeyword) {
if (! \str_contains($changelogLine, $filterKeyword)) {
continue;
}

$linesByCategory[$category][] = $changelogLine;
continue 2;
if (! $this->isKeywordsMatch($filterKeywords, $changelogLine)) {
continue;
}

$linesByCategory[$category][] = $changelogLine;
continue 2;
}

// fallback to fixed
$linesByCategory[ChangelogCategory::BUGFIXES][] = $changelogLine;
}

// remove skipped lines
Expand All @@ -52,16 +67,39 @@ private function generateFileContentsFromGroupedItems(array $linesByCategory): s
{
$fileContents = '';

$lastItemKey = array_key_last($linesByCategory);

foreach ($linesByCategory as $category => $lines) {
$fileContents .= PHP_EOL;
$fileContents .= '## ' . $category . PHP_EOL . PHP_EOL;
foreach ($lines as $line) {
$fileContents .= $line . PHP_EOL . PHP_EOL;
$fileContents .= $line . PHP_EOL;
}

// end space, only if this is not the last item
if ($lastItemKey === $category) {
continue;
}

// end space
$fileContents .= PHP_EOL . PHP_EOL;
$fileContents .= PHP_EOL . '<br>' . PHP_EOL;
}

return ltrim($fileContents);
}

/**
* @param string[] $filterKeywords
*/
private function isKeywordsMatch(array $filterKeywords, string $changelogLine): bool
{
$normalizedChangelogLine = strtolower($changelogLine);

foreach ($filterKeywords as $filterKeyword) {
if (\str_contains($normalizedChangelogLine, $filterKeyword)) {
return true;
}
}

return $fileContents;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// not to throttle the GitHub API
if ($i > 0 && $i % 8 === 0) {
sleep(30);
sleep(60);
}

++$i;
Expand Down
9 changes: 7 additions & 2 deletions utils/ChangelogGenerator/Enum/ChangelogCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class ChangelogCategory
/**
* @var string
*/
public const NEW_FEATURES = 'New Features';
public const NEW_FEATURES = 'New Features :partying_face:';

/**
* @var string
Expand All @@ -19,5 +19,10 @@ final class ChangelogCategory
/**
* @var string
*/
public const BUGFIXES = 'Bugfixes';
public const BUGFIXES = 'Bugfixes :bug:';

/**
* @var string
*/
public const REMOVED = 'Removed :skull:';
}
2 changes: 1 addition & 1 deletion utils/ChangelogGenerator/GithubApiCaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function sendRequest(string $requestUri): stdClass
->send();

if ($response->code !== 200) {
throw new GithubRequestException(get_debug_type($response->body), (int) $response->code);
throw new GithubRequestException($response->body->message, (int) $response->code);
}

return $response->body;
Expand Down

0 comments on commit bebea61

Please sign in to comment.