-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gitter.php
224 lines (197 loc) · 8.97 KB
/
Gitter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace thebuggenie\modules\gitter;
use thebuggenie\core\entities\Issue;
use thebuggenie\core\entities\Milestone;
use thebuggenie\core\entities\Project;
use thebuggenie\core\framework,
GuzzleHttp\Client as GuzzleHttpClient,
GuzzleHttp\Psr7\Request as GuzzleHttpRequest;
/**
* Gitter module for integrating with Gitter
*
* @author
* @version 1.0
* @license http://opensource.org/licenses/MPL-2.0 Mozilla Public License 2.0 (MPL 2.0)
* @package gitter
* @subpackage core
*/
/**
* Gitter module for integrating with Gitter
*
* @package gitter
* @subpackage core
*
* @Table(name="\thebuggenie\core\entities\tables\Modules")
*/
class Gitter extends \thebuggenie\core\entities\Module
{
const VERSION = '1.0';
const SETTING_PROJECT_WEBHOOK_URL = 'project_webhook_url_';
const SETTING_PROJECT_INTEGRATION_ENABLED = 'project_integration_enabled_';
const SETTING_PROJECT_POST_ON_NEW_ISSUES = 'project_post_to_channel_on_new_issues_';
const SETTING_PROJECT_POST_ON_NEW_COMMENTS = 'project_post_to_channel_on_new_comments_';
protected $_has_config_settings = false;
protected $_name = 'gitter';
protected $_longname = 'Gitter integration';
protected $_description = 'Gitter description here';
protected $_module_config_title = 'Gitter integration';
protected $_module_config_description = 'Configure the Gitter integration';
/**
* Return an instance of this module
*
* @return Gitter
*/
public static function getModule()
{
return framework\Context::getModule('gitter');
}
protected function _initialize()
{
require THEBUGGENIE_MODULES_PATH . 'gitter' . DS . 'vendor' . DS . 'autoload.php';
}
/**
* @param integer $project_id
* @return GuzzleHttpClient
*/
protected function _getProjectClient($project_id)
{
$client = new GuzzleHttpClient(['base_uri' => $this->getProjectWebhookUrl($project_id)]);
return $client;
}
/**
* Listener for issue creation
*
* @Listener(module="core", identifier="thebuggenie\core\entities\Issue::createNew")
* @param framework\Event $event
*/
public function listen_issueCreate(framework\Event $event)
{
framework\Context::loadLibrary('common');
$issue = $event->getSubject();
$project_id = $issue->getProjectID();
if ($this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewIssues($project_id))
{
$issueUrl = framework\Context::getRouting()->generate('viewissue', array('project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getFormattedIssueNo()), false);
$projectUrl = framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $issue->getProject()->getKey()), false);
$client = $this->_getProjectClient($project_id);
$response = $client->post($this->getProjectWebhookUrl($project_id), [
'timeout' => 10,
'json' => [
'event_key' => 'issue_create',
'user' => [
'name' => $issue->getPostedBy()->getDisplayName(),
'username' => $issue->getPostedBy()->getUsername()
],
'issue' => [
'id' => $issue->getId(),
'title' => $issue->getFormattedIssueNo(true, true),
'url' => $issueUrl
],
'project' => [
'name' => $issue->getProject()->getName(),
'url' => $projectUrl
]
]
]);
}
}
/**
* Listener for the comment post
*
* @Listener(module="core", identifier="thebuggenie\core\entities\Comment::_postSave")
* @param framework\Event $event
*/
public function listen_issueComment(framework\Event $event)
{
framework\Context::loadLibrary('common');
if (!$event->getParameter('issue') instanceof Issue)
return;
$comment = $event->getSubject();
$issue = $event->getParameter('issue');
$project_id = $issue->getProjectID();
if ($this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewComments($project_id))
{
$issueUrl = framework\Context::getRouting()->generate('viewissue', array('project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getFormattedIssueNo()), false);
$issueUrl .= '#comment_' . $comment->getId();
$projectUrl = framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $issue->getProject()->getKey()), false);
$client = $this->_getProjectClient($project_id);
$response = $client->post($this->getProjectWebhookUrl($project_id), [
'timeout' => 10,
'json' => [
'event_key' => 'issue_comment',
'user' => [
'name' => $issue->getPostedBy()->getDisplayName(),
'username' => $issue->getPostedBy()->getUsername()
],
'issue' => [
'id' => $issue->getId(),
'title' => $issue->getFormattedIssueNo(true, true),
'url' => $issueUrl
],
'project' => [
'name' => $issue->getProject()->getName(),
'url' => $projectUrl
]
]
]);
}
}
protected function _addListeners()
{
framework\Event::listen('core', 'thebuggenie\core\entities\Issue::createNew', array($this, 'listen_issueCreate'));
framework\Event::listen('core', 'thebuggenie\core\entities\Comment::_postSave', array($this, 'listen_issueComment'));
framework\Event::listen('core', 'config_project_tabs_other', array($this, 'listen_projectconfig_tab'));
framework\Event::listen('core', 'config_project_panes', array($this, 'listen_projectconfig_panel'));
}
public function listen_projectconfig_tab(framework\Event $event)
{
include_component('gitter/projectconfig_tab', array('selected_tab' => $event->getParameter('selected_tab'), 'module' => $this));
}
public function listen_projectconfig_panel(framework\Event $event)
{
include_component('gitter/projectconfig_panel', array('selected_tab' => $event->getParameter('selected_tab'), 'access_level' => $event->getParameter('access_level'), 'project' => $event->getParameter('project'), 'module' => $this));
}
protected function _install($scope)
{
}
protected function _loadFixtures($scope)
{
}
protected function _uninstall()
{
}
public function getProjectWebhookUrl($project_id)
{
return $this->getSetting(self::SETTING_PROJECT_WEBHOOK_URL . $project_id);
}
public function setProjectWebhookUrl($project_id, $value)
{
return $this->saveSetting(self::SETTING_PROJECT_WEBHOOK_URL . $project_id, $value);
}
public function isProjectIntegrationEnabled($project_id)
{
return (bool) $this->getSetting(self::SETTING_PROJECT_INTEGRATION_ENABLED . $project_id);
}
public function setProjectIntegrationEnabled($project_id, $value)
{
return $this->saveSetting(self::SETTING_PROJECT_INTEGRATION_ENABLED . $project_id, $value);
}
public function doesPostOnNewIssues($project_id, $value = null)
{
if ($value !== null) {
return $this->saveSetting(self::SETTING_PROJECT_POST_ON_NEW_ISSUES . $project_id, (bool) $value);
} else {
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ON_NEW_ISSUES . $project_id);
return (isset($setting)) ? $setting : true;
}
}
public function doesPostOnNewComments($project_id, $value = null)
{
if ($value !== null) {
return $this->saveSetting(self::SETTING_PROJECT_POST_ON_NEW_COMMENTS . $project_id, (bool) $value);
} else {
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ON_NEW_COMMENTS . $project_id);
return (isset($setting)) ? $setting : true;
}
}
}