-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathIssueRelation.php
More file actions
227 lines (192 loc) · 7.31 KB
/
Copy pathIssueRelation.php
File metadata and controls
227 lines (192 loc) · 7.31 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
<?php
declare(strict_types=1);
namespace Redmine\Api;
use Redmine\Client\Client;
use Redmine\Exception;
use Redmine\Exception\MissingParameterException;
use Redmine\Exception\SerializerException;
use Redmine\Exception\UnexpectedResponseException;
use Redmine\Future;
use Redmine\Http\HttpClient;
use Redmine\Http\HttpFactory;
use Redmine\Serializer\JsonSerializer;
/**
* Handling issue relations.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations
*
* @author Kevin Saliou <kevin at saliou dot name>
*/
class IssueRelation extends AbstractApi
{
final public static function fromHttpClient(HttpClient $httpClient): self
{
return new self($httpClient, true);
}
/**
* @deprecated v2.9.0 Use fromHttpClient() instead.
* @see IssueRelation::fromHttpClient()
*
* @param Client|HttpClient $client
*/
public function __construct($client/*, bool $privatelyCalled = false*/)
{
$privatelyCalled = (func_num_args() > 1) ? func_get_arg(1) : false;
if ($privatelyCalled === true) {
parent::__construct($client);
return;
}
if (static::class !== self::class) {
$className = (new \ReflectionClass($this))->isAnonymous() ? '' : ' in `' . static::class . '`';
@trigger_error('Class `' . self::class . '` will declared as final in v3.0.0, stop extending it' . $className . '.', E_USER_DEPRECATED);
} else {
@trigger_error('Method `' . __METHOD__ . '()` is deprecated since v2.9.0 and will declared as private in v3.0.0, use `' . self::class . '::fromHttpClient()` instead.', E_USER_DEPRECATED);
}
parent::__construct($client);
}
/**
* List relations of the given $issueId.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#GET
*
* @param int $issueId the issue id
* @param array<mixed> $params optional parameters to be passed to the api (offset, limit, ...)
*
* @throws UnexpectedResponseException if response body could not be converted into array
*
* @return array<mixed> list of relations found
*/
final public function listByIssueId(int $issueId, array $params = []): array
{
try {
return $this->retrieveData('/issues/' . strval($issueId) . '/relations.json', $params);
} catch (SerializerException $th) {
throw UnexpectedResponseException::create($this->getLastResponse(), $th);
}
}
/**
* List relations of the given issue.
*
* @deprecated v2.4.0 Use listByIssueId() instead.
* @see IssueRelation::listByIssueId()
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#GET
*
* @param int $issueId the issue id
* @param array<mixed> $params optional parameters to be passed to the api (offset, limit, ...)
*
* @return array<mixed>|string|false list of relations found or error message or false
*/
public function all($issueId, array $params = [])
{
@trigger_error('`' . __METHOD__ . '()` is deprecated since v2.4.0, use `' . self::class . '::listByIssueId()` instead.', E_USER_DEPRECATED);
try {
$relations = $this->listByIssueId($issueId, $params);
} catch (Exception $e) {
if ($this->getLastResponse()->getContent() === '') {
return false;
}
if ($e instanceof UnexpectedResponseException && $e->getPrevious() instanceof \Throwable) {
$e = $e->getPrevious();
}
return $e->getMessage();
}
return $relations;
}
/**
* Get extended information about the given relation $id.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#GET-2
*
* @param int $id the relation id
*
* @return array<mixed> relation's details or empty array on error
*/
public function show($id)
{
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeJsonRequest(
'GET',
'/relations/' . urlencode(strval($id)) . '.json',
));
$body = $this->lastResponse->getContent();
if ('' === $body) {
return [];
}
try {
$data = JsonSerializer::createFromString($body)->getNormalized();
} catch (SerializerException $e) {
return [];
}
if (! is_array($data) || ! array_key_exists('relation', $data) || ! is_array($data['relation'])) {
return [];
}
return $data['relation'];
}
/**
* Delete a relation.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#DELETE
*
* @param int $id the relation id
*
* @return string empty string on success
*/
public function remove($id)
{
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeXmlRequest(
'DELETE',
'/relations/' . $id . '.xml',
));
$body = $this->lastResponse->getContent();
$statusCode = $this->lastResponse->getStatusCode();
if ($statusCode !== 200 && $statusCode !== 204) {
if (!Future::isForwardCompatibilityEnabled()) {
return $body;
}
throw UnexpectedResponseException::create($this->lastResponse);
}
return $body;
}
/**
* Create a new issue relation.
*
* @see http://www.redmine.org/projects/redmine/wiki/Rest_IssueRelations#POST
* available $params:
* - issue_to_id (required): the id of the related issue
* - relation_type (required to explicit : default "relates"): the type of relation
* (in: "relates", "duplicates", "duplicated", "blocks", "blocked", "precedes", "follows", "copied_to", "copied_from")
* - delay (optional): the delay for a "precedes" or "follows" relation
*
* @param int $issueId the ID of the issue we are creating the relation on
* @param array<mixed> $params the new issue relation data
*
* @throws MissingParameterException Missing mandatory parameters
*
* @return array<mixed>
*/
public function create($issueId, array $params = [])
{
$defaults = [
'relation_type' => 'relates',
'issue_to_id' => null,
'delay' => null,
];
$params = $this->sanitizeParams($defaults, $params);
if (!isset($params['issue_to_id'])) {
throw new MissingParameterException('Theses parameters are mandatory: `issue_to_id`');
}
$this->lastResponse = $this->getHttpClient()->request(HttpFactory::makeJsonRequest(
'POST',
'/issues/' . urlencode(strval($issueId)) . '/relations.json',
JsonSerializer::createFromArray(['relation' => $params])->getEncoded(),
));
$body = $this->lastResponse->getContent();
$statusCode = $this->lastResponse->getStatusCode();
if ($statusCode !== 201) {
if (!Future::isForwardCompatibilityEnabled()) {
return JsonSerializer::createFromString($body)->getNormalized();
}
throw UnexpectedResponseException::create($this->lastResponse);
}
return JsonSerializer::createFromString($body)->getNormalized();
}
}