Skip to content

Fix create attachments for PHP 5.5 #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
.idea/
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Jira Api Rest Client

[![Build Status](https://secure.travis-ci.org/chobie/jira-api-restclient.png)](http://travis-ci.org/chobie/jira-api-restclient)

This fork is meant to fix an incompatibility issue with PHP 5.5 and is published because pull requests are not processed.

you know JIRA5 supports REST API. this is very useful to make some custom notifications and automated jobs.
(JIRA also supports email notification, but it's too much to custom templates, notification timing. unfortunately it requires Administration permission.)
Expand All @@ -12,7 +11,7 @@ this API library will help your problems regarding JIRA. hope you enjoy it.
composer.json

```
composer require chobie/jira-api-restclient 2.0.*
composer require DerMika/jira-api-restclient dev-master
```


Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "chobie/jira-api-restclient",
"name": "dermika/jira-api-restclient",
"description": "JIRA REST API.",
"keywords": ["jira","rest","api"],
"type": "library",
Expand All @@ -9,6 +9,10 @@
"name": "Shuhei Tanuma",
"homepage": "http://chobie.github.io/",
"email": "chobieeee@php.net"
},
{
"name": "DerMika",
"email": "dieter@dermika.be"
}
],
"minimum-stability": "dev",
Expand Down
118 changes: 96 additions & 22 deletions src/Jira/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Api
*
* @param $endpoint
* @param AuthenticationInterface $authentication
* @param ClientInterface $client
* @param ClientInterface|null $client
*/
public function __construct(
$endpoint,
Expand All @@ -82,6 +82,9 @@ public function __construct(
$this->client = $client;
}

/**
* @param int $options
*/
public function setOptions($options)
{
$this->options = $options;
Expand All @@ -100,7 +103,8 @@ public function getEndpoint()
/**
* set end point url.
*
* @param $url
* @param string $url
* @return void
*/
public function setEndPoint($url)
{
Expand Down Expand Up @@ -135,46 +139,70 @@ public function getFields()
*
* issue key should be YOURPROJ-221
*
* @param $issueKey
* @param $expand
* @return mixed
* @param string $issueKey
* @param string $expand
* @return Result|false|mixed
*/
public function getIssue($issueKey, $expand = '')
{
return $this->api(self::REQUEST_GET, sprintf("/rest/api/2/issue/%s", $issueKey), array('expand' => $expand));
}

/**
* @param string $issueKey
* @param array $params
* @return Result|false|mixed
*/
public function editIssue($issueKey, $params)
{
return $this->api(self::REQUEST_PUT, sprintf("/rest/api/2/issue/%s", $issueKey), $params);
}


/**
* @param string $attachmentId
* @return Result|false|mixed
*/
public function getAttachment($attachmentId)
{
$result = $this->api(self::REQUEST_GET, "/rest/api/2/attachment/$attachmentId", array(), true);

return $result;
}

/**
* @return Result|false|mixed
*/
public function getProjects()
{
return $this->api(self::REQUEST_GET, "/rest/api/2/project");
}

/**
* @param string $projectKey
* @return Result|false|mixed
*/
public function getProject($projectKey)
{
$result = $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}", array(), true);

return $result;
}

/**
* @param string $projectKey
* @return Result|false|mixed
*/
public function getRoles($projectKey)
{
$result = $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}/roles", array(), true);
return $result;
}

/**
* @param string $projectKey
* @param int $roleId
* @return Result|false|mixed
*/
public function getRoleDetails($projectKey, $roleId)
{
$result = $this->api(self::REQUEST_GET, "/rest/api/2/project/{$projectKey}/role/{$roleId}", array(), true);
Expand Down Expand Up @@ -226,7 +254,7 @@ public function getCreateMeta(
*
* @param $issueKey
* @param $params
* @return mixed
* @return Result|mixed|false
*/
public function addComment($issueKey, $params)
{
Expand All @@ -246,21 +274,21 @@ public function addComment($issueKey, $params)
*
* @param $issueKey
* @param $params
* @return mixed
* @return Result|mixed|false
*/
public function getTransitions($issueKey, $params)
{
return $this->api(self::REQUEST_GET, sprintf("/rest/api/2/issue/%s/transitions", $issueKey), $params);
}

/**
* transation a ticket
* transition a ticket
*
* issue key should be YOURPROJ-22
*
* @param $issueKey
* @param $params
* @return mixed
* @return Result|mixed|false
*/
public function transition($issueKey, $params)
{
Expand All @@ -270,7 +298,7 @@ public function transition($issueKey, $params)
/**
* get available issue types
*
* @return mixed
* @return IssueType[]
*/
public function getIssueTypes()
{
Expand All @@ -287,6 +315,7 @@ public function getIssueTypes()
/**
* get available versions
*
* @param string $projectKey
* @return mixed
*/
public function getVersions($projectKey)
Expand All @@ -296,11 +325,22 @@ public function getVersions($projectKey)
}

/**
* get available priorities
* For backwards compatibility
*
* @deprecated use getPriorities() instead
* @return mixed
*/
public function getPriorties()
{
return $this->getPriorities();
}

/**
* get available priorities
*
* @return mixed
*/
public function getPriorities()
{
if (!count($this->priorities)) {
$priorities = array();
Expand All @@ -317,7 +357,7 @@ public function getPriorties()
/**
* get available statuses
*
* @return mixed
* @return array
*/
public function getStatuses()
{
Expand All @@ -341,7 +381,7 @@ public function getStatuses()
* @param $summary
* @param $issueType
* @param array $options
* @return mixed
* @return Result|mixed|false
*/
public function createIssue($projectKey, $summary, $issueType, $options = array())
{
Expand Down Expand Up @@ -375,8 +415,7 @@ public function createIssue($projectKey, $summary, $issueType, $options = array(
* @param $startAt
* @param $maxResult
* @param string $fields
*
* @return Jira_API_Result
* @return Result|mixed|false
*/
public function search($jql, $startAt = 0, $maxResult = 20, $fields = '*navigable')
{
Expand Down Expand Up @@ -424,30 +463,36 @@ public function createVersion($project_id, $name, $options = array())
/**
* create JIRA Attachment
*
* @param $issue
* @param $filename
* @param string $issue Jira Issue key
* @param string $filename Path to file
* @param array $options
* @param string $uploadFileName The file name to be used as attachment name
* @return mixed
*/
public function createAttachment($issue, $filename, $options = array())
public function createAttachment($issue, $filename, $options = array(), $uploadFileName = null)
{
$cFile = $this->getCurlValue($filename, null, $uploadFileName);

$options = array_merge(
array(
"file" => '@' . $filename,
"file" => $cFile,
),
$options
);

return $this->api(self::REQUEST_POST, "/rest/api/2/issue/" . $issue . "/attachments", $options, false, true);
}

/**
* send request to specified host
*
* @param string $method
* @param $url
* @param string $url
* @param array $data
* @param bool $return_as_json
* @return mixed
* @param bool $isfile
* @param bool $debug
* @return Result|mixed|false
*/
public function api(
$method = self::REQUEST_GET,
Expand Down Expand Up @@ -574,4 +619,33 @@ public function closeIssue($issueKey)
}
return $result;
}

/**
* Helper function courtesy of
* https://github.com/guzzle/guzzle/blob/3a0787217e6c0246b457e637ddd33332efea1d2a/src/Guzzle/Http/Message/PostFile.php#L90
*
* @param string $fileName
* @param string $contentType
* @param string|null $postName
* @return \CURLFile|string
*/
protected function getCurlValue($fileName, $contentType, $postName = null)
{
if ($postName === null) {
$postName = basename($fileName);
}
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax
// See: https://wiki.php.net/rfc/curl-file-upload
if (function_exists('curl_file_create')) {
return curl_file_create($fileName, $contentType, $postName);
}

// Use the old style if using an older version of PHP
$value = "@{$fileName};filename=" . $postName;
if ($contentType) {
$value .= ';type=' . $contentType;
}

return $value;
}
}
13 changes: 8 additions & 5 deletions src/Jira/Api/Client/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ public function __construct()
/**
* send request to the api server
*
* @param $method
* @param $url
* @param string $method
* @param string $url
* @param array $data
* @param $endpoint
* @param $credential
* @param string $endpoint
* @param AuthenticationInterface $credential
* @param boolean $isFile
* @param boolean $debug
* @return array|string
* @throws Exception
* @throws \Exception
* @throws UnauthorizedException
*/
public function sendRequest($method, $url, $data = array(), $endpoint, AuthenticationInterface $credential, $isFile = false, $debug = false)
{
Expand Down