This repository is currently being migrated. It's locked while the migration is in progress.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
283 lines (256 loc) · 8.33 KB
/
helper.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* DokuWiki Plugin youtrack (Helper Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Dominic Pöllath <dominic.poellath@ils-gmbh.net>
* @author Anika Henke <anika@zopa.com>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
class helper_plugin_youtrack extends DokuWiki_Plugin {
protected $cookie = false;
/**
* Return info about supported methods in this Helper Plugin
*
* @return array of public methods
*/
public function getMethods() {
return array(
array(
'name' => 'request',
'desc' => 'Send request to REST API',
'params' => array(
'method' => 'string',
'endpoint' => 'string',
'params (optional)' => 'array'
),
'return' => array('Response' => 'mixed')
),
array(
'name' => 'getBaseUrl',
'desc' => 'Get YouTrack base URL',
'return' => array('URL' => 'string')
),
array(
'name' => 'login',
'desc' => 'Login to YouTrack',
'return' => array('If logged in or not' => 'bool')
),
array(
'name' => 'logout',
'desc' => 'Logout of YouTrack by deleting cookie',
),
array(
'name' => 'getIssue',
'desc' => 'Get issue by ID',
'params' => array(
'id' => 'string',
),
'return' => array('Issue data' => 'SimpleXMLElement')
),
array(
'name' => 'getIssues',
'desc' => 'Get issues by filter',
'params' => array(
'filter' => 'string',
),
'return' => array('Issues data' => 'SimpleXMLElement')
),
array(
'name' => 'getIssueUrl',
'desc' => 'Get issues by filter',
'params' => array(
'id' => 'string',
),
'return' => array('URL' => 'string')
),
array(
'name' => 'renderIssueTable',
'desc' => 'Render table of issues',
'params' => array(
'R' => 'Doku_Renderer',
'issues' => 'string',
'cols' => 'string'
),
),
);
}
/**
* Send request to REST API
*
* @param string $method HTTP methods: POST, PUT, GET etc
* @param string $endpoint API endpoint
* @param array $params Request params: array("param" => "value") ==> ?param=value
* @return SimpleXMLElement Response
*/
function request($method, $endpoint, $params = false) {
if (!extension_loaded('curl')) {
msg('You need to have curl installed and enabled to use the YouTrack plugin', -1);
return false;
}
$url = $this->getBaseUrl().$endpoint;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($params) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
}
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($params) {
$url = sprintf("%s?%s", $url, http_build_query($params, '', '&'));
}
}
curl_setopt($curl, CURLOPT_URL, $url);
// Optional Authentication:
// curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($curl, CURLOPT_USERPWD, "username:password");
// curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile);
if ($this->cookie) {
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie);
}
$content = curl_exec($curl);
curl_close($curl);
return $content ? $content : null;
}
/**
* Get YouTrack base URL (without ending slash)
*
* @return string URL
*/
function getBaseUrl() {
$url = $this->getConf('url');
if (empty($url)) {
msg('YouTrack URL is not defined.', -1);
}
return substr($url, -1) === '/' ? substr($url, 0, strlen($url)-1) : $url;
}
/**
* Login to YouTrack
*
* @return bool If logged in or not
*/
function login() {
$user = $this->getConf('user');
$password = $this->getConf('password');
$url = $this->getBaseUrl();
if (empty($user) || empty($password) || empty($url)) {
$this->cookie = false;
return false;
}
$this->cookie = tempnam(sys_get_temp_dir(), "CURLCOOKIE");
$content = $this->request(
"POST",
"/rest/user/login",
array(
"login" => $user,
"password" => $password
)
);
if ($content && simplexml_load_string($content) != "ok") {
msg('Login data not correct, or REST login is not enabled.', -1);
return false;
}
return true;
}
/**
* Logout of YouTrack by deleting cookie
*/
function logout() {
if ($this->cookie) {
unlink($this->cookie) or die("Can't unlink $this->cookie");
}
}
/**
* Get issue by ID
*
* @param string $id ID of issue
* @return SimpleXMLElement Issue data
*/
function getIssue($id) {
$this->login();
$xml = $this->request("GET", "/rest/issue/$id");
$this->logout();
return simplexml_load_string($xml);
}
/**
* Get issues by filter
*
* @param string $filter Filter in YouTrack query language
* @return SimpleXMLElement Issues data
*/
function getIssues($filter) {
$this->login();
$xml = $this->request(
"GET",
"/rest/issue/",
array(
'filter' => $filter,
'max' => 100 // TODO: set max via config?
)
);
$this->logout();
return simplexml_load_string($xml);
}
/**
* Get link to issue
*
* @param string $id ID of issue
* @return string
*/
function getIssueUrl($id) {
if (empty($id)) {
return '';
}
return $this->getBaseUrl().'/issue/'.$id;
}
/**
* Render table of issues
*
* @param Doku_Renderer $R Renderer
* @param array $issues Issues with their relevant values
* @param array $cols Columns to show
*/
function renderIssueTable(Doku_Renderer &$R, $issues, $cols) {
$R->table_open(count($cols));
$R->tablethead_open();
$R->tablerow_open();
foreach($cols as $col) {
$R->tableheader_open();
$R->cdata($col);
$R->tableheader_close();
}
$R->tablerow_close();
$R->tablethead_close();
if (method_exists($R, 'tabletbody_open')) {
$R->tabletbody_open();
}
foreach ($issues as $issue) {
$R->tablerow_open();
foreach($cols as $col) {
$R->tablecell_open();
if ($col == 'ID') {
$R->externallink($this->getIssueUrl($issue[$col]), $issue[$col]);
} else {
$R->cdata($issue[$col]);
}
$R->tablecell_close();
}
$R->tablerow_close();
}
if (method_exists($R, 'tabletbody_close')) {
$R->tabletbody_close();
}
$R->table_close();
}
}
// vim:ts=4:sw=4:et: