diff --git a/.env b/.env index 6f4f11c9..11adbffe 100644 --- a/.env +++ b/.env @@ -21,7 +21,7 @@ APP_SECRET=5dd8ffca252d95e8b4fb5b2d15310e92 SYMFONY_DOCS_SECRET='' SYMFONY_SECRET='' - +BOT_USERNAME='carsonbot-test' ###> knplabs/github-api ### #GITHUB_TOKEN=XXX ###< knplabs/github-api ### diff --git a/.env.test b/.env.test index bda08f6f..f887c229 100644 --- a/.env.test +++ b/.env.test @@ -5,3 +5,5 @@ SYMFONY_DEPRECATIONS_HELPER=999999 PANTHER_APP_ENV=panther SYMFONY_DOCS_SECRET='' SYMFONY_SECRET='' + +BOT_USERNAME='carsonbot-test' diff --git a/config/services.yaml b/config/services.yaml index 471f1655..f58badd1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -42,6 +42,9 @@ services: autowire: true autoconfigure: true + bind: + string $botUsername: '%env(BOT_USERNAME)%' + App\: resource: '../src/*' exclude: '../src/{DependencyInjection,Subscriber,Kernel.php,GitHubEvents.php}' @@ -62,6 +65,10 @@ services: factory: ['@Github\Client', api] arguments: [pullRequest] + Github\Api\Search: + factory: ['@Github\Client', api] + arguments: [search] + Github\Api\Issue\Labels: factory: ['@Github\Api\Issue', labels] diff --git a/src/Api/Issue/GithubIssueApi.php b/src/Api/Issue/GithubIssueApi.php index 6592c58d..19205fd8 100644 --- a/src/Api/Issue/GithubIssueApi.php +++ b/src/Api/Issue/GithubIssueApi.php @@ -3,15 +3,50 @@ namespace App\Api\Issue; use App\Model\Repository; +use Github\Api\Issue; use Github\Api\Issue\Comments; +use Github\Api\Search; class GithubIssueApi implements IssueApi { private $issueCommentApi; + private $botUsername; + private $issueApi; + private $searchApi; - public function __construct(Comments $issueCommentApi) + public function __construct(Comments $issueCommentApi, Issue $issueApi, Search $searchApi, string $botUsername) { $this->issueCommentApi = $issueCommentApi; + $this->issueApi = $issueApi; + $this->searchApi = $searchApi; + $this->botUsername = $botUsername; + } + + public function open(Repository $repository, string $title, string $body, array $labels) + { + $params = [ + 'title' => $title, + 'labels' => $labels, + 'body' => $body, + ]; + + $issueNumber = null; + $exitingIssues = $this->searchApi->issues(sprintf('repo:%s "%s" is:open author:%s', $repository->getFullName(), $title, $this->botUsername)); + foreach ($exitingIssues['items'] ?? [] as $issue) { + $issueNumber = $issue['number']; + } + + if (null === $issueNumber) { + $this->issueApi->create($repository->getVendor(), $repository->getName(), $params); + } else { + unset($params['labels']); + $this->issueApi->update($repository->getVendor(), $repository->getName(), $issueNumber, $params); + } + } + + public function close(Repository $repository, $issueNumber) + { + $this->issueApi->update($repository->getVendor(), $repository->getName(), $issueNumber, ['state' => 'closed']); } /** diff --git a/src/Api/Issue/IssueApi.php b/src/Api/Issue/IssueApi.php index 7a6f1641..6ddc30bb 100644 --- a/src/Api/Issue/IssueApi.php +++ b/src/Api/Issue/IssueApi.php @@ -12,5 +12,15 @@ */ interface IssueApi { + /** + * Open new issue or update existing issue. + */ + public function open(Repository $repository, string $title, string $body, array $labels); + public function commentOnIssue(Repository $repository, $issueNumber, string $commentBody); + + /** + * Close an issue or a pull request. + */ + public function close(Repository $repository, $issueNumber); } diff --git a/src/Api/Issue/NullIssueApi.php b/src/Api/Issue/NullIssueApi.php index be4675b1..c38b2513 100644 --- a/src/Api/Issue/NullIssueApi.php +++ b/src/Api/Issue/NullIssueApi.php @@ -6,7 +6,15 @@ class NullIssueApi implements IssueApi { + public function open(Repository $repository, string $title, string $body, array $labels) + { + } + public function commentOnIssue(Repository $repository, $issueNumber, string $commentBody) { } + + public function close(Repository $repository, $issueNumber) + { + } }