Skip to content

Commit

Permalink
Merge branch 'feature/update_issue' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed Mar 5, 2015
2 parents e36ce44 + f210b3d commit a8b6a6d
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 42 deletions.
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ try {
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion(null, "1.0.1")
->addVersion(null, "1.0.3");
->addVersion("1.0.1")
->addVersion("1.0.3");

$issueService = new IssueService();

Expand Down Expand Up @@ -152,6 +152,44 @@ try {
?>
````

## Update issue

````php
<?php
require 'vendor/autoload.php';
require_once 'config.jira.php';

use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;

$issueKey = "TEST-920";

//$this->markTestIncomplete();
try {
$issueField = new IssueField(true);

$issueField->setAssigneeName("admin")
->setPriorityName("Blocker")
->setIssueType("Task")
->addLabel("test-label-first")
->addLabel("test-label-second")
->addVersion("1.0.1")
->addVersion("1.0.2")
->setDescription("This is a shorthand for a set operation on the summary field")
;

$issueService = new IssueService();

$ret = $issueService->update($issueKey, $issueField);


} catch (JIRAException $e) {
$this->assertTrue(FALSE, "update Failed : " . $e->getMessage());
}

?>
````

# License

Apache V2 License
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"type": "library",
"keywords": ["jira", "jira-php", "jira-rest"],
"require": {
"php": ">=5.4.0",
"netresearch/jsonmapper": "0.4.*",
"monolog/monolog": "~1.12"
},
Expand Down
44 changes: 39 additions & 5 deletions src/JiraClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,24 @@ private function convertLogLevel($log_level) {
return Logger::INFO;
}

// serilize only not null field.
protected function filterNullVariable($haystack)
{
foreach ($haystack as $key => $value) {
if (is_array($value) ) {
$haystack[$key] = $this->filterNullVariable($haystack[$key]);
} else if (is_object($value)) {
$haystack[$key] = $this->filterNullVariable(get_class_vars(get_class($value)));
}

if (is_null($haystack[$key]) || empty($haystack[$key])) {
unset($haystack[$key]);
}
}

return $haystack;
}

public function __construct($config)
{
$this->json_mapper = new \JsonMapper();
Expand Down Expand Up @@ -102,11 +120,11 @@ public function exec($context, $post_data = null, $custom_request = null) {
if (!is_null($post_data)) {
// PUT REQUEST
if (!is_null($custom_request) && $custom_request == "PUT") {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
if (!is_null($custom_request) && $custom_request == "DELETE") {
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
else {
curl_setopt($ch, CURLOPT_POST, true);
Expand All @@ -130,8 +148,15 @@ public function exec($context, $post_data = null, $custom_request = null) {

// if request failed.
if (!$response) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);
curl_close($ch);

//The server successfully processed the request, but is not returning any content.
if ($this->http_response == 204){
return "";
}

// HostNotFound, No route to Host, etc Network error
$this->log->addError("CURL Error: = " . $body);
throw new JIRAException("CURL Error: = " . $body);
Expand Down Expand Up @@ -173,11 +198,13 @@ public function upload($context, $upload_file) {
'file' => '@' . realpath($upload_file)
);
*/
$attachments = new \CURLFile(realpath($upload_file));
$attachments = realpath($upload_file);
$filename = basename($upload_file);

// send file
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => $attachments));
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('file' => '@' . $attachments . ';filename=' . $filename));

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

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

// if request failed.
if (!$response) {
if (!$response) {
$this->http_response = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$body = curl_error($ch);
curl_close($ch);

//The server successfully processed the request, but is not returning any content.
if ($this->http_response == 204){
return "";
}

// HostNotFound, No route to Host, etc Network error
$this->log->addError("CURL Error: = " . $body);
throw new JIRAException("CURL Error: = " . $body);
Expand Down
7 changes: 6 additions & 1 deletion src/issue/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JiraRestApi\Issue;

class Attachment {
class Attachment implements \JsonSerializable{
/* @var string */
public $self;

Expand All @@ -29,6 +29,11 @@ class Attachment {

/* @var string */
public $thumbnail;

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}
}

?>
14 changes: 12 additions & 2 deletions src/issue/Comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JiraRestApi\Issue;

class Comment {
class Comment implements \JsonSerializable {
/* @var string */
public $self;

Expand All @@ -23,9 +23,14 @@ class Comment {

/* @var DateTime */
public $updated;

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}
}

class Comments {
class Comments implements \JsonSerializable {
/* @var int */
public $startAt;

Expand All @@ -37,6 +42,11 @@ class Comments {

/* @var CommentList[\JiraRestApi\Issue\Comment] */
public $comments;

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}
}

?>
6 changes: 5 additions & 1 deletion src/issue/Issue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace JiraRestApi\Issue;

class Issue {
class Issue implements \JsonSerializable{
/**
* return only if Project query by key(not id)
* @var string
Expand All @@ -21,6 +21,10 @@ class Issue {
/* @var IssueField */
public $fields;

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}
}

?>
62 changes: 46 additions & 16 deletions src/issue/IssueField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

namespace JiraRestApi\Issue;

class IssueField {
public function __construct() {
$this->project = new \JiraRestApi\Project\Project();

$this->assignee = new \JiraRestApi\Issue\Reporter();
$this->priority = new \JiraRestApi\Issue\Priority();
$this->versions = array();

$this->issuetype = new \JiraRestApi\Issue\IssueType();
class IssueField implements \JsonSerializable {
public function __construct($updateIssue = false) {
if ($updateIssue != true) {
$this->project = new \JiraRestApi\Project\Project();

$this->assignee = new \JiraRestApi\Issue\Reporter();
$this->priority = new \JiraRestApi\Issue\Priority();
$this->versions = array();

$this->issuetype = new \JiraRestApi\Issue\IssueType();
}
}

public function jsonSerialize()
{
return array_filter(get_object_vars($this));
}

public function getProjectKey() {
Expand All @@ -31,6 +38,9 @@ public function setProjectId($id) {
}

public function setIssueType($name) {
if (is_null($this->issuetype))
$this->issuetype = new \JiraRestApi\Issue\IssueType();

$this->issuetype->name = $name;
return $this;
}
Expand All @@ -49,11 +59,17 @@ public function setReporterName($name) {
}

public function setAssigneeName($name) {
if (is_null($this->assignee))
$this->assignee = new \JiraRestApi\Issue\Reporter();

$this->assignee->name = $name;
return $this;
}

public function setPriorityName($name) {
if (is_null($this->priority))
$this->priority = new \JiraRestApi\Issue\Priority();

$this->priority->name = $name;
return $this;
}
Expand All @@ -63,23 +79,37 @@ public function setDescription($description) {
return $this;
}

public function addVersion($id, $name) {
public function addVersion($name) {
if (is_null($this->versions))
$this->versions = array();

$v = new Version();
$v->name = $name;
array_push($this->versions, $v);
return $this;
}

if (isset($id))
$v->id = $id;
if (isset($name))
$v->name = $name;
public function addComment($comment) {
if (is_null($this->comments))
$this->comments = new \JiraRestApi\Issue\Comments();

array_push($this->versions, $v);
return $this;
}

public function addLabel($label) {
if (is_null($this->labels))
$this->labels = array();

array_push($this->labels, $label);
return $this;
}

/** @var string */
public $summary;

/** @var string */
public $progress;
//public $progress;

/** @var string */
public $timetracking;
Expand Down Expand Up @@ -121,7 +151,7 @@ public function addVersion($id, $name) {
public $components;

/** @var Comments */
public $comment;
public $comments;

/** @var string */
public $votes;
Expand Down
30 changes: 26 additions & 4 deletions src/issue/IssueService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function create($issueField) {
$issue = new Issue();

// serilize only not null field.
$issue->fields = array_filter((array) $issueField, function ($val) {
return !is_null($val);
});

$issue->fields = $issueField;

$data = json_encode($issue);

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

return $issue;
}

/**
* update issue
*
* @param $issueIdOrKey Issue Key
* @param $issueField object of Issue class
*
* @return created issue key
*/
public function update($issueIdOrKey, $issueField) {
$issue = new Issue();

// serilize only not null field.
$issue->fields = $issueField;

//$issue = $this->filterNullVariable((array)$issue);

$data = json_encode($issue);

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

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

return $ret;
}
}

?>
Expand Down
Loading

0 comments on commit a8b6a6d

Please sign in to comment.