Skip to content

Commit

Permalink
fixed: don't working DELETE request.
Browse files Browse the repository at this point in the history
new feature: attachment (get meta, remove)
  • Loading branch information
lesstif committed Jun 2, 2018
1 parent 8885d65 commit 7d11aa6
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 26 deletions.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ $iss = new IssueService(new ArrayConfiguration(
- [Get All Priority list](#get-all-priority-list)
- [Get Priority](#get-priority)

### Attachment
- [Get attachment Info](#get-attachment-info)
- [Remove attachment](#remove-attachment)

#### Get Project Info

[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/project-getProject)
Expand Down Expand Up @@ -1493,6 +1497,76 @@ try {
}
```

#### Get Attachment Info

[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/attachment-getAttachment)

```php
<?php
require 'vendor/autoload.php';

use JiraRestApi\Attachment\AttachmentService;
use JiraRestApi\JiraException;

try {
$attachmentId = 12345;

$atts = new AttachmentService();
$att = $atts->get($attachmentId);

var_dump($att);
} catch (JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
```



Gets the attachment information and saves the attachment into the outDir directory.

```php
<?php
require 'vendor/autoload.php';

use JiraRestApi\Attachment\AttachmentService;
use JiraRestApi\JiraException;

try {
$attachmentId = 12345;
$outDir = "attachment_dir";

$atts = new AttachmentService();
$att = $atts->get($attachmentId, $outDir, $overwrite = true);

var_dump($att);
} catch (JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
```


#### Remove attachment

[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/attachment-removeAttachment)

```php
<?php
require 'vendor/autoload.php';

use JiraRestApi\Attachment\AttachmentService;
use JiraRestApi\JiraException;

try {
$attachmentId = 12345;

$atts = new AttachmentService();

$atts->remove($attachmentId);
} catch (JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
```

# License

Apache V2 License
Expand Down
38 changes: 30 additions & 8 deletions src/Attachment/AttachmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,60 @@
*/
class AttachmentService extends \JiraRestApi\JiraClient
{
private $uri = '/attachment';
private $uri = '/attachment/';

/**
* Returns the meta-data for an attachment, including the URI of the actual attached file.
*
* @param $id
* @param $id string|int attachment Id
* @outDir string downloads the content and store into outDir
* @overwrite boolean determines whether to overwrite the file if it already exists.
*
* @return \JiraRestApi\Issue\Attachment
*
* @throws \JiraRestApi\JiraException
* @throws \JsonMapper_Exception
*/
public function get($id)
public function get($id, $outDir = null, $overwrite = false)
{
$ret = $this->exec($this->uri.'/'.$id, null);
$ret = $this->exec($this->uri.$id, null);

$this->log->addInfo("Result=\n".$ret);

return $this->json_mapper->map(
$attachment = $this->json_mapper->map(
json_decode($ret), new Attachment()
);

if ($outDir == null) {
return $attachment;
}

// download contents
if (! file_exists($outDir)) {
mkdir($outDir);
}

// extract filename
$file = substr(strrchr($attachment->content, '/'), 1);

if (file_exists($outDir.DIRECTORY_SEPARATOR.$file) && $overwrite == false) {
return $attachment;
}

$this->download($attachment->content, $outDir, $file);
}

/**
* Remove an attachment from an issue.
*
* @param $id
* @return string
* @param $id string|int attachment id
* @return boolean
*
* @throws \JiraRestApi\JiraException
*/
public function remove($id)
{
$ret = $this->exec($this->uri.'/'.$id, null, 'DELETE');
$ret = $this->exec($this->uri.$id, null, 'DELETE');

$this->log->addInfo("Result=\n".$ret);

Expand Down
13 changes: 8 additions & 5 deletions src/Issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,26 @@ public function addAttachments($issueIdOrKey, $filePathArray)

$this->log->addInfo('addAttachments result='.var_export($results, true));

$resArr = [];
$attachArr = [];
foreach ($results as $ret) {
$ret = json_decode($ret);
if (is_array($ret)) {
array_push($resArr, $this->json_mapper->mapArray(
$tmpArr = $this->json_mapper->mapArray(
$ret, new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
)
);

foreach($tmpArr as $t) {
array_push($attachArr, $t);
}
} elseif (is_object($ret)) {
array_push($resArr, $this->json_mapper->map(
array_push($attachArr, $this->json_mapper->map(
$ret, new Attachment()
)
);
}
}

return $resArr;
return $attachArr;
}

/**
Expand Down
83 changes: 82 additions & 1 deletion src/JiraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function exec($context, $post_data = null, $custom_request = null)
{
$url = $this->createUrlByContext($context);

$this->log->addDebug("Curl $url JsonData=".$post_data);
$this->log->addInfo("Curl $custom_request: $url JsonData=".$post_data);

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -192,6 +192,10 @@ public function exec($context, $post_data = null, $custom_request = null)
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
} else {
if (!is_null($custom_request) && $custom_request == 'DELETE') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
}
}

$this->authorization($ch);
Expand Down Expand Up @@ -493,4 +497,81 @@ public function toHttpQueryParameter($paramArray)

return $queryParam;
}

/**
* download and save into outDir
*
* @param $url full url
* @param $outDir save dir
* @param $file save filename
* @return bool|mixed
* @throws JiraException
*/
public function download($url, $outDir, $file)
{
$file = fopen($outDir .DIRECTORY_SEPARATOR.$file, 'w');

$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);

// output to file handle
curl_setopt($ch, CURLOPT_FILE, $file);

$this->authorization($ch);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());

// curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
if (!function_exists('ini_get') || !ini_get('open_basedir')) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
}

curl_setopt($ch, CURLOPT_HTTPHEADER,
['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']);

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

$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);
fclose($file);

/*
* 201: The request has been fulfilled, resulting in the creation of a new resource.
* 204: The server successfully processed the request, but is not returning any content.
*/
if ($this->http_response === 204 || $this->http_response === 201) {
return true;
}

// HostNotFound, No route to Host, etc Network error
$msg = sprintf('CURL Error: http response=%d, %s', $this->http_response, $body);

$this->log->addError($msg);

throw new JiraException($msg);
} else {
// if request was ok, parsing http response code.
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
fclose($file);

// 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;
}
}
18 changes: 6 additions & 12 deletions tests/AttachmentTest.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<?php

use JiraRestApi\Attachment\AttachmentService;
use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\RemoteIssueLink;
use JiraRestApi\JiraException;

class AttachmentTest extends PHPUnit_Framework_TestCase
{
public function testGetAttachment()
{
$attachmentId = getenv("ID");
if ($attachmentId == FALSE)
$attachmentId = 12622;
$attachmentId = 12643;

try {
$atts = new AttachmentService();

$att = $atts->get($attachmentId);
$att = $atts->get($attachmentId, "output", true);

dump($att);

return $attachmentId;
} catch (JiraException $e) {
Expand All @@ -25,21 +23,17 @@ public function testGetAttachment()
}

/**
*
* @depends testGetAttachment
*/
public function testRemoveAttachment()
public function testRemoveAttachment($attachmentId)
{
$attachmentId = 12622;
try {
$atts = new AttachmentService();

$atts->remove($attachmentId);

$this->assertGreaterThan(0, count(1));

//$this->assertInstanceOf(RemoteIssueLink::class, $rils[0]);

// return $issueKey;
} catch (HTTPException $e) {
$this->assertTrue(false, $e->getMessage());
}
Expand Down

0 comments on commit 7d11aa6

Please sign in to comment.