From 2261236fe1a030b64f72fd4a87ec920178a30377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Mon, 22 Jul 2013 20:33:25 +0200 Subject: [PATCH 01/12] Fixed queryIssue method from Jira.php to just query the jql without using any black magic. That logic doesn't belong there... MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- src/JiraApi/Jira.php | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index 7a7b240..e17ce36 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -108,20 +108,8 @@ public function getComments($issueKey) public function queryIssue($query) { - function createPairs($obj) { - $str = ""; - foreach ($obj as $key => $value) { - if ($key != 'jql') { - $str .= "$key=$value&"; - } else { - $str .= trim($value, '"\'@') . '&'; - } - } - return rtrim($str, '&'); - } - $qs = createPairs($query); - $qs = urlencode($qs); - $this->request->OpenConnect($this->host . 'search?jql=' . $qs); + $query = urlencode($query); + $this->request->OpenConnect($this->host . 'search?jql=' . $query); $this->request->execute(); $result = json_decode($this->request->getResponseBody()); if (isset($result->issues)) { From 947d39d8ea3683d49886312a6638bce2fb183169 Mon Sep 17 00:00:00 2001 From: Adam Maschek Date: Thu, 8 May 2014 18:05:37 +0200 Subject: [PATCH 02/12] added fields and maxresults parameters to queryIssue --- src/JiraApi/Jira.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index e17ce36..0247b2c 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -106,10 +106,30 @@ public function getComments($issueKey) return false; } - public function queryIssue($query) + /** + * Execute the actual JQL query against the JIRA API. + * + * The maxResults is needed, otherwise JIRA only returns the default page size (50). + * Here in the wrapper we default the maxResults to 500 to get much more results. + * + * @link https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Query+issues + * + * @param string $query The JQL query + * @param string $fields A filter of comma separated fields, or null, in case we want all fields + * @param int $maxResults Number of returned results (by default 500) + * @return mixed False in case of error, array of resultsets otherwise + */ + public function queryIssue($query, $fields = null, $maxResults = 500) { $query = urlencode($query); - $this->request->OpenConnect($this->host . 'search?jql=' . $query); + $url = $this->host . 'search?jql=' . $query; + if (isset($fields)) { + $url.= '&fields=' . $fields; + } + if (isset($maxResults)) { + $url.= '&maxResults=' . $maxResults; + } + $this->request->OpenConnect($url); $this->request->execute(); $result = json_decode($this->request->getResponseBody()); if (isset($result->issues)) { From 086bef89aca5fbd669528dff4515057ffd044484 Mon Sep 17 00:00:00 2001 From: Christian Steinhart Date: Wed, 11 Jun 2014 16:08:16 +0200 Subject: [PATCH 03/12] Added methods for creating and getting fixVersions --- src/JiraApi/Jira.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index 0247b2c..5d80c76 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -184,5 +184,26 @@ public function addComment($comment, $issueKey) return $this->request->lastRequestStatus(); } + + public function getVersions($project) + { + $this->request->openConnect($this->host . 'project/' . $project . '/versions'); + $this->request->execute(); + + $result = json_decode($this->request->getResponseBody()); + if (is_array($result)) { + return $result; + } + + return false; + } + + public function createVersion($json) + { + $this->request->openConnect($this->host . 'version/', 'POST', $json); + $this->request->execute(); + + return $this->request->lastRequestStatus(); + } } ?> From 9282f0200a9151122d71635dd1127cfe6033e8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Mon, 19 Jan 2015 15:01:22 +0100 Subject: [PATCH 04/12] Use UTF-8 for CURL requests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- src/JiraApi/RestRequest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JiraApi/RestRequest.php b/src/JiraApi/RestRequest.php index cc287a1..ddf93e5 100644 --- a/src/JiraApi/RestRequest.php +++ b/src/JiraApi/RestRequest.php @@ -159,6 +159,7 @@ protected function setCurlOpts(&$ch) curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_URL, $this->url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_ENCODING ,"UTF-8"); //curl_setopt($ch, CURLOPT_HEADER, true); //displays header in output. curl_setopt( $ch, From ec3cb37407bac507b7b56d99f725446a9fd58a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Mon, 19 Jan 2015 15:02:21 +0100 Subject: [PATCH 05/12] Add getComponents() and createComponent() methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- src/JiraApi/Jira.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index 5d80c76..d01beb9 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -12,7 +12,7 @@ public function __construct(array $config = array()) $this->request = new RestRequest(); $this->request->username = (isset($config['username'])) ? $config['username'] : null; $this->request->password = (isset($config['password'])) ? $config['password'] : null; - $host = (isset($config['host'])) ? $config['host'] : null; + $host = (isset($config['host'])) ? $config['host'] : null; $this->host = 'https://' . $host . '/rest/api/2/'; } @@ -205,5 +205,26 @@ public function createVersion($json) return $this->request->lastRequestStatus(); } + + public function getComponents($project) + { + $this->request->openConnect($this->host . 'project/' . $project . '/components'); + $this->request->execute(); + + $result = json_decode($this->request->getResponseBody()); + if (is_array($result)) { + return $result; + } + + return false; + } + + public function createComponent($json) + { + $this->request->openConnect($this->host . 'component/', 'POST', $json); + $this->request->execute(); + + return $this->request->lastRequestStatus(); + } } ?> From 4f9193296e0e4687a4a69d004bbf735ea704dd0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Wed, 29 Apr 2015 11:49:54 +0200 Subject: [PATCH 06/12] Add .gitignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ From 32999fbe24e58a91894903837c04e0985378ede7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Wed, 29 Apr 2015 11:50:19 +0200 Subject: [PATCH 07/12] Add version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 76c4111..f3a0b51 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,9 @@ { "name": "jira-api", "description": "PHP classes for interacting witht the Jira REST API", + "keywords": ["Jira", "api"], + "version": "1.0", + "type": "library", "autoload": { "psr-0": { "JiraApi": "src/" From 69c2a317969f5ff485959ba50042a0dccba10504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Wed, 29 Apr 2015 13:55:06 +0200 Subject: [PATCH 08/12] Count number of requests done and implement setter for it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- src/JiraApi/RestRequest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/JiraApi/RestRequest.php b/src/JiraApi/RestRequest.php index ddf93e5..32ee97b 100644 --- a/src/JiraApi/RestRequest.php +++ b/src/JiraApi/RestRequest.php @@ -15,6 +15,7 @@ class RestRequest protected $acceptType; protected $responseBody; protected $responseInfo; + protected $numberOfRequests = 0; public function openConnect($url = null, $verb = 'GET', $requestBody = null, $filename = null) { @@ -70,6 +71,8 @@ public function execute() curl_close($ch); throw $e; } + + $this->numberOfRequests++; } public function buildPostBody($data = null) @@ -117,6 +120,11 @@ public function lastRequestStatus() return false; } + public function getNumberOfRequests() + { + return $this->numberOfRequests; + } + protected function executeGet($ch) { $this->doExecute($ch); From 52d3abfa80ea2591edbae8acc8a36a2d5d6264db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Wed, 29 Apr 2015 13:58:29 +0200 Subject: [PATCH 09/12] Move $config from constructor to setter. Implemebt getter for $request and prevent direct access to $request property MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- src/JiraApi/Jira.php | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index d01beb9..e2604ac 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -5,11 +5,29 @@ class Jira { + /** + * @var string + */ protected $host; - public function __construct(array $config = array()) + /** + * @var array + */ + protected $config; + + /** + * @var RestRequest + */ + protected $request; + + public function __construct() { $this->request = new RestRequest(); + + } + + public function setConfig(array $config = array()) + { $this->request->username = (isset($config['username'])) ? $config['username'] : null; $this->request->password = (isset($config['password'])) ? $config['password'] : null; $host = (isset($config['host'])) ? $config['host'] : null; @@ -26,6 +44,11 @@ public function testLogin() return false; } + public function getRequest() + { + return $this->request; + } + public function getUser($username) { $this->request->openConnect($this->host . 'user/search/?username=' . $username, 'GET'); From d85565a29f8ace9872fbaa4171f75e1b52754589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josemi=20Li=C3=A9bana?= Date: Wed, 29 Apr 2015 14:00:28 +0200 Subject: [PATCH 10/12] Bump version to 1.1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Josemi Liébana --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f3a0b51..21cd05e 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "jira-api", "description": "PHP classes for interacting witht the Jira REST API", "keywords": ["Jira", "api"], - "version": "1.0", + "version": "1.1", "type": "library", "autoload": { "psr-0": { From 5b3f35a1f8246a21ef293c6718f51731ea959a3d Mon Sep 17 00:00:00 2001 From: Jacek Barecki Date: Wed, 23 Jan 2019 11:18:28 +0100 Subject: [PATCH 11/12] Fix json_decode integer overflow error --- src/JiraApi/Jira.php | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index e2604ac..3f2783d 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -53,7 +53,7 @@ public function getUser($username) { $this->request->openConnect($this->host . 'user/search/?username=' . $username, 'GET'); $this->request->execute(); - $user = json_decode($this->request->getResponseBody()); + $user = $this->getDecodedApiResponse($this->request->getResponseBody()); return $user; } @@ -62,7 +62,7 @@ public function getStatuses() { $this->request->openConnect($this->host . 'status', 'GET'); $this->request->execute(); - $statuses = json_decode($this->request->getResponseBody()); + $statuses = $this->getDecodedApiResponse($this->request->getResponseBody()); $returnStatuses = array(); foreach ($statuses as $status) { $returnStatuses[$status->id] = $status->name; @@ -75,7 +75,7 @@ public function getTransitions($issueKey) { $this->request->openConnect($this->host . 'issue/' . $issueKey . '/transitions', 'GET'); $this->request->execute(); - if ($result = json_decode($this->request->getResponseBody())) { + if ($result = $this->getDecodedApiResponse($this->request->getResponseBody())) { $returnTransitions = array(); foreach ($result->transitions as $transition) { $returnTransitions[$transition->id] = $transition->name; @@ -90,7 +90,7 @@ public function getChangelog($issueKey, $historyAsText = true) { $this->request->openConnect($this->host . 'issue/' . $issueKey . '/?expand=changelog', 'GET'); $this->request->execute(); - if ($result = json_decode($this->request->getResponseBody())) { + if ($result = $this->getDecodedApiResponse($this->request->getResponseBody())) { if (!isset($result->changelog)) { return false; } @@ -121,7 +121,7 @@ public function getComments($issueKey) { $this->request->openConnect($this->host . 'issue/' . $issueKey . '/comment?expand', 'GET'); $this->request->execute(); - $result = json_decode($this->request->getResponseBody()); + $result = $this->getDecodedApiResponse($this->request->getResponseBody()); if (isset($result->comments)) { return $result->comments; } @@ -154,7 +154,7 @@ public function queryIssue($query, $fields = null, $maxResults = 500) } $this->request->OpenConnect($url); $this->request->execute(); - $result = json_decode($this->request->getResponseBody()); + $result = $this->getDecodedApiResponse($this->request->getResponseBody()); if (isset($result->issues)) { return $result->issues; } @@ -213,7 +213,7 @@ public function getVersions($project) $this->request->openConnect($this->host . 'project/' . $project . '/versions'); $this->request->execute(); - $result = json_decode($this->request->getResponseBody()); + $result = $this->getDecodedApiResponse($this->request->getResponseBody()); if (is_array($result)) { return $result; } @@ -234,7 +234,7 @@ public function getComponents($project) $this->request->openConnect($this->host . 'project/' . $project . '/components'); $this->request->execute(); - $result = json_decode($this->request->getResponseBody()); + $result = $this->getDecodedApiResponse($this->request->getResponseBody()); if (is_array($result)) { return $result; } @@ -249,5 +249,22 @@ public function createComponent($json) return $this->request->lastRequestStatus(); } + + /** + * @param string $responseBody JSON with response body + * @return mixed Decoded JSON + */ + private function getDecodedApiResponse($responseBody) + { + /** + * workaround to json_decode(): integer overflow detected error + * @see https://stackoverflow.com/questions/19520487/json-bigint-as-string-removed-in-php-5-5/27909889 + */ + $maxIntLength = strlen((string) PHP_INT_MAX) - 1; + $safeJson = preg_replace('/:\s*(-?\d{'.$maxIntLength.',})/', ': "$1"', $responseBody); + $result = json_decode($safeJson); + + return $result; + } } ?> From 5400dd8f90fa5256a69d90711913185fcf7c7196 Mon Sep 17 00:00:00 2001 From: Jacek Barecki Date: Wed, 23 Jan 2019 11:27:30 +0100 Subject: [PATCH 12/12] Add update filter method; bump version --- composer.json | 2 +- src/JiraApi/Jira.php | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 21cd05e..6fe03a5 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "jira-api", "description": "PHP classes for interacting witht the Jira REST API", "keywords": ["Jira", "api"], - "version": "1.1", + "version": "1.2", "type": "library", "autoload": { "psr-0": { diff --git a/src/JiraApi/Jira.php b/src/JiraApi/Jira.php index 3f2783d..1ac3d4c 100644 --- a/src/JiraApi/Jira.php +++ b/src/JiraApi/Jira.php @@ -250,6 +250,18 @@ public function createComponent($json) return $this->request->lastRequestStatus(); } + public function updateFilter($json, $filterId) + { + $filterId = intval($filterId); + if ($data = json_decode($json)) { + $this->request->openConnect($this->host . 'filter/' . $filterId . '?expand', 'PUT', $data); + $this->request->execute(); + + return $this->request->lastRequestStatus(); + } + return false; + } + /** * @param string $responseBody JSON with response body * @return mixed Decoded JSON