1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* HipchatTask
|
22
|
|
* Sends a simple hipchat notification.
|
23
|
|
*
|
24
|
|
* (Yeah, HipChat API has lots of more awesome features than sending a lousy text notification
|
25
|
|
* but I refuse implementing more as long as the chat client lacks the most basic feature of
|
26
|
|
* sorting contacts. If you share my opinion then please upvote this feature request:
|
27
|
|
* https://jira.atlassian.com/browse/HCPUB-363 )
|
28
|
|
*
|
29
|
|
* <hipchat room="1337" authToken="********" color="red" notify="true" format="html">
|
30
|
|
* Hello <i>World</i>!
|
31
|
|
* </hipchat>
|
32
|
|
*
|
33
|
|
* @author Suat Özgür <suat.oezguer@mindgeek.com>
|
34
|
|
* @package phing.tasks.ext
|
35
|
|
*/
|
36
|
|
class HipchatTask extends Task
|
37
|
|
{
|
38
|
|
private $domain = 'api.hipchat.com';
|
39
|
|
private $room = null;
|
40
|
|
private $authToken = null;
|
41
|
|
private $color = 'yellow';
|
42
|
|
private $notify = false;
|
43
|
|
private $message = null;
|
44
|
|
private $format = 'text';
|
45
|
|
|
46
|
0
|
public function main()
|
47
|
|
{
|
48
|
0
|
if (null === $this->getRoom()) {
|
49
|
0
|
throw new BuildException('(HipChat) room is not defined');
|
50
|
|
}
|
51
|
|
|
52
|
0
|
if (null === $this->getAuthToken()) {
|
53
|
0
|
throw new BuildException('(HipChat) authToken is not defined');
|
54
|
|
}
|
55
|
|
|
56
|
|
$url =
|
57
|
|
'https://' .
|
58
|
0
|
$this->getDomain() .
|
59
|
0
|
'/v2/room/' .
|
60
|
0
|
$this->getRoom() .
|
61
|
0
|
'/notification?auth_token=' .
|
62
|
0
|
$this->getAuthToken();
|
63
|
|
|
64
|
|
$data = array(
|
65
|
0
|
'color' => $this->getColor(),
|
66
|
0
|
'message' => $this->getMessage(),
|
67
|
0
|
'notify' => $this->isNotify(),
|
68
|
0
|
'message_format' => $this->getFormat(),
|
69
|
|
);
|
70
|
|
|
71
|
0
|
$result = $this->executeApiCall($url, $data);
|
72
|
0
|
if ($result !== true) {
|
73
|
0
|
$this->log($result, Project::MSG_WARN);
|
74
|
|
} else {
|
75
|
0
|
$this->log('HipChat notification sent.');
|
76
|
|
}
|
77
|
|
}
|
78
|
|
|
79
|
|
/**
|
80
|
|
* @return string
|
81
|
|
*/
|
82
|
0
|
public function getDomain()
|
83
|
|
{
|
84
|
0
|
return $this->domain;
|
85
|
|
}
|
86
|
|
|
87
|
|
/**
|
88
|
|
* @param string $domain
|
89
|
|
*/
|
90
|
0
|
public function setDomain($domain)
|
91
|
|
{
|
92
|
0
|
$this->domain = $domain;
|
93
|
|
}
|
94
|
|
|
95
|
|
/**
|
96
|
|
* @return String
|
97
|
|
*/
|
98
|
0
|
public function getFormat()
|
99
|
|
{
|
100
|
0
|
return $this->format;
|
101
|
|
}
|
102
|
|
|
103
|
|
/**
|
104
|
|
* @param $format
|
105
|
|
*/
|
106
|
0
|
public function setFormat($format)
|
107
|
|
{
|
108
|
0
|
$format = ($format != 'text' && $format != 'html') ? 'text' : $format;
|
109
|
0
|
$this->format = $format;
|
110
|
|
}
|
111
|
|
|
112
|
|
/**
|
113
|
|
* @return string
|
114
|
|
*/
|
115
|
0
|
public function getRoom()
|
116
|
|
{
|
117
|
0
|
return $this->room;
|
118
|
|
}
|
119
|
|
|
120
|
|
/**
|
121
|
|
* @param string $room
|
122
|
|
*/
|
123
|
0
|
public function setRoom($room)
|
124
|
|
{
|
125
|
0
|
$this->room = $room;
|
126
|
|
}
|
127
|
|
|
128
|
|
/**
|
129
|
|
* @return string
|
130
|
|
*/
|
131
|
0
|
public function getAuthToken()
|
132
|
|
{
|
133
|
0
|
return $this->authToken;
|
134
|
|
}
|
135
|
|
|
136
|
|
/**
|
137
|
|
* @param string $authToken
|
138
|
|
*/
|
139
|
0
|
public function setAuthToken($authToken)
|
140
|
|
{
|
141
|
0
|
$this->authToken = $authToken;
|
142
|
|
}
|
143
|
|
|
144
|
|
/**
|
145
|
|
* @return string
|
146
|
|
*/
|
147
|
0
|
public function getColor()
|
148
|
|
{
|
149
|
0
|
return $this->color;
|
150
|
|
}
|
151
|
|
|
152
|
|
/**
|
153
|
|
* @param string $color
|
154
|
|
*/
|
155
|
0
|
public function setColor($color)
|
156
|
|
{
|
157
|
0
|
$this->color = $color;
|
158
|
|
}
|
159
|
|
|
160
|
|
/**
|
161
|
|
* @return string
|
162
|
|
*/
|
163
|
0
|
public function getMessage()
|
164
|
|
{
|
165
|
0
|
return $this->message;
|
166
|
|
}
|
167
|
|
|
168
|
|
/**
|
169
|
|
* @return boolean
|
170
|
|
*/
|
171
|
0
|
public function isNotify()
|
172
|
|
{
|
173
|
0
|
return $this->notify;
|
174
|
|
}
|
175
|
|
|
176
|
|
/**
|
177
|
|
* @param boolean $notify
|
178
|
|
*/
|
179
|
0
|
public function setNotify($notify)
|
180
|
|
{
|
181
|
0
|
$this->notify = $notify;
|
182
|
|
}
|
183
|
|
|
184
|
|
/**
|
185
|
|
* @param $message
|
186
|
|
*/
|
187
|
0
|
public function addText($message)
|
188
|
|
{
|
189
|
0
|
$this->message = trim($message);
|
190
|
|
}
|
191
|
|
|
192
|
0
|
private function executeApiCall($url, $data)
|
193
|
|
{
|
194
|
0
|
$postData = json_encode($data);
|
195
|
|
|
196
|
0
|
$ch = curl_init();
|
197
|
0
|
curl_setopt($ch, CURLOPT_URL, $url);
|
198
|
0
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
199
|
0
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
200
|
0
|
curl_setopt($ch, CURLOPT_HEADER, 0);
|
201
|
0
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
|
202
|
0
|
curl_setopt($ch, CURLOPT_POST, 1);
|
203
|
0
|
$response = curl_exec($ch);
|
204
|
0
|
if ($response !== '') {
|
205
|
0
|
$result = json_decode($response, 1);
|
206
|
0
|
return $result['error']['message'] . ' (' . $result['error']['code'] . ')';
|
207
|
|
}
|
208
|
0
|
return true;
|
209
|
|
}
|
210
|
|
}
|