Skip to content

Commit

Permalink
add geneate changelog to rector.php
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 15, 2021
1 parent 0b1345f commit 4770463
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
36 changes: 21 additions & 15 deletions bin/generate-changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Httpful\Request;
use Symfony\Component\Console\Application;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symplify\PackageBuilder\Console\Command\CommandNaming;

require __DIR__ . '/../vendor/autoload.php';
Expand All @@ -18,7 +21,7 @@
* GITHUB_TOKEN=<github_token> php bin/generate-changelog.php <from-commit> <to-commit> >> <file_to_dump.md>
* GITHUB_TOKEN=ghp_... php bin/generate-changelog.php 07736c1 cb74bb6 >> CHANGELOG_dumped.md
*/
final class GenerateChangelogCommand extends Symfony\Component\Console\Command\Command
final class GenerateChangelogCommand extends Command
{
/**
* @var string
Expand All @@ -40,6 +43,11 @@ final class GenerateChangelogCommand extends Symfony\Component\Console\Command\C
*/
private const OPTION_TO_COMMIT = 'to-commit';

/**
* @var string
*/
private const HASH = 'hash';

protected function configure(): void
{
$this->setName(CommandNaming::classToName(self::class));
Expand All @@ -57,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$commits = array_map(function (string $line): array {
[$hash, $message] = explode(' ', $line, 2);
return [
'hash' => $hash,
self::HASH => $hash,
'message' => $message,
];
}, $commitLines);
Expand All @@ -67,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
foreach ($commits as $commit) {
$searchPullRequestsUri = sprintf(
'https://api.github.com/search/issues?q=repo:' . self::DEVELOPMENT_REPOSITORY_NAME . '+%s',
$commit['hash']
$commit[self::HASH]
);

$searchPullRequestsResponse = Request::get($searchPullRequestsUri)
Expand All @@ -84,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$searchIssuesUri = sprintf(
'https://api.github.com/search/issues?q=repo:' . self::DEPLOY_REPOSITORY_NAME . '+%s',
$commit['hash']
$commit[self::HASH]
);

$searchIssuesResponse = Request::get($searchIssuesUri)
Expand All @@ -99,27 +107,25 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$searchIssuesResponse = $searchIssuesResponse->body;
$items = array_merge($searchPullRequestsResponse->items, $searchIssuesResponse->items);
$parenthesis = 'https://github.com/' . self::DEVELOPMENT_REPOSITORY_NAME . '/commit/' . $commit['hash'];
$parenthesis = 'https://github.com/' . self::DEVELOPMENT_REPOSITORY_NAME . '/commit/' . $commit[self::HASH];
$thanks = null;
$issuesToReference = [];

foreach ($items as $responseItem) {
if (isset($responseItem->pull_request)) {
foreach ($items as $item) {
if (property_exists($item, 'pull_request') && $item->pull_request !== null) {
$parenthesis = sprintf(
'[#%d](%s)',
$responseItem->number,
'https://github.com/' . self::DEVELOPMENT_REPOSITORY_NAME . '/pull/' . $responseItem->number
$item->number,
'https://github.com/' . self::DEVELOPMENT_REPOSITORY_NAME . '/pull/' . $item->number
);
$thanks = $responseItem->user->login;
$thanks = $item->user->login;
} else {
$issuesToReference[] = sprintf('#%d', $responseItem->number);
$issuesToReference[] = sprintf('#%d', $item->number);
}
}

$output->writeln(
sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, count(
$issuesToReference
) > 0 ? ', ' . implode(
sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, $issuesToReference !== [] ? ', ' . implode(
', ',
$issuesToReference
) : '', $thanks !== null ? sprintf(', Thanks @%s!', $thanks) : '')
Expand All @@ -139,7 +145,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
*/
private function exec(array $commandParts): string
{
$process = new Symfony\Component\Process\Process($commandParts);
$process = new Process($commandParts);
$process->run();

return $process->getOutput();
Expand Down
1 change: 1 addition & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
$parameters = $containerConfigurator->parameters();

$parameters->set(Option::PATHS, [
__DIR__ . '/bin',
__DIR__ . '/src',
__DIR__ . '/rules',
__DIR__ . '/rules-tests',
Expand Down

0 comments on commit 4770463

Please sign in to comment.