Skip to content

Commit b0cebd1

Browse files
committed
Adding functionality to auto-label PR's with "needs review"
1 parent 5ecdee0 commit b0cebd1

File tree

4 files changed

+472
-5
lines changed

4 files changed

+472
-5
lines changed

src/AppBundle/Controller/WebhookController.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AppBundle\Controller;
44

5+
use AppBundle\GitHub\StatusManager;
56
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
67
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
78
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
@@ -27,6 +28,17 @@ public function githubAction(Request $request)
2728
case 'issue_comment':
2829
$responseData = $this->handleIssueCommentEvent($data);
2930
break;
31+
case 'pull_request':
32+
switch ($data['action']) {
33+
case 'opened':
34+
$responseData = $this->handlePullRequestCreateEvent($data);
35+
break;
36+
default:
37+
$responseData = [
38+
'unsupported_action' => $data['action']
39+
];
40+
}
41+
break;
3042
default:
3143
$responseData = [
3244
'unsupported_event' => $event
@@ -61,4 +73,21 @@ private function handleIssueCommentEvent(array $data)
6173
'status_change' => $newStatus,
6274
];
6375
}
76+
77+
private function handlePullRequestCreateEvent(array $data)
78+
{
79+
$prNumber = $data['pull_request']['number'];
80+
$newStatus = StatusManager::STATUS_NEEDS_REVIEW;
81+
82+
// hacky way to not actually try to talk to GitHub when testing
83+
if ($this->container->getParameter('kernel.environment') != 'test') {
84+
$this->get('app.issue_status_changer')
85+
->setIssueStatusLabel($prNumber, $newStatus);
86+
}
87+
88+
return [
89+
'pull_request' => $prNumber,
90+
'status_change' => $newStatus,
91+
];
92+
}
6493
}

src/AppBundle/Tests/Controller/WebhookControllerTest.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@
66

77
class WebhookControllerTest extends WebTestCase
88
{
9-
public function testIssueComment()
9+
/**
10+
* @dataProvider getTests
11+
*/
12+
public function testIssueComment($eventHeader, $payloadFilename, $expectedResponse)
1013
{
1114
$client = $this->createClient();
12-
$body = file_get_contents(__DIR__.'/../webhook_examples/issue_comment.created.json');
13-
$client->request('POST', '/webhooks/github', array(), array(), array('HTTP_X-Github-Event' => 'issue_comment'), $body);
15+
$body = file_get_contents(__DIR__.'/../webhook_examples/'.$payloadFilename);
16+
$client->request('POST', '/webhooks/github', array(), array(), array('HTTP_X-Github-Event' => $eventHeader), $body);
1417
$response = $client->getResponse();
1518

1619
$responseData = json_decode($response->getContent(), true);
1720
$this->assertEquals(200, $response->getStatusCode());
21+
1822
// a weak sanity check that we went down "the right path" in the controller
19-
$this->assertEquals($responseData['status_change'], 'needs_review');
20-
$this->assertEquals($responseData['issue'], 1);
23+
$this->assertEquals($expectedResponse, $responseData);
2124
}
2225

26+
public function getTests()
27+
{
28+
$tests = array();
29+
$tests[] = array(
30+
'issue_comment',
31+
'issue_comment.created.json',
32+
array('status_change' => 'needs_review', 'issue' => 1)
33+
);
34+
$tests[] = array(
35+
'pull_request',
36+
'pull_request.opened.json',
37+
array('status_change' => 'needs_review', 'pull_request' => 3)
38+
);
39+
40+
return $tests;
41+
}
2342
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Request URL: http://carson.knpuniversity.com/webhooks/github
2+
Request method: POST
3+
content-type: application/json
4+
Expect:
5+
User-Agent: GitHub-Hookshot/2ee22c1
6+
X-GitHub-Delivery: 4640d280-1da9-11e5-9380-bbf5a1da51b6
7+
X-GitHub-Event: pull_request

0 commit comments

Comments
 (0)