Skip to content

Commit f210b3d

Browse files
committed
impl update issue.
1 parent 64574a2 commit f210b3d

File tree

12 files changed

+176
-52
lines changed

12 files changed

+176
-52
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

src/JiraClient.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,21 @@ private function convertLogLevel($log_level) {
5858
}
5959

6060
// serilize only not null field.
61-
protected function filterNullVariable ($arr) {
62-
return array_filter((array) $arr, function ($val) {
63-
return (!is_null($val) && !empty($val) );
64-
});
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;
6576
}
6677

6778
public function __construct($config)
@@ -137,8 +148,15 @@ public function exec($context, $post_data = null, $custom_request = null) {
137148

138149
// if request failed.
139150
if (!$response) {
151+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
140152
$body = curl_error($ch);
141153
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+
142160
// HostNotFound, No route to Host, etc Network error
143161
$this->log->addError("CURL Error: = " . $body);
144162
throw new JIRAException("CURL Error: = " . $body);
@@ -207,9 +225,16 @@ public function upload($context, $upload_file) {
207225
$response = curl_exec($ch);
208226

209227
// if request failed.
210-
if (!$response) {
228+
if (!$response) {
229+
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
211230
$body = curl_error($ch);
212231
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+
213238
// HostNotFound, No route to Host, etc Network error
214239
$this->log->addError("CURL Error: = " . $body);
215240
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: 31 additions & 18 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,14 +79,12 @@ 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();
68-
69-
if (isset($id))
70-
$v->id = $id;
71-
if (isset($name))
72-
$v->name = $name;
73-
87+
$v->name = $name;
7488
array_push($this->versions, $v);
7589
return $this;
7690
}
@@ -83,7 +97,6 @@ public function addComment($comment) {
8397
return $this;
8498
}
8599

86-
//@TODO
87100
public function addLabel($label) {
88101
if (is_null($this->labels))
89102
$this->labels = array();
@@ -96,7 +109,7 @@ public function addLabel($label) {
96109
public $summary;
97110

98111
/** @var string */
99-
public $progress;
112+
//public $progress;
100113

101114
/** @var string */
102115
public $timetracking;

src/issue/IssueService.php

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

3939
// serilize only not null field.
40-
$issue->fields = $this->filterNullVariable($issueField);
40+
$issue->fields = $issueField;
4141

4242
$data = json_encode($issue);
4343

@@ -85,19 +85,17 @@ public function update($issueIdOrKey, $issueField) {
8585
$issue = new Issue();
8686

8787
// serilize only not null field.
88-
$issue->fields = $this->filterNullVariable($issueField);
88+
$issue->fields = $issueField;
89+
90+
//$issue = $this->filterNullVariable((array)$issue);
8991

9092
$data = json_encode($issue);
9193

9294
$this->log->addInfo("Update Issue=\n" . $data );
9395

9496
$ret = $this->exec($this->uri . "/$issueIdOrKey", $data, "PUT");
9597

96-
$issue = $this->json_mapper->map(
97-
json_decode($ret), new Issue()
98-
);
99-
100-
return $issue;
98+
return $ret;
10199
}
102100
}
103101

src/issue/Priority.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 Priority {
5+
class Priority implements \JsonSerializable{
66
/* @var string */
77
public $self;
88

@@ -14,6 +14,11 @@ class Priority {
1414

1515
/* @var string */
1616
public $id;
17+
18+
public function jsonSerialize()
19+
{
20+
return array_filter(get_object_vars($this));
21+
}
1722
}
1823

1924
?>

src/issue/Reporter.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 Reporter {
5+
class Reporter implements \JsonSerializable{
66
/* @var string */
77
public $self;
88

@@ -20,6 +20,11 @@ class Reporter {
2020

2121
/* @var string */
2222
public $active;
23+
24+
public function jsonSerialize()
25+
{
26+
return array_filter(get_object_vars($this));
27+
}
2328
}
2429

2530
?>

src/issue/Version.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 Version {
5+
class Version implements \JsonSerializable{
66
/* @var string */
77
public $self;
88

@@ -23,6 +23,11 @@ class Version {
2323

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

2833
?>

0 commit comments

Comments
 (0)