forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-changelog.php
executable file
·106 lines (87 loc) · 3.62 KB
/
generate-changelog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 % 8 === 0) {
sleep(60);
}
$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();
})();