Skip to content

Commit 2f69398

Browse files
committed
Use GraphQL to find autoclosed issues when a PR is merged
1 parent e153fa4 commit 2f69398

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

changelog-generator/run.php

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
require_once __DIR__ . '/vendor/autoload.php';
77

8+
use Github\Api\GraphQL;
89
use Github\Api\Repo;
910
use Github\Api\Search;
1011
use Github\AuthMethod;
@@ -58,6 +59,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
5859
/** @var Repo $repoApi */
5960
$repoApi = $gitHubClient->api('repo');
6061

62+
/** @var GraphQL $graphqlApi */
63+
$graphqlApi = $gitHubClient->api('graphql');
64+
6165
$command = ['git', 'log', sprintf('%s..%s', $input->getArgument('fromCommit'), $input->getArgument('toCommit'))];
6266
$excludeBranch = $input->getOption('exclude-branch');
6367
if ($excludeBranch !== null) {
@@ -115,13 +119,42 @@ protected function execute(InputInterface $input, OutputInterface $output)
115119

116120
foreach ($commits as $commit) {
117121
$pullRequests = $repoApi->commits()->pulls('phpstan', 'phpstan-src', $commit['hash']);
118-
$items = $searchApi->issues(sprintf('repo:phpstan/phpstan %s is:issue', $commit['hash']), 'created')['items'];
119122
if (count($pullRequests) > 0) {
120-
$items[] = [
121-
'pull_request' => true,
122-
'number' => $pullRequests[0]['number'],
123-
'user' => $pullRequests[0]['user'],
123+
$items = [
124+
[
125+
'pull_request' => true,
126+
'number' => $pullRequests[0]['number'],
127+
'user' => $pullRequests[0]['user'],
128+
],
124129
];
130+
$autoclosedIssues = $graphqlApi->execute(
131+
<<<'QUERY'
132+
query ($owner:String!, $repo:String!, $pr:Int!){
133+
repository(owner:$owner, name:$repo){
134+
pullRequest(number:$pr){
135+
closingIssuesReferences(first:100){
136+
nodes { number title url repository { nameWithOwner } }
137+
}
138+
}
139+
}
140+
},
141+
QUERY,
142+
[
143+
'owner' => 'phpstan',
144+
'repo' => 'phpstan-src',
145+
'pr' => $pullRequests[0]['number'],
146+
],
147+
);
148+
foreach ($autoclosedIssues['data']['repository']['pullRequest']['closingIssuesReferences']['nodes'] as $closedIssue) {
149+
if ($closedIssue['repository']['nameWithOwner'] !== 'phpstan/phpstan') {
150+
continue;
151+
}
152+
$items[] = [
153+
'number' => $closedIssue['number'],
154+
];
155+
}
156+
} else {
157+
$items = $searchApi->issues(sprintf('repo:phpstan/phpstan %s is:issue', $commit['hash']), 'created')['items'];
125158
}
126159
$parenthesis = 'https://github.com/phpstan/phpstan-src/commit/' . $commit['hash'];
127160
$thanks = null;

0 commit comments

Comments
 (0)