Skip to content

Commit 65f7dfa

Browse files
committed
Bugfix with auto label
1 parent ab28239 commit 65f7dfa

File tree

2 files changed

+24
-63
lines changed

2 files changed

+24
-63
lines changed

src/Subscriber/AutoUpdateTitleWithLabelSubscriber.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,12 @@ public function onPullRequest(GitHubEvent $event)
5050
$lock = $this->lockFactory->createLock($repository->getFullName().'#'.$number);
5151
$lock->acquire(true); // blocking. Lock will be released at __destruct
5252

53-
$originalTitle = $prTitle = trim($data['pull_request']['title']);
53+
// Fetch the current PR just to make sure it has not changed
54+
$githubPullRequest = $this->pullRequestApi->show($repository, $number);
55+
$originalTitle = $prTitle = trim($githubPullRequest['title']);
5456
$validLabels = [];
55-
foreach ($data['pull_request']['labels'] as $label) {
57+
58+
foreach ($githubPullRequest['labels'] ?? [] as $label) {
5659
if ('dddddd' === strtolower($label['color'])) {
5760
$validLabels[] = $label['name'];
5861
// Remove label name from title
@@ -82,11 +85,6 @@ public function onPullRequest(GitHubEvent $event)
8285
return;
8386
}
8487

85-
// Refetch the current title just to make sure it has not changed
86-
if ($prTitle === ($this->pullRequestApi->show($repository, $number)['title'] ?? '')) {
87-
return;
88-
}
89-
9088
$this->pullRequestApi->updateTitle($repository, $number, $prTitle);
9189
$event->setResponseData([
9290
'pull_request' => $number,

tests/Subscriber/AutoUpdateTitleWithLabelSubscriberTest.php

Lines changed: 19 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,14 @@ protected function setUp()
4545

4646
public function testOnPullRequestLabeled()
4747
{
48-
$event = new GitHubEvent([
49-
'action' => 'labeled',
50-
'number' => 1234,
51-
'pull_request' => [
52-
'title' => '[fwb][bar] Foo',
53-
'labels' => [
54-
['name' => 'FrameworkBundle', 'color' => 'dddddd'],
55-
['name' => 'Console', 'color' => 'dddddd'],
56-
],
48+
$event = new GitHubEvent(['action' => 'labeled', 'number' => 1234, 'pull_request'=>[]], $this->repository);
49+
$this->pullRequestApi->method('show')->willReturn([
50+
'title' => '[fwb][bar] Foo',
51+
'labels' => [
52+
['name' => 'FrameworkBundle', 'color' => 'dddddd'],
53+
['name' => 'Console', 'color' => 'dddddd'],
5754
],
58-
], $this->repository);
55+
]);
5956

6057
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
6158
$responseData = $event->getResponseData();
@@ -67,16 +64,13 @@ public function testOnPullRequestLabeled()
6764

6865
public function testOnPullRequestLabeledCaseInsensitive()
6966
{
70-
$event = new GitHubEvent([
71-
'action' => 'labeled',
72-
'number' => 1234,
73-
'pull_request' => [
74-
'title' => '[PHPunitbridge] Foo',
75-
'labels' => [
76-
['name' => 'PhpUnitBridge', 'color' => 'dddddd'],
77-
],
67+
$event = new GitHubEvent(['action' => 'labeled', 'number' => 1234, 'pull_request'=>[]], $this->repository);
68+
$this->pullRequestApi->method('show')->willReturn([
69+
'title' => '[PHPunitbridge] Foo',
70+
'labels' => [
71+
['name' => 'PhpUnitBridge', 'color' => 'dddddd'],
7872
],
79-
], $this->repository);
73+
]);
8074

8175
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
8276
$responseData = $event->getResponseData();
@@ -88,42 +82,14 @@ public function testOnPullRequestLabeledCaseInsensitive()
8882

8983
public function testOnPullRequestLabeledWithExisting()
9084
{
91-
$event = new GitHubEvent([
92-
'action' => 'labeled',
93-
'number' => 1234,
94-
'pull_request' => [
85+
$event = new GitHubEvent(['action' => 'labeled', 'number' => 1234, 'pull_request'=>[]], $this->repository);
86+
$this->pullRequestApi->method('show')->willReturn([
9587
'title' => '[Messenger] Fix JSON',
9688
'labels' => [
9789
['name' => 'Status: Needs Review', 'color' => 'abcabc'],
9890
['name' => 'Messenger', 'color' => 'dddddd'],
9991
],
100-
],
101-
], $this->repository);
102-
103-
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
104-
$responseData = $event->getResponseData();
105-
$this->assertEmpty($responseData);
106-
}
107-
108-
/**
109-
* If a user add two labels at the same time. We will get two webhooks simultaneously.
110-
* We need to make sure that when we request the Github API it will return the changes
111-
* from the first webhook.
112-
*/
113-
public function testOnPullRequestLabeledTwice()
114-
{
115-
$this->pullRequestApi->method('show')->willReturn(['title' => '[Console][FrameworkBundle] Foo normal title']);
116-
$event = new GitHubEvent([
117-
'action' => 'labeled',
118-
'number' => 1234,
119-
'pull_request' => [
120-
'title' => 'Foo normal title',
121-
'labels' => [
122-
['name' => 'FrameworkBundle', 'color' => 'dddddd'],
123-
['name' => 'Console', 'color' => 'dddddd'],
124-
],
125-
],
126-
], $this->repository);
92+
]);
12793

12894
$this->dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);
12995
$responseData = $event->getResponseData();
@@ -132,16 +98,13 @@ public function testOnPullRequestLabeledTwice()
13298

13399
public function testRemoveLabel()
134100
{
135-
$event = new GitHubEvent([
136-
'action' => 'labeled',
137-
'number' => 1234,
138-
'pull_request' => [
101+
$event = new GitHubEvent(['action' => 'labeled', 'number' => 1234, 'pull_request'=>[]], $this->repository);
102+
$this->pullRequestApi->method('show')->willReturn([
139103
'title' => '[Console][FrameworkBundle] [Random] Foo normal title',
140104
'labels' => [
141105
['name' => 'Console', 'color' => 'dddddd'],
142106
],
143-
],
144-
], $this->repository);
107+
]);
145108

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

0 commit comments

Comments
 (0)