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