-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMattermost.php
779 lines (681 loc) · 34.9 KB
/
Mattermost.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
<?php
namespace thebuggenie\modules\mattermost;
use thebuggenie\core\entities\Milestone;
use thebuggenie\core\entities\Project;
use thebuggenie\core\entities\Issue;
use thebuggenie\core\entities\Comment;
use thebuggenie\core\framework;
use thebuggenie\core\framework\I18n;
use GuzzleHttp\Client as GuzzleClient;
use ThibaudDauce\Mattermost\Mattermost as MattermostClient;
use ThibaudDauce\Mattermost\Message as MattermostMessage;
use ThibaudDauce\Mattermost\Attachment as MattermostAttachment;
use League\HTMLToMarkdown\HtmlConverter;
/**
* Mattermost module for integrating The Bug Genie with Mattermost
*
* @author SHORELESS Limited
* @version 1.0
* @license http://opensource.org/licenses/MPL-2.0 Mozilla Public License 2.0 (MPL 2.0)
* @package mattermost
* @subpackage core
*/
/**
* Mattermost module for integrating with Mattermost
*
* @package mattermost
* @subpackage core
*
* @Table(name="\thebuggenie\core\entities\tables\Modules")
*/
class Mattermost 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_CHANNEL_LANGUAGE = 'project_post_language_';
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_CHANGE_ISSUES = 'project_post_to_channel_on_change_issues_';
const SETTING_PROJECT_POST_ISSUE_CHANGES = 'project_post_issue_changes_';
const SETTING_PROJECT_POST_ON_NEW_RELEASES = 'project_post_to_channel_on_new_releases_';
const SETTING_PROJECT_POST_ON_NEW_COMMENT = 'project_post_to_channel_on_new_comment_';
protected $_has_config_settings = true;
protected $_name = 'mattermost';
protected $_longname = 'Mattermost integration';
protected $_description = 'Mattermost description here';
protected $_module_config_title = 'Mattermost integration';
protected $_module_config_description = 'Configure the Mattermost integration';
protected $_mattermost_config = [];
/**
* Localization clients
*
* Associative array of language key and
* \thebuggenie\core\framework\I18n instances.
*
* @var array
*/
protected static $_i18n = [];
/**
* Return an instance of this module
*
* @return Mattermost
*/
public static function getModule()
{
return framework\Context::getModule('mattermost');
}
protected function _initialize()
{
require THEBUGGENIE_MODULES_PATH . 'mattermost' . DS . 'vendor' . DS . 'autoload.php';
framework\Context::loadLibrary('ui');
// Default settings.
$this->_mattermost_config = [
'username' => '',
'channel' => '',
'url' => '',
'icon' => image_url(framework\Settings::getHeaderIconUrl(), framework\Settings::isUsingCustomHeaderIcon(), 'core', false),
'link_names' => true,
'truncate_text' => 300,
'attachment_include_user' => false,
'language' => 'en_US',
];
}
/**
* Return project specific Mattermost posting settings
*
* @param Project $project
* The project object or a valid project ID.
* @return array
* The project specific settings.
*/
protected function _getSettings($project)
{
$project_id = $project->getID();
$settings = $this->_mattermost_config;
$settings['url'] = $this->getWebhookUrl($project_id);
$settings['channel'] = $this->getChannelName($project_id);
$settings['username'] = $this->getPostAsName($project_id);
$settings['language'] = $this->getPostLanguage($project_id);
if ($this->getPostAsLogo($project_id) != 'thebuggenie') {
$settings['icon'] = image_url($project->getLargeIconName(), $project->hasLargeIcon(), 'core', false);
}
return $settings;
}
/**
* Returns a localization client for the given language
*
* @param string $language
* @return I18n
*/
protected function _getI18n($language = 'en_US') {
$i18n = framework\Context::getI18n();
if ( ! $i18n instanceof I18n) {
$i18n = new I18n($language);
}
if ($i18n->getCurrentLanguage() != $language) {
$i18n->setLanguage($language);
}
$i18n->loadModuleStrings($this->_name);
return $i18n;
}
/**
* Event listener for new issues
*
* Posts to a Mattermost channel, if posting to Mattermost is enabled
* for the project.
*
* @param \thebuggenie\core\framework\Event $event
*/
public function listen_issueCreate(framework\Event $event)
{
$issue = $event->getSubject();
$project_id = $issue->getProjectID();
// Whether posting to Mattermost has been enabled.
if ( ! ($issue instanceof Issue && $this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewIssues($project_id))) {
return;
}
// Get project specific settings.
$settings = $this->_getSettings($issue->getProject());
// Check for webhook URL.
if (empty($settings['url'])) {
return;
}
framework\Context::loadLibrary('common');
$i18n = $this->_getI18n($settings['language']);
// Mattermost client.
$client = new MattermostClient(new GuzzleClient());
$converter = new HtmlConverter(['strip_tags' => true]);
// Issue properties.
$issueNo = $issue->getFormattedIssueNo(true, true);
$issueLink = framework\Context::getRouting()->generate('viewissue', ['project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getIssueNo()], false);
$issueDescription = $converter->convert($issue->getParsedDescription(['issue' => $issue]));
if (! empty($settings['truncate_text'])) {
$issueDescription = \tbg_truncateText($issueDescription);
}
$fields = [];
// Meta information.
if ($issue->hasIssueType()) {
$fields[] = [
'title' => $i18n->__('Issue type'),
'value' => $issue->getIssueType()->getName(),
'short' => true,
];
}
// Compose message.
$attachment = (new MattermostAttachment())
->fallback(html_entity_decode($i18n->__('New issue created')))
->title(html_entity_decode('[' . $issueNo . '] ' . $issue->getTitle()), $issueLink)
->text($issueDescription)
->success();
if ( ! empty($fields)) {
$attachment->fields($fields);
}
if ( ! empty($settings['attachment_include_user'])) {
$attachment->authorName($settings['username'])
->authorIcon($settings['icon']);
}
$message = (new MattermostMessage())
->channel($settings['channel'])
->username($settings['username'])
->iconUrl($settings['icon'])
->text($i18n->__('%user created [%issue_no](%issue_link) in project [%project_name](%project_link)', [
'%user' => ( ! empty($settings['link_names']) ? '@' : '') . $issue->getPostedBy()->getUsername(),
'%issue_no' => $issueNo,
'%issue_link' => $issueLink,
'%project_name' => $issue->getProject()->getName(),
'%project_link' => framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $issue->getProject()->getKey()), false),
]))
->attachments([$attachment]);
// Post the message to Mattermost.
$client->send($message, $settings['url']);
}
/**
* Event listener for altered issues
*
* Posts to a Mattermost channel, if posting to Mattermost is enabled
* for the project.
*
* @param \thebuggenie\core\framework\Event $event
*/
public function listen_issueSave(framework\Event $event)
{
$issue = $event->getSubject();
$project_id = $issue->getProjectID();
// Whether posting to Mattermost has been enabled.
if ( ! ($issue instanceof Issue && $this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnChangeIssues($project_id))) {
return;
}
// Get project specific settings.
$settings = $this->_getSettings($issue->getProject());
// Check for webhook URL.
if (empty($settings['url'])) {
return;
}
framework\Context::loadLibrary('common');
$i18n = $this->_getI18n($settings['language']);
// Mattermost client.
$client = new MattermostClient(new GuzzleClient());
$converter = new HtmlConverter(['strip_tags' => true]);
// Issue properties.
$issueNo = $issue->getFormattedIssueNo(true, true);
$issueLink = framework\Context::getRouting()->generate('viewissue', ['project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getIssueNo()], false);
$text = '';
$log_items = $event->getParameter('log_items');
$changes = [];
$enabledChanges = $this->getEnabledIssueChanges($project_id);
if ( ! empty($log_items)) {
foreach($log_items as $item) {
switch ($item->getChangeType())
{
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_CREATED:
case \thebuggenie\core\entities\tables\Log::LOG_COMMENT:
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_CLOSE:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_CLOSE, $enabledChanges)) {
$changes[] = $i18n->__('Issue closed');
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_REOPEN:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_REOPEN, $enabledChanges)) {
$changes[] = $i18n->__('Issue reopened');
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_UPDATE:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_UPDATE, $enabledChanges)) {
$changes[] = $converter->convert($item->getText());
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PAIN_BUG_TYPE:
if (in_array('others', $enabledChanges)) {
$changes[] = $i18n->__('Triaged bug type: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PAIN_LIKELIHOOD:
if (in_array('others', $enabledChanges)) {
$changes[] = $i18n->__('Triaged likelihood: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PAIN_EFFECT:
if (in_array('others', $enabledChanges)) {
$changes[] = $i18n->__('Triaged effect: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PAIN_CALCULATED:
if (in_array('others', $enabledChanges)) {
$changes[] = $i18n->__('Calculated user pain: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_CATEGORY:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_CATEGORY, $enabledChanges)) {
$changes[] = $i18n->__('Category changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_CUSTOMFIELD_CHANGED:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_CUSTOMFIELD_CHANGED, $enabledChanges)) {
$changes[] = $i18n->__('Custom field changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_STATUS:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_STATUS, $enabledChanges)) {
$changes[] = $i18n->__('Status changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_REPRODUCABILITY:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_REPRODUCABILITY, $enabledChanges)) {
$changes[] = $i18n->__('Reproducability changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PRIORITY:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_PRIORITY, $enabledChanges)) {
$changes[] = $i18n->__('Priority changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_SEVERITY:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_SEVERITY, $enabledChanges)) {
$changes[] = $i18n->__('Severity changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_RESOLUTION:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_RESOLUTION, $enabledChanges)) {
$changes[] = $i18n->__('Resolution changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_PERCENT:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_PERCENT, $enabledChanges)) {
$changes[] = $i18n->__('Percent completed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_MILESTONE:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_MILESTONE, $enabledChanges)) {
$changes[] = $i18n->__('Target milestone changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_ISSUETYPE:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_ISSUETYPE, $enabledChanges)) {
$changes[] = $i18n->__('Issue type changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_TIME_ESTIMATED:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_TIME_ESTIMATED, $enabledChanges)) {
$changes[] = $i18n->__('Estimation changed: %text', array('%text' => \thebuggenie\core\entities\common\Timeable::formatTimeableLog($item->getText(), $item->getPreviousValue(), $item->getCurrentValue(), true, true)));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_TIME_SPENT:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_TIME_SPENT, $enabledChanges)) {
$changes[] = $i18n->__('Time spent: %text', array('%text' => \thebuggenie\core\entities\common\Timeable::formatTimeableLog($item->getText(), $item->getPreviousValue(), $item->getCurrentValue(), true, true)));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_ASSIGNED:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_ASSIGNED, $enabledChanges)) {
$changes[] = $i18n->__('Assignee changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_OWNED:
if (in_array(\thebuggenie\core\entities\tables\Log::LOG_ISSUE_OWNED, $enabledChanges)) {
$changes[] = $i18n->__('Owner changed: %text', array('%text' => $item->getText()));
}
break;
case \thebuggenie\core\entities\tables\Log::LOG_ISSUE_POSTED:
if (in_array('others', $enabledChanges)) {
$changes[] = $i18n->__('Posted by changed: %text', array('%text' => $item->getText()));
}
break;
default:
if (in_array('others', $enabledChanges)) {
if (!$item->getText())
{
$changes[] = $i18n->__('Issue updated');
}
else
{
$changes[] = $converter->convert($item->getText());
}
}
break;
}
}
}
// Whether there are no changes to post.
if (empty($changes)) {
return;
}
foreach ($changes as $change) {
$text .= "\n * " . $change;
}
$updated_by = $event->getParameter('updated_by');
// Compose message.
$attachment = (new MattermostAttachment())
->fallback(html_entity_decode($i18n->__('Issue changed')))
->title(html_entity_decode($i18n->__('Changes')), $issueLink)
->text($text)
->color('#66f');
if ( ! empty($settings['attachment_include_user'])) {
$attachment->authorName($settings['username'])
->authorIcon($settings['icon']);
}
$message = (new MattermostMessage())
->channel($settings['channel'])
->username($settings['username'])
->iconUrl($settings['icon'])
->text($i18n->__('%user updated [%issue_no](%issue_link) in project [%project_name](%project_link)', [
'%user' => ( ! empty($settings['link_names']) ? '@' : '') . $updated_by->getUsername(),
'%issue_no' => '[' . $issueNo . '] ' . $issue->getTitle(),
'%issue_link' => $issueLink,
'%project_name' => $issue->getProject()->getName(),
'%project_link' => framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $issue->getProject()->getKey()), false),
]))
->attachments([$attachment]);
// Post the message to Mattermost.
$client->send($message, $settings['url']);
}
/**
* Event listener for new comments
*
* Posts to a Mattermost channel, if posting to Mattermost is enabled
* for the project.
*
* @param \thebuggenie\core\framework\Event $event
*/
public function listen_commentCreate(framework\Event $event)
{
$comment = $event->getSubject();
// Whether the comment was not posted on an issue or was
// system generated.
if ( ! $comment instanceof Comment || $comment->getTargetType() != Comment::TYPE_ISSUE || $comment->isSystemComment()) {
return;
}
$issue = $event->getParameter('issue');
$project_id = $issue->getProjectID();
// Whether posting on new comments is disabled for the project.
if ( ! ($this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewComments($project_id))) {
return;
}
// Get project specific settings.
$settings = $this->_getSettings($issue->getProject());
// Check for webhook URL.
if (empty($settings['url'])) {
return;
}
framework\Context::loadLibrary('common');
$i18n = $this->_getI18n($settings['language']);
$converter = new HtmlConverter(['strip_tags' => true]);
// Mattermost client.
$client = new MattermostClient(new GuzzleClient());
// Issue properties.
$issueNo = $issue->getFormattedIssueNo(true, true);
$issueLink = framework\Context::getRouting()->generate('viewissue', ['project_key' => $issue->getProject()->getKey(), 'issue_no' => $issue->getIssueNo()], false);
$commentContent = $converter->convert($comment->getParsedContent());
if ( ! empty($settings['truncate_text'])) {
$commentContent = \tbg_truncateText($commentContent, $settings['truncate_text']);
}
$commentLink = $issueLink . '#comment_' . $comment->getID();
// Compose message.
$attachment = (new MattermostAttachment())
->fallback(html_entity_decode($i18n->__('New comment')))
->title(html_entity_decode($i18n->__('Comment #%comment_no', ['%comment_no' => $comment->getCommentNumber()])), $commentLink)
->text($commentContent)
->color('#666');
if ( ! empty($settings['attachment_include_user'])) {
$attachment->authorName($settings['username'])
->authorIcon($settings['icon']);
}
$message = (new MattermostMessage())
->channel($settings['channel'])
->username($settings['username'])
->iconUrl($settings['icon'])
->text($i18n->__('%user commented on [%issue_no](%issue_link) in project [%project_name](%project_link)', [
'%user' => ( ! empty($settings['link_names']) ? '@' : '') . $comment->getPostedBy()->getUsername(),
'%issue_no' => '[' . $issueNo . '] ' . $issue->getTitle(),
'%issue_link' => $issueLink,
'%project_name' => $issue->getProject()->getName(),
'%project_link' => framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $issue->getProject()->getKey()), false),
]))
->attachments([$attachment]);
// Post the message to Mattermost.
$client->send($message, $settings['url']);
}
/**
* Event listener for new builds
*
* Posts to a Mattermost channel, if posting to Mattermost is enabled
* for the project.
*
* @param \thebuggenie\core\framework\Event $event
*/
public function listen_buildSave(framework\Event $event)
{
$release = $event->getSubject();
$project = $release->getProject();
$project_id = $project->getID();
if (! ($this->isProjectIntegrationEnabled($project_id) && $this->doesPostOnNewReleases($project_id))) {
return;
}
// Get project specific settings.
$settings = $this->_getSettings($project);
// Check for webhook URL.
if (empty($settings['url'])) {
return;
}
framework\Context::loadLibrary('common');
$i18n = $this->_getI18n($settings['language']);
$fields = [
[
'title' => $i18n->__('Version number'),
'value' => $release->getVersion(),
'short' => true,
]
];
if ($release->isReleased()) {
$fields[] = [
'title' => $i18n->__('Release date'),
'value' => tbg_formatTime($release->getReleaseDate(), 20),
'short' => true,
];
}
if ($release->getMilestone() instanceof Milestone) {
$fields[] = [
'title' => $i18n->__('Milestone'),
'value' => $release->getMilestone()->getName(),
'short' => true,
];
}
// Mattermost client.
$client = new MattermostClient(new GuzzleClient());
// Release data.
$releaseName = $release->getName();
$releaseLink = framework\Context::getRouting()->generate('project_releases', ['project_key' => $project->getKey()], false);
// Compose message.
$attachment = (new MattermostAttachment())
->fallback($i18n->__('New release'))
->title($releaseName, $releaseLink)
->color('#77A')
->fields($fields);
if ( ! empty($settings['attachment_include_user'])) {
$attachment->authorName($settings['username'])
->authorIcon($settings['icon']);
}
$message = (new MattermostMessage())
->channel($settings['channel'])
->username($settings['username'])
->iconUrl($settings['icon'])
->text($i18n->__('New release [%release_name](%release_link) for project [%project_name](%project_link)', [
'%release_name' => $releaseName,
'%release_link' => $releaseLink,
'%project_name' => $project->getName(),
'%project_link' => framework\Context::getRouting()->generate('project_dashboard', array('project_key' => $project->getKey()), false),
]))
->attachments([$attachment]);
// Post the message to Mattermost.
$client->send($message, $settings['url']);
}
/**
* Add event listeners
*/
protected function _addListeners()
{
framework\Event::listen('core', 'thebuggenie\core\entities\Issue::createNew', array($this, 'listen_issueCreate'));
framework\Event::listen('core', 'thebuggenie\core\entities\Issue::save', array($this, 'listen_issueSave'));
framework\Event::listen('core', 'thebuggenie\core\entities\Comment::createNew', array($this, 'listen_commentCreate'));
framework\Event::listen('core', 'thebuggenie\core\entities\Comment::_postSave', array($this, 'listen_commentCreate'));
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('mattermost/projectconfig_tab', array('selected_tab' => $event->getParameter('selected_tab'), 'module' => $this));
}
public function listen_projectconfig_panel(framework\Event $event)
{
include_component('mattermost/projectconfig_panel', array('selected_tab' => $event->getParameter('selected_tab'), 'access_level' => $event->getParameter('access_level'), 'project' => $event->getParameter('project'), 'module' => $this, 'languages' => framework\I18n::getLanguages()));
}
protected function _install($scope)
{
}
protected function _loadFixtures($scope)
{
}
protected function _uninstall()
{
}
public function getWebhookUrl($project_id = 0)
{
if ( ! empty($project_id) && ! empty($url = $this->getSetting(self::SETTING_WEBHOOK_URL . '_' . $project_id))) {
return $url;
}
return $this->getSetting(self::SETTING_WEBHOOK_URL);
}
/**
* Sets the webhook URL.
*
* @param string $value
* Webhook URL
* @param int $project_id
* Optional project ID. If not empty, the URL will be saved for the given
* project.
* @return void
*/
public function setWebhookUrl($value, $project_id)
{
$identifier = self::SETTING_WEBHOOK_URL;
if ( ! empty($project_id)) {
$identifier .= '_' . $project_id;
}
return $this->saveSetting($identifier, $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 getEnabledIssueChanges($project_id)
{
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ISSUE_CHANGES . $project_id);
return ! empty($setting) ? explode(',', $setting) : [];
}
public function setEnabledIssueChanges($project_id, $changes)
{
return $this->saveSetting(self::SETTING_PROJECT_POST_ISSUE_CHANGES . $project_id, implode(',', $changes));
}
public function getPostLanguage($project_id)
{
$setting = $this->getSetting(self::SETTING_PROJECT_CHANNEL_LANGUAGE . $project_id);
return $setting ?: 'en_US';
}
public function setPostLanguage($project_id, $language)
{
return $this->saveSetting(self::SETTING_PROJECT_CHANNEL_LANGUAGE . $project_id, $language);
}
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 doesPostOnChangeIssues($project_id, $value = null)
{
if ($value !== null) {
return $this->saveSetting(self::SETTING_PROJECT_POST_ON_CHANGE_ISSUES . $project_id, (bool) $value);
}
else {
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ON_CHANGE_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_COMMENT . $project_id, (bool) $value);
}
else {
$setting = $this->getSetting(self::SETTING_PROJECT_POST_ON_NEW_COMMENT . $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;
}
}
}