Skip to content

Commit 7d11aa6

Browse files
committed
fixed: don't working DELETE request.
new feature: attachment (get meta, remove)
1 parent 8885d65 commit 7d11aa6

File tree

5 files changed

+200
-26
lines changed

5 files changed

+200
-26
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ $iss = new IssueService(new ArrayConfiguration(
155155
- [Get All Priority list](#get-all-priority-list)
156156
- [Get Priority](#get-priority)
157157

158+
### Attachment
159+
- [Get attachment Info](#get-attachment-info)
160+
- [Remove attachment](#remove-attachment)
161+
158162
#### Get Project Info
159163

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

1500+
#### Get Attachment Info
1501+
1502+
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/attachment-getAttachment)
1503+
1504+
```php
1505+
<?php
1506+
require 'vendor/autoload.php';
1507+
1508+
use JiraRestApi\Attachment\AttachmentService;
1509+
use JiraRestApi\JiraException;
1510+
1511+
try {
1512+
$attachmentId = 12345;
1513+
1514+
$atts = new AttachmentService();
1515+
$att = $atts->get($attachmentId);
1516+
1517+
var_dump($att);
1518+
} catch (JiraException $e) {
1519+
print("Error Occured! " . $e->getMessage());
1520+
}
1521+
```
1522+
1523+
1524+
1525+
Gets the attachment information and saves the attachment into the outDir directory.
1526+
1527+
```php
1528+
<?php
1529+
require 'vendor/autoload.php';
1530+
1531+
use JiraRestApi\Attachment\AttachmentService;
1532+
use JiraRestApi\JiraException;
1533+
1534+
try {
1535+
$attachmentId = 12345;
1536+
$outDir = "attachment_dir";
1537+
1538+
$atts = new AttachmentService();
1539+
$att = $atts->get($attachmentId, $outDir, $overwrite = true);
1540+
1541+
var_dump($att);
1542+
} catch (JiraException $e) {
1543+
print("Error Occured! " . $e->getMessage());
1544+
}
1545+
```
1546+
1547+
1548+
#### Remove attachment
1549+
1550+
[See Jira API reference](https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#api/2/attachment-removeAttachment)
1551+
1552+
```php
1553+
<?php
1554+
require 'vendor/autoload.php';
1555+
1556+
use JiraRestApi\Attachment\AttachmentService;
1557+
use JiraRestApi\JiraException;
1558+
1559+
try {
1560+
$attachmentId = 12345;
1561+
1562+
$atts = new AttachmentService();
1563+
1564+
$atts->remove($attachmentId);
1565+
} catch (JiraException $e) {
1566+
print("Error Occured! " . $e->getMessage());
1567+
}
1568+
```
1569+
14961570
# License
14971571

14981572
Apache V2 License

src/Attachment/AttachmentService.php

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,60 @@
1212
*/
1313
class AttachmentService extends \JiraRestApi\JiraClient
1414
{
15-
private $uri = '/attachment';
15+
private $uri = '/attachment/';
1616

1717
/**
1818
* Returns the meta-data for an attachment, including the URI of the actual attached file.
1919
*
20-
* @param $id
20+
* @param $id string|int attachment Id
21+
* @outDir string downloads the content and store into outDir
22+
* @overwrite boolean determines whether to overwrite the file if it already exists.
23+
*
2124
* @return \JiraRestApi\Issue\Attachment
2225
*
2326
* @throws \JiraRestApi\JiraException
2427
* @throws \JsonMapper_Exception
2528
*/
26-
public function get($id)
29+
public function get($id, $outDir = null, $overwrite = false)
2730
{
28-
$ret = $this->exec($this->uri.'/'.$id, null);
31+
$ret = $this->exec($this->uri.$id, null);
2932

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

32-
return $this->json_mapper->map(
35+
$attachment = $this->json_mapper->map(
3336
json_decode($ret), new Attachment()
3437
);
38+
39+
if ($outDir == null) {
40+
return $attachment;
41+
}
42+
43+
// download contents
44+
if (! file_exists($outDir)) {
45+
mkdir($outDir);
46+
}
47+
48+
// extract filename
49+
$file = substr(strrchr($attachment->content, '/'), 1);
50+
51+
if (file_exists($outDir.DIRECTORY_SEPARATOR.$file) && $overwrite == false) {
52+
return $attachment;
53+
}
54+
55+
$this->download($attachment->content, $outDir, $file);
3556
}
3657

3758
/**
3859
* Remove an attachment from an issue.
3960
*
40-
* @param $id
41-
* @return string
61+
* @param $id string|int attachment id
62+
* @return boolean
63+
*
4264
* @throws \JiraRestApi\JiraException
4365
*/
4466
public function remove($id)
4567
{
46-
$ret = $this->exec($this->uri.'/'.$id, null, 'DELETE');
68+
$ret = $this->exec($this->uri.$id, null, 'DELETE');
4769

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

src/Issue/IssueService.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,26 @@ public function addAttachments($issueIdOrKey, $filePathArray)
152152

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

155-
$resArr = [];
155+
$attachArr = [];
156156
foreach ($results as $ret) {
157157
$ret = json_decode($ret);
158158
if (is_array($ret)) {
159-
array_push($resArr, $this->json_mapper->mapArray(
159+
$tmpArr = $this->json_mapper->mapArray(
160160
$ret, new \ArrayObject(), '\JiraRestApi\Issue\Attachment'
161-
)
162161
);
162+
163+
foreach($tmpArr as $t) {
164+
array_push($attachArr, $t);
165+
}
163166
} elseif (is_object($ret)) {
164-
array_push($resArr, $this->json_mapper->map(
167+
array_push($attachArr, $this->json_mapper->map(
165168
$ret, new Attachment()
166169
)
167170
);
168171
}
169172
}
170173

171-
return $resArr;
174+
return $attachArr;
172175
}
173176

174177
/**

src/JiraClient.php

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function exec($context, $post_data = null, $custom_request = null)
173173
{
174174
$url = $this->createUrlByContext($context);
175175

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

178178
$ch = curl_init();
179179
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@@ -192,6 +192,10 @@ public function exec($context, $post_data = null, $custom_request = null)
192192
curl_setopt($ch, CURLOPT_POST, true);
193193
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
194194
}
195+
} else {
196+
if (!is_null($custom_request) && $custom_request == 'DELETE') {
197+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
198+
}
195199
}
196200

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

494498
return $queryParam;
495499
}
500+
501+
/**
502+
* download and save into outDir
503+
*
504+
* @param $url full url
505+
* @param $outDir save dir
506+
* @param $file save filename
507+
* @return bool|mixed
508+
* @throws JiraException
509+
*/
510+
public function download($url, $outDir, $file)
511+
{
512+
$file = fopen($outDir .DIRECTORY_SEPARATOR.$file, 'w');
513+
514+
$ch = curl_init();
515+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
516+
curl_setopt($ch, CURLOPT_URL, $url);
517+
518+
// output to file handle
519+
curl_setopt($ch, CURLOPT_FILE, $file);
520+
521+
$this->authorization($ch);
522+
523+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $this->getConfiguration()->isCurlOptSslVerifyHost());
524+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->getConfiguration()->isCurlOptSslVerifyPeer());
525+
526+
// curl_setopt(): CURLOPT_FOLLOWLOCATION cannot be activated when an open_basedir is set
527+
if (!function_exists('ini_get') || !ini_get('open_basedir')) {
528+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
529+
}
530+
531+
curl_setopt($ch, CURLOPT_HTTPHEADER,
532+
['Accept: */*', 'Content-Type: application/json', 'X-Atlassian-Token: no-check']);
533+
534+
curl_setopt($ch, CURLOPT_VERBOSE, $this->getConfiguration()->isCurlOptVerbose());
535+
536+
$this->log->addDebug('Curl exec='.$url);
537+
$response = curl_exec($ch);
538+
539+
// if request failed.
540+
if (!$response) {
541+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
542+
$body = curl_error($ch);
543+
curl_close($ch);
544+
fclose($file);
545+
546+
/*
547+
* 201: The request has been fulfilled, resulting in the creation of a new resource.
548+
* 204: The server successfully processed the request, but is not returning any content.
549+
*/
550+
if ($this->http_response === 204 || $this->http_response === 201) {
551+
return true;
552+
}
553+
554+
// HostNotFound, No route to Host, etc Network error
555+
$msg = sprintf('CURL Error: http response=%d, %s', $this->http_response, $body);
556+
557+
$this->log->addError($msg);
558+
559+
throw new JiraException($msg);
560+
} else {
561+
// if request was ok, parsing http response code.
562+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
563+
564+
curl_close($ch);
565+
fclose($file);
566+
567+
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
568+
if ($this->http_response != 200 && $this->http_response != 201) {
569+
throw new JiraException('CURL HTTP Request Failed: Status Code : '
570+
.$this->http_response.', URL:'.$url
571+
."\nError Message : ".$response, $this->http_response);
572+
}
573+
}
574+
575+
return $response;
576+
}
496577
}

tests/AttachmentTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
22

33
use JiraRestApi\Attachment\AttachmentService;
4-
use JiraRestApi\Issue\IssueService;
5-
use JiraRestApi\Issue\RemoteIssueLink;
64
use JiraRestApi\JiraException;
75

86
class AttachmentTest extends PHPUnit_Framework_TestCase
97
{
108
public function testGetAttachment()
119
{
12-
$attachmentId = getenv("ID");
13-
if ($attachmentId == FALSE)
14-
$attachmentId = 12622;
10+
$attachmentId = 12643;
1511

1612
try {
1713
$atts = new AttachmentService();
1814

19-
$att = $atts->get($attachmentId);
15+
$att = $atts->get($attachmentId, "output", true);
16+
17+
dump($att);
2018

2119
return $attachmentId;
2220
} catch (JiraException $e) {
@@ -25,21 +23,17 @@ public function testGetAttachment()
2523
}
2624

2725
/**
28-
*
26+
* @depends testGetAttachment
2927
*/
30-
public function testRemoveAttachment()
28+
public function testRemoveAttachment($attachmentId)
3129
{
32-
$attachmentId = 12622;
3330
try {
3431
$atts = new AttachmentService();
3532

3633
$atts->remove($attachmentId);
3734

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

40-
//$this->assertInstanceOf(RemoteIssueLink::class, $rils[0]);
41-
42-
// return $issueKey;
4337
} catch (HTTPException $e) {
4438
$this->assertTrue(false, $e->getMessage());
4539
}

0 commit comments

Comments
 (0)