forked from symfony-tools/carsonbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGithubIssueApi.php
More file actions
84 lines (70 loc) · 2.8 KB
/
GithubIssueApi.php
File metadata and controls
84 lines (70 loc) · 2.8 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
<?php
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, 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;
$existingIssues = $this->searchApi->issues(sprintf('repo:%s "%s" is:open author:%s', $repository->getFullName(), $title, $this->botUsername));
foreach ($existingIssues['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 lastCommentWasMadeByBot(Repository $repository, $number): bool
{
$allComments = $this->issueCommentApi->all($repository->getVendor(), $repository->getName(), $number);
$lastComment = $allComments[count($allComments) - 1] ?? [];
return $this->botUsername === ($lastComment['user']['login'] ?? null);
}
public function show(Repository $repository, $issueNumber): array
{
return $this->issueApi->show($repository->getVendor(), $repository->getName(), $issueNumber);
}
public function close(Repository $repository, $issueNumber)
{
$this->issueApi->update($repository->getVendor(), $repository->getName(), $issueNumber, ['state' => 'closed']);
}
/**
* This will comment on both Issues and Pull Requests.
*/
public function commentOnIssue(Repository $repository, $issueNumber, string $commentBody)
{
$this->issueCommentApi->create(
$repository->getVendor(),
$repository->getName(),
$issueNumber,
['body' => $commentBody]
);
}
public function findStaleIssues(Repository $repository, \DateTimeImmutable $noUpdateAfter): array
{
$issues = $this->searchApi->issues(sprintf('repo:%s is:issue -linked:pr -label:"Keep open" is:open updated:<%s', $repository->getFullName(), $noUpdateAfter->format('Y-m-d')));
return $issues['items'] ?? [];
}
}