|
5 | 5 | use App\Model\Repository;
|
6 | 6 | use Github\Api\Issue;
|
7 | 7 | use Github\Api\Issue\Comments;
|
| 8 | +use Github\Api\Issue\Timeline; |
| 9 | +use Github\Api\PullRequest\Review; |
8 | 10 | use Github\Api\Search;
|
| 11 | +use Github\Exception\RuntimeException; |
9 | 12 |
|
10 | 13 | class GithubIssueApi implements IssueApi
|
11 | 14 | {
|
12 | 15 | private $issueCommentApi;
|
13 |
| - private $botUsername; |
| 16 | + private $reviewApi; |
14 | 17 | private $issueApi;
|
15 | 18 | private $searchApi;
|
| 19 | + private $timelineApi; |
| 20 | + private $botUsername; |
16 | 21 |
|
17 |
| - public function __construct(Comments $issueCommentApi, Issue $issueApi, Search $searchApi, string $botUsername) |
| 22 | + public function __construct(Comments $issueCommentApi, Review $reviewApi, Issue $issueApi, Search $searchApi, Timeline $timelineApi, string $botUsername) |
18 | 23 | {
|
19 | 24 | $this->issueCommentApi = $issueCommentApi;
|
| 25 | + $this->reviewApi = $reviewApi; |
20 | 26 | $this->issueApi = $issueApi;
|
21 | 27 | $this->searchApi = $searchApi;
|
| 28 | + $this->timelineApi = $timelineApi; |
22 | 29 | $this->botUsername = $botUsername;
|
23 | 30 | }
|
24 | 31 |
|
@@ -52,6 +59,31 @@ public function lastCommentWasMadeByBot(Repository $repository, $number): bool
|
52 | 59 | return $this->botUsername === ($lastComment['user']['login'] ?? null);
|
53 | 60 | }
|
54 | 61 |
|
| 62 | + /** |
| 63 | + * Has this PR or issue comments/reviews from others than the author? |
| 64 | + */ |
| 65 | + public function hasActivity(Repository $repository, $number): bool |
| 66 | + { |
| 67 | + $issue = $this->issueApi->show($repository->getVendor(), $repository->getName(), $number); |
| 68 | + $author = $issue['user']['login'] ?? null; |
| 69 | + |
| 70 | + try { |
| 71 | + $reviewComments = $this->reviewApi->all($repository->getVendor(), $repository->getName(), $number); |
| 72 | + } catch (RuntimeException $e) { |
| 73 | + // This was not a PR =) |
| 74 | + $reviewComments = []; |
| 75 | + } |
| 76 | + |
| 77 | + $all = array_merge($reviewComments, $this->issueCommentApi->all($repository->getVendor(), $repository->getName(), $number)); |
| 78 | + foreach ($all as $comment) { |
| 79 | + if (!in_array($comment['user']['login'], [$author, $this->botUsername])) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
55 | 87 | public function show(Repository $repository, $issueNumber): array
|
56 | 88 | {
|
57 | 89 | return $this->issueApi->show($repository->getVendor(), $repository->getName(), $issueNumber);
|
@@ -81,4 +113,23 @@ public function findStaleIssues(Repository $repository, \DateTimeImmutable $noUp
|
81 | 113 |
|
82 | 114 | return $issues['items'] ?? [];
|
83 | 115 | }
|
| 116 | + |
| 117 | + public function getUsers(Repository $repository, $issueNumber): array |
| 118 | + { |
| 119 | + $timeline = $this->timelineApi->all($repository->getVendor(), $repository->getName(), $issueNumber); |
| 120 | + $users = []; |
| 121 | + foreach ($timeline as $event) { |
| 122 | + $users[] = $event['actor']['login'] ?? $event['user']['login'] ?? $event['author']['email'] ?? ''; |
| 123 | + if (isset($event['body'])) { |
| 124 | + // Parse body for user reference |
| 125 | + if (preg_match_all('|@([a-zA-z_\-0-9]+)|', $event['body'], $matches)) { |
| 126 | + foreach ($matches[1] as $match) { |
| 127 | + $users[] = $match; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return array_map(function ($a) { return strtolower($a); }, array_unique($users)); |
| 134 | + } |
84 | 135 | }
|
0 commit comments