-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add 2 feature(create group, retrive users from group)
- Loading branch information
Showing
5 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace JiraRestApi\Group; | ||
use JiraRestApi\ClassSerialize; | ||
|
||
class GroupUser | ||
{ | ||
/** | ||
* @var integer | ||
*/ | ||
public $size; | ||
|
||
/** @var array */ | ||
public $items; | ||
|
||
/** @var integer */ | ||
public $max_results; | ||
|
||
/** @var integer */ | ||
public $start_index; | ||
|
||
/** @var integer */ | ||
public $end_index; | ||
} | ||
|
||
/** | ||
* Class Group | ||
* | ||
* @package JiraRestApi\Group | ||
* | ||
* @see https://docs.atlassian.com/jira/REST/server/#api/2/group | ||
*/ | ||
class Group implements \JsonSerializable | ||
{ | ||
use ClassSerialize; | ||
|
||
/** | ||
* uri which was hit. | ||
* | ||
* @var string | ||
*/ | ||
public $self; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $name; | ||
|
||
/** | ||
* @var GroupUser | ||
*/ | ||
public $users; | ||
|
||
/** | ||
* @var object | ||
*/ | ||
public $expand; | ||
|
||
public function jsonSerialize() | ||
{ | ||
return array_filter(get_object_vars($this)); | ||
} | ||
|
||
public function setName($name) { | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace JiraRestApi\Group; | ||
use JiraRestApi\ClassSerialize; | ||
|
||
/** | ||
* Class GroupSearchResult | ||
* | ||
* @package JiraRestApi\Group | ||
* | ||
* @see https://docs.atlassian.com/jira/REST/server/#api/2/group | ||
*/ | ||
class GroupSearchResult implements \JsonSerializable | ||
{ | ||
use ClassSerialize; | ||
|
||
/** | ||
* uri which was hit. | ||
* | ||
* @var string | ||
*/ | ||
public $self; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
public $maxResults; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
public $startAt; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
public $total; | ||
|
||
/** @var \JiraRestApi\User\User[] */ | ||
public $values; | ||
|
||
public function jsonSerialize() | ||
{ | ||
return array_filter(get_object_vars($this)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
namespace JiraRestApi\Group; | ||
|
||
/** | ||
* Class to perform all groups related queries. | ||
* @package JiraRestApi\Group | ||
*/ | ||
class GroupService extends \JiraRestApi\JiraClient | ||
{ | ||
private $uri = '/group'; | ||
|
||
/** | ||
* Function to get group. | ||
* | ||
* @param array $paramArray Possible values for $paramArray 'username', 'key'. | ||
* "Either the 'username' or the 'key' query parameters need to be provided". | ||
* | ||
* @return Group class | ||
*/ | ||
public function get($paramArray) | ||
{ | ||
$queryParam = '?'.http_build_query($paramArray); | ||
|
||
$ret = $this->exec($this->uri.$queryParam, null); | ||
|
||
$this->log->addInfo("Result=\n".$ret); | ||
|
||
return $this->json_mapper->map( | ||
json_decode($ret), new User() | ||
); | ||
} | ||
|
||
/** | ||
* Get users from group | ||
* | ||
* @param $paramArray groupname, includeInactiveUsers, startAt, maxResults | ||
* @return GroupSearchResult | ||
* @throws \JiraRestApi\JiraException | ||
* @throws \JsonMapper_Exception | ||
*/ | ||
public function getMembers($paramArray) | ||
{ | ||
$queryParam = '?' . http_build_query($paramArray); | ||
|
||
$ret = $this->exec($this->uri . '/member'.$queryParam, null); | ||
|
||
$this->log->addInfo("Result=\n".$ret); | ||
|
||
$userData = json_decode($ret); | ||
|
||
$res = $this->json_mapper->map($userData, new GroupSearchResult()); | ||
|
||
return $res; | ||
} | ||
|
||
/** | ||
* Creates a group by given group parameter | ||
* | ||
* @param $group \JiraRestApi\Group\Group | ||
* @return array | ||
* @throws \JiraRestApi\JiraException | ||
* @throws \JsonMapper_Exception | ||
*/ | ||
public function createGroup($group) | ||
{ | ||
$data = json_encode($group); | ||
|
||
$ret = $this->exec($this->uri, $data); | ||
|
||
$this->log->addInfo("Result=\n".$ret); | ||
|
||
$groupData = json_decode($ret); | ||
$groups = []; | ||
|
||
$group = $this->json_mapper->map( | ||
json_decode($ret), new Group() | ||
); | ||
|
||
return $group; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
use JiraRestApi\Dumper; | ||
use JiraRestApi\Group\Group; | ||
use JiraRestApi\Group\GroupService; | ||
use JiraRestApi\IssueLink\IssueLink; | ||
use JiraRestApi\IssueLink\IssueLinkService; | ||
use JiraRestApi\JiraException; | ||
|
||
class GroupTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testCreateGroup() | ||
{ | ||
$this->markTestSkipped(); | ||
try { | ||
$g = new Group(); | ||
|
||
$g->name = 'Test group for REST API'; | ||
|
||
$gs = new GroupService(); | ||
|
||
$ret = $gs->createGroup($g); | ||
|
||
Dumper::dump($ret); | ||
} catch (JiraException $e) { | ||
$this->assertTrue(false, 'testCreateGroup Failed : '.$e->getMessage()); | ||
} | ||
} | ||
|
||
public function testGetUsersFromGroup() | ||
{ | ||
try { | ||
$queryParam = [ | ||
'groupname' => 'Test group for REST API', | ||
'includeInactiveUsers' => true, // default false | ||
'startAt' => 0, | ||
'maxResults' => 50, | ||
]; | ||
|
||
$gs = new GroupService(); | ||
|
||
$ret = $gs->getMembers($queryParam); | ||
|
||
// print all users in the group | ||
foreach($ret->values as $user) { | ||
print_r($user); | ||
} | ||
|
||
} catch (JiraException $e) { | ||
$this->assertTrue(false, 'testCreateGroup Failed : '.$e->getMessage()); | ||
} | ||
} | ||
} |