From 0a3c52affde7738ff3211958a50c32c8cfaca05b Mon Sep 17 00:00:00 2001 From: Marek Knappe Date: Fri, 30 Oct 2015 11:45:44 +1000 Subject: [PATCH 1/2] Added deleteIssue function and changed curlClient to support DELETE request --- src/Jira/Api.php | 18 ++++++++++++++++++ src/Jira/Api/Client/CurlClient.php | 12 +++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 2c642a5..78c74ef 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -150,6 +150,24 @@ public function editIssue($issueKey, $params) } + /** + * Delete issue + * + * @param $issueKey should be YOURPROJ-221 + * @param $deleteSubtasks if all subtask should be deleted + * @return mixed + */ + public function deleteIssue($issueKey, $deleteSubtasks = 'true') + { + return $this->api( + self::REQUEST_DELETE, sprintf("/rest/api/2/issue/%s", $issueKey), + array ( + 'deleteSubtasks' => $deleteSubtasks + ) + ); + } + + public function getAttachment($attachmentId) { $result = $this->api(self::REQUEST_GET, "/rest/api/2/attachment/$attachmentId", array(), true); diff --git a/src/Jira/Api/Client/CurlClient.php b/src/Jira/Api/Client/CurlClient.php index 4a30d41..e5c595c 100644 --- a/src/Jira/Api/Client/CurlClient.php +++ b/src/Jira/Api/Client/CurlClient.php @@ -82,12 +82,14 @@ public function sendRequest($method, $url, $data = array(), $endpoint, Authentic } else { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); } - } else { - if ($method == "PUT") { - curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); - curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); - } + } elseif ($method == "DELETE") { + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + + } elseif ($method == "PUT") { + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); } + $data = curl_exec($curl); From 9b3195a409bf9f603eddc1bb50000e31c684c436 Mon Sep 17 00:00:00 2001 From: Marek Knappe Date: Sun, 1 Nov 2015 16:18:00 +1000 Subject: [PATCH 2/2] added function transitionByStepName and changed closeIssue to use that generic one --- src/Jira/Api.php | 63 ++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/src/Jira/Api.php b/src/Jira/Api.php index 78c74ef..28da149 100644 --- a/src/Jira/Api.php +++ b/src/Jira/Api.php @@ -271,6 +271,8 @@ public function getTransitions($issueKey, $params) return $this->api(self::REQUEST_GET, sprintf("/rest/api/2/issue/%s/transitions", $issueKey), $params); } + + /** * transation a ticket * @@ -285,6 +287,43 @@ public function transition($issueKey, $params) return $this->api(self::REQUEST_POST, sprintf("/rest/api/2/issue/%s/transitions", $issueKey), $params); } + /** + * transation by step name + * + * + * @param $issueKey like YOURPROJ-22 + * @param $stepName Step name like 'Done' or 'To Do' + * @param $params (array of parameters from JIRA API) + * @return mixed + */ + public function transitionByStepName($issueKey, $stepName, $params = array()) + { + $result = array(); + // get available transitions + $tmp_transitions = $this->getTransitions($issueKey, array()); + $tmp_transitions_result = $tmp_transitions->getResult(); + $transitions = $tmp_transitions_result['transitions']; + + // search id for closing ticket + foreach ($transitions as $v) { + // Close ticket if required id was found + if ($v['name'] == $stepName) { + $result = $this->transition( + $issueKey, + array_merge($params, + array( + 'transition' => array( + 'id' => $v['id'] + ) + ) + ) + ); + break; + } + } + return $result; + } + /** * get available issue types * @@ -565,31 +604,9 @@ public function setWatchers($issueKey, $watchers) * @param $issueKey * @return mixed * - * @TODO: should have parameters? (e.g comment) */ public function closeIssue($issueKey) { - $result = array(); - // get available transitions - $tmp_transitions = $this->getTransitions($issueKey, array()); - $tmp_transitions_result = $tmp_transitions->getResult(); - $transitions = $tmp_transitions_result['transitions']; - - // search id for closing ticket - foreach ($transitions as $v) { - // Close ticket if required id was found - if ($v['name'] == "Close Issue") { - $result = $this->transition( - $issueKey, - array( - 'transition' => array( - 'id' => $v['id'] - ) - ) - ); - break; - } - } - return $result; + return $this->transitionByStepName($issueKey,'Close Issue'); } }