Skip to content

Commit 13eeb35

Browse files
committed
Add label "Staled" on issues
1 parent 45b4697 commit 13eeb35

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

config/services.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ parameters:
1111
- 'App\Subscriber\MilestoneNewPRSubscriber'
1212
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
1313
- 'App\Subscriber\CloseDraftPRSubscriber'
14+
- 'App\Subscriber\RemoveStaledLabelOnCommentSubscriber'
1415
secret: '%env(SYMFONY_SECRET)%'
1516

1617
symfony/symfony-docs:
@@ -23,6 +24,7 @@ parameters:
2324
- 'App\Subscriber\BugLabelNewIssueSubscriber'
2425
- 'App\Subscriber\AutoLabelFromContentSubscriber'
2526
- 'subscriber.symfony_docs.milestone'
27+
- 'App\Subscriber\RemoveStaledLabelOnCommentSubscriber'
2628
secret: '%env(SYMFONY_DOCS_SECRET)%'
2729

2830
# used in a functional test
@@ -38,6 +40,7 @@ parameters:
3840
- 'App\Subscriber\MilestoneNewPRSubscriber'
3941
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
4042
- 'App\Subscriber\CloseDraftPRSubscriber'
43+
- 'App\Subscriber\RemoveStaledLabelOnCommentSubscriber'
4144

4245
services:
4346
_defaults:

src/Command/PingStaleIssuesCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Api\Issue\IssueApi;
66
use App\Api\Issue\IssueType;
7+
use App\Api\Label\LabelApi;
78
use App\Entity\Task;
89
use App\Service\RepositoryProvider;
910
use App\Service\StaleIssueCommentGenerator;
@@ -30,14 +31,16 @@ class PingStaleIssuesCommand extends Command
3031
private $issueApi;
3132
private $scheduler;
3233
private $commentGenerator;
34+
private $labelApi;
3335

34-
public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler, StaleIssueCommentGenerator $commentGenerator)
36+
public function __construct(RepositoryProvider $repositoryProvider, IssueApi $issueApi, TaskScheduler $scheduler, StaleIssueCommentGenerator $commentGenerator, LabelApi $labelApi)
3537
{
3638
parent::__construct();
3739
$this->repositoryProvider = $repositoryProvider;
3840
$this->issueApi = $issueApi;
3941
$this->scheduler = $scheduler;
4042
$this->commentGenerator = $commentGenerator;
43+
$this->labelApi = $labelApi;
4144
}
4245

4346
protected function configure()
@@ -62,6 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6265
foreach ($issues as $issue) {
6366
$comment = $this->commentGenerator->getComment($this->extractType($issue));
6467
$this->issueApi->commentOnIssue($repository, $issue['number'], $comment);
68+
$this->labelApi->addIssueLabel($issue['number'], 'Staled', $repository);
6569

6670
// add a scheduled task to process this issue again after 2 weeks
6771
$this->scheduler->runLater($repository, $issue['number'], Task::ACTION_INFORM_CLOSE_STALE, new \DateTimeImmutable(self::MESSAGE_TWO_AFTER));

src/Service/TaskHandler/CloseStaleIssuesHandler.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@ public function handle(Task $task): void
3838
}
3939
$labels = $this->labelApi->getIssueLabels($task->getNumber(), $repository);
4040
if (in_array('Keep open', $labels)) {
41+
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
4142
return;
4243
}
4344

4445
if ($this->issueApi->lastCommentWasMadeByBot($repository, $task->getNumber())) {
4546
$this->issueApi->commentOnIssue($repository, $task->getNumber(), $this->commentGenerator->getClosingComment());
4647
$this->issueApi->close($repository, $task->getNumber());
48+
} else {
49+
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
4750
}
4851
}
4952

src/Service/TaskHandler/InformAboutClosingStaleIssuesHandler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,16 @@ public function handle(Task $task): void
4242
}
4343
$labels = $this->labelApi->getIssueLabels($task->getNumber(), $repository);
4444
if (in_array('Keep open', $labels)) {
45+
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
46+
4547
return;
4648
}
4749

4850
if ($this->issueApi->lastCommentWasMadeByBot($repository, $task->getNumber())) {
4951
$this->issueApi->commentOnIssue($repository, $task->getNumber(), $this->commentGenerator->getInformAboutClosingComment());
5052
$this->scheduler->runLater($repository, $task->getNumber(), Task::ACTION_CLOSE_STALE, new \DateTimeImmutable(PingStaleIssuesCommand::MESSAGE_THREE_AND_CLOSE_AFTER));
53+
} else {
54+
$this->labelApi->removeIssueLabel($task->getNumber(), 'Staled', $repository);
5155
}
5256
}
5357

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\Subscriber;
4+
5+
use App\Api\Label\LabelApi;
6+
use App\Event\GitHubEvent;
7+
use App\GitHubEvents;
8+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9+
10+
/**
11+
* If somebody (not the bot) makes a comment on an issue that is stale, then remove
12+
* the stale label.
13+
*
14+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
15+
*/
16+
class RemoveStaledLabelOnCommentSubscriber implements EventSubscriberInterface
17+
{
18+
private $labelApi;
19+
private $botUsername;
20+
21+
public function __construct(LabelApi $labelApi, string $botUsername)
22+
{
23+
$this->labelApi = $labelApi;
24+
$this->botUsername = $botUsername;
25+
}
26+
27+
public function onIssueComment(GitHubEvent $event)
28+
{
29+
$data = $event->getData();
30+
$repository = $event->getRepository();
31+
32+
// If bot, then nothing.
33+
if ($data['comment']['user']['login'] === $this->botUsername) {
34+
return;
35+
}
36+
37+
$removed = false;
38+
$issueNumber = $data['issue']['number'];
39+
foreach ($data['issue']['labels'] as $label) {
40+
if ('Staled' === $label['name']) {
41+
$removed = true;
42+
$this->labelApi->removeIssueLabel($issueNumber, 'Staled', $repository);
43+
}
44+
}
45+
46+
if ($removed) {
47+
$event->setResponseData([
48+
'issue' => $issueNumber,
49+
'removed_staled_label' => true,
50+
]);
51+
}
52+
}
53+
54+
public static function getSubscribedEvents()
55+
{
56+
return [
57+
GitHubEvents::ISSUE_COMMENT => 'onIssueComment',
58+
];
59+
}
60+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Subscriber;
6+
7+
use App\Api\Label\NullLabelApi;
8+
use App\Event\GitHubEvent;
9+
use App\GitHubEvents;
10+
use App\Model\Repository;
11+
use App\Subscriber\RemoveStaledLabelOnCommentSubscriber;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\EventDispatcher\EventDispatcher;
14+
15+
class RemoveStaledLabelOnCommentSubscriberTest extends TestCase
16+
{
17+
private $subscriber;
18+
19+
private $repository;
20+
21+
/**
22+
* @var EventDispatcher
23+
*/
24+
private $dispatcher;
25+
26+
protected function setUp()
27+
{
28+
$this->subscriber = new RemoveStaledLabelOnCommentSubscriber(new NullLabelApi(), 'carsonbot');
29+
$this->repository = new Repository('carsonbot-playground', 'symfony', null);
30+
31+
$this->dispatcher = new EventDispatcher();
32+
$this->dispatcher->addSubscriber($this->subscriber);
33+
}
34+
35+
public function testOnComment()
36+
{
37+
$event = new GitHubEvent([
38+
'issue' => ['number' => 1234, 'labels' => []], 'comment' => ['user' => ['login' => 'nyholm']],
39+
], $this->repository);
40+
41+
$this->dispatcher->dispatch($event, GitHubEvents::ISSUE_COMMENT);
42+
43+
$responseData = $event->getResponseData();
44+
$this->assertEmpty($responseData);
45+
}
46+
47+
public function testOnCommentOnStale()
48+
{
49+
$event = new GitHubEvent([
50+
'issue' => ['number' => 1234, 'labels' => [['name' => 'Foo'], ['name' => 'Staled']]], 'comment' => ['user' => ['login' => 'nyholm']],
51+
], $this->repository);
52+
53+
$this->dispatcher->dispatch($event, GitHubEvents::ISSUE_COMMENT);
54+
55+
$responseData = $event->getResponseData();
56+
$this->assertCount(2, $responseData);
57+
$this->assertSame(1234, $responseData['issue']);
58+
$this->assertSame(true, $responseData['removed_staled_label']);
59+
}
60+
61+
public function testOnBotCommentOnStale()
62+
{
63+
$event = new GitHubEvent([
64+
'issue' => ['number' => 1234, 'labels' => [['name' => 'Foo'], ['name' => 'Staled']]], 'comment' => ['user' => ['login' => 'carsonbot']],
65+
], $this->repository);
66+
67+
$this->dispatcher->dispatch($event, GitHubEvents::ISSUE_COMMENT);
68+
69+
$responseData = $event->getResponseData();
70+
$this->assertEmpty($responseData);
71+
}
72+
}

0 commit comments

Comments
 (0)