Skip to content

Commit a8b6a6d

Browse files
committed
Merge branch 'feature/update_issue' into develop
2 parents e36ce44 + f210b3d commit a8b6a6d

13 files changed

+244
-42
lines changed

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ try {
114114
->setPriorityName("Critical")
115115
->setIssueType("Bug")
116116
->setDescription("Full description for issue")
117-
->addVersion(null, "1.0.1")
118-
->addVersion(null, "1.0.3");
117+
->addVersion("1.0.1")
118+
->addVersion("1.0.3");
119119

120120
$issueService = new IssueService();
121121

@@ -152,6 +152,44 @@ try {
152152
?>
153153
````
154154

155+
## Update issue
156+
157+
````php
158+
<?php
159+
require 'vendor/autoload.php';
160+
require_once 'config.jira.php';
161+
162+
use JiraRestApi\Issue\IssueService;
163+
use JiraRestApi\Issue\IssueField;
164+
165+
$issueKey = "TEST-920";
166+
167+
//$this->markTestIncomplete();
168+
try {
169+
$issueField = new IssueField(true);
170+
171+
$issueField->setAssigneeName("admin")
172+
->setPriorityName("Blocker")
173+
->setIssueType("Task")
174+
->addLabel("test-label-first")
175+
->addLabel("test-label-second")
176+
->addVersion("1.0.1")
177+
->addVersion("1.0.2")
178+
->setDescription("This is a shorthand for a set operation on the summary field")
179+
;
180+
181+
$issueService = new IssueService();
182+
183+
$ret = $issueService->update($issueKey, $issueField);
184+
185+
186+
} catch (JIRAException $e) {
187+
$this->assertTrue(FALSE, "update Failed : " . $e->getMessage());
188+
}
189+
190+
?>
191+
````
192+
155193
# License
156194

157195
Apache V2 License

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "library",
55
"keywords": ["jira", "jira-php", "jira-rest"],
66
"require": {
7+
"php": ">=5.4.0",
78
"netresearch/jsonmapper": "0.4.*",
89
"monolog/monolog": "~1.12"
910
},

src/JiraClient.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ private function convertLogLevel($log_level) {
5757
return Logger::INFO;
5858
}
5959

60+
// serilize only not null field.
61+
protected function filterNullVariable($haystack)
62+
{
63+
foreach ($haystack as $key => $value) {
64+
if (is_array($value) ) {
65+
$haystack[$key] = $this->filterNullVariable($haystack[$key]);
66+
} else if (is_object($value)) {
67+
$haystack[$key] = $this->filterNullVariable(get_class_vars(get_class($value)));
68+
}
69+
70+
if (is_null($haystack[$key]) || empty($haystack[$key])) {
71+
unset($haystack[$key]);
72+
}
73+
}
74+
75+
return $haystack;
76+
}
77+
6078
public function __construct($config)
6179
{
6280
$this->json_mapper = new \JsonMapper();
@@ -102,11 +120,11 @@ public function exec($context, $post_data = null, $custom_request = null) {
102120
if (!is_null($post_data)) {
103121
// PUT REQUEST
104122
if (!is_null($custom_request) && $custom_request == "PUT") {
105-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
123+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
106124
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
107125
}
108126
if (!is_null($custom_request) && $custom_request == "DELETE") {
109-
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
127+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
110128
}
111129
else {
112130
curl_setopt($ch, CURLOPT_POST, true);
@@ -130,8 +148,15 @@ public function exec($context, $post_data = null, $custom_request = null) {
130148

131149
// if request failed.
132150
if (!$response) {
151+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
133152
$body = curl_error($ch);
134153
curl_close($ch);
154+
155+
//The server successfully processed the request, but is not returning any content.
156+
if ($this->http_response == 204){
157+
return "";
158+
}
159+
135160
// HostNotFound, No route to Host, etc Network error
136161
$this->log->addError("CURL Error: = " . $body);
137162
throw new JIRAException("CURL Error: = " . $body);
@@ -173,11 +198,13 @@ public function upload($context, $upload_file) {
173198
'file' => '@' . realpath($upload_file)
174199
);
175200
*/
176-
$attachments = new \CURLFile(realpath($upload_file));
201+
$attachments = realpath($upload_file);
202+
$filename = basename($upload_file);
177203

178204
// send file
179205
curl_setopt($ch, CURLOPT_POST, true);
180-
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $attachments));
206+
curl_setopt($ch, CURLOPT_POSTFIELDS,
207+
array('file' => '@' . $attachments . ';filename=' . $filename));
181208

182209
curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
183210

@@ -198,9 +225,16 @@ public function upload($context, $upload_file) {
198225
$response = curl_exec($ch);
199226

200227
// if request failed.
201-
if (!$response) {
228+
if (!$response) {
229+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
202230
$body = curl_error($ch);
203231
curl_close($ch);
232+
233+
//The server successfully processed the request, but is not returning any content.
234+
if ($this->http_response == 204){
235+
return "";
236+
}
237+
204238
// HostNotFound, No route to Host, etc Network error
205239
$this->log->addError("CURL Error: = " . $body);
206240
throw new JIRAException("CURL Error: = " . $body);

src/issue/Attachment.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace JiraRestApi\Issue;
44

5-
class Attachment {
5+
class Attachment implements \JsonSerializable{
66
/* @var string */
77
public $self;
88

@@ -29,6 +29,11 @@ class Attachment {
2929

3030
/* @var string */
3131
public $thumbnail;
32+
33+
public function jsonSerialize()
34+
{
35+
return array_filter(get_object_vars($this));
36+
}
3237
}
3338

3439
?>

src/issue/Comments.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace JiraRestApi\Issue;
44

5-
class Comment {
5+
class Comment implements \JsonSerializable {
66
/* @var string */
77
public $self;
88

@@ -23,9 +23,14 @@ class Comment {
2323

2424
/* @var DateTime */
2525
public $updated;
26+
27+
public function jsonSerialize()
28+
{
29+
return array_filter(get_object_vars($this));
30+
}
2631
}
2732

28-
class Comments {
33+
class Comments implements \JsonSerializable {
2934
/* @var int */
3035
public $startAt;
3136

@@ -37,6 +42,11 @@ class Comments {
3742

3843
/* @var CommentList[\JiraRestApi\Issue\Comment] */
3944
public $comments;
45+
46+
public function jsonSerialize()
47+
{
48+
return array_filter(get_object_vars($this));
49+
}
4050
}
4151

4252
?>

src/issue/Issue.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace JiraRestApi\Issue;
44

5-
class Issue {
5+
class Issue implements \JsonSerializable{
66
/**
77
* return only if Project query by key(not id)
88
* @var string
@@ -21,6 +21,10 @@ class Issue {
2121
/* @var IssueField */
2222
public $fields;
2323

24+
public function jsonSerialize()
25+
{
26+
return array_filter(get_object_vars($this));
27+
}
2428
}
2529

2630
?>

src/issue/IssueField.php

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
namespace JiraRestApi\Issue;
44

5-
class IssueField {
6-
public function __construct() {
7-
$this->project = new \JiraRestApi\Project\Project();
8-
9-
$this->assignee = new \JiraRestApi\Issue\Reporter();
10-
$this->priority = new \JiraRestApi\Issue\Priority();
11-
$this->versions = array();
12-
13-
$this->issuetype = new \JiraRestApi\Issue\IssueType();
5+
class IssueField implements \JsonSerializable {
6+
public function __construct($updateIssue = false) {
7+
if ($updateIssue != true) {
8+
$this->project = new \JiraRestApi\Project\Project();
9+
10+
$this->assignee = new \JiraRestApi\Issue\Reporter();
11+
$this->priority = new \JiraRestApi\Issue\Priority();
12+
$this->versions = array();
13+
14+
$this->issuetype = new \JiraRestApi\Issue\IssueType();
15+
}
16+
}
17+
18+
public function jsonSerialize()
19+
{
20+
return array_filter(get_object_vars($this));
1421
}
1522

1623
public function getProjectKey() {
@@ -31,6 +38,9 @@ public function setProjectId($id) {
3138
}
3239

3340
public function setIssueType($name) {
41+
if (is_null($this->issuetype))
42+
$this->issuetype = new \JiraRestApi\Issue\IssueType();
43+
3444
$this->issuetype->name = $name;
3545
return $this;
3646
}
@@ -49,11 +59,17 @@ public function setReporterName($name) {
4959
}
5060

5161
public function setAssigneeName($name) {
62+
if (is_null($this->assignee))
63+
$this->assignee = new \JiraRestApi\Issue\Reporter();
64+
5265
$this->assignee->name = $name;
5366
return $this;
5467
}
5568

5669
public function setPriorityName($name) {
70+
if (is_null($this->priority))
71+
$this->priority = new \JiraRestApi\Issue\Priority();
72+
5773
$this->priority->name = $name;
5874
return $this;
5975
}
@@ -63,23 +79,37 @@ public function setDescription($description) {
6379
return $this;
6480
}
6581

66-
public function addVersion($id, $name) {
82+
public function addVersion($name) {
83+
if (is_null($this->versions))
84+
$this->versions = array();
85+
6786
$v = new Version();
87+
$v->name = $name;
88+
array_push($this->versions, $v);
89+
return $this;
90+
}
6891

69-
if (isset($id))
70-
$v->id = $id;
71-
if (isset($name))
72-
$v->name = $name;
92+
public function addComment($comment) {
93+
if (is_null($this->comments))
94+
$this->comments = new \JiraRestApi\Issue\Comments();
7395

7496
array_push($this->versions, $v);
7597
return $this;
7698
}
7799

100+
public function addLabel($label) {
101+
if (is_null($this->labels))
102+
$this->labels = array();
103+
104+
array_push($this->labels, $label);
105+
return $this;
106+
}
107+
78108
/** @var string */
79109
public $summary;
80110

81111
/** @var string */
82-
public $progress;
112+
//public $progress;
83113

84114
/** @var string */
85115
public $timetracking;
@@ -121,7 +151,7 @@ public function addVersion($id, $name) {
121151
public $components;
122152

123153
/** @var Comments */
124-
public $comment;
154+
public $comments;
125155

126156
/** @var string */
127157
public $votes;

src/issue/IssueService.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public function create($issueField) {
3737
$issue = new Issue();
3838

3939
// serilize only not null field.
40-
$issue->fields = array_filter((array) $issueField, function ($val) {
41-
return !is_null($val);
42-
});
43-
40+
$issue->fields = $issueField;
4441

4542
$data = json_encode($issue);
4643

@@ -75,6 +72,31 @@ public function addAttachments($issueIdOrKey, $filePath) {
7572

7673
return $issue;
7774
}
75+
76+
/**
77+
* update issue
78+
*
79+
* @param $issueIdOrKey Issue Key
80+
* @param $issueField object of Issue class
81+
*
82+
* @return created issue key
83+
*/
84+
public function update($issueIdOrKey, $issueField) {
85+
$issue = new Issue();
86+
87+
// serilize only not null field.
88+
$issue->fields = $issueField;
89+
90+
//$issue = $this->filterNullVariable((array)$issue);
91+
92+
$data = json_encode($issue);
93+
94+
$this->log->addInfo("Update Issue=\n" . $data );
95+
96+
$ret = $this->exec($this->uri . "/$issueIdOrKey", $data, "PUT");
97+
98+
return $ret;
99+
}
78100
}
79101

80102
?>

0 commit comments

Comments
 (0)