Skip to content

Commit def61e0

Browse files
authored
Merge pull request #38 from wouterj/need_review_after_push
Switch "Needs Work" to "Needs Review" after a push
2 parents dd3c437 + 4059183 commit def61e0

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

app/config/github.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ services:
66
- '@app.status_api'
77
- '@logger'
88

9+
app.subscriber.status_change_on_push_subscriber:
10+
class: AppBundle\Subscriber\StatusChangeOnPushSubscriber
11+
arguments:
12+
- '@app.status_api'
13+
914
app.subscriber.needs_review_new_pr_subscriber:
1015
class: AppBundle\Subscriber\NeedsReviewNewPRSubscriber
1116
arguments:
@@ -35,6 +40,7 @@ parameters:
3540
symfony/symfony-docs:
3641
subscribers:
3742
- app.subscriber.status_change_by_comment_subscriber
43+
- app.subscriber.status_change_on_push_subscriber
3844
- app.subscriber.needs_review_new_pr_subscriber
3945
- app.subscriber.bug_label_new_issue_subscriber
4046
# secret: %symfony_docs_secret%
@@ -43,6 +49,7 @@ parameters:
4349
weaverryan/symfony:
4450
subscribers:
4551
- app.subscriber.status_change_by_comment_subscriber
52+
- app.subscriber.status_change_on_push_subscriber
4653
- app.subscriber.needs_review_new_pr_subscriber
4754
- app.subscriber.bug_label_new_issue_subscriber
4855
- app.subscriber.auto_label_pr_from_content_subscriber
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+
/**
12+
* Changes the status from "Needs Work" to "Needs Review" when pushing new
13+
* commits.
14+
*
15+
* @author Wouter de Jong <wouter@wouterj.nl>
16+
*/
17+
class StatusChangeOnPushSubscriber implements EventSubscriberInterface
18+
{
19+
private $statusApi;
20+
21+
public function __construct(StatusApi $statusApi)
22+
{
23+
$this->statusApi = $statusApi;
24+
}
25+
26+
public function onPullRequest(GitHubEvent $event)
27+
{
28+
$data = $event->getData();
29+
if ('synchronize' !== $data['action']) {
30+
$event->setResponseData(array('unsupported_action' => $data['action']));
31+
32+
return;
33+
}
34+
35+
$repository = $event->getRepository();
36+
$pullRequestNumber = $data['pull_request']['number'];
37+
$responseData = array('pull_request' => $pullRequestNumber);
38+
$currentStatus = $this->statusApi->getIssueStatus($pullRequestNumber, $repository);
39+
$pullRequestTitle = $data['pull_request']['title'];
40+
41+
if (Status::NEEDS_WORK !== $currentStatus || false !== stripos($pullRequestTitle, '[wip]')) {
42+
$responseData['status_change'] = null;
43+
$event->setResponseData($responseData);
44+
45+
return;
46+
}
47+
48+
$newStatus = Status::NEEDS_REVIEW;
49+
50+
$this->statusApi->setIssueStatus($pullRequestNumber, $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::PULL_REQUEST => 'onPullRequest',
60+
);
61+
}
62+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
namespace AppBundle\Tests\Subscriber;
4+
5+
use AppBundle\Event\GitHubEvent;
6+
use AppBundle\GitHubEvents;
7+
use AppBundle\Issues\Status;
8+
use AppBundle\Repository\Repository;
9+
use AppBundle\Subscriber\StatusChangeOnPushSubscriber;
10+
use Symfony\Component\EventDispatcher\EventDispatcher;
11+
12+
class StatusChangeOnPushSubscriberTest extends \PHPUnit_Framework_TestCase
13+
{
14+
private $statusChangeSubscriber;
15+
16+
private $statusApi;
17+
18+
private $repository;
19+
20+
/**
21+
* @var EventDispatcher
22+
*/
23+
private static $dispatcher;
24+
25+
public static function setUpBeforeClass()
26+
{
27+
self::$dispatcher = new EventDispatcher();
28+
}
29+
30+
protected function setUp()
31+
{
32+
$this->statusApi = $this->getMock('AppBundle\Issues\StatusApi');
33+
$this->statusChangeSubscriber = new StatusChangeOnPushSubscriber($this->statusApi);
34+
$this->repository = new Repository('weaverryan', 'symfony', [], null);
35+
36+
self::$dispatcher->addSubscriber($this->statusChangeSubscriber);
37+
}
38+
39+
/**
40+
* @dataProvider getStatuses
41+
*/
42+
public function testOnPushingCommits($currentStatus, $statusChange)
43+
{
44+
$this->statusApi->expects($this->any())
45+
->method('getIssueStatus')
46+
->with(1234, $this->repository)
47+
->will($this->returnValue($currentStatus));
48+
49+
if (null !== $statusChange) {
50+
$this->statusApi->expects($this->once())
51+
->method('setIssueStatus')
52+
->with(1234, Status::NEEDS_REVIEW, $this->repository);
53+
}
54+
55+
$event = new GitHubEvent([
56+
'action' => 'synchronize',
57+
'pull_request' => $this->getPullRequestData(),
58+
], $this->repository);
59+
60+
self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event);
61+
62+
$responseData = $event->getResponseData();
63+
64+
$this->assertCount(2, $responseData);
65+
$this->assertSame(1234, $responseData['pull_request']);
66+
$this->assertSame($statusChange, $responseData['status_change']);
67+
}
68+
69+
public function getStatuses()
70+
{
71+
return array(
72+
array(Status::NEEDS_WORK, Status::NEEDS_REVIEW),
73+
array(Status::REVIEWED, null),
74+
array(Status::WORKS_FOR_ME, null),
75+
array(Status::NEEDS_REVIEW, null),
76+
);
77+
}
78+
79+
public function testOnNonPushPullRequestEvent()
80+
{
81+
$this->statusApi->expects($this->any())
82+
->method('getIssueStatus')
83+
->with(1234, $this->repository)
84+
->will($this->returnValue(Status::NEEDS_WORK));
85+
86+
$event = new GitHubEvent(array(
87+
'action' => 'labeled',
88+
'pull_request' => $this->getPullRequestData(),
89+
), $this->repository);
90+
91+
self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event);
92+
93+
$responseData = $event->getResponseData();
94+
95+
$this->assertCount(1, $responseData);
96+
$this->assertSame('labeled', $responseData['unsupported_action']);
97+
}
98+
99+
public function testWipPullRequest()
100+
{
101+
$this->statusApi->expects($this->any())
102+
->method('getIssueStatus')
103+
->with(1234, $this->repository)
104+
->will($this->returnValue(Status::NEEDS_WORK));
105+
106+
$event = new GitHubEvent(array(
107+
'action' => 'synchronize',
108+
'pull_request' => $this->getPullRequestData('[wip] needs some more work.'),
109+
), $this->repository);
110+
111+
self::$dispatcher->dispatch(GitHubEvents::PULL_REQUEST, $event);
112+
113+
$responseData = $event->getResponseData();
114+
115+
$this->assertCount(2, $responseData);
116+
$this->assertSame(1234, $responseData['pull_request']);
117+
$this->assertSame(null, $responseData['status_change']);
118+
}
119+
120+
private function getPullRequestData($title = 'Default title.')
121+
{
122+
return [
123+
'number' => 1234,
124+
'title' => $title,
125+
];
126+
}
127+
}

0 commit comments

Comments
 (0)