Repository navigation
Expand file tree
/
Copy pathdrinfoslack.php
More file actions
112 lines (84 loc) · 2.68 KB
/
Copy pathdrinfoslack.php
File metadata and controls
112 lines (84 loc) · 2.68 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
<?php
$whereami = dirname(__FILE__);
$config = file_get_contents($whereami . '/config.json');
$config = json_decode($config, TRUE);
$updates_available = [];
foreach ($config['sites'] as $site_name => $site_path) {
$packages = [];
chdir($site_path);
$drushjson = shell_exec('drush ups --format=json');
// if this comes back empty if means there are no updates available
if ($drushjson == '') {
$updates_available[$site_name] = $packages;
continue;
}
$drushsays = json_decode($drushjson, TRUE);
foreach ($drushsays as $package => $info) {
$current_version = $info['info']['version'];
if (array_key_exists('updateable', $info) && $info['updateable']) {
$recommended = $info['recommended'];
}
$packages[$package] = [
'current' => $current_version,
'recommended' => $recommended
];
}
$updates_available[$site_name] = $packages;
}
$msg = '';
//
// okay if i don't have updates, let me know
if (count($updates_available) == 0) {
$msg = count($config['sites']) . ' sites. No updates available. :smile:';
}
else {
// if we DO have updates available, make something nice out of them
foreach ($updates_available as $site => $packages) {
if (count($packages) == 0) {
$msg .= $site . ' has no updates available.';
}
else if (count($packages) == 1) {
$msg .= $site . ' has an update available for ';
$msg .= '`' . current(array_keys($packages)) . '`';
}
else {
$msg .= $site . ' has ' . count($packages) . ' update(s) available';
if (count($packages) <= 3) {
$msg .= ': ';
foreach ($packages as $pkg => $pkginfo) {
$msg .= '`' . $pkg . '` ';
}
}
}
if (array_key_exists('drupal', $packages)) {
$msg .= ' *DRUPAL CORE*';
}
$msg .= chr(10);
}
}
if (array_key_exists('testmode', $config) && $config['testmode']) {
echo $msg . chr(10);
exit;
}
// send to Slack
if ($msg) {
slack($msg, $config['servername'], $config['slack']);
}
function slack($message, $from, $cfg) {
$channel = ($cfg['channel']) ? $cfg['channel'] : "drupal";
$data = "payload=" . json_encode(array(
"channel" => "#{$cfg['channel']}",
"text" => $message,
"username" => $from,
"icon_emoji" => $cfg['icon']
));
// You can get your webhook endpoint from your Slack settings
$ch = curl_init($cfg['webhook']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}