-
Notifications
You must be signed in to change notification settings - Fork 1
/
DoneDone.php
executable file
·319 lines (301 loc) · 10.5 KB
/
DoneDone.php
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
<?php
/**
* @file
* Provide access to the DoneDone IssueTracker API.
*
* @see http://www.getdonedone.com/api
*
* @author Daniel Chen <[email protected]>
*/
class IssueTracker {
protected $baseURL;
protected $username;
protected $password;
protected $token;
/**
* Default constructor
*
* @param string $domain - company's DoneDone domain
* @param string $token - the project API token
* @param string $username - DoneDone username
* @param string $password - DoneDone password
*/
function __construct($domain, $token, $username, $password) {
$this->baseURL = "https://{$domain}.mydonedone.com/IssueTracker/API/";
$this->token = $token;
$this->username = $username;
$this->password = $password;
}
/**
* Calculate signature for each request
*
* @param string $url - DoneDone API url
* @param array $data - optional POST form data
*
* @return string
*/
protected function _calculateSignature($url, array $data = null) {
if ($data) {
ksort($data);
foreach ($data as $key => $value) {
$url .= $key . $value;
}
}
return base64_encode(hash_hmac('sha1', $url, $this->token, true));
}
/**
* Perform generic API calling
*
* This is the base method for all IssueTracker API calls.
*
* @param string $methodURL - IssueTracker method URL
* @param array $data - optional POST form data
* @param array $attachemnts - optional list of file paths
* @param bool $update - flag to indicate if this is a PUT operation
*
* @return string - the JSON string returned from server
*/
public function API($methodURL, array $data = null, array $attachments = null, $update = false) {
$url = $this->baseURL . $methodURL;
try {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'X-DoneDone-Signature: ' . $this->_calculateSignature($url, $data)));
if ($data || $attachments) {
if ($update) {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
} else {
curl_setopt($curl, CURLOPT_POST, true);
}
if ($attachments) {
foreach ($attachments as $key => $value) {
$data['attachment-' . $key] = '@' . $value;
}
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
$result = curl_exec($curl);
$info = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$timeout = 5;
while($info == 409)
{
$timeout *= 2;
sleep($timeout);
$result = curl_exec($curl);
$info = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
curl_close($curl);
return $result;
}
catch (Exception $e)
{
return $e->Message();
}
}
/**
* Get all Projects with API enabled
*
* @param book $loadWithIssues: Passing true will deep load all of the
* projects as well as all of their active issues.
*
* @return string - the JSON string returned from server
*/
public function getProjects($loadWithIssues = false) {
$url = $loadWithIssues ? 'Projects/true' : 'Projects';
return $this->API($url);
}
/**
* Get priority levels
*
* @return string - the JSON string returned from server
*/
public function getPriorityLevels() {
return $this->API('PriorityLevels');
}
/**
* Get all people in a project
*
* @param int $projectID: project id
*
* @return string - the JSON string returned from server
*/
public function getAllPeopleInProject($projectID) {
return $this->API('PeopleInProject/' . $projectID);
}
/**
* Get all issues in a project
*
* @param int $projectID: project id
*
* @return string - the JSON string returned from server
*/
public function getAllIssuesInProject($projectID) {
return $this->API('IssuesInProject/' . $projectID);
}
/**
* Check if an issue exists
*
* @param int $projectID: project id
* @param string $title: required $title.
*
* @return string - the JSON string returned from server
*/
public function doesIssueExist($projectID, $issueID) {
return $this->API("DoesIssueExist/{$projectID}/{$issueID}");
}
/**
* Get potential statuses for issue
*
* Note: If you are an admin, you'll get both all allowed statuses
* as well as ALL statuses back from the server
*
* @param int $projectID: project id
* @param string $title: required $title.
*
* @return string - the JSON string returned from server
*/
public function getPotentialStatusesForIssue($projectID, $issueID) {
return $this->API("PotentialStatusesForIssue/{$projectID}/{$issueID}");
}
/**
* Get issue details
*
* Note: You can use this to check if an issue exists as well,
* since it will return a 404 if the issue does not exist
*
* @param int $projectID: project id
* @param string $title: required $title.
*
* @return string - the JSON string returned from server
*/
public function getIssueDetails($projectID, $issueID) {
return $this->API("Issue/{$projectID}/{$issueID}");
}
/**
* Get a list of people that can be assigend to an issue
*
* @param int $projectID: project id
* @param string $title: required $title.
*
* @return string - the JSON string returned from server
*/
public function getPeopleForIssueAssignment($projectID, $issueID) {
return $this->API("PeopleForIssueAssignment/{$projectID}/{$issueID}");
}
/**
* Create Issue
*
* @param int $projectID: project id
* @param string $title: required $title.
* @param int $priorityID: priority levels.
* @param int $resolverID: person assigned to solve this issue.
* @param int $testerID: person assigned to test and verify if a issue is
* resolved.
* @param string $description: optional description of the issue.
* @param string $tags: a string of tags delimited by comma.
* @param string $watcherIDs: a string of people's id delimited by comma.
* @param array $attachment: list of file paths
*
* @return string - the JSON string returned from server
*/
public function createIssue(
$projectID, $title, $priorityID, $resolverID, $testerID,
$description = null, $tags = null, $watcherIDs = null,
$attachments = null) {
$data = array(
'title' => $title,
'priority_level_id' => $priorityID,
'resolver_id' => $resolverID,
'tester_id' => $testerID,
);
if ($description) {
$data['description'] = $description;
}
if ($tags) {
$data['tags'] = $tags;
}
if ($watcherIDs) {
$data['watcher_id'] = $watcherIDs;
}
return $this->API("Issue/{$projectID}", $data, $attachments);
}
/**
* Create Comment on issue
*
* @param int $projectID: project id
* @param int $issueID: issue id
* @param string $comment: comment string
* @param string $peopleToCCIDs: a string of people to be CCed on this comment,
* delimited by comma.
* @param array $attachments: list of file paths
*
* @return string - the JSON string returned from server
*/
public function createComment(
$projectID, $issueID, $comment,
$peopleToCCIDs = null, $attachments = null) {
$data = array('comment' => $comment);
if ($peopleToCCIDs) {
$data['people_to_cc_ids'] = $peopleToCCIDs;
}
return $this->API("Comment/{$projectID}/{$issueID}", $data, $attachments);
}
/**
* Update Issue
*
* If you provide any parameters then the value you pass will be
* used to update the issue. If you wish to keep the value that's
* already on an issue, then do not provide the parameter in your
* PUT data. Any value you provide, including tags, will overwrite
* the existing values on the issue. If you wish to retain the
* tags for an issue and update it by adding one new tag, then
* you'll have to provide all of the existing tags as well as the
* new tag in your tags parameter, for example.
*
* @param int $projectID: project id
* @param int $issueID: issue id
* @param string $title: required $title
* @param int $priorityID: priority levels
* @param int $resolverID: person assigned to solve this issue
* @param int $testerID: person assigned to test and verify if a issue is
* resolved
* @param string $description: optional description of the issue
* @param string $tags: a string of tags delimited by comma
* @param string $stateID: a valid state that this issue can transition to
* @param array attachments: list of file paths
*
* @return string - the JSON string returned from server
*/
public function updateIssue(
$projectID, $issueID, $stateID = null, array $attachments = null,$title = null, $priorityID = null,
$resolverID = null, $testerID = null, $description = null,
$tags = null, array $attachments = null) {
$data = array();
if ($title) {
$data['title'] = $title;
}
if ($priorityID) {
$data['priority_level_id'] = $priorityID;
}
if ($resolverID) {
$data['resolver_id'] = $resolverID;
}
if ($testerID) {
$data['tester_id'] = $testerID;
}
if ($description) {
$data['description'] = $description;
}
if ($tags) {
$data['tags'] = $tags;
}
if ($stateID) {
$data['state_id'] = $stateID;
}
return $this->API("Issue/{$projectID}/{$issueID}", $data, $attachments, true);
}
}