Repository navigation
Expand file tree
/
Copy pathPullRequestResource.php
More file actions
527 lines (466 loc) · 16 KB
/
Copy pathPullRequestResource.php
File metadata and controls
527 lines (466 loc) · 16 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
namespace JordanPartridge\GithubClient\Resources;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestCommentDTO;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestDetailDTO;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestDTO;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestFileDTO;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestReviewDTO;
use JordanPartridge\GithubClient\Data\Pulls\PullRequestSummaryDTO;
use JordanPartridge\GithubClient\Enums\MergeMethod;
use JordanPartridge\GithubClient\Requests\Pulls\Comments;
use JordanPartridge\GithubClient\Requests\Pulls\CommentsWithFilters;
use JordanPartridge\GithubClient\Requests\Pulls\Create;
use JordanPartridge\GithubClient\Requests\Pulls\CreateComment;
use JordanPartridge\GithubClient\Requests\Pulls\CreateReview;
use JordanPartridge\GithubClient\Requests\Pulls\DeleteComment;
use JordanPartridge\GithubClient\Requests\Pulls\Files;
use JordanPartridge\GithubClient\Requests\Pulls\Get;
use JordanPartridge\GithubClient\Requests\Pulls\GetComment;
use JordanPartridge\GithubClient\Requests\Pulls\GetWithDetailDTO;
use JordanPartridge\GithubClient\Requests\Pulls\Index;
use JordanPartridge\GithubClient\Requests\Pulls\IndexWithSummaryDTO;
use JordanPartridge\GithubClient\Requests\Pulls\Merge;
use JordanPartridge\GithubClient\Requests\Pulls\Reviews;
use JordanPartridge\GithubClient\Requests\Pulls\Update;
use JordanPartridge\GithubClient\Requests\Pulls\UpdateComment;
use Illuminate\Support\Collection;
readonly class PullRequestResource extends BaseResource
{
public function all(string $owner, string $repo, array $parameters = []): array
{
$response = $this->github()->connector()->send(new Index("{$owner}/{$repo}", $parameters));
return $response->dto();
}
public function paginate(string $owner, string $repo, array $parameters = []): array
{
$page = 1;
$allPrs = [];
$maxPages = 1000; // Prevent infinite loops
do {
if ($page > $maxPages) {
throw new \RuntimeException("Maximum page limit ($maxPages) exceeded during pagination");
}
$response = $this->github()->connector()->send(new Index("{$owner}/{$repo}", array_merge($parameters, [
'page' => $page,
])));
$prs = $response->dto();
if (! empty($prs)) {
$allPrs = array_merge($allPrs, $prs);
}
// Check if there are more pages by examining the Link header
$linkHeader = $response->header('Link');
$hasNextPage = $linkHeader && str_contains($linkHeader, 'rel="next"');
$page++;
} while ($hasNextPage && ! empty($prs));
return $allPrs;
}
public function get(string $owner, string $repo, int $number): PullRequestDTO
{
$response = $this->github()->connector()->send(new Get("{$owner}/{$repo}", $number));
return $response->dto();
}
public function create(
string $owner,
string $repo,
string $title,
string $head,
string $base,
string $body = '',
bool $draft = false,
): PullRequestDTO {
$response = $this->github()->connector()->send(new Create(
"{$owner}/{$repo}",
$title,
$head,
$base,
$body,
$draft,
));
return $response->dto();
}
public function update(
string $owner,
string $repo,
int $number,
array $parameters = [],
): PullRequestDTO {
$response = $this->github()->connector()->send(new Update("{$owner}/{$repo}", $number, $parameters));
return $response->dto();
}
public function merge(
string $owner,
string $repo,
int $number,
?string $commitMessage = null,
?string $sha = null,
MergeMethod $mergeMethod = MergeMethod::Merge,
): bool {
$response = $this->github()->connector()->send(new Merge(
"{$owner}/{$repo}",
$number,
$commitMessage,
$sha,
$mergeMethod,
));
$result = $response->dto();
return $result->merged;
}
public function reviews(
string $owner,
string $repo,
int $number,
): Collection {
$response = $this->github()->connector()->send(new Reviews("{$owner}/{$repo}", $number));
return $response->dto();
}
public function createReview(
string $owner,
string $repo,
int $number,
string $body,
string $event = 'COMMENT',
array $comments = [],
): PullRequestReviewDTO {
$response = $this->github()->connector()->send(new CreateReview(
"{$owner}/{$repo}",
$number,
$body,
$event,
$comments,
));
return $response->dto();
}
public function comments(
string $owner,
string $repo,
int $number,
): array {
$response = $this->github()->connector()->send(new Comments("{$owner}/{$repo}", $number));
return $response->dto();
}
public function createComment(
string $owner,
string $repo,
int $number,
string $body,
string $commitId,
string $path,
int $position,
): PullRequestCommentDTO {
$response = $this->github()->connector()->send(new CreateComment(
"{$owner}/{$repo}",
$number,
$body,
$commitId,
$path,
$position,
));
return $response->dto();
}
/**
* Get PR comments with filtering capabilities.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $number Pull request number
* @param array $filters Filtering options
*
* @return array<PullRequestCommentDTO>
*
* @example
* // Get all CodeRabbit comments
* $comments = $github->pullRequests()->commentsWithFilters('owner', 'repo', 42, [
* 'author' => 'coderabbitai'
* ]);
*
* // Get all bot comments with high severity
* $comments = $github->pullRequests()->commentsWithFilters('owner', 'repo', 42, [
* 'author_type' => 'bot',
* 'severity' => 'high'
* ]);
*
* // Get comments from specific file
* $comments = $github->pullRequests()->commentsWithFilters('owner', 'repo', 42, [
* 'file_path' => 'app/Http/Controllers/UserController.php'
* ]);
*/
public function commentsWithFilters(
string $owner,
string $repo,
int $number,
array $filters = [],
): array {
$response = $this->github()->connector()->send(new CommentsWithFilters(
"{$owner}/{$repo}",
$number,
$filters,
));
return $response->dto();
}
/**
* Batch fetch all comments from a pull request with author filtering.
* Alias for commentsWithFilters() for backward compatibility.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $number Pull request number
* @param array $filters Filtering options
*
* @return array<PullRequestCommentDTO>
*/
public function forPullRequest(
string $owner,
string $repo,
int $number,
array $filters = [],
): array {
return $this->commentsWithFilters($owner, $repo, $number, $filters);
}
/**
* Get a single pull request review comment by ID
*/
public function getComment(
string $owner,
string $repo,
int $commentId,
): PullRequestCommentDTO {
$response = $this->github()->connector()->send(new GetComment(
owner: $owner,
repo: $repo,
commentId: $commentId,
));
return $response->dto();
}
/**
* Update a pull request review comment
*/
public function updateComment(
string $owner,
string $repo,
int $commentId,
string $body,
): PullRequestCommentDTO {
$response = $this->github()->connector()->send(new UpdateComment(
owner: $owner,
repo: $repo,
commentId: $commentId,
bodyText: $body,
));
return $response->dto();
}
/**
* Delete a pull request review comment
*/
public function deleteComment(
string $owner,
string $repo,
int $commentId,
): bool {
$response = $this->github()->connector()->send(new DeleteComment(
owner: $owner,
repo: $repo,
commentId: $commentId,
));
return $response->successful();
}
// === NEW DTO-SPECIFIC METHODS ===
/**
* Get PR summaries (lightweight, no comment counts) - RECOMMENDED for lists.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param array $parameters Query parameters
*
* @return array<PullRequestSummaryDTO> Lightweight PR summaries
*/
public function summaries(string $owner, string $repo, array $parameters = []): array
{
$response = $this->github()->connector()->send(new IndexWithSummaryDTO("{$owner}/{$repo}", $parameters));
return $response->dto();
}
/**
* Get PR with complete details including comment counts.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $number Pull request number
*
* @return PullRequestDetailDTO Complete PR data with statistics
*/
public function detail(string $owner, string $repo, int $number): PullRequestDetailDTO
{
$response = $this->github()->connector()->send(new GetWithDetailDTO("{$owner}/{$repo}", $number));
return $response->dto();
}
/**
* Get multiple PRs with complete details (WARNING: Rate limit intensive).
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param array $prNumbers Array of PR numbers
* @param int $maxRequests Safety limit to prevent rate limit exhaustion
*
* @return array<PullRequestDetailDTO> Complete PR data with statistics
*/
public function detailsForMultiple(
string $owner,
string $repo,
array $prNumbers,
int $maxRequests = 20,
): array {
// Safety limit to prevent rate limit issues
$prNumbers = array_slice($prNumbers, 0, $maxRequests);
$details = [];
foreach ($prNumbers as $number) {
try {
$details[] = $this->detail($owner, $repo, $number);
} catch (\Exception $e) {
// Skip PRs that can't be fetched
continue;
}
}
return $details;
}
/**
* Get recent PRs with complete details (optimized for common workflow).
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $limit Number of recent PRs (max 10 for rate limit protection)
* @param string $state PR state filter
*
* @return array<PullRequestDetailDTO> Recent PRs with complete data
*/
public function recentDetails(
string $owner,
string $repo,
int $limit = 5,
string $state = 'open',
): array {
// First get PR list
$summaries = $this->summaries($owner, $repo, [
'state' => $state,
'sort' => 'updated',
'direction' => 'desc',
'per_page' => min($limit, 10), // Protect against rate limit abuse
]);
// Get detailed data for each
$prNumbers = array_map(fn ($pr) => $pr->number, $summaries);
return $this->detailsForMultiple($owner, $repo, $prNumbers, $limit);
}
// === PR DIFF ANALYSIS METHODS ===
/**
* Get all files changed in a pull request with diff statistics.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $number Pull request number
*
* @return array<PullRequestFileDTO> Array of file changes with diff data
*
* @example
* // Get all changed files
* $files = $github->pullRequests()->files('owner', 'repo', 42);
*
* // Analyze file changes
* foreach ($files as $file) {
* echo "{$file->filename}: +{$file->additions}/-{$file->deletions}\n";
* echo "File type: {$file->getFileType()}\n";
* echo "Tags: " . implode(', ', $file->getAnalysisTags()) . "\n";
* }
*/
public function files(string $owner, string $repo, int $number): array
{
$response = $this->github()->connector()->send(new Files("{$owner}/{$repo}", $number));
return $response->dto();
}
/**
* Get diff analysis for a pull request with categorized file changes.
*
* @param string $owner Repository owner
* @param string $repo Repository name
* @param int $number Pull request number
*
* @return array Analysis data with categorized files and statistics
*
* @example
* $analysis = $github->pullRequests()->diff('owner', 'repo', 42);
*
* echo "Total files: {$analysis['summary']['total_files']}\n";
* echo "Large changes: {$analysis['summary']['large_changes']}\n";
* echo "Test files: " . count($analysis['categories']['tests']) . "\n";
* echo "Config files: " . count($analysis['categories']['config']) . "\n";
*/
public function diff(string $owner, string $repo, int $number): array
{
$files = $this->files($owner, $repo, $number);
$categories = [
'tests' => [],
'config' => [],
'docs' => [],
'code' => [],
'other' => [],
];
$summary = [
'total_files' => count($files),
'total_additions' => 0,
'total_deletions' => 0,
'total_changes' => 0,
'large_changes' => 0,
'new_files' => 0,
'deleted_files' => 0,
'modified_files' => 0,
'renamed_files' => 0,
];
foreach ($files as $file) {
// Update summary statistics
$summary['total_additions'] += $file->additions;
$summary['total_deletions'] += $file->deletions;
$summary['total_changes'] += $file->changes;
if ($file->isLargeChange()) {
$summary['large_changes']++;
}
if ($file->isAdded()) {
$summary['new_files']++;
}
if ($file->isDeleted()) {
$summary['deleted_files']++;
}
if ($file->isModified()) {
$summary['modified_files']++;
}
if ($file->isRenamed()) {
$summary['renamed_files']++;
}
// Categorize files
if ($file->isTestFile()) {
$categories['tests'][] = $file;
} elseif ($file->isConfigFile()) {
$categories['config'][] = $file;
} elseif ($file->isDocumentationFile()) {
$categories['docs'][] = $file;
} elseif (in_array($file->getFileType(), ['php', 'javascript', 'typescript', 'python', 'java', 'go', 'rust', 'c', 'cpp'])) {
$categories['code'][] = $file;
} else {
$categories['other'][] = $file;
}
}
return [
'summary' => $summary,
'categories' => $categories,
'files' => $files,
'analysis_tags' => $this->extractAnalysisTags($files),
];
}
/**
* Extract unique analysis tags from all files for AI assessment.
*/
private function extractAnalysisTags(array $files): array
{
$allTags = [];
foreach ($files as $file) {
$allTags = array_merge($allTags, $file->getAnalysisTags());
}
return array_unique($allTags);
}
}