Completed
Push — master ( 13f825...37d938 )
by KwangSeob
10s
created

IssueService::search()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 6
dl 0
loc 19
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace JiraRestApi\Issue;
4
5
use JiraRestApi\JiraException;
6
7
class IssueService extends \JiraRestApi\JiraClient
8
{
9
    private $uri = '/issue';
10
11
    /**
12
     * @param $json
13
     *
14
     * @throws \JsonMapper_Exception
15
     *
16
     * @return Issue|object
17
     */
18
    public function getIssueFromJSON($json)
19
    {
20
        $issue = $this->json_mapper->map(
21
            $json, new Issue()
22
        );
23
24
        return $issue;
25
    }
26
27
    /**
28
     *  get all project list.
29
     *
30
     * @param string|int $issueIdOrKey
31
     * @param array      $paramArray   Query Parameter key-value Array.
32
     * @param Issue      $issueObject
33
     *
34
     * @throws JiraException
35
     * @throws \JsonMapper_Exception
36
     *
37
     * @return Issue|object class
38
     */
39
    public function get($issueIdOrKey, $paramArray = [], $issueObject = null)
40
    {
41
        $issueObject = ($issueObject) ? $issueObject : new Issue();
42
43
        $ret = $this->exec($this->uri.'/'.$issueIdOrKey.$this->toHttpQueryParameter($paramArray), null);
44
45
        $this->log->addInfo("Result=\n".$ret);
46
47
        return $issue = $this->json_mapper->map(
0 ignored issues
show
Unused Code introduced by
The assignment to $issue is dead and can be removed.
Loading history...
48
            json_decode($ret), $issueObject
49
        );
50
    }
51
52
    /**
53
     * create new issue.
54
     *
55
     * @param IssueField $issueField
56
     *
57
     * @throws JiraException
58
     * @throws \JsonMapper_Exception
59
     *
60
     * @return Issue|object created issue key
61
     */
62
    public function create($issueField)
63
    {
64
        $issue = new Issue();
65
66
        // serilize only not null field.
67
        $issue->fields = $issueField;
68
69
        $data = json_encode($issue);
70
71
        $this->log->addInfo("Create Issue=\n".$data);
72
73
        $ret = $this->exec($this->uri, $data, 'POST');
74
75
        return $this->getIssueFromJSON(json_decode($ret));
76
    }
77
78
    /**
79
     * Create multiple issues using bulk insert.
80
     *
81
     * @param IssueField[] $issueFields Array of IssueField objects
82
     * @param int          $batchSize   Maximum number of issues to send in each request
83
     *
84
     * @throws JiraException
85
     * @throws \JsonMapper_Exception
86
     *
87
     * @return array Array of results, where each result represents one batch of insertions
88
     */
89
    public function createMultiple($issueFields, $batchSize = 50)
90
    {
91
        $issues = [];
92
93
        foreach ($issueFields as $issueField) {
94
            $issue = new Issue();
95
            $issue->fields = $issueField;
96
            $issues[] = $issue;
97
        }
98
99
        $batches = array_chunk($issues, $batchSize);
100
101
        $results = [];
102
        foreach ($batches as $batch) {
103
            $results = array_merge($results, $this->bulkInsert($batch));
104
        }
105
106
        return $results;
107
    }
108
109
    /**
110
     * Makes API call to bulk insert issues.
111
     *
112
     * @param Issue[] $issues Array of issue arrays that are sent to Jira one by one in single create
113
     *
114
     * @throws JiraException
115
     * @throws \JsonMapper_Exception
116
     *
117
     * @return Issue[] Result of API call to insert many issues
118
     */
119
    private function bulkInsert($issues)
120
    {
121
        $data = json_encode(['issueUpdates' => $issues]);
122
123
        $this->log->addInfo("Create Issues=\n".$data);
124
        $results = $this->exec($this->uri.'/bulk', $data, 'POST');
125
126
        $issues = [];
127
        foreach (json_decode($results)->issues as $result) {
128
            $issues[] = $this->getIssueFromJSON($result);
129
        }
130
131
        return $issues;
132
    }
133
134
    /**
135
     * Add one or more file to an issue.
136
     *
137
     * @param string|int   $issueIdOrKey  Issue id or key
138
     * @param array|string $filePathArray attachment file path.
139
     *
140
     * @throws JiraException
141
     * @throws \JsonMapper_Exception
142
     *
143
     * @return Attachment[]
144
     */
145
    public function addAttachments($issueIdOrKey, $filePathArray)
146
    {
147
        if (is_array($filePathArray) == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
148
            $filePathArray = [$filePathArray];
149
        }
150
151
        $results = $this->upload($this->uri."/$issueIdOrKey/attachments", $filePathArray);
0 ignored issues
show
Bug introduced by
It seems like $filePathArray can also be of type string; however, parameter $filePathArray of JiraRestApi\JiraClient::upload() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

151
        $results = $this->upload($this->uri."/$issueIdOrKey/attachments", /** @scrutinizer ignore-type */ $filePathArray);
Loading history...
152
153
        $this->log->addInfo('addAttachments result='.var_export($results, true));
154
155
        $attachArr = [];
156
        foreach ($results as $ret) {
157
            $ret = json_decode($ret);
158
            if (is_array($ret)) {
159
                $tmpArr = $this->json_mapper->mapArray(
160
                    $ret, new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
161
                );
162
163
                foreach ($tmpArr as $t) {
164
                    array_push($attachArr, $t);
165
                }
166
            } elseif (is_object($ret)) {
167
                array_push($attachArr, $this->json_mapper->map(
168
                    $ret, new Attachment()
169
                    )
170
                );
171
            }
172
        }
173
174
        return $attachArr;
175
    }
176
177
    /**
178
     * update issue.
179
     *
180
     * @param string|int $issueIdOrKey Issue Key
181
     * @param IssueField $issueField   object of Issue class
182
     * @param array      $paramArray   Query Parameter key-value Array.
183
     *
184
     * @throws JiraException
185
     *
186
     * @return string created issue key
187
     */
188
    public function update($issueIdOrKey, $issueField, $paramArray = [])
189
    {
190
        $issue = new Issue();
191
192
        // serilize only not null field.
193
        $issue->fields = $issueField;
194
195
        //$issue = $this->filterNullVariable((array)$issue);
196
197
        $data = json_encode($issue);
198
199
        $this->log->addInfo("Update Issue=\n".$data);
200
201
        $queryParam = '?'.http_build_query($paramArray);
202
203
        $ret = $this->exec($this->uri."/$issueIdOrKey".$queryParam, $data, 'PUT');
204
205
        return $ret;
206
    }
207
208
    /**
209
     * Adds a new comment to an issue.
210
     *
211
     * @param string|int $issueIdOrKey Issue id or key
212
     * @param string     $comment
213
     *
214
     * @throws JiraException
215
     * @throws \JsonMapper_Exception
216
     *
217
     * @return Comment|object Comment class
218
     */
219
    public function addComment($issueIdOrKey, $comment)
220
    {
221
        $this->log->addInfo("addComment=\n");
222
223
        $data = json_encode($comment);
224
225
        $ret = $this->exec($this->uri."/$issueIdOrKey/comment", $data);
226
227
        $this->log->addDebug('add comment result='.var_export($ret, true));
228
        $comment = $this->json_mapper->map(
229
           json_decode($ret), new Comment()
230
        );
231
232
        return $comment;
233
    }
234
235
    /**
236
     * Get a comment on an issue.
237
     *
238
     * @param string|int $issueIdOrKey Issue id or key
239
     * @param string|int $id           Comment id
240
     *
241
     * @throws JiraException
242
     * @throws \JsonMapper_Exception
243
     *
244
     * @return Comment|object Comment class
245
     */
246
    public function getComment($issueIdOrKey, $id)
247
    {
248
        $this->log->addInfo("getComment=\n");
249
250
        $ret = $this->exec($this->uri."/$issueIdOrKey/comment/$id");
251
252
        $this->log->addDebug('get comment result='.var_export($ret, true));
253
        $comment = $this->json_mapper->map(
254
                json_decode($ret), new Comment()
255
                );
256
257
        return $comment;
258
    }
259
260
    /**
261
     * Get all comments on an issue.
262
     *
263
     * @param string|int $issueIdOrKey Issue id or key
264
     *
265
     * @throws JiraException
266
     * @throws \JsonMapper_Exception
267
     *
268
     * @return Comment|object Comment class
269
     */
270
    public function getComments($issueIdOrKey)
271
    {
272
        $this->log->addInfo("getComments=\n");
273
274
        $ret = $this->exec($this->uri."/$issueIdOrKey/comment");
275
276
        $this->log->addDebug('get comments result='.var_export($ret, true));
277
        $comment = $this->json_mapper->map(
278
                json_decode($ret), new Comment()
279
                );
280
281
        return $comment;
282
    }
283
284
    /**
285
     * Delete a comment on an issue.
286
     *
287
     * @param string|int $issueIdOrKey Issue id or key
288
     * @param string|int $id           Comment id
289
     *
290
     * @throws JiraException
291
     *
292
     * @return string|bool
293
     */
294
    public function deleteComment($issueIdOrKey, $id)
295
    {
296
        $this->log->addInfo("deleteComment=\n");
297
298
        $ret = $this->exec($this->uri."/$issueIdOrKey/comment/$id", '', 'DELETE');
299
300
        $this->log->addInfo('delete comment '.$issueIdOrKey.' '.$id.' result='.var_export($ret, true));
301
302
        return $ret;
303
    }
304
305
    /**
306
     * Change a issue assignee.
307
     *
308
     * @param string|int  $issueIdOrKey
309
     * @param string|null $assigneeName Assigns an issue to a user.
310
     *                                  If the assigneeName is "-1" automatic assignee is used.
311
     *                                  A null name will remove the assignee.
312
     *
313
     * @throws JiraException
314
     *
315
     * @return string|bool
316
     */
317
    public function changeAssignee($issueIdOrKey, $assigneeName)
318
    {
319
        $this->log->addInfo("changeAssignee=\n");
320
321
        $ar = ['name' => $assigneeName];
322
323
        $data = json_encode($ar);
324
325
        $ret = $this->exec($this->uri."/$issueIdOrKey/assignee", $data, 'PUT');
326
327
        $this->log->addInfo('change assignee of '.$issueIdOrKey.' to '.$assigneeName.' result='.var_export($ret, true));
328
329
        return $ret;
330
    }
331
332
    /**
333
     * Delete a issue.
334
     *
335
     * @param string|int $issueIdOrKey Issue id or key
336
     * @param array      $paramArray   Query Parameter key-value Array.
337
     *
338
     * @throws JiraException
339
     *
340
     * @return string|bool
341
     */
342
    public function deleteIssue($issueIdOrKey, $paramArray = [])
343
    {
344
        $this->log->addInfo("deleteIssue=\n");
345
346
        $queryParam = '?'.http_build_query($paramArray);
347
348
        $ret = $this->exec($this->uri."/$issueIdOrKey".$queryParam, '', 'DELETE');
349
350
        $this->log->addInfo('delete issue '.$issueIdOrKey.' result='.var_export($ret, true));
351
352
        return $ret;
353
    }
354
355
    /**
356
     * Get a list of the transitions possible for this issue by the current user, along with fields that are required and their types.
357
     *
358
     * @param string|int $issueIdOrKey Issue id or key
359
     *
360
     * @throws JiraException
361
     *
362
     * @return Transition[] array of Transition class
363
     */
364
    public function getTransition($issueIdOrKey)
365
    {
366
        $ret = $this->exec($this->uri."/$issueIdOrKey/transitions");
367
368
        $this->log->addDebug('getTransitions result='.var_export($ret, true));
369
370
        $data = json_encode(json_decode($ret)->transitions);
371
372
        $transitions = $this->json_mapper->mapArray(
373
           json_decode($data), new \ArrayObject(), '\JiraRestApi\Issue\Transition'
374
        );
375
376
        return $transitions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $transitions returns the type ArrayObject which is incompatible with the documented return type JiraRestApi\Issue\Transition[].
Loading history...
377
    }
378
379
    /**
380
     * find transition id by transition's to field name(aka 'Resolved').
381
     *
382
     * @param string|int $issueIdOrKey
383
     * @param string     $transitionToName
384
     *
385
     * @throws JiraException
386
     *
387
     * @return string
388
     */
389
    public function findTransitonId($issueIdOrKey, $transitionToName)
390
    {
391
        $this->log->addDebug('findTransitonId=');
392
393
        $ret = $this->getTransition($issueIdOrKey);
394
395
        foreach ($ret as $trans) {
396
            $toName = $trans->to->name;
397
398
            $this->log->addDebug('getTransitions result='.var_export($ret, true));
399
400
            if (strcmp($toName, $transitionToName) == 0) {
401
                return $trans->id;
402
            }
403
        }
404
405
        // transition keyword not found
406
        throw new JiraException("Transition name '$transitionToName' not found on JIRA Server.");
407
    }
408
409
    /**
410
     * Perform a transition on an issue.
411
     *
412
     * @param string|int $issueIdOrKey Issue id or key
413
     * @param Transition $transition
414
     *
415
     * @throws JiraException
416
     *
417
     * @return string|null nothing - if transition was successful return http 204(no contents)
418
     */
419
    public function transition($issueIdOrKey, $transition)
420
    {
421
        $this->log->addDebug('transition='.var_export($transition, true));
422
423
        if (!isset($transition->transition['id'])) {
424
            $transition->transition['id'] = $this->findTransitonId($issueIdOrKey, $transition->transition['name']);
425
        }
426
427
        $data = json_encode($transition);
428
429
        $this->log->addDebug("transition req=$data\n");
430
431
        $ret = $this->exec($this->uri."/$issueIdOrKey/transitions", $data, 'POST');
432
433
        $this->log->addDebug('getTransitions result='.var_export($ret, true));
434
435
        return $ret;
436
    }
437
438
    /**
439
     * Search issues.
440
     *
441
     * @param string $jql
442
     * @param int    $startAt
443
     * @param int    $maxResults
444
     * @param array  $fields
445
     * @param array  $expand
446
     * @param bool   $validateQuery
447
     *
448
     * @throws JiraException
449
     * @throws \JsonMapper_Exception
450
     *
451
     * @return IssueSearchResult|object
452
     */
453
    public function search($jql, $startAt = 0, $maxResults = 15, $fields = [], $expand = [], $validateQuery = true)
454
    {
455
        $data = json_encode([
456
            'jql'           => $jql,
457
            'startAt'       => $startAt,
458
            'maxResults'    => $maxResults,
459
            'fields'        => $fields,
460
            'expand'        => $expand,
461
            'validateQuery' => $validateQuery,
462
        ]);
463
464
        $ret = $this->exec('search', $data, 'POST');
465
        $json = json_decode($ret);
466
467
        $result = $this->json_mapper->map(
468
            $json, new IssueSearchResult()
469
        );
470
471
        return $result;
472
    }
473
474
    /**
475
     * get TimeTracking info.
476
     *
477
     * @param string|int $issueIdOrKey
478
     *
479
     * @throws JiraException
480
     * @throws \JsonMapper_Exception
481
     *
482
     * @return TimeTracking
483
     */
484
    public function getTimeTracking($issueIdOrKey)
485
    {
486
        $ret = $this->exec($this->uri."/$issueIdOrKey", null);
487
        $this->log->addDebug("getTimeTracking res=$ret\n");
488
489
        $issue = $this->json_mapper->map(
490
             json_decode($ret), new Issue()
491
        );
492
493
        return $issue->fields->timeTracking;
494
    }
495
496
    /**
497
     * TimeTracking issues.
498
     *
499
     * @param string|int   $issueIdOrKey Issue id or key
500
     * @param TimeTracking $timeTracking
501
     *
502
     * @throws JiraException
503
     *
504
     * @return string
505
     */
506
    public function timeTracking($issueIdOrKey, $timeTracking)
507
    {
508
        $array = [
509
            'update' => [
510
                'timetracking' => [
511
                    ['edit' => $timeTracking],
512
                ],
513
            ],
514
        ];
515
516
        $data = json_encode($array);
517
518
        $this->log->addDebug("TimeTracking req=$data\n");
519
520
        // if success, just return HTTP 201.
521
        $ret = $this->exec($this->uri."/$issueIdOrKey", $data, 'PUT');
522
523
        return $ret;
524
    }
525
526
    /**
527
     * get getWorklog.
528
     *
529
     * @param string|int $issueIdOrKey
530
     *
531
     * @throws JiraException
532
     * @throws \JsonMapper_Exception
533
     *
534
     * @return PaginatedWorklog|object
535
     */
536
    public function getWorklog($issueIdOrKey)
537
    {
538
        $ret = $this->exec($this->uri."/$issueIdOrKey/worklog");
539
        $this->log->addDebug("getWorklog res=$ret\n");
540
        $worklog = $this->json_mapper->map(
541
            json_decode($ret), new PaginatedWorklog()
542
        );
543
544
        return $worklog;
545
    }
546
547
    /**
548
     * get getWorklog by Id.
549
     *
550
     * @param string|int $issueIdOrKey
551
     * @param int        $workLogId
552
     *
553
     * @throws JiraException
554
     * @throws \JsonMapper_Exception
555
     *
556
     * @return Worklog|object PaginatedWorklog object
557
     */
558
    public function getWorklogById($issueIdOrKey, $workLogId)
559
    {
560
        $ret = $this->exec($this->uri."/$issueIdOrKey/worklog/$workLogId");
561
        $this->log->addDebug("getWorklogById res=$ret\n");
562
        $worklog = $this->json_mapper->map(
563
            json_decode($ret), new Worklog()
564
        );
565
566
        return $worklog;
567
    }
568
569
    /**
570
     * add work log to issue.
571
     *
572
     * @param string|int     $issueIdOrKey
573
     * @param Worklog|object $worklog
574
     *
575
     * @throws JiraException
576
     * @throws \JsonMapper_Exception
577
     *
578
     * @return Worklog|object Worklog Object
579
     */
580
    public function addWorklog($issueIdOrKey, $worklog)
581
    {
582
        $this->log->addInfo("addWorklog=\n");
583
584
        $data = json_encode($worklog);
585
        $url = $this->uri."/$issueIdOrKey/worklog";
586
        $type = 'POST';
587
588
        $ret = $this->exec($url, $data, $type);
589
590
        $ret_worklog = $this->json_mapper->map(
591
           json_decode($ret), new Worklog()
592
        );
593
594
        return $ret_worklog;
595
    }
596
597
    /**
598
     * edit the worklog.
599
     *
600
     * @param string|int     $issueIdOrKey
601
     * @param Worklog|object $worklog
602
     * @param string|int     $worklogId
603
     *
604
     * @throws JiraException
605
     * @throws \JsonMapper_Exception
606
     *
607
     * @return Worklog|object
608
     */
609
    public function editWorklog($issueIdOrKey, $worklog, $worklogId)
610
    {
611
        $this->log->addInfo("editWorklog=\n");
612
613
        $data = json_encode($worklog);
614
        $url = $this->uri."/$issueIdOrKey/worklog/$worklogId";
615
        $type = 'PUT';
616
617
        $ret = $this->exec($url, $data, $type);
618
619
        $ret_worklog = $this->json_mapper->map(
620
            json_decode($ret), new Worklog()
621
        );
622
623
        return $ret_worklog;
624
    }
625
626
    /**
627
     * Get all priorities.
628
     *
629
     * @throws JiraException
630
     *
631
     * @return Priority[] array of priority class
632
     */
633
    public function getAllPriorities()
634
    {
635
        $ret = $this->exec('priority', null);
636
637
        $priorities = $this->json_mapper->mapArray(
638
             json_decode($ret, false), new \ArrayObject(), '\JiraRestApi\Issue\Priority'
639
        );
640
641
        return $priorities;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $priorities returns the type ArrayObject which is incompatible with the documented return type JiraRestApi\Issue\Priority[].
Loading history...
642
    }
643
644
    /**
645
     * Get priority by id.
646
     * throws  HTTPException if the priority is not found, or the calling user does not have permission or view it.
647
     *
648
     * @param string|int $priorityId Id of priority.
649
     *
650
     * @throws JiraException
651
     * @throws \JsonMapper_Exception
652
     *
653
     * @return Priority|object priority
654
     */
655
    public function getPriority($priorityId)
656
    {
657
        $ret = $this->exec("priority/$priorityId", null);
658
659
        $this->log->addInfo('Result='.$ret);
660
661
        $prio = $this->json_mapper->map(
662
             json_decode($ret), new Priority()
663
        );
664
665
        return $prio;
666
    }
667
668
    /**
669
     * Get priority by id.
670
     * throws HTTPException if the priority is not found, or the calling user does not have permission or view it.
671
     *
672
     * @param string|int $priorityId Id of priority.
673
     *
674
     * @throws JiraException
675
     * @throws \JsonMapper_Exception
676
     *
677
     * @return Priority|object priority
678
     */
679
    public function getCustomFields($priorityId)
680
    {
681
        $ret = $this->exec("priority/$priorityId", null);
682
683
        $this->log->addInfo('Result='.$ret);
684
685
        $prio = $this->json_mapper->map(
686
            json_decode($ret), new Priority()
687
        );
688
689
        return $prio;
690
    }
691
692
    /**
693
     * get watchers.
694
     *
695
     * @param $issueIdOrKey
696
     *
697
     * @throws JiraException
698
     *
699
     * @return Reporter[]
700
     */
701
    public function getWatchers($issueIdOrKey)
702
    {
703
        $this->log->addInfo("getWatchers=\n");
704
705
        $url = $this->uri."/$issueIdOrKey/watchers";
706
707
        $ret = $this->exec($url, null);
708
709
        $watchers = $this->json_mapper->mapArray(
710
            json_decode($ret, false)->watchers, new \ArrayObject(), '\JiraRestApi\Issue\Reporter'
711
        );
712
713
        return $watchers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $watchers returns the type ArrayObject which is incompatible with the documented return type JiraRestApi\Issue\Reporter[].
Loading history...
714
    }
715
716
    /**
717
     * add watcher to issue.
718
     *
719
     * @param string|int $issueIdOrKey
720
     * @param string     $watcher      watcher id
721
     *
722
     * @throws JiraException
723
     *
724
     * @return bool
725
     */
726
    public function addWatcher($issueIdOrKey, $watcher)
727
    {
728
        $this->log->addInfo("addWatcher=\n");
729
730
        $data = json_encode($watcher);
731
        $url = $this->uri."/$issueIdOrKey/watchers";
732
        $type = 'POST';
733
734
        $this->exec($url, $data, $type);
735
736
        return $this->http_response == 204 ? true : false;
737
    }
738
739
    /**
740
     * Get the meta data for creating issues.
741
     *
742
     * @param array $paramArray Possible keys for $paramArray: 'projectIds', 'projectKeys', 'issuetypeIds', 'issuetypeNames'.
743
     * @param bool  $expand     Retrieve all issue fields and values
744
     *
745
     * @throws JiraException
746
     *
747
     * @return object array of meta data for creating issues.
748
     */
749
    public function getCreateMeta($paramArray = [], $expand = true)
750
    {
751
        $paramArray['expand'] = ($expand) ? 'projects.issuetypes.fields' : null;
752
        $paramArray = array_filter($paramArray);
753
754
        $queryParam = '?'.http_build_query($paramArray);
755
756
        $ret = $this->exec($this->uri.'/createmeta'.$queryParam, null);
757
758
        return json_decode($ret);
759
    }
760
761
    /**
762
     * returns the metadata(include custom field) for an issue.
763
     *
764
     * @param string $idOrKey                issue id or key
765
     * @param bool   $overrideEditableFlag   Allows retrieving edit metadata for fields in non-editable status
766
     * @param bool   $overrideScreenSecurity Allows retrieving edit metadata for the fields hidden on Edit screen.
767
     *
768
     * @throws JiraException
769
     *
770
     * @return array of custom fields
771
     *
772
     * @see https://confluence.atlassian.com/jirakb/how-to-retrieve-available-options-for-a-multi-select-customfield-via-jira-rest-api-815566715.html How to retrieve available options for a multi-select customfield via JIRA REST API
773
     * @see https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-issue-issueIdOrKey-editmeta-get
774
     */
775
    public function getEditMeta($idOrKey, $overrideEditableFlag = false, $overrideScreenSecurity = false)
776
    {
777
        $queryParam = '?'.http_build_query([
778
            'overrideEditableFlag'   => $overrideEditableFlag,
779
            'overrideScreenSecurity' => $overrideScreenSecurity,
780
            ]);
781
782
        $uri = sprintf('%s/%s/editmeta', $this->uri, $idOrKey).$queryParam;
783
784
        $ret = $this->exec($uri, null);
785
786
        $metas = json_decode($ret, true);
787
788
        // extract only custom field(startWith customefield_XXXXX)
789
        $cfs = array_filter($metas['fields'], function ($key) {
790
            $pos = strpos($key, 'customfield');
791
792
            return $pos !== false;
793
        }, ARRAY_FILTER_USE_KEY);
794
795
        return $cfs;
796
    }
797
798
    /**
799
     * Sends a notification (email) to the list or recipients defined in the request.
800
     *
801
     * @param string|int $issueIdOrKey Issue id Or Key
802
     * @param Notify     $notify
803
     *
804
     * @throws JiraException
805
     *
806
     * @see https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/issue-notify
807
     */
808
    public function notify($issueIdOrKey, $notify)
809
    {
810
        $full_uri = $this->uri."/$issueIdOrKey/notify";
811
812
        // set self value
813
        foreach ($notify->to['groups'] as &$g) {
814
            $g['self'] = $this->getConfiguration()->getJiraHost().'/rest/api/2/group?groupname='.$g['name'];
815
        }
816
        foreach ($notify->restrict['groups'] as &$g) {
817
            $g['self'] = $this->getConfiguration()->getJiraHost().'/rest/api/2/group?groupname='.$g['name'];
818
        }
819
820
        $data = json_encode($notify, JSON_UNESCAPED_SLASHES);
821
822
        $this->log->addDebug("notify=$data\n");
823
824
        $ret = $this->exec($full_uri, $data, 'POST');
825
826
        if ($ret !== true) {
0 ignored issues
show
introduced by
The condition $ret !== true is always true.
Loading history...
827
            throw new JiraException('notify failed: response code='.$ret);
828
        }
829
    }
830
831
    /**
832
     * Get a remote issue links on the issue.
833
     *
834
     * @param string|int $issueIdOrKey Issue id Or Key
835
     *
836
     * @throws JiraException
837
     *
838
     * @return array array os RemoteIssueLink class
839
     *
840
     * @see https://developer.atlassian.com/server/jira/platform/jira-rest-api-for-remote-issue-links/
841
     * @see https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/issue-getRemoteIssueLinks
842
     */
843
    public function getRemoteIssueLink($issueIdOrKey)
844
    {
845
        $full_uri = $this->uri."/$issueIdOrKey/remotelink";
846
847
        $ret = $this->exec($full_uri, null);
848
849
        $rils = $this->json_mapper->mapArray(
850
            json_decode($ret, false), new \ArrayObject(), RemoteIssueLink::class
851
        );
852
853
        return $rils;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $rils returns the type ArrayObject which is incompatible with the documented return type array.
Loading history...
854
    }
855
856
    /**
857
     * @param string|int      $issueIdOrKey
858
     * @param RemoteIssueLink $ril
859
     *
860
     * @throws JiraException
861
     * @throws \JsonMapper_Exception
862
     *
863
     * @return object
864
     */
865
    public function createOrUpdateRemoteIssueLink($issueIdOrKey, RemoteIssueLink $ril)
866
    {
867
        $full_uri = $this->uri."/$issueIdOrKey/remotelink";
868
869
        $data = json_encode($ril, JSON_UNESCAPED_SLASHES);
870
871
        $this->log->addDebug("create remoteIssueLink=$data\n");
872
873
        $ret = $this->exec($full_uri, $data, 'POST');
874
875
        $res = $this->json_mapper->map(
876
            json_decode($ret), new RemoteIssueLink()
877
        );
878
879
        return $res;
880
    }
881
882
    /**
883
     * @param string|int $issueIdOrKey
884
     * @param string|int $globalId
885
     *
886
     * @throws JiraException
887
     *
888
     * @return string|bool
889
     */
890
    public function removeRemoteIssueLink($issueIdOrKey, $globalId)
891
    {
892
        $query = http_build_query(['globalId' => $globalId]);
893
894
        $full_uri = sprintf('%s/%s/remotelink?%s', $this->uri, $issueIdOrKey, $query);
895
896
        $ret = $this->exec($full_uri, '', 'DELETE');
897
898
        $this->log->addInfo(
899
            sprintf(
900
                'delete remote issue link for issue "%s" with globalId "%s". Result=%s',
901
                $issueIdOrKey,
902
                $globalId,
903
                var_export($ret, true)
904
            )
905
        );
906
907
        return $ret;
908
    }
909
910
    /**
911
     * get all issue security schemes.
912
     *
913
     * @throws JiraException
914
     * @throws \JsonMapper_Exception
915
     *
916
     * @return SecurityScheme[] array of SecurityScheme class
917
     */
918
    public function getAllIssueSecuritySchemes()
919
    {
920
        $url = '/issuesecurityschemes';
921
922
        $ret = $this->exec($url);
923
924
        $data = json_decode($ret, true);
925
926
        // extract schem field
927
        $schemes = json_decode(json_encode($data['issueSecuritySchemes']), false);
928
929
        $res = $this->json_mapper->mapArray(
930
            $schemes, new \ArrayObject(), '\JiraRestApi\Issue\SecurityScheme'
931
        );
932
933
        return $res;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $res returns the type ArrayObject which is incompatible with the documented return type JiraRestApi\Issue\SecurityScheme[].
Loading history...
934
    }
935
936
    /**
937
     *  get issue security scheme.
938
     *
939
     * @param int $securityId security scheme id
940
     *
941
     * @throws JiraException
942
     * @throws \JsonMapper_Exception
943
     *
944
     * @return SecurityScheme SecurityScheme
945
     */
946
    public function getIssueSecuritySchemes($securityId)
947
    {
948
        $url = '/issuesecurityschemes/'.$securityId;
949
950
        $ret = $this->exec($url);
951
952
        $res = $this->json_mapper->map(
953
            json_decode($ret), new SecurityScheme()
954
        );
955
956
        return $res;
957
    }
958
959
    /**
960
     * convenient wrapper function for add or remove labels.
961
     *
962
     * @param string|int $issueIdOrKey
963
     * @param array|null $addLablesParam
964
     * @param array|null $removeLabelsParam
965
     * @param bool       $notifyUsers
966
     *
967
     * @throws JiraException
968
     *
969
     * @return Issue|object class
970
     */
971
    public function updateLabels($issueIdOrKey, $addLablesParam, $removeLabelsParam, $notifyUsers = true)
972
    {
973
        $labels = [];
974
        foreach ($addLablesParam as $a) {
975
            array_push($labels, ['add' => $a]);
976
        }
977
978
        foreach ($removeLabelsParam as $r) {
979
            array_push($labels, ['remove' => $r]);
980
        }
981
982
        $postData = json_encode([
983
            'update' => [
984
                'labels' => $labels,
985
            ],
986
        ], JSON_UNESCAPED_UNICODE);
987
988
        $this->log->addInfo("Update labels=\n".$postData);
989
990
        $queryParam = '?'.http_build_query(['notifyUsers' => $notifyUsers]);
991
992
        $ret = $this->exec($this->uri."/$issueIdOrKey".$queryParam, $postData, 'PUT');
993
994
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type string which is incompatible with the documented return type JiraRestApi\Issue\Issue|object.
Loading history...
995
    }
996
}
997