-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceDeskRest.class.php
More file actions
701 lines (586 loc) · 16.3 KB
/
ServiceDeskRest.class.php
File metadata and controls
701 lines (586 loc) · 16.3 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
<?php
/**
* JIRA REST library
*
* @package lib-jira
* @file ServiceDeskRest.class.php
* @created 11.15.2012
* @author JKCampbell <jimcampbell@psu.edu>
* http://docs.atlassian.com/jira/REST/latest
*/
class ServiceDeskRest {
private $host;
private $username;
private $password;
private $pclient;
/**
* build! destroy!
*
*/
function __construct($host) {
$this->host = $host;
$this->pclient = new PestJSON($host);
}
function __destruct() {
}
/**
* function setHost - sets the host of a jira REST session
*
* @param $host - string, the URL of the jira REST server
* @return
*
*/
public function setHost($host) {
$this->host = $host;
try {
$this->pclient = new PestJSON($host);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
}
/**
* function login - logs into a jira REST session
*
* @param $username - string, jira username
* @param $password - string, jira password
* @return $this->auth - string, jira authorization string
*
*/
public function login($username, $password) {
$this->username = $username;
$this->password = $password;
try {
$this->auth = $this->pclient->post('/rest/auth/1/session', array (
'username' => $username,
'password' => $password
));
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $this->auth;
}
/**
* function logout - destroys the jira REST session
*
* @param
* @return
*
*/
public function logout() {
try {
$results = $this->pclient->delete('/rest/auth/1/session');
$this->pclient->__destruct();
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
}
/**
* function issueAssign - assign a jira issue to a user
*
* @param $rIssueKey - string, the jira issue key
* @param $rUser - string, the jira user
* @return $results - array, complete jira issues with all fields
*
*/
public function issueAssign($rIssueKey, $rUser) {
$userArray = array ();
$userArray['name'] = $rUser;
try {
$results = $this->pclient->put('/rest/api/2/issue/' . $rIssueKey . '/assignee', $userArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueCreate - creates a jira issue
*
* @param $issueObject - object (project, issuetype, and summary are required)
* @return $results - array, a jira issue stub
*
*/
public function issueCreate($issueObject) {
// create issue array
$issueArray = array (
'fields' => array (
'project' => array (
'key' => $issueObject->project
),
'issuetype' => array (
'id' => $issueObject->type
),
)
);
// for code that uses deprecated notation
if (property_exists($issueObject, 'description')) {
$issueArray['fields']['description'] = $issueObject->description;
}
if (property_exists($issueObject, 'summary')) {
$issueArray['fields']['summary'] = $issueObject->summary;
}
// add custom fields
foreach ($issueObject->fields as $key => $value) {
$issueArray['fields'][$key] = $value;
}
// create issue
try {
$results = $this->pclient->post('/rest/api/2/issue', $issueArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueGet - get a jira issue
*
* @param $rIssueKey - string, the jira issue key
* @return $results - array, complete jira issues with all fields
*
*/
public function issueGet($rIssueKey) {
try {
$results = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueDelete - delete a jira issue
*
* @param $rIssueKey - string, the jira issue key
* @param $subtasks - boolean, true or false to delete subtasks
* @return $results - deleted
*
*/
public function issueDelete($rIssueKey, $subtasks = false) {
try {
$results = $this->pclient->delete('/rest/api/2/issue/' . $rIssueKey . '?deleteSubtasks=' . $subtasks);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueKeyChanged - see if issue key changed
*
* @param $rIssueKey - string, the jira issue key
* @return $results - boolean, new key if changed, 0 if not
*
*/
public function issueKeyChanged($rIssueKey) {
try {
$rIssue = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey. '?fields=key');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
if ($rIssue['key'] != $rIssueKey) {
return $rIssue['key'];
} else {
return 0;
}
}
/**
* function issueMetaGet - return issue search results
*
* @param $queryURL - string, the jira query URL
* @return $results - array, results of the query
*
*/
public function issueMetaGet($queryURL) {
try {
$results = $this->pclient->get('/rest/api/2/issue/createmeta' . $queryURL);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueSearch - get a list of issues via jql
*
* @param
* @return $results - array, stubs of issues
*
* https://confluence.atlassian.com/display/JIRA/Advanced+Searching
*
*/
public function issueSearch($queryObject) {
$queryArray = array ();
foreach ($queryObject as $key => $value) {
$queryArray[$key] = $value;
}
try {
$results = $this->pclient->post('/rest/api/2/search', $queryArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueUpdate - update a jira issue
*
* @param $rIssueKey - string, the jira issue key
* @param $issueObject - object, fields set to new values
* @return $results - array, a jira issue stub
*
*/
public function issueUpdate($rIssueKey, $issueObject) {
// create issue array
$issueArray = array ();
foreach ($issueObject->fields as $key => $value) {
$issueArray['fields'][$key] = $value;
}
try {
$results = $this->pclient->put('/rest/api/2/issue/' . $rIssueKey, $issueArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function issueUserAccess - return issue search results
*
* @param $rIssueKey - string, the jira issue key
* @param $username - string, jira username
* @return $results - array, user stubs of users that can browse the issue
*
* admin perms required to see who has browse access to issues
*
*/
public function issueUserAccess($rIssueKey, $username) {
$queryURL = '?username=' . $username;
$queryURL .= '&issueKey=' . $rIssueKey;
try {
$results = $this->pclient->get('/rest/api/2/user/viewissue/search' . $queryURL);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function commentCreate - add a comment to an issue
*
* @param $rIssueKey - string, the jira issue key
* @return $results - array, the full comment
*
*/
public function commentCreate($rIssueKey, $comment) {
$commentArray = array ();
$commentArray['body'] = $comment;
try {
$results = $this->pclient->post('/rest/api/2/issue/' . $rIssueKey . '/comment', $commentArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function commentDelete - delete an issue's comment
*
* @param $rIssueKey - string, the jira issue key
* @param $rCommentId - string, the comment id
* @return $results - array, full comments for the given issue
*
*/
public function commentDelete($rIssueKey, $rCommentId) {
try {
$results = $this->pclient->delete('/rest/api/2/issue/' . $rIssueKey . '/comment/' . $rCommentId);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function commentGet - return an issue's comments
*
* @param $rIssueKey - string, the jira issue key
* @return $results - array, full comments for the given issue
*
*/
public function commentGet($rIssueKey) {
try {
$results = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey . '/comment');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function commentGetId - return an issue's comment by Id
*
* @param $rIssueKey - string, the jira issue key
* @param $rCommentId - string, the comment id
* @return $results - array, full comments for the given issue
*
*/
public function commentGetId($rIssueKey, $rCommentId) {
try {
$results = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey . '/comment/' . $rCommentId);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function transitionGet - get possible transitions for an issue
*
* @param $rIssueKey - string, the jira issue key
* @return $results - array, transition ids
*
*/
public function transitionGet($rIssueKey) {
try {
$rTransitions = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey . '/transitions');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
$transitions = $rTransitions['transitions'];
return $transitions;
}
/**
* function componentGet - get possible components for a project
*
* @param $rProjectKey - string, the jira project key
* @return $results - array, component ids and names
*
*/
public function componentGet($rProjectKey) {
try {
$rComponents = $this->pclient->get('/rest/api/2/project/' . $rProjectKey . '/components');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
$components = array();
foreach ($rComponents as $component) {
$id = $component['id'];
$name = $component['name'];
$components[$id] = $name;
}
return $components;
}
/**
* function transitionDo - transition an issue through the workflow
*
* @param $rIssueKey - string, the jira issue key
* @param $transitionObject - object, the jira issue with transition and field changes
* @return $results - array, full transitions
*
*/
public function transitionDo($rIssueKey, $transitionID) {
// create issue array
$transitionArray = array ();
$transitionArray['transition'] = array (
'id' => $transitionID
);
try {
$results = $this->pclient->post('/rest/api/2/issue/' . $rIssueKey . '/transitions', $transitionArray);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function attachmentCreate - add an attachment to an issue
*
* @param $rIssueKey - string, the jira issue key
* @param $filenames - array, names of attachment files
* @param $filedata - array, base64 encoded file attachment data
* @return $results - array, stub attachment
*
*/
public function attachmentCreate($rIssueKey, $filename) {
// curl via command line
$host = $this->host . '/rest/api/2/issue/' . $rIssueKey . '/attachments';
$command = "curl --silent --insecure -u $this->username:$this->password -X POST -H 'X-Atlassian-Token: nocheck' -F 'file=@$filename' $host";
$output = shell_exec($command);
$results = json_decode($output);
if (!$results) {
$exception = new ServiceDeskException('Create of attachment failed.');
throw ($exception);
}
return $results;
}
/**
* function attachmentGet - get an issue's attachments
*
* @param $rIssueKey - string, the jira issue key
* @return $attachments - array, attachments stubs
*
*/
public function attachmentGet($rIssueKey) {
$attachments = array ();
// get attachment ids
try {
$results = $this->pclient->get('/rest/api/2/issue/' . $rIssueKey . '?fields=attachment');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
// get individual attachments
$stubs = $results['fields']['attachment'];
// foreach ($stubs as $attachment) {
//
// $attachmentId = $attachment['id'];
// try {
// $results = $this->pclient->get('/rest/api/2/attachment/' . $attachmentId);
// } catch (Exception $e) {
// $exception = new ServiceDeskException($e);
// throw ($exception);
// }
// $attachments[] = $results;
// }
return $stubs;
}
/**
* function attachmentGetContent - get attachment content
*
* @param $url - string, the url of the attachment
* @return $content - array, the content of the attachment, 0 - failure
*
*/
public function attachmentGetContent($url) {
// curl via command line
$command = "curl --silent --fail --insecure -u $this->username:$this->password -H 'X-Atlassian-Token: nocheck' $url";
$content = shell_exec($command);
if (!$content) {
$exception = new ServiceDeskException('Get attachment content failed.');
throw ($exception);
}
return $content;
}
/**
* function projectGet - get a list of all projects
*
* @param
* @return $results - array, stubs of projects
*
*/
public function projectGet() {
try {
$results = $this->pclient->get('/rest/api/2/project');
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function userGet - get Jira user
*
* @param $username - string, the username you want to fetch
* @return
*
*/
public function userGet($username) {
try {
$results = $this->pclient->get('/rest/api/2/user?username=' . $username);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function userPermission - get Jira user permissions
*
* @param $username - string, the username you want to check for permissions
* @param $permission - string, the permission you are checking for
* @param $project - string, the project you want to check for user permissions
* @return
*
*/
public function userPermission($username, $project, $permission = 'CREATE_ISSUE') {
try {
$results = $this->pclient->get('/rest/api/2/user/permission/search?username=' . $username . '&permissions=' . $permission . '&projectKey=' . $project);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
/**
* function userSwitch - switch loggen in Jira user
*
* @param $username - string, the username you want to switch to
* @return
*
*/
public function userSwitch($username) {
// reset user password
$password = $this->generatePassword();
try {
$results = $this->pclient->put('/rest/api/2/user/password?username=' . $username, $password);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
// logout of REST
$this->logout();
// login to REST with new user
$this->login($username, $password);
return $this;
}
/**
* function worklogUpdate - update an issue's worklog
*
* @param $key - issue key to update
* @param $worklog - a worklog array
* @return $results - array, stubs of projects
*
*/
public function worklogUpdate($key, $worklog) {
try {
$results = $this->pclient->post('/rest/api/2/issue/' . $key . '/worklog?adjustEstimate=auto', $worklog);
} catch (Exception $e) {
$exception = new ServiceDeskException($e);
throw ($exception);
}
return $results;
}
private function generatePassword($len = 16) {
if (@ is_readable('/dev/urandom')) {
$f = fopen('/dev/urandom', 'r');
$urandom = fread($f, $len);
fclose($f);
}
$return = '';
for ($i = 0; $i < $len; ++ $i) {
if (!isset ($urandom)) {
if ($i % 2 == 0)
mt_srand(time() % 2147 * 1000000 + (double) microtime() * 1000000);
$rand = 48 + mt_rand() % 64;
} else
$rand = 48 + ord($urandom[$i]) % 64;
if ($rand > 57)
$rand += 7;
if ($rand > 90)
$rand += 6;
if ($rand == 123)
$rand = 45;
if ($rand == 124)
$rand = 46;
$return .= chr($rand);
}
return $return;
}
}
?>