-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathrun.php
More file actions
executable file
·233 lines (195 loc) · 7.21 KB
/
Copy pathrun.php
File metadata and controls
executable file
·233 lines (195 loc) · 7.21 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env php
<?php declare(strict_types = 1);
namespace PHPStan\ChangelogGenerator;
require_once __DIR__ . '/vendor/autoload.php';
use Github\Api\GraphQL;
use Github\Api\PullRequest;
use Github\Api\Repo;
use Github\Api\Search;
use Github\AuthMethod;
use Github\Client;
use Github\HttpClient\Builder;
use InvalidArgumentException;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function array_key_exists;
use function array_map;
use function array_slice;
use function array_unique;
use function array_values;
use function count;
use function escapeshellarg;
use function exec;
use function explode;
use function implode;
use function sprintf;
(function (): void {
require_once __DIR__ . '/../vendor/autoload.php';
$command = new class() extends Command {
protected function configure(): void
{
$this->setName('run');
$this->addArgument('fromCommit', InputArgument::REQUIRED);
$this->addArgument('toCommit', InputArgument::REQUIRED);
$this->addOption('exclude-branch', null, InputOption::VALUE_REQUIRED);
$this->addOption('include-headings', null, InputOption::VALUE_NONE);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$token = $_SERVER['GITHUB_PAT_TOKEN'];
$rateLimitPlugin = new RateLimitPlugin();
$httpBuilder = new Builder();
$httpBuilder->addPlugin($rateLimitPlugin);
$gitHubClient = new Client($httpBuilder);
$gitHubClient->authenticate($token, AuthMethod::ACCESS_TOKEN);
$rateLimitPlugin->setClient($gitHubClient);
/** @var Search $searchApi */
$searchApi = $gitHubClient->api('search');
/** @var Repo $repoApi */
$repoApi = $gitHubClient->api('repo');
/** @var PullRequest $pullRequestApi */
$pullRequestApi = $gitHubClient->api('pull_request');
/** @var GraphQL $graphqlApi */
$graphqlApi = $gitHubClient->api('graphql');
$command = ['git', 'log', sprintf('%s..%s', $input->getArgument('fromCommit'), $input->getArgument('toCommit'))];
$excludeBranch = $input->getOption('exclude-branch');
if ($excludeBranch !== null) {
$command[] = '--not';
$command[] = $excludeBranch;
$command[] = '--no-merges';
}
$command[] = '--reverse';
$command[] = '--pretty=%H %s';
$commitLines = $this->exec($command);
$commits = array_map(static function (string $line): array {
[$hash, $message] = explode(' ', $line, 2);
return [
'hash' => $hash,
'message' => $message,
];
}, explode("\n", $commitLines));
if ($input->getOption('include-headings') === true) {
$output->writeln(<<<'MARKDOWN'
Major new features 🚀
=====================
Bleeding edge 🔪
=====================
*
*If you want to see the shape of things to come and adopt bleeding edge features early, you can include this config file in your project's `phpstan.neon`:*
```
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
```
*Of course, there are no backwards compatibility guarantees when you include this file. The behaviour and reported errors can change in minor versions with this file included. [Learn more](https://phpstan.org/blog/what-is-bleeding-edge)*
Improvements 🔧
=====================
Bugfixes 🐛
=====================
Function signature fixes 🤖
=======================
Internals 🔍
=====================
MARKDOWN);
}
foreach ($commits as $commit) {
$pullRequests = $repoApi->commits()->pulls('phpstan', 'phpstan-src', $commit['hash']);
if (count($pullRequests) > 0) {
$items = [
[
'pull_request' => true,
'number' => $pullRequests[0]['number'],
'user' => $pullRequests[0]['user'],
],
];
$autoclosedIssues = $graphqlApi->execute(
<<<'QUERY'
query ($owner:String!, $repo:String!, $pr:Int!){
repository(owner:$owner, name:$repo){
pullRequest(number:$pr){
closingIssuesReferences(first:100){
nodes { number title url repository { nameWithOwner } }
}
}
}
},
QUERY,
[
'owner' => 'phpstan',
'repo' => 'phpstan-src',
'pr' => $pullRequests[0]['number'],
],
);
foreach ($autoclosedIssues['data']['repository']['pullRequest']['closingIssuesReferences']['nodes'] as $closedIssue) {
if ($closedIssue['repository']['nameWithOwner'] !== 'phpstan/phpstan') {
continue;
}
$items[] = [
'number' => $closedIssue['number'],
];
}
} else {
$items = $searchApi->issues(sprintf('repo:phpstan/phpstan %s is:issue', $commit['hash']), 'created')['items'];
}
$parenthesis = 'https://github.com/phpstan/phpstan-src/commit/' . $commit['hash'];
$thanksNames = [];
$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']);
if ($responseItem['user']['login'] === 'phpstan-bot') {
$reviews = $pullRequestApi->reviews()->all('phpstan', 'phpstan-src', $responseItem['number']);
foreach ($reviews as $review) {
if (!array_key_exists('user', $review) || !array_key_exists('login', $review['user'])) {
continue;
}
$reviewerLogin = $review['user']['login'];
if ($reviewerLogin === 'ondrejmirtes' || $reviewerLogin === 'phpstan-bot') {
continue;
}
$thanksNames[] = $reviewerLogin;
}
$thanksNames = array_values(array_unique($thanksNames));
} elseif ($responseItem['user']['login'] !== 'ondrejmirtes') {
$thanksNames = [$responseItem['user']['login']];
}
} else {
$issuesToReference[] = sprintf('#%d', $responseItem['number']);
}
}
if (count($thanksNames) === 1) {
$thanksText = sprintf(', thanks @%s!', $thanksNames[0]);
} elseif (count($thanksNames) > 1) {
$last = $thanksNames[count($thanksNames) - 1];
$rest = implode(', ', array_map(static fn (string $name): string => '@' . $name, array_slice($thanksNames, 0, -1)));
$thanksText = sprintf(', thanks %s and @%s!', $rest, $last);
} else {
$thanksText = '';
}
$output->writeln(sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, count($issuesToReference) > 0 ? ', ' . implode(', ', $issuesToReference) : '', $thanksText));
}
return 0;
}
/**
* @param string[] $commandParts
*/
private function exec(array $commandParts): string
{
$command = implode(' ', array_map(static fn (string $part): string => 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 Application();
$application->add($command);
$application->setDefaultCommand('run', true);
$application->setCatchExceptions(false);
$application->run();
})();