Skip to content

Commit a646014

Browse files
committed
Implementing the communication with GitHub for issue status updating
1 parent 668496b commit a646014

File tree

7 files changed

+285
-4
lines changed

7 files changed

+285
-4
lines changed

app/config/parameters.yml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ parameters:
2020

2121
# token used to update labels on the repository, etc
2222
github_token: XXXX
23+
# point to the symfony/symfony repository
24+
repository_username: symfony
25+
repository_name: symfony

app/config/services.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,16 @@ services:
77
app.github.status_manager:
88
class: AppBundle\GitHub\StatusManager
99
arguments: []
10+
11+
app.github_client:
12+
class: Github\Client
13+
arguments: []
14+
calls:
15+
- method: authenticate
16+
arguments:
17+
- http_token
18+
- %github_token%
19+
20+
app.issue_status_changer:
21+
class: AppBundle\GitHub\IssueStatusChanger
22+
arguments: ['@app.github_client', '%repository_username%', '%repository_name%']

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"symfony/monolog-bundle": "~2.4",
2020
"sensio/distribution-bundle": "~4.0",
2121
"sensio/framework-extra-bundle": "~3.0,>=3.0.2",
22-
"incenteev/composer-parameter-handler": "~2.0"
22+
"incenteev/composer-parameter-handler": "~2.0",
23+
"knplabs/github-api": "^1.4"
2324
},
2425
"require-dev": {
2526
"sensio/generator-bundle": "~2.3"

composer.lock

Lines changed: 157 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AppBundle/Controller/WebhookController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ private function handleIssueCommentEvent(array $data)
5050
$newStatus = $this->get('app.github.status_manager')
5151
->getStatusChangeFromComment($commentText);
5252

53-
// todo - send this status back to GitHub
53+
// hacky way to not actually try to talk to GitHub when testing
54+
if ($this->container->getParameter('kernel.environment') == 'test') {
55+
$this->get('app.issue_status_changer')
56+
->setIssueStatusLabel($issueNumber, $newStatus);
57+
}
5458

5559
return [
5660
'issue' => $issueNumber,
5761
'status_change' => $newStatus,
5862
];
5963
}
60-
6164
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace AppBundle\GitHub;
4+
5+
use Github\Client;
6+
use Github\Api\Issue\Labels;
7+
8+
class IssueStatusChanger
9+
{
10+
/**
11+
* @var Client
12+
*/
13+
private $githubClient;
14+
private $repositoryUsername;
15+
private $repositoryName;
16+
17+
public function __construct(Client $githubClient, $repositoryUsername, $repositoryName)
18+
{
19+
$this->githubClient = $githubClient;
20+
$this->repositoryUsername = $repositoryUsername;
21+
$this->repositoryName = $repositoryName;
22+
}
23+
24+
/**
25+
* @param integer $issueNumber The GitHub issue number
26+
* @param string $newStatus A StatusManager::STATUS_ constant
27+
*/
28+
public function setIssueStatusLabel($issueNumber, $newStatus)
29+
{
30+
$newLabel = StatusManager::getLabelForStatus($newStatus);
31+
32+
$currentLabels = $this->getIssueLabelsApi()->all(
33+
$this->repositoryUsername,
34+
$this->repositoryName,
35+
$issueNumber
36+
);
37+
38+
$labelMap = StatusManager::getLabelToStatusMap();
39+
foreach ($currentLabels as $currentLabel) {
40+
// is the label a "status label"? No? Then skip it
41+
if (!isset($labelMap[$currentLabel])) {
42+
continue;
43+
}
44+
45+
// if the label is already the new label, we don't need to do anything!
46+
if ($currentLabel == $newLabel) {
47+
return;
48+
}
49+
50+
// remove the old status label
51+
$this->getIssueLabelsApi()->remove(
52+
$this->repositoryUsername,
53+
$this->repositoryName,
54+
$issueNumber,
55+
$currentLabel
56+
);
57+
}
58+
59+
// add the new label
60+
$this->getIssueLabelsApi()->add(
61+
$this->repositoryUsername,
62+
$this->repositoryName,
63+
$issueNumber,
64+
$newLabel
65+
);
66+
}
67+
68+
/**
69+
* @return Labels
70+
*/
71+
private function getIssueLabelsApi()
72+
{
73+
return $this->githubClient->api('issue')->labels();
74+
}
75+
}

src/AppBundle/GitHub/StatusManager.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class StatusManager
1616
self::STATUS_REVIEWED => ['reviewed'],
1717
];
1818

19+
private static $labels = [
20+
self::STATUS_NEEDS_REVIEW => 'Status: Needs Review',
21+
self::STATUS_NEEDS_WORK => 'Status: Needs Work',
22+
self::STATUS_WORKS_FOR_ME => 'Status: Works for me',
23+
self::STATUS_REVIEWED => 'Status: Reviewed',
24+
];
25+
1926
/**
2027
* Parses the text of the comment and looks for keywords to see
2128
* if this should cause any status change.
@@ -54,6 +61,29 @@ public function getStatusChangeFromComment($comment)
5461
return $newStatus;
5562
}
5663

64+
/**
65+
* Returns the name of the label we use on GitHub for a status
66+
*
67+
* @param $status
68+
* @return string
69+
*/
70+
public static function getLabelForStatus($status)
71+
{
72+
if (!isset(self::$labels[$status])) {
73+
throw new \InvalidArgumentException(sprintf('Invalid status "%s"', $status));
74+
}
75+
76+
return self::$labels[$status];
77+
}
78+
79+
/**
80+
* @return array
81+
*/
82+
public static function getLabelToStatusMap()
83+
{
84+
return array_keys(self::$labels);
85+
}
86+
5787
/**
5888
* Finds the position where the status string will start - e.g.
5989
* for "Status: Needs review", this would return the position

0 commit comments

Comments
 (0)