Skip to content

Commit 2e07e94

Browse files
committed
Added a second message
1 parent 9eaeaf4 commit 2e07e94

File tree

5 files changed

+88
-7
lines changed

5 files changed

+88
-7
lines changed

.symfony.cloud.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ crons:
4040

4141
stale_issues_symfony:
4242
spec: '58 12 * * *'
43-
cmd: croncape bin/console app:issue:close-stale symfony/symfony
43+
cmd: croncape bin/console app:issue:ping-stale symfony/symfony
4444

4545
stale_issues_docs:
4646
spec: '48 12 * * *'
47-
cmd: croncape bin/console app:issue:close-stale symfony/symfony-docs
47+
cmd: croncape bin/console app:issue:ping-stale symfony/symfony-docs
4848

4949
relationships:
5050
database: "mydatabase:postgresql"

src/Command/CloseStaleIssuesCommand.php renamed to src/Command/PingStaleIssuesCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@
1818
*
1919
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2020
*/
21-
class CloseStaleIssuesCommand extends Command
21+
class PingStaleIssuesCommand extends Command
2222
{
23-
protected static $defaultName = 'app:issue:close-stale';
23+
public const STALE_IF_NOT_UPDATED_SINCE = '-12months';
24+
public const MESSAGE_TWO_AFTER = '+2weeks';
25+
public const MESSAGE_THREE_AND_CLOSE_AFTER = '+2weeks';
26+
27+
protected static $defaultName = 'app:issue:ping-stale';
28+
2429
private $repositoryProvider;
2530
private $issueApi;
2631
private $scheduler;
@@ -51,15 +56,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
5156
return 1;
5257
}
5358

54-
$notUpdatedAfter = new \DateTimeImmutable('-12months');
59+
$notUpdatedAfter = new \DateTimeImmutable(self::STALE_IF_NOT_UPDATED_SINCE);
5560
$issues = $this->issueApi->findStaleIssues($repository, $notUpdatedAfter);
5661

5762
foreach ($issues as $issue) {
5863
$comment = $this->commentGenerator->getComment($this->extractType($issue));
5964
$this->issueApi->commentOnIssue($repository, $issue['number'], $comment);
6065

6166
// 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'));
67+
$this->scheduler->runLater($repository, $issue['number'], Task::ACTION_INFORM_CLOSE_STALE, new \DateTimeImmutable(self::MESSAGE_TWO_AFTER));
6368
}
6469

6570
return 0;

src/Entity/Task.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Task
1717
{
1818
const ACTION_CLOSE_STALE = 1;
1919
const ACTION_CLOSE_DRAFT = 2;
20+
const ACTION_INFORM_CLOSE_STALE = 3;
2021

2122
/**
2223
* @var int

src/Service/StaleIssueCommentGenerator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@
1111
*/
1212
class StaleIssueCommentGenerator
1313
{
14+
/**
15+
* Get a comment to say: "I will close this soon".
16+
*/
17+
public function getInformAboutClosingComment(): string
18+
{
19+
$messages = [
20+
'Hello? This issue is about to be closed if nobody replies.',
21+
'Friendly ping? Should this still be open? I will close if I dont hear anything.',
22+
'Could I get a reply or should I close this?',
23+
'Just a quick reminder to make a comment on this. If I don\'t hear anything I\'ll close this.',
24+
'Friendly reminder that this issue exists. If I don\'t hear anything I\'ll close this.',
25+
'Could I get an answer? If I do not hear anything I will assume this issue is resolved or abandoned. Please get back to me <3',
26+
];
27+
28+
return $messages[array_rand($messages)];
29+
}
30+
1431
/**
1532
* Get a comment to say: "I'm closing this now".
1633
*/
@@ -24,7 +41,7 @@ public function getClosingComment(): string
2441
}
2542

2643
/**
27-
* Get a comment that encurage users to reply or close the issue themselves.
44+
* Get a comment that encourage users to reply or close the issue themselves.
2845
*
2946
* @param string $type Valid types are IssueType::*
3047
*/
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Service\TaskHandler;
6+
7+
use App\Api\Issue\IssueApi;
8+
use App\Api\Label\LabelApi;
9+
use App\Command\PingStaleIssuesCommand;
10+
use App\Entity\Task;
11+
use App\Service\RepositoryProvider;
12+
use App\Service\StaleIssueCommentGenerator;
13+
use App\Service\TaskScheduler;
14+
15+
/**
16+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
17+
*/
18+
class InformAboutClosingStaleIssuesHandler implements TaskHandlerInterface
19+
{
20+
private $issueApi;
21+
private $repositoryProvider;
22+
private $labelApi;
23+
private $commentGenerator;
24+
private $scheduler;
25+
26+
public function __construct(LabelApi $labelApi, IssueApi $issueApi, RepositoryProvider $repositoryProvider, StaleIssueCommentGenerator $commentGenerator, TaskScheduler $scheduler)
27+
{
28+
$this->issueApi = $issueApi;
29+
$this->repositoryProvider = $repositoryProvider;
30+
$this->labelApi = $labelApi;
31+
$this->commentGenerator = $commentGenerator;
32+
$this->scheduler = $scheduler;
33+
}
34+
35+
/**
36+
* Close the issue if the last comment was made by the bot and if "Keep open" label does not exist.
37+
*/
38+
public function handle(Task $task): void
39+
{
40+
if (null === $repository = $this->repositoryProvider->getRepository($task->getRepositoryFullName())) {
41+
return;
42+
}
43+
$labels = $this->labelApi->getIssueLabels($task->getNumber(), $repository);
44+
if (in_array('Keep open', $labels)) {
45+
return;
46+
}
47+
48+
if ($this->issueApi->lastCommentWasMadeByBot($repository, $task->getNumber())) {
49+
$this->issueApi->commentOnIssue($repository, $task->getNumber(), $this->commentGenerator->getInformAboutClosingComment());
50+
$this->scheduler->runLater($repository, $task->getNumber(), Task::ACTION_CLOSE_STALE, new \DateTimeImmutable(PingStaleIssuesCommand::MESSAGE_THREE_AND_CLOSE_AFTER));
51+
}
52+
}
53+
54+
public function supports(Task $task): bool
55+
{
56+
return Task::ACTION_INFORM_CLOSE_STALE === $task->getAction();
57+
}
58+
}

0 commit comments

Comments
 (0)