forked from kommuna/vicky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.example.php
More file actions
189 lines (157 loc) · 5.66 KB
/
index.example.php
File metadata and controls
189 lines (157 loc) · 5.66 KB
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
<?php
/**
* An example index file for the main module of vicky project, that receives data from JIRA webhook
* https://developer.atlassian.com/jiradev/jira-apis/webhooks), contains jiraWebhook listeners for events, that sends
* messages to slack by slack client and contains converters declaration.
*
* @credits https://github.com/kommuna
* @author chewbacca@devadmin.com
*
* For the full copyright and license information, please view the LICENSE
* file that is distributed with this source code.
*/
namespace Vicky;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Maknz\Slack\Client;
use Vicky\src\modules\Jira\JiraDefaultToSlackBotConverter;
use Vicky\src\modules\Slack\SlackBotSender;
use Vicky\src\modules\Slack\SlackMessageSender;
use Vicky\src\modules\Vicky;
use JiraWebhook\JiraWebhook;
use JiraWebhook\Models\JiraWebhookData;
use JiraWebhook\Exceptions\JiraWebhookException;
require __DIR__ . '/vendor/autoload.php';
$config = require '/etc/vicky/config.php';
ini_set('log_errors', 'On');
ini_set('error_log', $config['error_log']);
date_default_timezone_set($config['timeZone']);
$log = new Logger('vicky');
$log->pushHandler(
new StreamHandler(
$config['error_log'],
$config['loggerDebugLevel'] ? Logger::DEBUG : Logger::ERROR
)
);
if ($config['environment'] === 'local'){
$log->pushHandler(new StreamHandler('php://output', Logger::DEBUG)); // <<< uses a stream
}
$start = microtime(true);
$log->info("The script ".__FILE__." started.");
SlackBotSender::getInstance(
$config['slackBot']['url'],
$config['slackBot']['auth'],
$config['slackBot']['timeout']
);
new Vicky($config);
$jiraWebhook = new JiraWebhook();
/**
* Set the converters Vicky will use to "translate" JIRA webhook
* payload into formatted, human readable Slack messages
*/
JiraWebhook::setConverter('JiraDefaultToSlack', new JiraDefaultToSlackBotConverter());
/*
|--------------------------------------------------------------------------
| Register default listeners
|--------------------------------------------------------------------------
|
| These are just a few default listeners that would make sense for most teams.
| To add your own you should follow instructions in the README.md file
|
*/
/**
* Send a message to the project's channel when a blocker issue is created or updated
*/
$jiraWebhook->addListener('*', function($e, JiraWebhookData $data)
{
// Catch all events
});
/**
* Send message to user if a newly created issue
* was assigned to them
*/
$jiraWebhook->addListener('jira:issue_created', function ($e, JiraWebhookData $data)
{
$assignee = $data->getIssue()->getAssignee();
if ($assignee->getName()) {
$slackClientMessage = SlackMessageSender::getMessage();
JiraWebhook::convert('JiraDefaultToSlack', $data, $slackClientMessage);
$slackClientMessage->to('@' . $assignee->getName());
$slackClientMessage->send();
}
});
/**
* Send message to user's channel if an issue gets assigned to them
*/
$jiraWebhook->addListener('jira:issue_updated', function ($e, JiraWebhookData $data)
{
$issue = $data->getIssue();
if ($data->isIssueAssigned()) {
$data->overrideIssueEventDescription("An issue has been assigned to you");
$slackClientMessage = SlackMessageSender::getMessage();
JiraWebhook::convert('JiraDefaultToSlack', $data, $slackClientMessage);
$slackClientMessage->to('@' . $issue->getAssignee()->getName());
$slackClientMessage->send();
}
});
/**
* Send message to user's channel if someone comments on an issue
* assigned to them
*/
$jiraWebhook->addListener('jira:issue_updated', function ($e, JiraWebhookData $data)
{
$issue = $data->getIssue();
if ($data->isIssueCommented()) {
$data->overrideIssueEventDescription("A new comment has been posted on your issue");
$slackClientMessage = SlackMessageSender::getMessage();
JiraWebhook::convert('JiraDefaultToSlack', $data, $slackClientMessage);
$slackClientMessage->to('@' . $issue->getAssignee()->getName());
$slackClientMessage->send();
}
});
/**
* Send message to user's channel if someone mentions them in a new comment
*/
$jiraWebhook->addListener('jira:issue_updated', function ($e, JiraWebhookData $data)
{
$issue = $data->getIssue();
if ($data->isIssueCommented()) {
$users = $issue->getIssueComments()->getLastComment()->getMentionedUsersNicknames();
$data->overrideIssueEventDescription("You've been mentioned in a comment");
foreach ($users as $user) {
$slackClientMessage = SlackMessageSender::getMessage();
JiraWebhook::convert('JiraDefaultToSlack', $data, $slackClientMessage);
$slackClientMessage->to('@' . $user);
$slackClientMessage->send();
}
}
});
/*
|--------------------------------------------------------------------------
| Custom listeners
|--------------------------------------------------------------------------
| ADD YOUR CUSTOM LISTENERS HERE
|
*/
/*
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
*/
try {
/**
* Get raw data from JIRA webhook
*/
$f = fopen('php://input', 'r');
$data = stream_get_contents($f);
if (!$data) {
$log->error('There is no data in the Jira webhook');
throw new JiraWebhookException('There is no data in the Jira webhook');
}
$jiraWebhook->run($data);
} catch (\Exception $e) {
$log->error($e->getMessage());
$log->error($e->getLine());
$log->error($e->getFile());
$log->error($e->getCode());
}
$log->info("Script finished in ".(microtime(true) - $start)." sec.");