Skip to content

multiple connections #8

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -3,9 +3,11 @@
Easy access Jira rest api in Laravel5.

* [Installation and Requirements](#installation)
* [Configuration](#configuration)
* [Searching issues](#searching)
* [Creating issues](#creating)
* [Editing issues](#editing)
* [Changing connections](#connections)

<a name="installation"></a>
## Installation and Requirements
@@ -33,13 +35,36 @@ Then, update `config/app.php` by adding an entry for the service provider.
Finally, from the command line again, run `php artisan vendor:publish` to publish
the default configuration file to config/jira.php.

<a name="configuration"></a>
## Configuration

```php
'default' => env('JIRA_CONNECTION', 'example'),

'connections' => [
'example' => [
'url' => env('JIRA_URL', 'http://jira.mydomain.com'),
'username' => env('JIRA_USER', 'johndoe'),
'password' => env('JIRA_PASS', 'pass123'),
]
]
```
Using the package without setting the connection will default to your 'default' setting.
Define as many connections as you want in the 'connections' array.
You can add your Jira configuration to your environment file (may need to change config accordingly).
Choose your connection using the connection() method (see below [Changing connections](#connections)).

<a name="searching"></a>
## Searching issues

The search method will take the jql query string:

```php
$response = Jira::search( 'project = YourProject AND labels = somelabel' );
use Jira;

public function index(){
$response = Jira::search( 'project = YourProject AND labels = somelabel' );
}
```

You can build and test the jql beforehand if you go to your Jira site Issues > Search for Issues > Advanced Search.
@@ -83,6 +108,26 @@ Further information can be found on [JIRA documentation - edit issue](https://de

> **NOTE** fields parameter is already included in the payload

<a name="connections"></a>
## Changing connections

Multiple connections can be defined to access different JIRA instances.

Calling the connection with empty parameter will default to your 'default' config in config/jira.php.
```php
$response = Jira::connection()->search( 'project = YourProject AND labels = somelabel' );
```

You can call the connection() method with a string to select your connection defined in config/jira.php.
```php
$response = Jira::connection('example')->search( 'project = YourProject AND labels = somelabel' );
```

Finally you can call the connection() method with an array of url, username and password.
```php
$response = Jira::connection(['newUrl','newUsername','newPassword'])->search( 'project = YourProject AND labels = somelabel' );
```

---

Released under the MIT License. See the LICENSE file for details.
40 changes: 31 additions & 9 deletions src/Jira.php
Original file line number Diff line number Diff line change
@@ -5,6 +5,27 @@
class Jira
{

/**
* Handling conncetions on demand
*
* @param type $connection
* @return \self
*/
public static function connection( $connection = NULL )
{
if( is_string( $connection ) )
{
config( ['jira.connection' => $connection] );
} elseif( is_array( $connection ) )
{
config( ['jira.connection' => 'custom'] );
config( ['jira.connections.custom.url' => $connection[0]] );
config( ['jira.connections.custom.username' => $connection[1]] );
config( ['jira.connections.custom.password' => $connection[2]] );
}
return new self;
}

/**
* Search function to search issues with JQL string
*
@@ -13,7 +34,7 @@ class Jira
*/
public static function search( $jql = NULL )
{
$data = json_encode( array( 'jql' => $jql ) );
$data = json_encode( array('jql' => $jql) );
$result = self::request( 'search', $data );

return json_decode( $result );
@@ -27,8 +48,8 @@ public static function search( $jql = NULL )
*/
public static function create( array $data )
{
$data = json_encode( array( 'fields' => $data ) );
$data = str_replace('\\\\','\\',$data);
$data = json_encode( array('fields' => $data) );
$data = str_replace( '\\\\', '\\', $data );
$result = self::request( 'issue', $data, 1 );

return json_decode( $result );
@@ -43,8 +64,8 @@ public static function create( array $data )
*/
public static function update( $issue, array $data )
{
$data = json_encode( array( 'fields' => $data ) );
$data = str_replace('\\\\','\\',$data);
$data = json_encode( array('fields' => $data) );
$data = str_replace( '\\\\', '\\', $data );
$result = self::request( 'issue/' . $issue, $data, 0, 1 );

return json_decode( $result );
@@ -63,11 +84,12 @@ private static function request( $request, $data, $is_post = 0, $is_put = 0 )
{
$ch = curl_init();

$connection = config( 'jira.connection', config( 'jira.default' ) );
curl_setopt_array( $ch, array(
CURLOPT_URL => config( 'jira.url' ) . '/rest/api/2/' . $request,
CURLOPT_USERPWD => config( 'jira.username' ) . ':' . config( 'jira.password' ),
CURLOPT_URL => config( "jira.connections.{$connection}.url" ) . '/rest/api/2/' . $request,
CURLOPT_USERPWD => config( "jira.connections.{$connection}.username" ) . ':' . config( "jira.connections.{$connection}.password" ),
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => array( 'Content-type: application/json' ),
CURLOPT_HTTPHEADER => array('Content-type: application/json'),
CURLOPT_RETURNTRANSFER => 1,
) );

@@ -88,4 +110,4 @@ private static function request( $request, $data, $is_post = 0, $is_put = 0 )
return $response;
}

}
}
18 changes: 11 additions & 7 deletions src/config/jira.php
Original file line number Diff line number Diff line change
@@ -2,10 +2,14 @@

return [

'url' => '',

'username' => '',

'password' => '',

];
'default' => env('JIRA_CONNECTION', 'example'),

'connections' => [
'example' => [
'url' => env('JIRA_URL', 'http://jira.mydomain.com'),
'username' => env('JIRA_USER', 'johndoe'),
'password' => env('JIRA_PASS', 'pass123'),
]
]

];