Skip to content

Commit e47f276

Browse files
committed
Splitting the IssueListener into 2 different "features"
1 parent 7f6db6a commit e47f276

File tree

5 files changed

+198
-145
lines changed

5 files changed

+198
-145
lines changed

app/config/github.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
# listener services that you can opt into for a repository
22
services:
3-
app.subscriber.issue_subscriber:
4-
class: AppBundle\Issues\IssueListener
3+
app.subscriber.status_change_by_comment_subscriber:
4+
class: AppBundle\Subscriber\StatusChangeByCommentSubscriber
5+
arguments:
6+
- '@app.status_api'
7+
8+
app.subscriber.needs_review_new_pr_subscriber:
9+
class: AppBundle\Subscriber\NeedsReviewNewPRSubscriber
10+
arguments:
11+
- '@app.status_api'
12+
13+
app.subscriber.bug_label_new_issue_subscriber:
14+
class: AppBundle\Subscriber\BugLabelNewIssueSubscriber
515
arguments:
616
- '@app.status_api'
717

@@ -10,8 +20,12 @@ parameters:
1020
repositories:
1121
symfony/symfony:
1222
subscribers:
13-
- app.subscriber.issue_subscriber
23+
- app.subscriber.status_change_by_comment_subscriber
24+
- app.subscriber.needs_review_new_pr_subscriber
25+
- app.subscriber.bug_label_new_issue_subscriber
1426
# secret: change_me
1527
symfony/symfony-docs:
1628
subscribers:
17-
- app.subscriber.issue_subscriber
29+
- app.subscriber.status_change_by_comment_subscriber
30+
- app.subscriber.needs_review_new_pr_subscriber
31+
- app.subscriber.bug_label_new_issue_subscriber

src/AppBundle/Issues/IssueListener.php

Lines changed: 0 additions & 141 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace AppBundle\Subscriber;
4+
5+
use AppBundle\Event\GitHubEvent;
6+
use AppBundle\GitHubEvents;
7+
use AppBundle\Issues\Status;
8+
use AppBundle\Issues\StatusApi;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class BugLabelNewIssueSubscriber implements EventSubscriberInterface
12+
{
13+
/**
14+
* @var StatusApi
15+
*/
16+
private $statusApi;
17+
18+
public function __construct(StatusApi $statusApi)
19+
{
20+
$this->statusApi = $statusApi;
21+
}
22+
23+
/**
24+
* Changes "Bug" issues to "Needs Review".
25+
*
26+
* @param GitHubEvent $event
27+
*/
28+
public function onIssues(GitHubEvent $event)
29+
{
30+
$data = $event->getData();
31+
$repository = $event->getRepository();
32+
if ('labeled' !== $action = $data['action']) {
33+
$event->setResponseData(array('unsupported_action' => $action));
34+
35+
return;
36+
}
37+
38+
$responseData = array('issue' => $issueNumber = $data['issue']['number']);
39+
// Ignore non-bugs or issue which already has a status
40+
$currentStatus = $currentStatus = $this->statusApi->getIssueStatus($issueNumber, $repository);
41+
if ('bug' !== strtolower($data['label']['name']) || null !== $currentStatus) {
42+
$responseData['status_change'] = null;
43+
$event->setResponseData($responseData);
44+
45+
return;
46+
}
47+
48+
$newStatus = Status::NEEDS_REVIEW;
49+
50+
$this->statusApi->setIssueStatus($issueNumber, $newStatus, $repository);
51+
$responseData['status_change'] = $newStatus;
52+
53+
$event->setResponseData($responseData);
54+
}
55+
56+
public static function getSubscribedEvents()
57+
{
58+
return array(
59+
GitHubEvents::ISSUES => 'onIssues',
60+
);
61+
}
62+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace AppBundle\Subscriber;
4+
5+
use AppBundle\Event\GitHubEvent;
6+
use AppBundle\GitHubEvents;
7+
use AppBundle\Issues\Status;
8+
use AppBundle\Issues\StatusApi;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class NeedsReviewNewPRSubscriber implements EventSubscriberInterface
12+
{
13+
private $statusApi;
14+
15+
public function __construct(StatusApi $statusApi)
16+
{
17+
$this->statusApi = $statusApi;
18+
}
19+
20+
/**
21+
* Adds a "Needs Review" label to new PRs.
22+
*
23+
* @param GitHubEvent $event
24+
*/
25+
public function onPullRequest(GitHubEvent $event)
26+
{
27+
$data = $event->getData();
28+
$repository = $event->getRepository();
29+
if ('opened' !== $action = $data['action']) {
30+
$event->setResponseData(array('unsupported_action' => $action));
31+
32+
return;
33+
}
34+
35+
$pullRequestNumber = $data['pull_request']['number'];
36+
$newStatus = Status::NEEDS_REVIEW;
37+
38+
$this->statusApi->setIssueStatus($pullRequestNumber, $newStatus, $repository);
39+
40+
$event->setResponseData(array(
41+
'pull_request' => $pullRequestNumber,
42+
'status_change' => $newStatus,
43+
));
44+
}
45+
46+
public static function getSubscribedEvents()
47+
{
48+
return array(
49+
GitHubEvents::PULL_REQUEST => 'onPullRequest',
50+
);
51+
}
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace AppBundle\Subscriber;
4+
5+
use AppBundle\Event\GitHubEvent;
6+
use AppBundle\GitHubEvents;
7+
use AppBundle\Issues\Status;
8+
use AppBundle\Issues\StatusApi;
9+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10+
11+
class StatusChangeByCommentSubscriber implements EventSubscriberInterface
12+
{
13+
private static $triggerWordToStatus = [
14+
'needs review' => Status::NEEDS_REVIEW,
15+
'needs work' => Status::NEEDS_WORK,
16+
'works for me' => Status::WORKS_FOR_ME,
17+
'reviewed' => Status::REVIEWED,
18+
'needs comments' => Status::NEEDS_COMMENTS,
19+
];
20+
21+
private $statusApi;
22+
23+
public function __construct(StatusApi $statusApi)
24+
{
25+
$this->statusApi = $statusApi;
26+
}
27+
28+
/**
29+
* Parses the text of the comment and looks for keywords to see
30+
* if this should cause any status change.
31+
*
32+
* @param GitHubEvent $event
33+
*/
34+
public function onIssueComment(GitHubEvent $event)
35+
{
36+
$data = $event->getData();
37+
$repository = $event->getRepository();
38+
$newStatus = null;
39+
$issueNumber = $data['issue']['number'];
40+
41+
$triggerWord = implode('|', array_keys(static::$triggerWordToStatus));
42+
$formatting = '[\\s\\*]*';
43+
// Match first character after "status:"
44+
// Case insensitive ("i"), ignores formatting with "*" before or after the ":"
45+
$pattern = "~(?=\n|^)${formatting}status${formatting}:${formatting}[\"']?($triggerWord)[\"']?${formatting}[.!]?${formatting}(?<=\r\n|\n|$)~i";
46+
47+
if (preg_match_all($pattern, $data['comment']['body'], $matches)) {
48+
// Second subpattern = first status character
49+
$newStatus = static::$triggerWordToStatus[strtolower(end($matches[1]))];
50+
51+
$this->statusApi->setIssueStatus($issueNumber, $newStatus, $repository);
52+
}
53+
54+
$event->setResponseData(array(
55+
'issue' => $issueNumber,
56+
'status_change' => $newStatus,
57+
));
58+
}
59+
60+
public static function getSubscribedEvents()
61+
{
62+
return array(
63+
GitHubEvents::ISSUE_COMMENT => 'onIssueComment',
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)