Skip to content

Commit

Permalink
multiple attachments on issue(using curl-multi).
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed Mar 10, 2015
1 parent 3993865 commit 65a076d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 52 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ require 'vendor/autoload.php';

use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;

$issueKey = "TEST-879";

try {
$issueService = new IssueService();

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

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

$issueKey = "TEST-920";
$issueKey = "TEST-879";

try {
$issueField = new IssueField(true);
Expand All @@ -176,7 +181,6 @@ try {

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


} catch (JIRAException $e) {
$this->assertTrue(FALSE, "update Failed : " . $e->getMessage());
}
Expand Down Expand Up @@ -206,6 +210,7 @@ Adds a new comment to an issue.
** sub Bullet 2
* Bullet 3
COMMENT;

$comment->setBody($body)
->setVisibility('role', 'Users');
;
Expand Down
124 changes: 84 additions & 40 deletions src/JiraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,18 +174,7 @@ public function exec($context, $post_data = null, $custom_request = null) {
return $response;
}

/**
* file upload
*
* @param context url context
* @param upload_file upload file.
*
* @todo multiple file upload suppport.
*
*/
public function upload($context, $upload_file) {
$url = $this->host . $this->api_uri . '/' . preg_replace('/\//', '', $context, 1);

private function createUploadHandle($url, $upload_file) {
$ch=curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
Expand Down Expand Up @@ -227,38 +216,93 @@ public function upload($context, $upload_file) {

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

$this->log->addDebug('Curl exec=' . $url);
$response = curl_exec($ch);

// if request failed.
if (!$response) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);
curl_close($ch);
$this->log->addDebug('Curl exec=' . $url);
return $ch;
}

//The server successfully processed the request, but is not returning any content.
if ($this->http_response == 204){
return "";
}

// HostNotFound, No route to Host, etc Network error
$this->log->addError("CURL Error: = " . $body);
throw new JIRAException("CURL Error: = " . $body);
} else {
// if request was ok, parsing http response code.
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
/**
* file upload
*
* @param context url context
* @param filePathArray upload file path.
*
*/
public function upload($context, $filePathArray) {
$url = $this->host . $this->api_uri . '/' . preg_replace('/\//', '', $context, 1);

// return value
$result_code = 200;

$chArr = array();
$results = array();
$mh = curl_multi_init();

for($idx = 0; $idx < count($filePathArray); $idx++) {
$file = $filePathArray[$idx];
if (file_exists($file) == false) {
$body = "File $file not found";
$result_code = -1;
goto end;
}
$chArr[$idx] = $this->createUploadHandle($url, $filePathArray[$idx]);

curl_multi_add_handle($mh, $chArr[$idx]);
}

$running = null;
do {
curl_multi_exec($mh, $running);
}
while ($running > 0);

// Get content and remove handles.
for($idx = 0; $idx < count($chArr); $idx++) {
$ch = $chArr[$idx];

$results[$idx] = curl_multi_getcontent($ch);

// if request failed.
if (!$results[$idx]) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);

//The server successfully processed the request, but is not returning any content.
if ($this->http_response == 204){
continue;
}

// HostNotFound, No route to Host, etc Network error
$result_code = -1;
$body = "CURL Error: = " . $body;
$this->log->addError($body );
} else {
// if request was ok, parsing http response code.
$result_code = $this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
if ($this->http_response != 200 && $this->http_response != 201) {
$body = "CURL HTTP Request Failed: Status Code : "
. $this->http_response . ", URL:" . $url
. "\nError Message : " . $response;
$this->log->addError($body);
}
}
}

// clean up
end:
foreach ($chArr as $ch) {
$this->log->addDebug("CURL Close handle..");
curl_close($ch);
curl_multi_remove_handle($mh, $ch);
}
$this->log->addDebug("CURL Multi Close handle..");
curl_multi_close($mh);
if ($result_code != 200) {
throw new JIRAException("CURL Error: = " . $body, $result_code);
}

// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
if ($this->http_response != 200 && $this->http_response != 201) {
throw new JIRAException("CURL HTTP Request Failed: Status Code : "
. $this->http_response . ", URL:" . $url
. "\nError Message : " . $response, $this->http_response);
}
}

return $response;
return $results;
}
}

Expand Down
20 changes: 12 additions & 8 deletions src/issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,25 @@ public function create($issueField) {
* Add one or more file to an issue
*
* @param issueIdOrKey Issue id or key
* @param filePath attachment file.
* @param filePathArray attachment file path.
*
* @return
*/
public function addAttachments($issueIdOrKey, $filePath) {
public function addAttachments($issueIdOrKey, $filePathArray) {

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

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

$issue = $this->json_mapper->mapArray(
json_decode($ret), new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
);
$resArr = array();
foreach($results as $ret) {
array_push($resArr, $this->json_mapper->mapArray(
json_decode($ret), new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
)
);
}

return $issue;
return $resArr;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/IssueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public function testAddAttachment($issueKey)

$issueService = new IssueService();

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

print_r($ret);

Expand Down Expand Up @@ -125,6 +126,7 @@ public function testAddcomment($issueKey)
** sub Bullet 1
** sub Bullet 2
COMMENT;

$comment->setBody($body)
->setVisibility('role', 'Users');
;
Expand Down

0 comments on commit 65a076d

Please sign in to comment.