Skip to content

Commit 32b8076

Browse files
committed
Autolabel new Pull Requests
1 parent 26984df commit 32b8076

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

src/AppBundle/Controller/WebhookController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function githubAction(Request $request)
4040
$responseData = [
4141
'pull_request' => $data['pull_request']['number'],
4242
'status_change' => $listener->handlePullRequestCreatedEvent(
43-
$data['pull_request']['number']
43+
$data['pull_request']['number'],
44+
$data['pull_request']['title'],
45+
$data['pull_request']['body']
4446
),
4547
];
4648
break;

src/AppBundle/Issues/GitHub/GitHubStatusApi.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,20 @@ public function setIssueStatus($issueNumber, $newStatus)
7878
}
7979
}
8080

81+
/**
82+
* Replaces the existing issue labels (if any) with the given array of
83+
* new labels. Use an empty array to remove all the existing labels.
84+
*
85+
* @param int $issueNumber The GitHub issue number
86+
* @param array $newLabels
87+
*/
88+
public function setIssueLabels($issueNumber, array $newLabels)
89+
{
90+
foreach ($newLabels as $label) {
91+
$this->labelsApi->addIssueLabel($issueNumber, $label);
92+
}
93+
}
94+
8195
public function getIssueStatus($issueNumber)
8296
{
8397
$currentLabels = $this->labelsApi->getIssueLabels($issueNumber);

src/AppBundle/Issues/IssueListener.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,39 @@ public function handleCommentAddedEvent($issueNumber, $comment)
5858
* Adds a "Needs Review" label to new PRs.
5959
*
6060
* @param int $prNumber The number of the PR
61+
* @param int $prTitle The title of the PR
62+
* @param int $prBody The full text description of the PR
6163
*
6264
* @return string The new status
6365
*/
64-
public function handlePullRequestCreatedEvent($prNumber)
66+
public function handlePullRequestCreatedEvent($prNumber, $prTitle, $prBody)
6567
{
68+
$prLabels = array();
69+
70+
// new PRs always require review
6671
$newStatus = Status::NEEDS_REVIEW;
72+
$prLabels[] = $newStatus;
73+
74+
// the PR title usually indicates the affected component
75+
if (preg_match('/^\[(?P<component>.*)\] .*/$', $prTitle, $matches)) {
76+
$prLabels[] = $matches['component'];
77+
}
78+
79+
// the PR body usually indicates if this is a Bug, Feature, BC Break or Deprecation
80+
if (preg_match('/^\|\s*Bug fix?\s*\|\s*yes\s*$/', $prBody, $matches)) {
81+
$prLabels[] = 'Bug';
82+
}
83+
if (preg_match('/^\|\s*New feature?\s*\|\s*yes\s*$/', $prBody, $matches)) {
84+
$prLabels[] = 'Feature';
85+
}
86+
if (preg_match('/^\|\s*BC breaks?\s*\|\s*yes\s*$/', $prBody, $matches)) {
87+
$prLabels[] = 'BC Break';
88+
}
89+
if (preg_match('/^\|\s*Deprecations?\s*\|\s*yes\s*$/', $prBody, $matches)) {
90+
$prLabels[] = 'Deprecation';
91+
}
6792

68-
$this->statusApi->setIssueStatus($prNumber, $newStatus);
93+
$this->statusApi->setIssueLabels($prNumber, $prLabels);
6994

7095
return $newStatus;
7196
}

src/AppBundle/Issues/NullStatusApi.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ public function setIssueStatus($issueNumber, $newStatus)
1515
{
1616
}
1717

18+
public function setIssueLabels($issueNumber, array $newLabels)
19+
{
20+
}
21+
1822
public function getNeedsReviewUrl()
1923
{
2024
}

src/AppBundle/Issues/StatusApi.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public function getIssueStatus($issueNumber);
1313

1414
public function setIssueStatus($issueNumber, $newStatus);
1515

16+
public function setIssueLabels($issueNumber, array $newLabels);
17+
1618
public function getNeedsReviewUrl();
1719
}

0 commit comments

Comments
 (0)