Skip to content

Commit cbe36cd

Browse files
committed
impl..
1 parent f61f6f6 commit cbe36cd

File tree

5 files changed

+186
-5
lines changed

5 files changed

+186
-5
lines changed

src/JiraClient.php

Lines changed: 71 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,74 @@ public function exec($context, $post_data = null, $custom_request = null) {
152152
return $response;
153153
}
154154

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

157225

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 fileList attachment file array
63+
*
64+
* @return
65+
*/
66+
public function addAttachments($issueIdOrKey, $fileList) {
67+
68+
$this->log->addInfo("addAttachments=\n");
69+
70+
$ret = $this->upload($this->uri . "/$issueIdOrKey/attachments", $fileList);
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: 57 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,58 @@ public function testCreateIssue()
5255
}
5356
}
5457
//
58+
59+
public function testAddAttachment()
60+
{
61+
$this->markTestIncomplete();
62+
try {
63+
$fileList = array(
64+
'screen_capture.png',
65+
'bug-description.pdf'
66+
);
67+
68+
$issueService = new IssueService();
69+
70+
$ret = $issueService->addAttachments("TEST-879", $fileList);
71+
72+
print_r($ret);
73+
} catch (JIRAException $e) {
74+
$this->assertTrue(FALSE, "Attach Failed : " . $e->getMessage());
75+
}
76+
}
77+
78+
79+
static function addAttachments($issueIdOrKey, $fileList) {
80+
$c = new IssueService();
81+
82+
$ret = $c->upload("/issue/$issueIdOrKey/attachments", $fileList);
83+
84+
$issue = $c->json_mapper->map(
85+
json_decode($ret), new Issue()
86+
);
87+
88+
return $issue;
89+
}
90+
91+
public function testRefection()
92+
{
93+
//$this->markTestIncomplete();
94+
try {
95+
$fileList = array(
96+
'screen_capture.png',
97+
'bug-description.pdf'
98+
);
99+
100+
$this->addAttachments("TEST-879", $fileList);
101+
102+
print_r($ret);
103+
} catch (JIRAException $e) {
104+
$this->assertTrue(FALSE, "Attach Failed : " . $e->getMessage());
105+
}
106+
}
107+
108+
109+
55110
}
56111

57112
?>

0 commit comments

Comments
 (0)