-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
100 lines (79 loc) · 2.83 KB
/
plugin.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
<?php
class gitlab_issues extends SlackServicePlugin {
public $name = "GitLab Issues";
public $desc = "Source control and issue management.";
public $cfg = array(
'has_token' => true,
);
function onInit() {
$channels = $this->getChannelsList();
foreach ($channels as $k => $v) {
if ($v == '#general') {
$this->icfg['channel'] = $k;
$this->icfg['channel_name'] = $v;
}
}
$this->icfg['botname'] = 'gitlab';
}
function onView() {
return $this->smarty->fetch('view.tpl');
}
function onEdit() {
$channels = $this->getChannelsList();
if ($_GET['save']) {
$this->icfg['channel'] = $_POST['channel'];
$this->icfg['channel_name'] = $channels[$_POST['channel']];
$this->icfg['botname'] = $_POST['botname'];
$this->saveConfig();
header("location: {$this->getViewUrl()}&saved=1");
exit;
}
$this->smarty->assign('channels', $channels);
return $this->smarty->fetch('edit.tpl');
}
function onHook($req) {
global $cfg;
if (!$this->icfg['channel']) {
return array(
'ok' => false,
'error' => "No channel configured",
);
}
$gitlab_payload = json_decode($req['post_body']);
if (!$gitlab_payload || !is_object($gitlab_payload) || $gitlab_payload->object_kind != "issue") {
return array(
'ok' => false,
'error' => "No payload received from gitlab",
);
}
$message = sprintf(
'*Issue #<%s|%s>* - %s - *[%s]*',
$gitlab_payload->object_attributes->url,
$gitlab_payload->object_attributes->iid,
$gitlab_payload->object_attributes->title,
$gitlab_payload->object_attributes->action
);
if (count($fields) > 0) {
$this->postToChannel($message, array(
'channel' => $this->icfg['channel'],
'username' => $this->icfg['botname'],
'attachments' => $fields,
'icon_url' => 'https://cdn.pancentric.com/cdn/libs/icons/gitlab.png'
));
}
else {
$this->postToChannel($message, array(
'channel' => $this->icfg['channel'],
'username' => $this->icfg['botname'],
'icon_url' => 'https://cdn.pancentric.com/cdn/libs/icons/gitlab.png'
));
}
return array(
'ok' => true,
'status' => "Nothing found to report",
);
}
function getLabel() {
return "Post issue updates to {$this->icfg['channel_name']} as {$this->icfg['botname']}";
}
}