Skip to content

Commit

Permalink
Utility script to generate changelog from Git history
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 8, 2019
1 parent 3013dd8 commit 98a14b0
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
106 changes: 106 additions & 0 deletions bin/generate-changelog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

use Httpful\Request;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

(function () {
require_once __DIR__ . '/../vendor/autoload.php';

$command = new class() extends Symfony\Component\Console\Command\Command {

protected function configure()
{
$this->setName('run');
$this->addArgument('fromCommit', InputArgument::REQUIRED);
$this->addArgument('toCommit', InputArgument::REQUIRED);
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$commitLines = $this->exec(['git', 'log', sprintf('%s..%s', $input->getArgument('fromCommit'), $input->getArgument('toCommit')), '--reverse', '--pretty=%H %s']);
$commits = array_map(function (string $line): array {
[$hash, $message] = explode(' ', $line, 2);

return [
'hash' => $hash,
'message' => $message
];
}, explode("\n", $commitLines));

$i = 0;

foreach ($commits as $commit) {
$searchPullRequestsResponse = Request::get(sprintf('https://api.github.com/search/issues?q=repo:phpstan/phpstan-src+%s', $commit['hash']))
->sendsAndExpectsType('application/json')
->basicAuth('ondrejmirtes', getenv('GITHUB_TOKEN'))
->send();
if ($searchPullRequestsResponse->code !== 200) {
$output->writeln(var_export($searchPullRequestsResponse->body, true));
throw new \InvalidArgumentException((string) $searchPullRequestsResponse->code);
}
$searchPullRequestsResponse = $searchPullRequestsResponse->body;

$searchIssuesResponse = Request::get(sprintf('https://api.github.com/search/issues?q=repo:phpstan/phpstan+%s', $commit['hash']))
->sendsAndExpectsType('application/json')
->basicAuth('ondrejmirtes', getenv('GITHUB_TOKEN'))
->send();
if ($searchIssuesResponse->code !== 200) {
$output->writeln(var_export($searchIssuesResponse->body, true));
throw new \InvalidArgumentException((string) $searchIssuesResponse->code);
}
$searchIssuesResponse = $searchIssuesResponse->body;
$items = array_merge($searchPullRequestsResponse->items, $searchIssuesResponse->items);
$parenthesis = 'https://github.com/phpstan/phpstan-src/commit/' . $commit['hash'];
$thanks = null;
$issuesToReference = [];
foreach ($items as $responseItem) {
if (isset($responseItem->pull_request)) {
$parenthesis = sprintf('[#%d](%s)', $responseItem->number, 'https://github.com/phpstan/phpstan-src/pull/' . $responseItem->number);
$thanks = $responseItem->user->login;
} else {
$issuesToReference[] = sprintf('#%d', $responseItem->number);
}
}

$output->writeln(sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, count($issuesToReference) > 0 ? ', ' . implode(', ', $issuesToReference) : '', $thanks !== null ? sprintf(', thanks @%s!', $thanks) : ''));

if ($i > 0 && $i % 10 === 0) {
sleep(30);
}

$i++;
}

return 0;
}

/**
* @param string[] $commandParts
* @return string
*/
private function exec(array $commandParts): string
{
$command = implode(' ', array_map(function (string $part): string {
return escapeshellarg($part);
}, $commandParts));

exec($command, $outputLines, $statusCode);
$output = implode("\n", $outputLines);
if ($statusCode !== 0) {
throw new \InvalidArgumentException(sprintf('Command %s failed: %s', $command, $output));
}

return $output;
}

};

$application = new \Symfony\Component\Console\Application();
$application->add($command);
$application->setDefaultCommand('run', true);
$application->run();

})();
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"dealerdirect/phpcodesniffer-composer-installer": "^0.5.0",
"jakub-onderka/php-parallel-lint": "^1.0",
"localheinz/composer-normalize": "^1.1.0",
"nategood/httpful": "^0.2.20",
"phing/phing": "^2.16.0",
"phpstan/phpstan-deprecation-rules": "^0.12",
"phpstan/phpstan-php-parser": "^0.12",
Expand Down

0 comments on commit 98a14b0

Please sign in to comment.