forked from symfony-tools/carsonbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCloseStaleIssuesHandler.php
More file actions
58 lines (49 loc) · 1.87 KB
/
CloseStaleIssuesHandler.php
File metadata and controls
58 lines (49 loc) · 1.87 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
<?php
declare(strict_types=1);
namespace App\Service\TaskHandler;
use App\Api\Issue\IssueApi;
use App\Api\Label\LabelApi;
use App\Entity\Task;
use App\Service\RepositoryProvider;
use App\Service\StaleIssueCommentGenerator;
/**
* @author Tobias Nyholm <[email protected]>
*/
class CloseStaleIssuesHandler implements TaskHandlerInterface
{
private $issueApi;
private $repositoryProvider;
private $labelApi;
private $commentGenerator;
public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, StaleIssueCommentGenerator $commentGenerator)
{
$this->issueApi = $issueApi;
$this->repositoryProvider = $repositoryProvider;
$this->labelApi = $labelApi;
$this->commentGenerator = $commentGenerator;
}
/**
* Close the issue if the last comment was made by the bot and if "Keep open" label does not exist.
*/
public function handle(Task $task): void
{
if (null === $repository = $this->repositoryProvider->getRepository($task->getRepositoryFullName())) {
return;
}
$labels = $this->labelApi->getIssueLabels($task->getNumber(), $repository);
if (in_array('Keep open', $labels)) {
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
return;
}
if ($this->issueApi->lastCommentWasMadeByBot($repository, $task->getNumber())) {
$this->issueApi->commentOnIssue($repository, $task->getNumber(), $this->commentGenerator->getClosingComment());
$this->issueApi->close($repository, $task->getNumber());
} else {
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
}
}
public function supports(Task $task): bool
{
return Task::ACTION_CLOSE_STALE === $task->getAction();
}
}