Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Autolabel new Pull Requests #29

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 3 additions & 1 deletion 4 src/AppBundle/Controller/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ public function githubAction(Request $request)
$responseData = [
'pull_request' => $data['pull_request']['number'],
'status_change' => $listener->handlePullRequestCreatedEvent(
$data['pull_request']['number']
$data['pull_request']['number'],
$data['pull_request']['title'],
$data['pull_request']['body']
),
];
break;
Expand Down
14 changes: 14 additions & 0 deletions 14 src/AppBundle/Issues/GitHub/GitHubStatusApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ public function setIssueStatus($issueNumber, $newStatus)
}
}

/**
* Replaces the existing issue labels (if any) with the given array of
* new labels. Use an empty array to remove all the existing labels.
*
* @param int $issueNumber The GitHub issue number
* @param array $newLabels
*/
public function setIssueLabels($issueNumber, array $newLabels)
{
foreach ($newLabels as $label) {
$this->labelsApi->addIssueLabel($issueNumber, $label);
}
}

public function getIssueStatus($issueNumber)
{
$currentLabels = $this->labelsApi->getIssueLabels($issueNumber);
Expand Down
65 changes: 63 additions & 2 deletions 65 src/AppBundle/Issues/IssueListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,39 @@ public function handleCommentAddedEvent($issueNumber, $comment)
* Adds a "Needs Review" label to new PRs.
*
* @param int $prNumber The number of the PR
* @param int $prTitle The title of the PR
* @param int $prBody The full text description of the PR
*
* @return string The new status
*/
public function handlePullRequestCreatedEvent($prNumber)
public function handlePullRequestCreatedEvent($prNumber, $prTitle, $prBody)
Copy link
Contributor

Choose a reason for hiding this comment

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

If we listen to this event, we should also listen for changes to the title/body after the PR was created.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably ... but we can leave that enhancement for the future.

{
$prLabels = array();

// new PRs always require review
$newStatus = Status::NEEDS_REVIEW;
$prLabels[] = $newStatus;

$this->statusApi->setIssueStatus($prNumber, $newStatus);
// the PR title usually contains one or more labels
foreach ($this->extractLabels($prTitle) as $label) {
$prLabels[] = $label;
}

// the PR body usually indicates if this is a Bug, Feature, BC Break or Deprecation
if (preg_match('/^\|\s*Bug fix?\s*\|\s*yes\s*$/', $prBody, $matches)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

\s* should be replaced by \s+ around the middle |

Copy link
Contributor Author

Choose a reason for hiding this comment

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

\s* gives us more flexible "for free".

Copy link
Contributor

Choose a reason for hiding this comment

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

\s* is incorrect, there is no reason to allow incorrect flexibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is my reasoning. If some user writes this by mistake:

| Bug fix? |yes

\s* will work but \s+ won't. I can be wrong of course.

$prLabels[] = 'Bug';
}
if (preg_match('/^\|\s*New feature?\s*\|\s*yes\s*$/', $prBody, $matches)) {
$prLabels[] = 'Feature';
}
Copy link
Contributor

Choose a reason for hiding this comment

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

do we want to allow both Bug and Feature labels on one PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Here it is an example: symfony/symfony#17458

if (preg_match('/^\|\s*BC breaks?\s*\|\s*yes\s*$/', $prBody, $matches)) {
$prLabels[] = 'BC Break';
}
if (preg_match('/^\|\s*Deprecations?\s*\|\s*yes\s*$/', $prBody, $matches)) {
$prLabels[] = 'Deprecation';
}

$this->statusApi->setIssueLabels($prNumber, $prLabels);

return $newStatus;
}
Expand Down Expand Up @@ -98,4 +123,40 @@ public function handleLabelAddedEvent($issueNumber, $label)

return $newStatus;
}

private function extractLabels($prTitle)
{
$labels = array();

// e.g. "[PropertyAccess] [RFC] [WIP] Allow custom methods on property accesses"
if (preg_match_all('/\[(?P<labels>.+)\]/U', $prTitle, $matches)) {
foreach ($matches['labels'] as $label) {
if (in_array($label, $this->getValidLabels())) {
$labels[] = $label;
}
}
}

return $labels;
}

/**
* TODO: get valid labels from the repository via GitHub API
*/
private function getValidLabels()
{
return array(
'Asset', 'BC Break', 'BrowserKit', 'Bug', 'Cache', 'ClassLoader',
'Config', 'Console', 'Critical', 'CssSelector', 'Debug', 'DebugBundle',
'DependencyInjection', 'Deprecation', 'Doctrine', 'DoctrineBridge',
'DomCrawler', 'Drupal related', 'DX', 'Easy Pick', 'Enhancement',
'EventDispatcher', 'ExpressionLanguage', 'Feature', 'Filesystem',
'Finder', 'Form', 'FrameworkBundle', 'HttpFoundation', 'HttpKernel',
'Intl', 'Ldap', 'Locale', 'MonologBridge', 'OptionsResolver',
'PhpUnitBridge', 'Process', 'PropertyAccess', 'PropertyInfo', 'Ready',
'RFC', 'Routing', 'Security', 'SecurityBundle', 'Serializer',
'Stopwatch', 'Templating', 'Translator', 'TwigBridge', 'TwigBundle',
'Unconfirmed', 'Validator', 'VarDumper', 'WebProfilerBundle', 'Yaml',
);
}
}
4 changes: 4 additions & 0 deletions 4 src/AppBundle/Issues/NullStatusApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public function setIssueStatus($issueNumber, $newStatus)
{
}

public function setIssueLabels($issueNumber, array $newLabels)
{
}

public function getNeedsReviewUrl()
{
}
Expand Down
2 changes: 2 additions & 0 deletions 2 src/AppBundle/Issues/StatusApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public function getIssueStatus($issueNumber);

public function setIssueStatus($issueNumber, $newStatus);

public function setIssueLabels($issueNumber, array $newLabels);

public function getNeedsReviewUrl();
}
6 changes: 3 additions & 3 deletions 6 src/AppBundle/Tests/Issues/IssueListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ public function getCommentsForStatusChange()
public function testHandlePullRequestCreatedEvent()
{
$this->statusApi->expects($this->once())
->method('setIssueStatus')
->with(1234, Status::NEEDS_REVIEW);
->method('setIssueLabels')
->with(1234, array(Status::NEEDS_REVIEW));

$newStatus = $this->listener->handlePullRequestCreatedEvent(1234);
$newStatus = $this->listener->handlePullRequestCreatedEvent(1234, 'The title', 'The description.');

$this->assertSame(Status::NEEDS_REVIEW, $newStatus);
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.