Skip to content

Commit b6d6ce6

Browse files
authored
feat(project): Adding fetching roles datas for a project (#98)
* feat(project): Add a function to get all project roles * feat(role): Get project role id info * fix: Fix StyleCI
1 parent 54ba79f commit b6d6ce6

4 files changed

Lines changed: 172 additions & 0 deletions

File tree

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ $iss = new IssueService(new ArrayConfiguration(
124124
- [Get Project Components](#get-project-components)
125125
- [Get Project Type](#get-project-type)
126126
- [Get Project Version](#get-project-version)
127+
- [Get Project Roles](#get-project-roles)
128+
- [Get Project Role](#get-project-role)
127129

128130
### Custom Field
129131
- [Get All Field list](#get-all-field-list)
@@ -490,6 +492,51 @@ try {
490492

491493
```
492494

495+
#### Get Project Roles
496+
497+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-roles/#api-rest-api-3-project-projectidorkey-role-get)
498+
499+
```php
500+
<?php
501+
require 'vendor/autoload.php';
502+
503+
use JiraCloud\Project\ProjectService;
504+
use JiraCloud\JiraException;
505+
506+
try {
507+
$projectService = new ProjectService();
508+
509+
// return project roles list.
510+
$ret = $projectService->getProjectRoles('TEST');
511+
512+
var_dump($ret);
513+
} catch (JiraCloud\JiraException $e) {
514+
print('Error Occurred! ' . $e->getMessage());
515+
}
516+
```
517+
518+
#### Get Project Role
519+
520+
[See Jira API reference](https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-project-roles/#api-rest-api-3-project-projectidorkey-role-id-get)
521+
522+
```php
523+
<?php
524+
require 'vendor/autoload.php';
525+
526+
use JiraCloud\Project\ProjectService;
527+
use JiraCloud\JiraException;
528+
529+
try {
530+
$projectService = new ProjectService();
531+
532+
// return project role data with reporter assigned.
533+
$ret = $projectService->getProjectRoles('TEST', '1');
534+
535+
var_dump($ret);
536+
} catch (JiraCloud\JiraException $e) {
537+
print('Error Occurred! ' . $e->getMessage());
538+
}
539+
```
493540

494541
#### Get All Field List
495542

src/Project/ProjectService.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use JiraCloud\Issue\Reporter;
88
use JiraCloud\Issue\Version;
99
use JiraCloud\JiraException;
10+
use JiraCloud\Role\Role;
1011

1112
class ProjectService extends \JiraCloud\JiraClient
1213
{
@@ -392,4 +393,61 @@ public function archiveProject($projectIdOrKey)
392393

393394
return $ret;
394395
}
396+
397+
/**
398+
* @param $projectIdOrKey
399+
*
400+
* @throws JiraException
401+
*
402+
* @return array<string, string>
403+
*
404+
* STATUS 401 Returned if the user is not logged in.
405+
* STATUS 404 - Returned if the project does not exist.
406+
*/
407+
public function getProjectRoles($projectIdOrKey)
408+
{
409+
$response = $this->exec($this->uri.'/'.$projectIdOrKey.'/role');
410+
411+
$this->log->info('getProjectRoles Result='.$response);
412+
413+
return (array) json_decode($response);
414+
}
415+
416+
/**
417+
* @param $projectIdOrKey
418+
* @param $roleId
419+
*
420+
* @throws JiraException
421+
*
422+
* @return Role
423+
*
424+
* STATUS 401 Returned if the user is not logged in.
425+
* STATUS 404 - Returned if the project does not exist.
426+
*/
427+
public function getProjectRole($projectIdOrKey, $roleId, $excludeInactiveUsers = false)
428+
{
429+
$response = $this->exec($this->uri.'/'.$projectIdOrKey.'/role/'.$roleId.'?excludeInactiveUsers='.$excludeInactiveUsers);
430+
431+
$this->log->info('getProjectRole Result='.$response);
432+
433+
$reporters = array_map(
434+
function ($elem) {
435+
$reporter = $this->json_mapper->map($elem, new Reporter());
436+
$reporter->accountId = $elem->actorUser->accountId;
437+
438+
return $reporter;
439+
},
440+
array_filter(json_decode($response)->actors, function ($elem) {
441+
return $elem->type === 'atlassian-user-role-actor';
442+
}),
443+
);
444+
445+
$role = $this->json_mapper->map(
446+
json_decode($response),
447+
new Role()
448+
);
449+
$role->actors = $reporters;
450+
451+
return $role;
452+
}
395453
}

src/Role/Role.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace JiraCloud\Role;
4+
5+
use JiraCloud\ClassSerialize;
6+
7+
class Role
8+
{
9+
use ClassSerialize;
10+
11+
public string $name;
12+
public int $id;
13+
public string $description;
14+
public string $self;
15+
public array $actors;
16+
}

tests/ProjectTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use JiraCloud\JiraException;
88
use JiraCloud\Project\Project;
99
use JiraCloud\Project\ProjectType;
10+
use JiraCloud\Role\Role;
1011
use PHPUnit\Framework\TestCase;
1112
use JiraCloud\Project\ProjectService;
1213

@@ -202,4 +203,54 @@ public function get_unknown_project_type_expect_to_JiraException(string $projKey
202203

203204
return $projKey;
204205
}
206+
207+
/**
208+
* @test
209+
* @depends get_project_lists
210+
*/
211+
public function get_project_roles_array(string $projKey): array
212+
{
213+
try {
214+
$projectService = new ProjectService();
215+
216+
$roles = $projectService->getProjectRoles($projKey);
217+
218+
$this->assertIsArray($roles);
219+
220+
$parts = explode('/', rtrim(array_shift($roles), '/'));
221+
$roleId = end($parts);
222+
} catch (JiraException $e) {
223+
$this->fail('get_project_roles_array ' . $e->getMessage());
224+
}
225+
226+
return ['projKey' => 'JUL', 'roleId' => $roleId];
227+
}
228+
229+
/**
230+
* @test
231+
* @depends get_project_roles_array
232+
* @param array<string, string> $datas
233+
* projKey: project key
234+
* roleId: role id
235+
*
236+
* @return string
237+
* @throws \JsonMapper_Exception
238+
*/
239+
public function get_project_role(array $datas): string
240+
{
241+
try {
242+
$projectService = new ProjectService();
243+
244+
$role = $projectService->getProjectRole($datas['projKey'], $datas['roleId'], true);
245+
246+
self::assertInstanceOf(Role::class, $role);
247+
self::assertIsString($role->name);
248+
self::assertIsString($role->description);
249+
self::assertIsInt($role->id);
250+
} catch (JiraException $e) {
251+
$this->fail('get_project_role ' . $e->getMessage());
252+
}
253+
254+
return '$projKey';
255+
}
205256
}

0 commit comments

Comments
 (0)