|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TestMonitor\DevOps\Actions; |
|
4
|
|
|
|
|
5
|
|
|
use TestMonitor\DevOps\Builders\WIQL\WIQL; |
|
6
|
|
|
use TestMonitor\DevOps\Resources\WorkItem; |
|
7
|
|
|
use TestMonitor\DevOps\Transforms\TransformsWorkItems; |
|
8
|
|
|
|
|
9
|
|
|
trait ManagesWorkItems |
|
10
|
|
|
{ |
|
11
|
|
|
use TransformsWorkItems; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get a single work item. |
|
15
|
|
|
* |
|
16
|
|
|
* @param string $id |
|
17
|
|
|
* @param string $projectId |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \TestMonitor\DevOps\Exceptions\InvalidDataException |
|
20
|
|
|
* |
|
21
|
|
|
* @return \TestMonitor\DevOps\Resources\WorkItem |
|
22
|
|
|
*/ |
|
23
|
4 |
|
public function workitem(string $id, string $projectId): WorkItem |
|
24
|
|
|
{ |
|
25
|
4 |
|
$response = $this->get("{$projectId}/_apis/wit/workitems/{$id}"); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
1 |
|
return $this->fromDevOpsWorkItem($response); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get a list of work items. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $projectId |
|
34
|
|
|
* @param \TestMonitor\DevOps\Builders\WIQL $query |
|
35
|
|
|
* @param int $limit |
|
36
|
|
|
* |
|
37
|
|
|
* @throws \TestMonitor\DevOps\Exceptions\InvalidDataException |
|
38
|
|
|
* |
|
39
|
|
|
* @return \TestMonitor\DevOps\Resources\WorkItem[] |
|
40
|
|
|
*/ |
|
41
|
6 |
|
public function workitems(string $projectId, WIQL $query = null, int $limit = 50): array |
|
42
|
|
|
{ |
|
43
|
|
|
// Retrieve work items using WIQL |
|
44
|
6 |
|
$results = $this->post("{$projectId}/_apis/wit/wiql", [ |
|
|
|
|
|
|
45
|
6 |
|
'json' => [ |
|
46
|
6 |
|
'query' => $query instanceof WIQL ? $query->getQuery() : (new WIQL)->getQuery(), |
|
47
|
6 |
|
'$top' => $limit, |
|
48
|
6 |
|
], |
|
49
|
6 |
|
]); |
|
50
|
|
|
|
|
51
|
|
|
// Return an empty array when there are no results |
|
52
|
3 |
|
if (empty($results['workItems'])) { |
|
53
|
1 |
|
return []; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Gather work item ID's |
|
57
|
2 |
|
$ids = array_column($results['workItems'], 'id'); |
|
58
|
|
|
|
|
59
|
|
|
// Fetch work items by their ID's |
|
60
|
2 |
|
$response = $this->get("{$projectId}/_apis/wit/workitems/", [ |
|
61
|
2 |
|
'query' => [ |
|
62
|
2 |
|
'ids' => implode(',', $ids), |
|
63
|
2 |
|
'api-version' => $this->apiVersion, |
|
64
|
2 |
|
], |
|
65
|
2 |
|
]); |
|
66
|
|
|
|
|
67
|
2 |
|
return $this->fromDevOpsWorkItems($response['value']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Create a new work item. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \TestMonitor\DevOps\Resources\WorkItem $workItem |
|
74
|
|
|
* @param string $projectId |
|
75
|
|
|
* |
|
76
|
|
|
* @throws \TestMonitor\DevOps\Exceptions\InvalidDataException |
|
77
|
|
|
* |
|
78
|
|
|
* @return WorkItem |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function createWorkItem(WorkItem $workItem, string $projectId): WorkItem |
|
81
|
|
|
{ |
|
82
|
2 |
|
$response = $this->post( |
|
83
|
2 |
|
"{$projectId}/_apis/wit/workitems/\${$workItem->workItemType}", |
|
84
|
2 |
|
[ |
|
85
|
2 |
|
'headers' => ['Content-Type' => 'application/json-patch+json'], |
|
86
|
2 |
|
'json' => $this->toNewDevOpsWorkItem($workItem), |
|
87
|
2 |
|
] |
|
88
|
2 |
|
); |
|
89
|
|
|
|
|
90
|
1 |
|
return $this->fromDevOpsWorkItem($response); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Updates an existing work item. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $id |
|
97
|
|
|
* @param string $projectId |
|
98
|
|
|
* @param array $attributes |
|
99
|
|
|
* |
|
100
|
|
|
* @throws \TestMonitor\DevOps\Exceptions\InvalidDataException |
|
101
|
|
|
* |
|
102
|
|
|
* @return WorkItem |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function updateWorkItem(string $id, string $projectId, array $attributes): WorkItem |
|
105
|
|
|
{ |
|
106
|
2 |
|
$response = $this->patch( |
|
|
|
|
|
|
107
|
2 |
|
"{$projectId}/_apis/wit/workitems/{$id}", |
|
108
|
2 |
|
[ |
|
109
|
2 |
|
'headers' => ['Content-Type' => 'application/json-patch+json'], |
|
110
|
2 |
|
'json' => $this->toUpdateDevOpsWorkItem($attributes), |
|
111
|
2 |
|
] |
|
112
|
2 |
|
); |
|
113
|
|
|
|
|
114
|
1 |
|
return $this->fromDevOpsWorkItem($response); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|