-
Notifications
You must be signed in to change notification settings - Fork 31
Add PR message if branch desc mismatch branch target #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
weaverryan
merged 1 commit into
symfony-tools:master
from
alamirault:feature/176-detect-target-branch-mismatch
Oct 31, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
185
tests/Subscriber/MismatchBranchDescriptionSubscriberTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.