Skip to content

Commit 9727da9

Browse files
committed
[#4] Auto-adding "Needs Review" to un-status-labeled issues that receieve the "Bug" label
1 parent 182080b commit 9727da9

File tree

6 files changed

+383
-2
lines changed

6 files changed

+383
-2
lines changed

src/AppBundle/Controller/WebhookController.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ public function githubAction(Request $request)
3939
];
4040
}
4141
break;
42+
case 'issues':
43+
switch ($data['action']) {
44+
case 'labeled':
45+
$responseData = $this->handleIssueLabelAdd($data);
46+
break;
47+
default:
48+
$responseData = [
49+
'unsupported_action' => $data['action']
50+
];
51+
}
52+
break;
4253
default:
4354
$responseData = [
4455
'unsupported_event' => $event
@@ -90,4 +101,30 @@ private function handlePullRequestCreateEvent(array $data)
90101
'status_change' => $newStatus,
91102
];
92103
}
104+
105+
/**
106+
* Changes "Bug" issues to "Needs Review"
107+
*/
108+
private function handleIssueLabelAdd(array $data)
109+
{
110+
$issueNumber = $data['issue']['number'];
111+
$newLabel = $data['label']['name'];
112+
113+
if ($newLabel == 'bug') {
114+
$newStatus = StatusManager::STATUS_NEEDS_REVIEW;
115+
116+
// hacky way to not actually try to talk to GitHub when testing
117+
if ($this->container->getParameter('kernel.environment') != 'test') {
118+
$this->get('app.issue_status_changer')
119+
->setIssueStatusLabel($issueNumber, $newStatus, false);
120+
}
121+
} else {
122+
$newStatus = null;
123+
}
124+
125+
return [
126+
'issue' => $issueNumber,
127+
'status_change' => $newStatus,
128+
];
129+
}
93130
}

src/AppBundle/GitHub/IssueStatusChanger.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ public function __construct(Client $githubClient, $repositoryUsername, $reposito
2323

2424
/**
2525
* @param integer $issueNumber The GitHub issue number
26-
* @param string $newStatus A StatusManager::STATUS_ constant
26+
* @param string $newStatus A StatusManager::STATUS_ constant
27+
* @param bool $replaceExistingStatus If true, the status WILL be set. If false, it's only
28+
* set if there isn't already a "status" label
2729
*/
28-
public function setIssueStatusLabel($issueNumber, $newStatus)
30+
public function setIssueStatusLabel($issueNumber, $newStatus, $replaceExistingStatus = true)
2931
{
3032
$newLabel = StatusManager::getLabelForStatus($newStatus);
3133

@@ -53,6 +55,13 @@ public function setIssueStatusLabel($issueNumber, $newStatus)
5355
continue;
5456
}
5557

58+
if (!$replaceExistingStatus) {
59+
// there IS an existing status, but we should not replace it
60+
// in fact, we should do nothing - leave the current status
61+
62+
return;
63+
}
64+
5665
// remove the old status label
5766
$this->getIssueLabelsApi()->remove(
5867
$this->repositoryUsername,

src/AppBundle/Tests/Controller/WebhookControllerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ public function getTests()
3636
'pull_request.opened.json',
3737
array('status_change' => 'needs_review', 'pull_request' => 3)
3838
);
39+
$tests[] = array(
40+
'issues',
41+
'issues.labeled.bug.json',
42+
array('status_change' => 'needs_review', 'issue' => 5)
43+
);
44+
$tests[] = array(
45+
'issues',
46+
'issues.labeled.feature.json',
47+
array('status_change' => null, 'issue' => 5)
48+
);
3949

4050
return $tests;
4151
}

src/AppBundle/Tests/GitHub/IssueStatusChangerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public function testIntegrationSetIssueStatusLabel()
2222
$testIssueNumber,
2323
array(StatusManager::getLabelForStatus(StatusManager::STATUS_NEEDS_WORK))
2424
);
25+
26+
// try to set it, but don't "replace" it. So, nothing will happen
27+
$issueStatusChanger->setIssueStatusLabel($testIssueNumber, StatusManager::STATUS_REVIEWED, false);
28+
$this->assertIssueLabels(
29+
$testIssueNumber,
30+
array(StatusManager::getLabelForStatus(StatusManager::STATUS_NEEDS_WORK))
31+
);
2532
}
2633

2734
/**
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
{
2+
"action": "labeled",
3+
"issue": {
4+
"url": "https://api.github.com/repos/weaverryan/symfony/issues/5",
5+
"labels_url": "https://api.github.com/repos/weaverryan/symfony/issues/5/labels{/name}",
6+
"comments_url": "https://api.github.com/repos/weaverryan/symfony/issues/5/comments",
7+
"events_url": "https://api.github.com/repos/weaverryan/symfony/issues/5/events",
8+
"html_url": "https://github.com/weaverryan/symfony/issues/5",
9+
"id": 95027129,
10+
"number": 5,
11+
"title": "Brand new issue",
12+
"user": {
13+
"login": "weaverryan",
14+
"id": 121003,
15+
"avatar_url": "https://avatars.githubusercontent.com/u/121003?v=3",
16+
"gravatar_id": "",
17+
"url": "https://api.github.com/users/weaverryan",
18+
"html_url": "https://github.com/weaverryan",
19+
"followers_url": "https://api.github.com/users/weaverryan/followers",
20+
"following_url": "https://api.github.com/users/weaverryan/following{/other_user}",
21+
"gists_url": "https://api.github.com/users/weaverryan/gists{/gist_id}",
22+
"starred_url": "https://api.github.com/users/weaverryan/starred{/owner}{/repo}",
23+
"subscriptions_url": "https://api.github.com/users/weaverryan/subscriptions",
24+
"organizations_url": "https://api.github.com/users/weaverryan/orgs",
25+
"repos_url": "https://api.github.com/users/weaverryan/repos",
26+
"events_url": "https://api.github.com/users/weaverryan/events{/privacy}",
27+
"received_events_url": "https://api.github.com/users/weaverryan/received_events",
28+
"type": "User",
29+
"site_admin": false
30+
},
31+
"labels": [
32+
{
33+
"url": "https://api.github.com/repos/weaverryan/symfony/labels/bug",
34+
"name": "bug",
35+
"color": "fc2929"
36+
}
37+
],
38+
"state": "open",
39+
"locked": false,
40+
"assignee": null,
41+
"milestone": null,
42+
"comments": 0,
43+
"created_at": "2015-07-14T20:00:15Z",
44+
"updated_at": "2015-07-14T20:00:19Z",
45+
"closed_at": null,
46+
"body": ""
47+
},
48+
"label": {
49+
"url": "https://api.github.com/repos/weaverryan/symfony/labels/bug",
50+
"name": "bug",
51+
"color": "fc2929"
52+
},
53+
"repository": {
54+
"id": 21151556,
55+
"name": "symfony",
56+
"full_name": "weaverryan/symfony",
57+
"owner": {
58+
"login": "weaverryan",
59+
"id": 121003,
60+
"avatar_url": "https://avatars.githubusercontent.com/u/121003?v=3",
61+
"gravatar_id": "",
62+
"url": "https://api.github.com/users/weaverryan",
63+
"html_url": "https://github.com/weaverryan",
64+
"followers_url": "https://api.github.com/users/weaverryan/followers",
65+
"following_url": "https://api.github.com/users/weaverryan/following{/other_user}",
66+
"gists_url": "https://api.github.com/users/weaverryan/gists{/gist_id}",
67+
"starred_url": "https://api.github.com/users/weaverryan/starred{/owner}{/repo}",
68+
"subscriptions_url": "https://api.github.com/users/weaverryan/subscriptions",
69+
"organizations_url": "https://api.github.com/users/weaverryan/orgs",
70+
"repos_url": "https://api.github.com/users/weaverryan/repos",
71+
"events_url": "https://api.github.com/users/weaverryan/events{/privacy}",
72+
"received_events_url": "https://api.github.com/users/weaverryan/received_events",
73+
"type": "User",
74+
"site_admin": false
75+
},
76+
"private": false,
77+
"html_url": "https://github.com/weaverryan/symfony",
78+
"description": "The Symfony PHP framework",
79+
"fork": true,
80+
"url": "https://api.github.com/repos/weaverryan/symfony",
81+
"forks_url": "https://api.github.com/repos/weaverryan/symfony/forks",
82+
"keys_url": "https://api.github.com/repos/weaverryan/symfony/keys{/key_id}",
83+
"collaborators_url": "https://api.github.com/repos/weaverryan/symfony/collaborators{/collaborator}",
84+
"teams_url": "https://api.github.com/repos/weaverryan/symfony/teams",
85+
"hooks_url": "https://api.github.com/repos/weaverryan/symfony/hooks",
86+
"issue_events_url": "https://api.github.com/repos/weaverryan/symfony/issues/events{/number}",
87+
"events_url": "https://api.github.com/repos/weaverryan/symfony/events",
88+
"assignees_url": "https://api.github.com/repos/weaverryan/symfony/assignees{/user}",
89+
"branches_url": "https://api.github.com/repos/weaverryan/symfony/branches{/branch}",
90+
"tags_url": "https://api.github.com/repos/weaverryan/symfony/tags",
91+
"blobs_url": "https://api.github.com/repos/weaverryan/symfony/git/blobs{/sha}",
92+
"git_tags_url": "https://api.github.com/repos/weaverryan/symfony/git/tags{/sha}",
93+
"git_refs_url": "https://api.github.com/repos/weaverryan/symfony/git/refs{/sha}",
94+
"trees_url": "https://api.github.com/repos/weaverryan/symfony/git/trees{/sha}",
95+
"statuses_url": "https://api.github.com/repos/weaverryan/symfony/statuses/{sha}",
96+
"languages_url": "https://api.github.com/repos/weaverryan/symfony/languages",
97+
"stargazers_url": "https://api.github.com/repos/weaverryan/symfony/stargazers",
98+
"contributors_url": "https://api.github.com/repos/weaverryan/symfony/contributors",
99+
"subscribers_url": "https://api.github.com/repos/weaverryan/symfony/subscribers",
100+
"subscription_url": "https://api.github.com/repos/weaverryan/symfony/subscription",
101+
"commits_url": "https://api.github.com/repos/weaverryan/symfony/commits{/sha}",
102+
"git_commits_url": "https://api.github.com/repos/weaverryan/symfony/git/commits{/sha}",
103+
"comments_url": "https://api.github.com/repos/weaverryan/symfony/comments{/number}",
104+
"issue_comment_url": "https://api.github.com/repos/weaverryan/symfony/issues/comments{/number}",
105+
"contents_url": "https://api.github.com/repos/weaverryan/symfony/contents/{+path}",
106+
"compare_url": "https://api.github.com/repos/weaverryan/symfony/compare/{base}...{head}",
107+
"merges_url": "https://api.github.com/repos/weaverryan/symfony/merges",
108+
"archive_url": "https://api.github.com/repos/weaverryan/symfony/{archive_format}{/ref}",
109+
"downloads_url": "https://api.github.com/repos/weaverryan/symfony/downloads",
110+
"issues_url": "https://api.github.com/repos/weaverryan/symfony/issues{/number}",
111+
"pulls_url": "https://api.github.com/repos/weaverryan/symfony/pulls{/number}",
112+
"milestones_url": "https://api.github.com/repos/weaverryan/symfony/milestones{/number}",
113+
"notifications_url": "https://api.github.com/repos/weaverryan/symfony/notifications{?since,all,participating}",
114+
"labels_url": "https://api.github.com/repos/weaverryan/symfony/labels{/name}",
115+
"releases_url": "https://api.github.com/repos/weaverryan/symfony/releases{/id}",
116+
"created_at": "2014-06-24T04:00:07Z",
117+
"updated_at": "2015-06-28T12:37:42Z",
118+
"pushed_at": "2015-06-28T15:31:08Z",
119+
"git_url": "git://github.com/weaverryan/symfony.git",
120+
"ssh_url": "git@github.com:weaverryan/symfony.git",
121+
"clone_url": "https://github.com/weaverryan/symfony.git",
122+
"svn_url": "https://github.com/weaverryan/symfony",
123+
"homepage": "symfony.com",
124+
"size": 55804,
125+
"stargazers_count": 0,
126+
"watchers_count": 0,
127+
"language": "PHP",
128+
"has_issues": true,
129+
"has_downloads": false,
130+
"has_wiki": false,
131+
"has_pages": false,
132+
"forks_count": 0,
133+
"mirror_url": null,
134+
"open_issues_count": 4,
135+
"forks": 0,
136+
"open_issues": 4,
137+
"watchers": 0,
138+
"default_branch": "master"
139+
},
140+
"sender": {
141+
"login": "weaverryan",
142+
"id": 121003,
143+
"avatar_url": "https://avatars.githubusercontent.com/u/121003?v=3",
144+
"gravatar_id": "",
145+
"url": "https://api.github.com/users/weaverryan",
146+
"html_url": "https://github.com/weaverryan",
147+
"followers_url": "https://api.github.com/users/weaverryan/followers",
148+
"following_url": "https://api.github.com/users/weaverryan/following{/other_user}",
149+
"gists_url": "https://api.github.com/users/weaverryan/gists{/gist_id}",
150+
"starred_url": "https://api.github.com/users/weaverryan/starred{/owner}{/repo}",
151+
"subscriptions_url": "https://api.github.com/users/weaverryan/subscriptions",
152+
"organizations_url": "https://api.github.com/users/weaverryan/orgs",
153+
"repos_url": "https://api.github.com/users/weaverryan/repos",
154+
"events_url": "https://api.github.com/users/weaverryan/events{/privacy}",
155+
"received_events_url": "https://api.github.com/users/weaverryan/received_events",
156+
"type": "User",
157+
"site_admin": false
158+
}
159+
}

0 commit comments

Comments
 (0)