-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slack.php
262 lines (226 loc) · 9.92 KB
/
Slack.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
namespace thebuggenie\modules\slack;
use thebuggenie\core\entities\Milestone;
use thebuggenie\core\entities\Project;
use thebuggenie\core\framework,
Maknz\Slack\Client as SlackClient;
/**
* Slack module for integrating with Slack
*
* @author
* @version 1.0
* @license http://opensource.org/licenses/MPL-2.0 Mozilla Public License 2.0 (MPL 2.0)
* @package slack
* @subpackage core
*/
/**
* Slack module for integrating with Slack
*
* @package slack
* @subpackage core
*
* @Table(name="\thebuggenie\core\entities\tables\Modules")
*/
class Slack extends \thebuggenie\core\entities\Module
{
const VERSION = '1.0';
const SETTING_WEBHOOK_URL = 'webhook_url';
const SETTING_PROJECT_INTEGRATION_ENABLED = 'project_integration_enabled_';
const SETTING_PROJECT_CHANNEL_NAME = 'project_post_to_channel_';
const SETTING_PROJECT_POST_AS_NAME = 'project_post_to_channel_as_name_';
const SETTING_PROJECT_POST_AS_LOGO = 'project_post_to_channel_as_logo_';
const SETTING_PROJECT_POST_ON_NEW_ISSUES = 'project_post_to_channel_on_new_issues_';
const SETTING_PROJECT_POST_ON_NEW_RELEASES = 'project_post_to_channel_on_new_releases_';
protected $_has_config_settings = true;
protected $_name = 'slack';
protected $_longname = 'Slack integration';
protected $_description = 'Slack description here';
protected $_module_config_title = 'Slack integration';
protected $_module_config_description = 'Configure the Slack integration';
protected $_slack_config = [];
/**
* Return an instance of this module
*
* @return Slack
*/
public static function getModule()
{
return framework\Context::getModule('slack');
}
protected function _initialize()
{
require THEBUGGENIE_MODULES_PATH . 'slack' . DS . 'vendor' . DS . 'autoload.php';
$this->_slack_config = [
'username' => '',
'channel' => '',
'icon' => 'http://thebuggenie.com/images/logo_32.png',
'link_names' => true
];
}
protected function _getSettings(Project $project)
{
$project_id = $project->getID();
$settings = $this->_slack_config;
$settings['channel'] = $this->getChannelName($project_id);
$settings['username'] = $this->getPostAsName($project_id);
return $settings;
}
/**
* @param Project $project
* @return SlackClient
*/
protected function _getProjectClient(Project $project)
{
$settings = $this->_getSettings($project);
$client = new SlackClient($this->getWebhookUrl(), $settings);
return $client;
}
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))
{
$client = $this->_getProjectClient($issue->getProject());
$text = \tbg_truncateText($issue->getDescription());
$client
->to($this->getChannelName($project_id))
->enableMarkdown()
->attach([
'fallback' => 'New issue posted',
'text' => $text,
'title' => $issue->getFormattedIssueNo(true, true) . ' - ' . $issue->getTitle(),
'title_link' => framework\Context::getRouting()->generate('viewissue', ['project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getFormattedIssueNo()], false),
'color' => 'good',
])
->send('['.$issue->getProject()->getKey().'] A new issue was posted by @'.$issue->getPostedBy()->getUsername());
}
}
public function listen_buildSave(framework\Event $event)
{
framework\Context::loadLibrary('common');
$release = $event->getSubject();
$project_id = $release->getProject()->getID();
if ($this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewReleases($project_id))
{
$client = $this->_getProjectClient($release->getProject());
$fields = [
[
'title' => 'Version number',
'value' => $release->getVersion(),
'short' => true
]
];
if ($release->isReleased()) {
$fields[] = [
'title' => 'Release date',
'value' => tbg_formatTime($release->getReleaseDate(), 20),
'short' => true
];
}
if ($release->getMilestone() instanceof Milestone) {
$fields[] = [
'title' => 'Milestone',
'value' => $release->getMilestone()->getName(),
'short' => true
];
}
$client
->to($this->getChannelName($project_id))
->enableMarkdown()
->attach([
'fallback' => 'New release',
'title' => $release->getName(),
'title_link' => framework\Context::getRouting()->generate('project_releases', ['project_key' => $release->getProject()->getKey()], false),
'color' => '#77A',
'fields' => $fields
])
->send('['.$release->getProject()->getKey().'] A new release is available');
}
}
protected function _addListeners()
{
framework\Event::listen('core', 'thebuggenie\core\entities\Issue::createNew', array($this, 'listen_issueCreate'));
framework\Event::listen('core', 'thebuggenie\core\entities\Build::_postSave', array($this, 'listen_buildSave'));
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('slack/projectconfig_tab', array('selected_tab' => $event->getParameter('selected_tab'), 'module' => $this));
}
public function listen_projectconfig_panel(framework\Event $event)
{
include_component('slack/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 getWebhookUrl()
{
return $this->getSetting(self::SETTING_WEBHOOK_URL);
}
public function setWebhookUrl($value)
{
return $this->saveSetting(self::SETTING_WEBHOOK_URL, $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 getChannelName($project_id)
{
return $this->getSetting(self::SETTING_PROJECT_CHANNEL_NAME . $project_id);
}
public function setChannelName($project_id, $channel_name)
{
return $this->saveSetting(self::SETTING_PROJECT_CHANNEL_NAME . $project_id, $channel_name);
}
public function getPostAsName($project_id)
{
$setting = $this->getSetting(self::SETTING_PROJECT_POST_AS_NAME . $project_id);
return $setting ?: 'TBG Autobot';
}
public function setPostAsName($project_id, $name)
{
return $this->saveSetting(self::SETTING_PROJECT_POST_AS_NAME . $project_id, $name);
}
public function getPostAsLogo($project_id)
{
$setting = $this->getSetting(self::SETTING_PROJECT_POST_AS_LOGO . $project_id);
return $setting ?: 'thebuggenie';
}
public function setPostAsLogo($project_id, $key)
{
return $this->saveSetting(self::SETTING_PROJECT_POST_AS_LOGO . $project_id, $key);
}
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 doesPostOnNewReleases($project_id, $value = null)
{
if ($value !== null) {
return $this->saveSetting(self::SETTING_PROJECT_POST_ON_NEW_RELEASES . $project_id, (bool) $value);
} else {
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ON_NEW_RELEASES . $project_id);
return (isset($setting)) ? $setting : true;
}
}
}