-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmppNotify.php
More file actions
188 lines (153 loc) · 4.42 KB
/
Copy pathXmppNotify.php
File metadata and controls
188 lines (153 loc) · 4.42 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
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\Plugin\Plugin;
/**
* XMPP Notification - Send notification for successful or failure build.
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author Alexandre Russo <dev.github@ange7.com>
*/
class XmppNotify extends Plugin
{
/**
* @var string Username of sender account xmpp
*/
private string $username = '';
/**
* @var string Alias server of sender account xmpp
*/
private string $server = '';
/**
* @var string Password of sender account xmpp
*/
private string $password = '';
/**
* @var string Alias for sender
*/
private string $alias = '';
/**
* @var bool Use tls
*/
private bool $tls = false;
/**
* @var array List of recipients xmpp accounts
*/
private array $recipients = [];
/**
* @var string Mask to format date
*/
private string $dateFormat = '%c';
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'xmpp_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$executable = $this->commandExecutor->findBinary($this->binaryNames, $this->binaryPath);
if (!$this->recipients) {
return false;
}
$configFile = $this->build->getBuildPath() . '.sendxmpprc';
if (!$this->findConfigFile()) {
\file_put_contents($configFile, $this->getConfigFormat());
\chmod($configFile, 0600);
}
$tls = '';
if ($this->tls) {
$tls = ' -t';
}
$messageFile = $this->build->getBuildPath() . \uniqid('xmppmessage');
if (!$this->buildMessage($messageFile)) {
return false;
}
$cmd = $executable . "%s -f %s -m %s %s";
$recipients = \implode(' ', $this->recipients);
$success = $this->commandExecutor->executeCommand($cmd, $tls, $configFile, $messageFile, $recipients);
echo $this->commandExecutor->getLastCommandOutput();
$this->commandExecutor->executeCommand("rm -rf " . $messageFile);
return $success;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
$this->recipients = (array)$this->options->get('recipients', $this->recipients);
}
/**
* {@inheritDoc}
*/
protected function getPluginDefaultBinaryNames(): array
{
return [
'sendxmpp',
];
}
/**
* Get config format for sendxmpp config file
*/
private function getConfigFormat(): string
{
$conf = $this->username;
if (!empty($this->server)) {
$conf .= ';' . $this->server;
}
$conf .= ' ' . $this->password;
if (!empty($this->alias)) {
$conf .= ' ' . $this->alias;
}
return $conf;
}
/**
* Find config file for sendxmpp binary (default is .sendxmpprc)
*/
private function findConfigFile(): bool
{
if (\file_exists($this->build->getBuildPath() . '.sendxmpprc')) {
if (
\md5(\file_get_contents($this->build->getBuildPath() . '.sendxmpprc')) !== \md5($this->getConfigFormat())
) {
return false;
}
return true;
}
return false;
}
private function buildMessage(string $messageFile): bool
{
if ($this->build->isSuccessful()) {
$message = "✔ [" . $this->project->getTitle() . "] Build #" . $this->build->getId() . " successful";
} else {
$message = "✘ [" . $this->project->getTitle() . "] Build #" . $this->build->getId() . " failure";
}
$message .= ' (' . \strftime($this->dateFormat) . ')';
return (bool)\file_put_contents($messageFile, $message);
}
}