Skip to content

Commit f2e1c15

Browse files
claudeondrejmirtes
authored andcommitted
Changelog generator: thank PR reviewers instead of phpstan-bot
When a PR was created by @phpstan-bot, the changelog would previously thank the bot. Instead, fetch the PR's reviews via the GitHub API and thank the people who actually reviewed the PR, excluding the maintainer and the bot itself. Supports thanking multiple reviewers. https://claude.ai/code/session_01AAuDLXqrwvhNzwseRW7Wko
1 parent 9ac6d7a commit f2e1c15

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

changelog-generator/run.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require_once __DIR__ . '/vendor/autoload.php';
77

88
use Github\Api\GraphQL;
9+
use Github\Api\PullRequest;
910
use Github\Api\Repo;
1011
use Github\Api\Search;
1112
use Github\AuthMethod;
@@ -18,7 +19,11 @@
1819
use Symfony\Component\Console\Input\InputInterface;
1920
use Symfony\Component\Console\Input\InputOption;
2021
use Symfony\Component\Console\Output\OutputInterface;
22+
use function array_key_exists;
2123
use function array_map;
24+
use function array_slice;
25+
use function array_unique;
26+
use function array_values;
2227
use function count;
2328
use function escapeshellarg;
2429
use function exec;
@@ -58,6 +63,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
5863
/** @var Repo $repoApi */
5964
$repoApi = $gitHubClient->api('repo');
6065

66+
/** @var PullRequest $pullRequestApi */
67+
$pullRequestApi = $gitHubClient->api('pull_request');
68+
6169
/** @var GraphQL $graphqlApi */
6270
$graphqlApi = $gitHubClient->api('graphql');
6371

@@ -156,21 +164,44 @@ protected function execute(InputInterface $input, OutputInterface $output)
156164
$items = $searchApi->issues(sprintf('repo:phpstan/phpstan %s is:issue', $commit['hash']), 'created')['items'];
157165
}
158166
$parenthesis = 'https://github.com/phpstan/phpstan-src/commit/' . $commit['hash'];
159-
$thanks = null;
167+
$thanksNames = [];
160168
$issuesToReference = [];
161169
foreach ($items as $responseItem) {
162170
if (isset($responseItem['pull_request'])) {
163171
$parenthesis = sprintf('[#%d](%s)', $responseItem['number'], 'https://github.com/phpstan/phpstan-src/pull/' . $responseItem['number']);
164172

165-
if ($responseItem['user']['login'] !== 'ondrejmirtes') {
166-
$thanks = $responseItem['user']['login'];
173+
if ($responseItem['user']['login'] === 'phpstan-bot') {
174+
$reviews = $pullRequestApi->reviews()->all('phpstan', 'phpstan-src', $responseItem['number']);
175+
foreach ($reviews as $review) {
176+
if (!array_key_exists('user', $review) || !array_key_exists('login', $review['user'])) {
177+
continue;
178+
}
179+
$reviewerLogin = $review['user']['login'];
180+
if ($reviewerLogin === 'ondrejmirtes' || $reviewerLogin === 'phpstan-bot') {
181+
continue;
182+
}
183+
$thanksNames[] = $reviewerLogin;
184+
}
185+
$thanksNames = array_values(array_unique($thanksNames));
186+
} elseif ($responseItem['user']['login'] !== 'ondrejmirtes') {
187+
$thanksNames = [$responseItem['user']['login']];
167188
}
168189
} else {
169190
$issuesToReference[] = sprintf('#%d', $responseItem['number']);
170191
}
171192
}
172193

173-
$output->writeln(sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, count($issuesToReference) > 0 ? ', ' . implode(', ', $issuesToReference) : '', $thanks !== null ? sprintf(', thanks @%s!', $thanks) : ''));
194+
if (count($thanksNames) === 1) {
195+
$thanksText = sprintf(', thanks @%s!', $thanksNames[0]);
196+
} elseif (count($thanksNames) > 1) {
197+
$last = $thanksNames[count($thanksNames) - 1];
198+
$rest = implode(', ', array_map(static fn (string $name): string => '@' . $name, array_slice($thanksNames, 0, -1)));
199+
$thanksText = sprintf(', thanks %s and @%s!', $rest, $last);
200+
} else {
201+
$thanksText = '';
202+
}
203+
204+
$output->writeln(sprintf('* %s (%s)%s%s', $commit['message'], $parenthesis, count($issuesToReference) > 0 ? ', ' . implode(', ', $issuesToReference) : '', $thanksText));
174205
}
175206

176207
return 0;

0 commit comments

Comments
 (0)