Skip to content

Commit 65a076d

Browse files
committed
multiple attachments on issue(using curl-multi).
1 parent 3993865 commit 65a076d

File tree

4 files changed

+107
-52
lines changed

4 files changed

+107
-52
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,15 @@ require 'vendor/autoload.php';
135135

136136
use JiraRestApi\Issue\IssueService;
137137
use JiraRestApi\Issue\IssueField;
138+
139+
$issueKey = "TEST-879";
140+
138141
try {
139142
$issueService = new IssueService();
140143

141-
$ret = $issueService->addAttachments("TEST-879", 'screen_capture.png');
144+
// multiple file upload support.
145+
$ret = $issueService->addAttachments($issueKey,
146+
array('screen_capture.png', 'bug-description.pdf', 'README.md'));
142147

143148
print_r($ret);
144149
} catch (JIRAException $e) {
@@ -157,7 +162,7 @@ require 'vendor/autoload.php';
157162
use JiraRestApi\Issue\IssueService;
158163
use JiraRestApi\Issue\IssueField;
159164

160-
$issueKey = "TEST-920";
165+
$issueKey = "TEST-879";
161166

162167
try {
163168
$issueField = new IssueField(true);
@@ -176,7 +181,6 @@ try {
176181

177182
$ret = $issueService->update($issueKey, $issueField);
178183

179-
180184
} catch (JIRAException $e) {
181185
$this->assertTrue(FALSE, "update Failed : " . $e->getMessage());
182186
}
@@ -206,6 +210,7 @@ Adds a new comment to an issue.
206210
** sub Bullet 2
207211
* Bullet 3
208212
COMMENT;
213+
209214
$comment->setBody($body)
210215
->setVisibility('role', 'Users');
211216
;

src/JiraClient.php

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,7 @@ public function exec($context, $post_data = null, $custom_request = null) {
174174
return $response;
175175
}
176176

177-
/**
178-
* file upload
179-
*
180-
* @param context url context
181-
* @param upload_file upload file.
182-
*
183-
* @todo multiple file upload suppport.
184-
*
185-
*/
186-
public function upload($context, $upload_file) {
187-
$url = $this->host . $this->api_uri . '/' . preg_replace('/\//', '', $context, 1);
188-
177+
private function createUploadHandle($url, $upload_file) {
189178
$ch=curl_init();
190179
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
191180
curl_setopt($ch, CURLOPT_URL, $url);
@@ -227,38 +216,93 @@ public function upload($context, $upload_file) {
227216

228217
curl_setopt($ch, CURLOPT_VERBOSE, $this->CURLOPT_VERBOSE);
229218

230-
$this->log->addDebug('Curl exec=' . $url);
231-
$response = curl_exec($ch);
232-
233-
// if request failed.
234-
if (!$response) {
235-
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
236-
$body = curl_error($ch);
237-
curl_close($ch);
219+
$this->log->addDebug('Curl exec=' . $url);
220+
return $ch;
221+
}
238222

239-
//The server successfully processed the request, but is not returning any content.
240-
if ($this->http_response == 204){
241-
return "";
242-
}
243-
244-
// HostNotFound, No route to Host, etc Network error
245-
$this->log->addError("CURL Error: = " . $body);
246-
throw new JIRAException("CURL Error: = " . $body);
247-
} else {
248-
// if request was ok, parsing http response code.
249-
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
223+
/**
224+
* file upload
225+
*
226+
* @param context url context
227+
* @param filePathArray upload file path.
228+
*
229+
*/
230+
public function upload($context, $filePathArray) {
231+
$url = $this->host . $this->api_uri . '/' . preg_replace('/\//', '', $context, 1);
250232

233+
// return value
234+
$result_code = 200;
235+
236+
$chArr = array();
237+
$results = array();
238+
$mh = curl_multi_init();
239+
240+
for($idx = 0; $idx < count($filePathArray); $idx++) {
241+
$file = $filePathArray[$idx];
242+
if (file_exists($file) == false) {
243+
$body = "File $file not found";
244+
$result_code = -1;
245+
goto end;
246+
}
247+
$chArr[$idx] = $this->createUploadHandle($url, $filePathArray[$idx]);
248+
249+
curl_multi_add_handle($mh, $chArr[$idx]);
250+
}
251+
252+
$running = null;
253+
do {
254+
curl_multi_exec($mh, $running);
255+
}
256+
while ($running > 0);
257+
258+
// Get content and remove handles.
259+
for($idx = 0; $idx < count($chArr); $idx++) {
260+
$ch = $chArr[$idx];
261+
262+
$results[$idx] = curl_multi_getcontent($ch);
263+
264+
// if request failed.
265+
if (!$results[$idx]) {
266+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
267+
$body = curl_error($ch);
268+
269+
//The server successfully processed the request, but is not returning any content.
270+
if ($this->http_response == 204){
271+
continue;
272+
}
273+
274+
// HostNotFound, No route to Host, etc Network error
275+
$result_code = -1;
276+
$body = "CURL Error: = " . $body;
277+
$this->log->addError($body );
278+
} else {
279+
// if request was ok, parsing http response code.
280+
$result_code = $this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
281+
282+
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
283+
if ($this->http_response != 200 && $this->http_response != 201) {
284+
$body = "CURL HTTP Request Failed: Status Code : "
285+
. $this->http_response . ", URL:" . $url
286+
. "\nError Message : " . $response;
287+
$this->log->addError($body);
288+
}
289+
}
290+
}
291+
292+
// clean up
293+
end:
294+
foreach ($chArr as $ch) {
295+
$this->log->addDebug("CURL Close handle..");
251296
curl_close($ch);
297+
curl_multi_remove_handle($mh, $ch);
298+
}
299+
$this->log->addDebug("CURL Multi Close handle..");
300+
curl_multi_close($mh);
301+
if ($result_code != 200) {
302+
throw new JIRAException("CURL Error: = " . $body, $result_code);
303+
}
252304

253-
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
254-
if ($this->http_response != 200 && $this->http_response != 201) {
255-
throw new JIRAException("CURL HTTP Request Failed: Status Code : "
256-
. $this->http_response . ", URL:" . $url
257-
. "\nError Message : " . $response, $this->http_response);
258-
}
259-
}
260-
261-
return $response;
305+
return $results;
262306
}
263307
}
264308

src/issue/IssueService.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,25 @@ public function create($issueField) {
5656
* Add one or more file to an issue
5757
*
5858
* @param issueIdOrKey Issue id or key
59-
* @param filePath attachment file.
59+
* @param filePathArray attachment file path.
6060
*
6161
* @return
6262
*/
63-
public function addAttachments($issueIdOrKey, $filePath) {
63+
public function addAttachments($issueIdOrKey, $filePathArray) {
6464

65-
$this->log->addInfo("addAttachments=\n");
65+
$results = $this->upload($this->uri . "/$issueIdOrKey/attachments", $filePathArray);
6666

67-
$ret = $this->upload($this->uri . "/$issueIdOrKey/attachments", $filePath);
67+
$this->log->addInfo("addAttachments result=" . var_export($results, true));
6868

69-
$issue = $this->json_mapper->mapArray(
70-
json_decode($ret), new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
71-
);
69+
$resArr = array();
70+
foreach($results as $ret) {
71+
array_push($resArr, $this->json_mapper->mapArray(
72+
json_decode($ret), new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
73+
)
74+
);
75+
}
7276

73-
return $issue;
77+
return $resArr;
7478
}
7579

7680
/**

tests/IssueTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public function testAddAttachment($issueKey)
6868

6969
$issueService = new IssueService();
7070

71-
$ret = $issueService->addAttachments($issueKey, 'screen_capture.png');
71+
$ret = $issueService->addAttachments($issueKey,
72+
array('screen_capture.png', 'bug-description.pdf', 'README.md'));
7273

7374
print_r($ret);
7475

@@ -125,6 +126,7 @@ public function testAddcomment($issueKey)
125126
** sub Bullet 1
126127
** sub Bullet 2
127128
COMMENT;
129+
128130
$comment->setBody($body)
129131
->setVisibility('role', 'Users');
130132
;

0 commit comments

Comments
 (0)