Skip to content

Commit 8217553

Browse files
committed
Merge branch 'feature/impl-attachment' into develop
2 parents b625087 + b19958a commit 8217553

File tree

6 files changed

+148
-5
lines changed

6 files changed

+148
-5
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "lesstif/php-jira-rest-client",
33
"description": "JIRA REST API Client for PHP Users.",
4+
"type": "library",
5+
"keywords": ["jira", "jira-php", "jira-rest"],
46
"require": {
57
"netresearch/jsonmapper": "0.4.*",
68
"monolog/monolog": "~1.12"

src/JiraClient.php

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ public function __construct($config)
6767
$this->password = $config['password'];
6868

6969
if (isset($config['CURLOPT_SSL_VERIFYHOST']))
70-
$this->CURLOPT_SSL_VERIFYHOST = $config['CURLOPT_SSL_VERIFYHOST'] === 'true'? true: false;
70+
$this->CURLOPT_SSL_VERIFYHOST = $config['CURLOPT_SSL_VERIFYHOST'] === true ? true: false;
7171

7272
if (isset($config['CURLOPT_SSL_VERIFYPEER']))
73-
$this->CURLOPT_SSL_VERIFYPEER = $config['CURLOPT_SSL_VERIFYPEER'] === 'true'? true: false;
73+
$this->CURLOPT_SSL_VERIFYPEER = $config['CURLOPT_SSL_VERIFYPEER'] === true ? true: false;
7474

7575
if (isset($config['CURLOPT_VERBOSE']))
76-
$this->CURLOPT_VERBOSE = $config['CURLOPT_VERBOSE'] === 'true'? true: false;
76+
$this->CURLOPT_VERBOSE = $config['CURLOPT_VERBOSE'] === true ? true: false;
7777

7878
if (isset($config['LOG_FILE']))
7979
$this->LOG_FILE = $config['LOG_FILE'];
@@ -152,6 +152,71 @@ public function exec($context, $post_data = null, $custom_request = null) {
152152
return $response;
153153
}
154154

155+
/**
156+
* file upload
157+
*
158+
* @param context url context
159+
* @param upload_file upload file.
160+
*
161+
* @todo multiple file upload suppport.
162+
*
163+
*/
164+
public function upload($context, $upload_file) {
165+
$url = $this->host . $this->api_uri . '/' . preg_replace('/\//', '', $context, 1);
166+
167+
$ch=curl_init();
168+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
169+
curl_setopt($ch, CURLOPT_URL, $url);
170+
171+
$attachments = array(
172+
'file' => '@' . realpath($upload_file)
173+
);
174+
175+
// send file
176+
curl_setopt($ch, CURLOPT_POST, true);
177+
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachments);
178+
179+
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
180+
181+
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, $this->CURLOPT_SSL_VERIFYHOST);
182+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->CURLOPT_SSL_VERIFYPEER);
183+
184+
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
185+
curl_setopt($ch, CURLOPT_HTTPHEADER,
186+
array(
187+
'Accept: */*',
188+
'Content-Type: multipart/form-data',
189+
'X-Atlassian-Token: nocheck'
190+
));
191+
192+
curl_setopt($ch, CURLOPT_VERBOSE, $this->CURLOPT_VERBOSE);
193+
194+
$this->log->addDebug('Curl exec=' . $url);
195+
$response = curl_exec($ch);
196+
197+
// if request failed.
198+
if (!$response) {
199+
$body = curl_error($ch);
200+
curl_close($ch);
201+
// HostNotFound, No route to Host, etc Network error
202+
$this->log->addError("CURL Error: = " . $body);
203+
throw new JIRAException("CURL Error: = " . $body);
204+
} else {
205+
// if request was ok, parsing http response code.
206+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
207+
208+
curl_close($ch);
209+
210+
// don't check 301, 302 because setting CURLOPT_FOLLOWLOCATION
211+
if ($this->http_response != 200 && $this->http_response != 201) {
212+
throw new JIRAException("CURL HTTP Request Failed: Status Code : "
213+
. $this->http_response . ", URL:" . $url
214+
. "\nError Message : " . $response, $this->http_response);
215+
}
216+
}
217+
218+
return $response;
219+
}
155220
}
156221

157222

src/issue/Attachment.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace JiraRestApi\Issue;
4+
5+
class Attachment {
6+
/* @var string */
7+
public $self;
8+
9+
/* @var string */
10+
public $id;
11+
12+
/* @var string */
13+
public $filename;
14+
15+
/* @var Reporter */
16+
public $author;
17+
18+
/* @var DateTime */
19+
public $created;
20+
21+
/* @var int */
22+
public $size;
23+
24+
/* @var string */
25+
public $mimeType;
26+
27+
/* @var string */
28+
public $content;
29+
30+
/* @var string */
31+
public $thumbnail;
32+
}
33+
34+
?>

src/issue/IssueField.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ public function addVersion($id, $name) {
146146

147147
/* @var VersionList[\JiraRestApi\Issue\Version] */
148148
public $versions;
149+
150+
/** @var AttachmentList[\JiraRestApi\Issue\Attachment] */
151+
public $attachments;
149152
}
150153

151154
?>

src/issue/IssueService.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ public function create($issueField) {
5454

5555
return $issue;
5656
}
57+
58+
/**
59+
* Add one or more file to an issue
60+
*
61+
* @param issueIdOrKey Issue id or key
62+
* @param filePath attachment file.
63+
*
64+
* @return
65+
*/
66+
public function addAttachments($issueIdOrKey, $filePath) {
67+
68+
$this->log->addInfo("addAttachments=\n");
69+
70+
$ret = $this->upload($this->uri . "/$issueIdOrKey/attachments", $filePath);
71+
72+
$issue = $this->json_mapper->map(
73+
json_decode($ret), new Issue()
74+
);
75+
76+
return $issue;
77+
}
5778
}
5879

5980
?>

tests/IssueTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function testIssue()
2828

2929
public function testCreateIssue()
3030
{
31-
//$this->markTestIncomplete();
31+
$this->markTestIncomplete();
3232
try {
3333
$issueField = new IssueField();
3434

@@ -39,7 +39,10 @@ public function testCreateIssue()
3939
->setIssueType("Bug")
4040
->setDescription("Full description for issue")
4141
->addVersion(null, "1.0.1")
42-
->addVersion(null, "1.0.3");
42+
->addVersion(null, "1.0.3")
43+
->addAttachment('screen_capture.png')
44+
->addAttachment('bug-description.pdf')
45+
;
4346

4447
$issueService = new IssueService();
4548

@@ -52,6 +55,21 @@ public function testCreateIssue()
5255
}
5356
}
5457
//
58+
59+
public function testAddAttachment()
60+
{
61+
$this->markTestIncomplete();
62+
try {
63+
64+
$issueService = new IssueService();
65+
66+
$ret = $issueService->addAttachments("TEST-879", 'screen_capture.png');
67+
68+
print_r($ret);
69+
} catch (JIRAException $e) {
70+
$this->assertTrue(FALSE, "Attach Failed : " . $e->getMessage());
71+
}
72+
}
5573
}
5674

5775
?>

0 commit comments

Comments
 (0)