|
3 | 3 | namespace App\Api\Issue;
|
4 | 4 |
|
5 | 5 | use App\Model\Repository;
|
| 6 | +use Github\Api\Issue; |
6 | 7 | use Github\Api\Issue\Comments;
|
| 8 | +use Github\Api\Search; |
7 | 9 |
|
8 | 10 | class GithubIssueApi implements IssueApi
|
9 | 11 | {
|
10 | 12 | private $issueCommentApi;
|
| 13 | + private $botUsername; |
| 14 | + private $issueApi; |
| 15 | + private $searchApi; |
11 | 16 |
|
12 |
| - public function __construct(Comments $issueCommentApi) |
| 17 | + public function __construct(Comments $issueCommentApi, Issue $issueApi, Search $searchApi, string $botUsername) |
13 | 18 | {
|
14 | 19 | $this->issueCommentApi = $issueCommentApi;
|
| 20 | + $this->issueApi = $issueApi; |
| 21 | + $this->searchApi = $searchApi; |
| 22 | + $this->botUsername = $botUsername; |
| 23 | + } |
| 24 | + |
| 25 | + public function open(Repository $repository, string $title, string $body, array $labels) |
| 26 | + { |
| 27 | + $params = [ |
| 28 | + 'title' => $title, |
| 29 | + 'labels' => $labels, |
| 30 | + 'body' => $body, |
| 31 | + ]; |
| 32 | + |
| 33 | + $issueNumber = null; |
| 34 | + $exitingIssues = $this->searchApi->issues(sprintf('repo:%s "%s" is:open author:%s', $repository->getFullName(), $title, $this->botUsername)); |
| 35 | + foreach ($exitingIssues['items'] ?? [] as $issue) { |
| 36 | + $issueNumber = $issue['number']; |
| 37 | + } |
| 38 | + |
| 39 | + if (null === $issueNumber) { |
| 40 | + $this->issueApi->create($repository->getVendor(), $repository->getName(), $params); |
| 41 | + } else { |
| 42 | + unset($params['labels']); |
| 43 | + $this->issueApi->update($repository->getVendor(), $repository->getName(), $issueNumber, $params); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public function close(Repository $repository, $issueNumber) |
| 48 | + { |
| 49 | + $this->issueApi->update($repository->getVendor(), $repository->getName(), $issueNumber, ['state' => 'closed']); |
15 | 50 | }
|
16 | 51 |
|
17 | 52 | /**
|
|
0 commit comments