Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ the first 20 hours.
If a PR is opened towards a branch that is not maintained anymore, Carson will
kindly explain to the author what to do.

### Add a warning if pull request description mismatch the targeted branch.

If a PR is opened towards a branch but the description does not match, Carson will
post a nice comment to explain to the author what to do.

### Open issues when docs for config reference is incomplete

The Symfony documentation includes some pages with "configuration reference", to
Expand Down
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ parameters:
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
- 'App\Subscriber\CloseDraftPRSubscriber'
- 'App\Subscriber\UnsupportedBranchSubscriber'
- 'App\Subscriber\MismatchBranchDescriptionSubscriber'
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
- 'App\Subscriber\RewriteUnwantedPhrasesSubscriber'
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
Expand Down Expand Up @@ -55,6 +56,7 @@ parameters:
- 'App\Subscriber\WelcomeFirstTimeContributorSubscriber'
- 'App\Subscriber\CloseDraftPRSubscriber'
- 'App\Subscriber\UnsupportedBranchSubscriber'
- 'App\Subscriber\MismatchBranchDescriptionSubscriber'
- 'App\Subscriber\RemoveStalledLabelOnCommentSubscriber'
- 'App\Subscriber\RewriteUnwantedPhrasesSubscriber'
- 'App\Subscriber\UpdateMilestoneWhenLabeledWaitingCodeMergeSubscriber'
Expand Down
94 changes: 94 additions & 0 deletions src/Subscriber/MismatchBranchDescriptionSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Subscriber;

use App\Api\Issue\IssueApi;
use App\Event\GitHubEvent;
use App\GitHubEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\String\UnicodeString;

/**
* @author Antoine Makdessi <[email protected]>
* @author Antoine Lamirault <[email protected]>
*/
class MismatchBranchDescriptionSubscriber implements EventSubscriberInterface
{
private IssueApi $issueApi;
private LoggerInterface $logger;

public function __construct(IssueApi $issueApi, LoggerInterface $logger)
{
$this->issueApi = $issueApi;
$this->logger = $logger;
}

public function onPullRequest(GitHubEvent $event): void
{
$data = $event->getData();
if (!in_array($data['action'], ['opened', 'ready_for_review']) || ($data['pull_request']['draft'] ?? false)) {
return;
}

$number = $data['pull_request']['number'];

$descriptionBranch = $this->extractDescriptionBranchFromBody($data['pull_request']['body']);
if (null === $descriptionBranch) {
$this->logger->notice('Pull Request without default template.', ['pull_request_number' => $number]);

return;
}

$targetBranch = $data['pull_request']['base']['ref'];
if ($targetBranch === $descriptionBranch) {
return;
}

$this->issueApi->commentOnIssue($event->getRepository(), $number, <<<TXT
Hey!

Thanks for your PR. You are targeting branch "$targetBranch" but it seems your PR description refers to branch "$descriptionBranch".
Could you update the PR description or change target branch? This helps core maintainers a lot.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps core maintainers a lot to triage PRs regarding if they are features or bugs


Cheers!

Carsonbot
TXT
);

$event->setResponseData([
'pull_request' => $number,
]);
}

public static function getSubscribedEvents(): array
{
return [
GitHubEvents::PULL_REQUEST => 'onPullRequest',
];
}

private function extractDescriptionBranchFromBody(string $body): ?string
{
$s = new UnicodeString($body);

// @see symfony/symfony/.github/PULL_REQUEST_TEMPLATE.md
if (!$s->containsAny('Branch?')) {
return null;
}

$rowsDescriptionBranch = $s->match('/.*Branch.*/');

$rowDescriptionBranch = $rowsDescriptionBranch[0]; // row matching

$descriptionBranchParts = \explode('|', $rowDescriptionBranch);
if (false === array_key_exists(2, $descriptionBranchParts)) { // Branch description is in second Markdown table column
return null;
}

$descriptionBranch = $descriptionBranchParts[2]; // get the version

return \trim($descriptionBranch);
}
}
185 changes: 185 additions & 0 deletions tests/Subscriber/MismatchBranchDescriptionSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

namespace App\Tests\Subscriber;

use App\Api\Issue\IssueApi;
use App\Api\Issue\NullIssueApi;
use App\Event\GitHubEvent;
use App\GitHubEvents;
use App\Model\Repository;
use App\Subscriber\MismatchBranchDescriptionSubscriber;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\EventDispatcher;

final class MismatchBranchDescriptionSubscriberTest extends TestCase
{
/**
* @var IssueApi
*/
private $issueApi;

/**
* @var Repository
*/
private $repository;

/**
* @var EventDispatcher
*/
private $dispatcher;

protected function setUp(): void
{
$this->issueApi = $this->createMock(NullIssueApi::class);

$subscriber = new MismatchBranchDescriptionSubscriber($this->issueApi, new NullLogger());
$this->repository = new Repository('carsonbot-playground', 'symfony', null);

$this->dispatcher = new EventDispatcher();
$this->dispatcher->addSubscriber($subscriber);
}

public function testOnPullRequestOpenMatch()
{
$this->issueApi->expects($this->never())
->method('commentOnIssue');

$body = <<<TXT
| Q | A
| ------------- | ---
| Branch? | 6.2
| Bug fix? | yes/no
TXT;

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'body' => $body,
'base' => [
'ref' => '6.2',
],
],
], $this->repository);

$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
$responseData = $event->getResponseData();

$this->assertCount(0, $responseData);
}

public function testOnPullRequestOpenNotMatch()
{
$this->issueApi->expects($this->once())
->method('commentOnIssue');

$body = <<<TXT
| Q | A
| ------------- | ---
| Branch? | 6.1
| Bug fix? | yes/no
TXT;

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'body' => $body,
'base' => [
'ref' => '6.2',
],
],
], $this->repository);

$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
$responseData = $event->getResponseData();

$this->assertCount(1, $responseData);
$this->assertSame(1234, $responseData['pull_request']);
}

public function testOnPullRequestOpenWithoutBranchRow()
{
$this->issueApi->expects($this->never())
->method('commentOnIssue');

$body = <<<TXT
| Q | A
| ------------- | ---
| Bug fix? | yes/no
TXT;

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'body' => $body,
'base' => [
'ref' => '6.2',
],
],
], $this->repository);

$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
$responseData = $event->getResponseData();

$this->assertCount(0, $responseData);
}

public function testOnPullRequestOpenBadBranchFormat()
{
$this->issueApi->expects($this->once())
->method('commentOnIssue');

$body = <<<TXT
| Q | A
| ------------- | ---
| Branch? | 6.2 for features / 4.4, 5.4, 6.0 or 6.1 for bug fixes <!-- see below -->
| Bug fix? | yes/no
TXT;

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'body' => $body,
'base' => [
'ref' => '6.2',
],
],
], $this->repository);

$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
$responseData = $event->getResponseData();

$this->assertCount(1, $responseData);
$this->assertSame(1234, $responseData['pull_request']);
}

public function testOnPullRequestOpenBranchNotInTable()
{
$this->issueApi->expects($this->never())
->method('commentOnIssue');

$body = <<<TXT
Branch? 6.2
TXT;

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'body' => $body,
'base' => [
'ref' => '6.2',
],
],
], $this->repository);

$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
$responseData = $event->getResponseData();

$this->assertCount(0, $responseData);
}
}