Skip to content

Commit 450f7b9

Browse files
Nyholmweaverryan
authored andcommitted
Don't mark PRs with "WIP" as "ready for review"
1 parent 074528d commit 450f7b9

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

src/Repository/.gitignore

Whitespace-only changes.

src/Service/WipParser.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Service;
4+
5+
/**
6+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
7+
*/
8+
class WipParser
9+
{
10+
/**
11+
* Returns true if the title begins by [WIP], (WIP) or WIP:.
12+
*/
13+
public static function matchTitle(string $title): bool
14+
{
15+
return (bool) preg_match('@^(\[wip\]|\(wip\)|wip:)@mi', $title);
16+
}
17+
}

src/Subscriber/NeedsReviewNewPRSubscriber.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Api\Status\StatusApi;
77
use App\Event\GitHubEvent;
88
use App\GitHubEvents;
9+
use App\Service\WipParser;
910
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1011

1112
class NeedsReviewNewPRSubscriber implements EventSubscriberInterface
@@ -28,6 +29,10 @@ public function onPullRequest(GitHubEvent $event)
2829
return;
2930
}
3031

32+
if (WipParser::matchTitle($data['pull_request']['title'])) {
33+
return;
34+
}
35+
3136
$pullRequestNumber = $data['pull_request']['number'];
3237
$newStatus = Status::NEEDS_REVIEW;
3338

src/Subscriber/StatusChangeOnPushSubscriber.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Api\Status\StatusApi;
77
use App\Event\GitHubEvent;
88
use App\GitHubEvents;
9+
use App\Service\WipParser;
910
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1011

1112
/**
@@ -36,7 +37,7 @@ public function onPullRequest(GitHubEvent $event)
3637
$currentStatus = $this->statusApi->getIssueStatus($pullRequestNumber, $repository);
3738
$pullRequestTitle = $data['pull_request']['title'];
3839

39-
if (Status::NEEDS_WORK !== $currentStatus || false !== stripos($pullRequestTitle, '[wip]')) {
40+
if (Status::NEEDS_WORK !== $currentStatus || WipParser::matchTitle($pullRequestTitle)) {
4041
$responseData['status_change'] = null;
4142
$event->setResponseData($responseData);
4243

tests/Service/WipParserTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Service;
6+
7+
use App\Service\WipParser;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class WipParserTest extends TestCase
11+
{
12+
public function testMatchTitle()
13+
{
14+
$this->assertTrue(WipParser::matchTitle('[WIP] foo'));
15+
$this->assertTrue(WipParser::matchTitle('WIP: bar'));
16+
$this->assertTrue(WipParser::matchTitle('(WIP) xas'));
17+
$this->assertTrue(WipParser::matchTitle('[WIP]foo'));
18+
$this->assertTrue(WipParser::matchTitle('[wip] foo'));
19+
20+
$this->assertFalse(WipParser::matchTitle('Bar [WIP] foo'));
21+
$this->assertFalse(WipParser::matchTitle('FOOWIP: foo'));
22+
}
23+
}

tests/Subscriber/NeedsReviewNewPRSubscriberTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function testOnPullRequestOpen()
4242

4343
$event = new GitHubEvent([
4444
'action' => 'opened',
45-
'pull_request' => ['number' => 1234],
45+
'pull_request' => ['number' => 1234, 'title' => 'foobar'],
4646
], $this->repository);
4747

4848
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
@@ -54,6 +54,23 @@ public function testOnPullRequestOpen()
5454
$this->assertSame(Status::NEEDS_REVIEW, $responseData['status_change']);
5555
}
5656

57+
public function testOnPullRequestOpenWIP()
58+
{
59+
$this->statusApi->expects($this->never())
60+
->method('setIssueStatus')
61+
->with(1234, Status::NEEDS_REVIEW);
62+
63+
$event = new GitHubEvent([
64+
'action' => 'opened',
65+
'pull_request' => ['number' => 1234, 'title' => '[WIP] foobar'],
66+
], $this->repository);
67+
68+
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
69+
70+
$responseData = $event->getResponseData();
71+
$this->assertEmpty($responseData);
72+
}
73+
5774
public function testOnPullRequestNotOpen()
5875
{
5976
$this->statusApi->expects($this->never())

0 commit comments

Comments
 (0)