diff --git a/README.md b/README.md index cd3d41c..59827cf 100644 --- a/README.md +++ b/README.md @@ -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) ## 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. + +## 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)). + ## 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 + +## 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. \ No newline at end of file diff --git a/src/Jira.php b/src/Jira.php index 2b4c512..361f3a5 100644 --- a/src/Jira.php +++ b/src/Jira.php @@ -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; } -} +} \ No newline at end of file diff --git a/src/config/jira.php b/src/config/jira.php index 33e991a..41f1f2d 100644 --- a/src/config/jira.php +++ b/src/config/jira.php @@ -2,10 +2,14 @@ return [ - 'url' => '', - - 'username' => '', - - 'password' => '', - -]; \ No newline at end of file + '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'), + ] + ] + +];