Skip to content

Commit 541ad5e

Browse files
authored
Update IssueApi to support create and close issues (#106)
1 parent ecf5677 commit 541ad5e

File tree

6 files changed

+64
-2
lines changed

6 files changed

+64
-2
lines changed

.env

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ APP_SECRET=5dd8ffca252d95e8b4fb5b2d15310e92
2121

2222
SYMFONY_DOCS_SECRET=''
2323
SYMFONY_SECRET=''
24-
24+
BOT_USERNAME='carsonbot-test'
2525
###> knplabs/github-api ###
2626
#GITHUB_TOKEN=XXX
2727
###< knplabs/github-api ###

.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ SYMFONY_DEPRECATIONS_HELPER=999999
55
PANTHER_APP_ENV=panther
66
SYMFONY_DOCS_SECRET=''
77
SYMFONY_SECRET=''
8+
9+
BOT_USERNAME='carsonbot-test'

config/services.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ services:
4242
autowire: true
4343
autoconfigure: true
4444

45+
bind:
46+
string $botUsername: '%env(BOT_USERNAME)%'
47+
4548
App\:
4649
resource: '../src/*'
4750
exclude: '../src/{DependencyInjection,Subscriber,Kernel.php,GitHubEvents.php}'
@@ -62,6 +65,10 @@ services:
6265
factory: ['@Github\Client', api]
6366
arguments: [pullRequest]
6467

68+
Github\Api\Search:
69+
factory: ['@Github\Client', api]
70+
arguments: [search]
71+
6572
Github\Api\Issue\Labels:
6673
factory: ['@Github\Api\Issue', labels]
6774

src/Api/Issue/GithubIssueApi.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,50 @@
33
namespace App\Api\Issue;
44

55
use App\Model\Repository;
6+
use Github\Api\Issue;
67
use Github\Api\Issue\Comments;
8+
use Github\Api\Search;
79

810
class GithubIssueApi implements IssueApi
911
{
1012
private $issueCommentApi;
13+
private $botUsername;
14+
private $issueApi;
15+
private $searchApi;
1116

12-
public function __construct(Comments $issueCommentApi)
17+
public function __construct(Comments $issueCommentApi, Issue $issueApi, Search $searchApi, string $botUsername)
1318
{
1419
$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']);
1550
}
1651

1752
/**

src/Api/Issue/IssueApi.php

+10
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,15 @@
1212
*/
1313
interface IssueApi
1414
{
15+
/**
16+
* Open new issue or update existing issue.
17+
*/
18+
public function open(Repository $repository, string $title, string $body, array $labels);
19+
1520
public function commentOnIssue(Repository $repository, $issueNumber, string $commentBody);
21+
22+
/**
23+
* Close an issue or a pull request.
24+
*/
25+
public function close(Repository $repository, $issueNumber);
1626
}

src/Api/Issue/NullIssueApi.php

+8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66

77
class NullIssueApi implements IssueApi
88
{
9+
public function open(Repository $repository, string $title, string $body, array $labels)
10+
{
11+
}
12+
913
public function commentOnIssue(Repository $repository, $issueNumber, string $commentBody)
1014
{
1115
}
16+
17+
public function close(Repository $repository, $issueNumber)
18+
{
19+
}
1220
}

0 commit comments

Comments
 (0)