Skip to content

Commit b3ff62d

Browse files
committed
Added better comments on the issue
1 parent cae3791 commit b3ff62d

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

src/Api/Issue/IssueType.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Api\Issue;
6+
7+
class IssueType
8+
{
9+
public const BUG = 'Bug';
10+
public const FEATURE = 'Feature';
11+
public const UNKNOWN = 'Unknown';
12+
public const RFC = 'RFC';
13+
}

src/Command/CloseStaleIssuesCommand.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App\Command;
44

55
use App\Api\Issue\IssueApi;
6+
use App\Api\Issue\IssueType;
67
use App\Entity\Task;
78
use App\Service\RepositoryProvider;
9+
use App\Service\StaleIssueCommentGenerator;
810
use App\Service\TaskScheduler;
911
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Input\InputArgument;
@@ -22,13 +24,15 @@ class CloseStaleIssuesCommand extends Command
2224
private $repositoryProvider;
2325
private $issueApi;
2426
private $scheduler;
27+
private $commentGenerator;
2528

26-
public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler)
29+
public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler, StaleIssueCommentGenerator $commentGenerator)
2730
{
2831
parent::__construct();
2932
$this->repositoryProvider = $repositoryProvider;
3033
$this->issueApi = $issueApi;
3134
$this->scheduler = $scheduler;
35+
$this->commentGenerator = $commentGenerator;
3236
}
3337

3438
protected function configure()
@@ -51,21 +55,40 @@ protected function execute(InputInterface $input, OutputInterface $output)
5155
$issues = $this->issueApi->findStaleIssues($repository, $notUpdatedAfter);
5256

5357
foreach ($issues as $issue) {
54-
$this->issueApi->commentOnIssue($repository, $issue['number'], <<<TXT
55-
Hey,
58+
$comment = $this->commentGenerator->getComment($this->extractType($issue));
59+
//$this->issueApi->commentOnIssue($repository, $issue['number'], $comment);
5660

57-
Is this issue still relevant? It has not been any activity in a while. I will close this if nobody makes a comment soon.
61+
// add a scheduled task to process this issue again after 2 weeks
62+
//$this->scheduler->runLater($repository, $issue['number'], Task::ACTION_CLOSE_STALE, new \DateTimeImmutable('+2weeks'));
63+
}
5864

59-
Cheers!
65+
return 0;
66+
}
6067

61-
Carsonbot
62-
TXT
63-
);
68+
/**
69+
* Extract type form issue array. Make sure we priorities labels if there are
70+
* more than one type defined.
71+
*/
72+
private function extractType(array $issue)
73+
{
74+
$types = [
75+
IssueType::FEATURE => false,
76+
IssueType::BUG => false,
77+
IssueType::RFC => false,
78+
];
6479

65-
// add a scheduled task to process this issue again after 2 weeks
66-
$this->scheduler->runLater($repository, $issue['number'], Task::ACTION_CLOSE_STALE, new \DateTimeImmutable('+2weeks'));
80+
foreach ($issue['labels'] as $label) {
81+
if (isset($types[$label['name']])) {
82+
$types[$label['name']] = true;
83+
}
6784
}
6885

69-
return 0;
86+
foreach ($types as $type => $exists) {
87+
if ($exists) {
88+
return $type;
89+
}
90+
}
91+
92+
return IssueType::UNKNOWN;
7093
}
7194
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service;
6+
7+
use App\Api\Issue\IssueType;
8+
9+
/**
10+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
11+
*/
12+
class StaleIssueCommentGenerator
13+
{
14+
/**
15+
* Get a comment to say: "I'm closing this now".
16+
*/
17+
public function getClosingComment()
18+
{
19+
return <<<TXT
20+
Hey,
21+
22+
I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!
23+
TXT;
24+
}
25+
26+
/**
27+
* Get a comment that encurage users to reply or close the issue themselves.
28+
*
29+
* @param string $type Valid types are IssueType::*
30+
*/
31+
public function getComment(string $type): string
32+
{
33+
switch ($type) {
34+
case IssueType::BUG:
35+
return $this->bug();
36+
case IssueType::FEATURE:
37+
case IssueType::RFC:
38+
return $this->feature();
39+
case IssueType::UNKNOWN:
40+
return $this->unknown();
41+
}
42+
}
43+
44+
private function bug(): string
45+
{
46+
return <<<TXT
47+
Hey,
48+
Is this bug still relevant? Have you managed to find a workaround?
49+
TXT;
50+
}
51+
52+
private function feature(): string
53+
{
54+
return <<<TXT
55+
Thank you for this suggestion.
56+
Is this still something we want to do?
57+
TXT;
58+
}
59+
60+
private function unknown(): string
61+
{
62+
return <<<TXT
63+
Thank you for this issue. Has this been resolved?
64+
TXT;
65+
}
66+
}

src/Service/TaskHandler/CloseStaleIssuesHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Api\Label\LabelApi;
99
use App\Entity\Task;
1010
use App\Service\RepositoryProvider;
11+
use App\Service\StaleIssueCommentGenerator;
1112

1213
/**
1314
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -17,12 +18,14 @@ class CloseStaleIssuesHandler implements TaskHandlerInterface
1718
private $issueApi;
1819
private $repositoryProvider;
1920
private $labelApi;
21+
private $commentGenerator;
2022

21-
public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider)
23+
public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, StaleIssueCommentGenerator $commentGenerator)
2224
{
2325
$this->issueApi = $issueApi;
2426
$this->repositoryProvider = $repositoryProvider;
2527
$this->labelApi = $labelApi;
28+
$this->commentGenerator = $commentGenerator;
2629
}
2730

2831
/**
@@ -39,6 +42,7 @@ public function handle(Task $task): void
3942
}
4043

4144
if ($this->issueApi->lastCommentWasMadeByBot($repository, $task->getNumber())) {
45+
$this->issueApi->commentOnIssue($repository, $task->getNumber(), $this->commentGenerator->getClosingComment());
4246
$this->issueApi->close($repository, $task->getNumber());
4347
}
4448
}

0 commit comments

Comments
 (0)