Skip to content

Commit

Permalink
add 2 feature(create group, retrive users from group)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesstif committed May 29, 2017
1 parent 60ab818 commit 904cfa0
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 0 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ $iss = new IssueService(new ArrayConfiguration(
### User
- [Get User Info](#get-user-info)

### Group
- [Create Group](#create-group)
- [Get Users from group](#get-users-from-group)

#### Get Project Info

```php
Expand Down Expand Up @@ -927,6 +931,65 @@ try {

```

#### Create Group

Create new group.

```php
<?php
require 'vendor/autoload.php';

use JiraRestApi\JiraException;
use JiraRestApi\Group\GroupService;

try {
$g = new Group();

$g->name = 'Test group for REST API';

$gs = new GroupService();

$ret = $gs->createGroup($g);
var_dump($user);
} catch (JiraException $e) {
print("Error Occured! " . $e->getMessage());
}

```

#### Get Users from group

returns a paginated list of users who are members of the specified group and its subgroups.

```php
<?php
require 'vendor/autoload.php';

use JiraRestApi\JiraException;
use JiraRestApi\Group\GroupService;

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) {
print("Error Occured! " . $e->getMessage());
}

```

# License

Apache V2 License
Expand Down
70 changes: 70 additions & 0 deletions src/Group/Group.php
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;
}

}
46 changes: 46 additions & 0 deletions src/Group/GroupSearchResult.php
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));
}
}
82 changes: 82 additions & 0 deletions src/Group/GroupService.php
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;
}
}
53 changes: 53 additions & 0 deletions tests/groupTest.php
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());
}
}
}

0 comments on commit 904cfa0

Please sign in to comment.