-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Utility script to generate changelog from Git history
- Loading branch information
1 parent
3013dd8
commit 98a14b0
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters