Skip to content

Commit 859f904

Browse files
committed
Add API endpoints to interact with organiztion roles
1 parent 71fec50 commit 859f904

File tree

5 files changed

+362
-0
lines changed

5 files changed

+362
-0
lines changed

doc/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ v3 APIs:
4444
* [Secrets](organization/actions/secrets.md)
4545
* [Variables](organization/actions/variables.md)
4646
* [Secret Scanning Alert](organization/secret-scanning.md)
47+
* [Organization Roles](organization/organization-roles.md)
4748
* [Projects](project/projects.md)
4849
* [Columns](project/columns.md)
4950
* [Cards](project/cards.md)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
## Organization / Webhooks API
2+
[Back to the navigation](../README.md)
3+
4+
Listing, showing, assigning, and removing orgniazationroles.
5+
Wraps [GitHub Organization Roles API](https://docs.github.com/en/rest/orgs/organization-roles).
6+
7+
Additional APIs:
8+
* [Organization](../doc/organization)
9+
10+
### List all organizaton roles in an organization
11+
12+
> Requires [authentication](../security.md).
13+
14+
```php
15+
$roles = $client->organization()->organizationRoles()->all('acme');
16+
```
17+
18+
Returns a counter and a list of organization roles in the organization.
19+
20+
### Get an organization role in an organization
21+
22+
> Requires [authentication](../security.md).
23+
24+
```php
25+
$role = $client->organization()->organizationRoles()->show('acme', 123);
26+
```
27+
28+
Returns a single organization role in the organization.
29+
30+
### List all teams with role assigned in an organization
31+
32+
> Requires [authentication](../security.md).
33+
34+
```php
35+
$users = $client->organization()->organizationRoles()->listTeamsWithRole('acme', 1);
36+
```
37+
38+
Returns a list of teams with the role assigned to them.
39+
40+
### Assign a single role to a team in an organization
41+
42+
> Requires [authentication](../security.md).
43+
44+
```php
45+
$client->organization()->organizationRoles()->assignRoleToTeam('acme', 1, 'admin-user');
46+
```
47+
48+
No content is returned.
49+
50+
### Remove a single role from a team in an organization
51+
52+
> Requires [authentication](../security.md).
53+
54+
```php
55+
$client->organization()->organizationRoles()->removeRoleFromTeam('acme', 1, 'admin-team');
56+
```
57+
58+
No content is returned.
59+
60+
### Remove all roles from a team in an organization
61+
62+
> Requires [authentication](../security.md).
63+
64+
```php
65+
$client->organization()->organizationRoles()->removeAllRolesFromTeam('acme', 'admin-team');
66+
```
67+
68+
No content is returned.
69+
70+
### List all users with role assigned in an organization
71+
72+
> Requires [authentication](../security.md).
73+
74+
```php
75+
$users = $client->organization()->organizationRoles()->listUsersWithRole('acme', 1);
76+
```
77+
78+
Returns a list of users with the role assigned to them.
79+
80+
### Assign a single role to a user in an organization
81+
82+
> Requires [authentication](../security.md).
83+
84+
```php
85+
$client->organization()->organizationRoles()->assignRoleToUser('acme', 1, 'admin-user');
86+
```
87+
88+
No content is returned.
89+
90+
### Remove a single role from a user in an organization
91+
92+
> Requires [authentication](../security.md).
93+
94+
```php
95+
$client->organization()->organizationRoles()->removeRoleFromUser('acme', 1, 'admin-user');
96+
```
97+
98+
No content is returned.
99+
100+
### Remove all roles from a user in an organization
101+
102+
> Requires [authentication](../security.md).
103+
104+
```php
105+
$client->organization()->organizationRoles()->removeAllRolesFromUser('acme', 'admin-user');
106+
```
107+
108+
No content is returned.

lib/Github/Api/Organization.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Github\Api\Organization\Actions\Variables;
88
use Github\Api\Organization\Hooks;
99
use Github\Api\Organization\Members;
10+
use Github\Api\Organization\OrganizationRoles;
1011
use Github\Api\Organization\OutsideCollaborators;
1112
use Github\Api\Organization\SecretScanning;
1213
use Github\Api\Organization\Teams;
@@ -158,4 +159,9 @@ public function secretScanning(): SecretScanning
158159
{
159160
return new SecretScanning($this->getClient());
160161
}
162+
163+
public function organizationRoles(): OrganizationRoles
164+
{
165+
return new OrganizationRoles($this->getClient());
166+
}
161167
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace Github\Api\Organization;
4+
5+
use Github\Api\AbstractApi;
6+
7+
/**
8+
* @link https://docs.github.com/en/rest/orgs/organization-roles
9+
*/
10+
class OrganizationRoles extends AbstractApi
11+
{
12+
public function all(string $organization)
13+
{
14+
return $this->get('/orgs/' . rawurlencode($organization) . '/organization-roles');
15+
}
16+
17+
public function show(string $organization, int $roleId)
18+
{
19+
return $this->get('/orgs/' . rawurlencode($organization) . '/organization-roles/' . $roleId);
20+
}
21+
22+
public function listTeamsWithRole(string $organization, int $roleId)
23+
{
24+
return $this->get('/orgs/' . rawurlencode($organization) . '/organization-roles/' . $roleId . '/teams');
25+
}
26+
27+
public function assignRoleToTeam(string $organization, int $roleId, string $teamSlug): void
28+
{
29+
$this->put('/orgs/' . rawurlencode($organization) . '/organization-roles/teams/' . rawurlencode($teamSlug) . '/' . $roleId);
30+
}
31+
32+
public function removeRoleFromTeam(string $organization, int $roleId, string $teamSlug): void
33+
{
34+
$this->delete('/orgs/' . rawurlencode($organization) . '/organization-roles/teams/' . rawurlencode($teamSlug) . '/' . $roleId);
35+
}
36+
37+
public function removeAllRolesFromTeam(string $organization, string $teamSlug): void
38+
{
39+
$this->delete('/orgs/' . rawurlencode($organization) . '/organization-roles/teams/' . rawurlencode($teamSlug));
40+
}
41+
42+
public function listUsersWithRole(string $organization, int $roleId): array
43+
{
44+
return $this->get('/orgs/' . rawurlencode($organization) . '/organization-roles/' . $roleId . '/users');
45+
}
46+
47+
public function assignRoleToUser(string $organization, int $roleId, string $username): void
48+
{
49+
$this->put('/orgs/' . rawurlencode($organization) . '/organization-roles/users/' . rawurlencode($username) . '/' . $roleId);
50+
}
51+
52+
public function removeRoleFromUser(string $organization, int $roleId, string $username): void
53+
{
54+
$this->delete('/orgs/' . rawurlencode($organization) . '/organization-roles/users/' . rawurlencode($username) . '/' . $roleId);
55+
}
56+
57+
public function removeAllRolesFromUser(string $organization, string $username): void
58+
{
59+
$this->delete('/orgs/' . rawurlencode($organization) . '/organization-roles/users/' . rawurlencode($username));
60+
}
61+
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Github\Tests\Api\Organization;
4+
5+
use Github\Api\Organization\OrganizationRoles;
6+
use Github\Tests\Api\TestCase;
7+
8+
class OrganizationRolesTest extends TestCase
9+
{
10+
/**
11+
* @test
12+
*/
13+
public function shouldGetAllOrganizationRoles()
14+
{
15+
$expectedValue = [
16+
'total_count' => 1,
17+
'roles' => [[
18+
'id' => 1,
19+
'name' => 'all_repo_admin',
20+
'description' => 'Grants admin access to all repositories in the organization.',
21+
'permissions' => [],
22+
'organization' => NULL,
23+
'created_at' => '2023-01-01T00:00:00Z',
24+
'updated_at' => '2023-01-01T00:00:00Z',
25+
'source' => 'Predefined',
26+
'base_role' => 'admin',
27+
]],
28+
];
29+
30+
$api = $this->getApiMock();
31+
$api->expects($this->once())
32+
->method('get')
33+
->with('/orgs/acme/organization-roles')
34+
->will($this->returnValue($expectedValue));
35+
36+
$this->assertEquals($expectedValue, $api->all('acme'));
37+
}
38+
39+
/**
40+
* @test
41+
*/
42+
public function shouldShowSingleOrganizationRole()
43+
{
44+
$expectedValue = [
45+
'id' => 1,
46+
'name' => 'all_repo_admin',
47+
'description' => 'Grants admin access to all repositories in the organization.',
48+
'permissions' => [],
49+
'organization' => NULL,
50+
'created_at' => '2023-01-01T00:00:00Z',
51+
'updated_at' => '2023-01-01T00:00:00Z',
52+
'source' => 'Predefined',
53+
'base_role' => 'admin',
54+
];
55+
56+
$api = $this->getApiMock();
57+
$api->expects($this->once())
58+
->method('get')
59+
->with('/orgs/acme/organization-roles/1')
60+
->will($this->returnValue($expectedValue));
61+
62+
$this->assertEquals($expectedValue, $api->show('acme', 1));
63+
}
64+
65+
66+
/**
67+
* @test
68+
*/
69+
public function shouldGetAllTeamsWithRole()
70+
{
71+
$expectedValue = [['name' => 'Acme Admins']];
72+
73+
$api = $this->getApiMock();
74+
$api->expects($this->once())
75+
->method('get')
76+
->with('/orgs/acme/organization-roles/1/teams')
77+
->will($this->returnValue($expectedValue));
78+
79+
$this->assertEquals($expectedValue, $api->listTeamsWithRole('acme', 1));
80+
}
81+
82+
/**
83+
* @test
84+
*/
85+
public function shouldAssignRoleToTeam()
86+
{
87+
$api = $this->getApiMock();
88+
$api->expects($this->once())
89+
->method('put')
90+
->with('/orgs/acme/organization-roles/teams/acme-admins/1')
91+
->will($this->returnValue(''));
92+
93+
$api->assignRoleToTeam('acme', 1, 'acme-admins');
94+
}
95+
96+
/**
97+
* @test
98+
*/
99+
public function shouldRemoveRoleFromTeam()
100+
{
101+
$api = $this->getApiMock();
102+
$api->expects($this->once())
103+
->method('delete')
104+
->with('/orgs/acme/organization-roles/teams/acme-admins/1')
105+
->will($this->returnValue(''));
106+
107+
$api->removeRoleFromTeam('acme', 1, 'acme-admins');
108+
}
109+
110+
/**
111+
* @test
112+
*/
113+
public function shouldRemoveAllRolesFromTeam()
114+
{
115+
$api = $this->getApiMock();
116+
$api->expects($this->once())
117+
->method('delete')
118+
->with('/orgs/acme/organization-roles/teams/acme-admins')
119+
->will($this->returnValue(''));
120+
121+
$api->removeAllRolesFromTeam('acme', 'acme-admins');
122+
}
123+
124+
/**
125+
* @test
126+
*/
127+
public function shouldGetAllUsersWithRole()
128+
{
129+
$expectedValue = [['username' => 'Admin']];
130+
131+
$api = $this->getApiMock();
132+
$api->expects($this->once())
133+
->method('get')
134+
->with('/orgs/acme/organization-roles/1/users')
135+
->will($this->returnValue($expectedValue));
136+
137+
$this->assertEquals($expectedValue, $api->listUsersWithRole('acme', 1));
138+
}
139+
140+
/**
141+
* @test
142+
*/
143+
public function shouldAssignRoleToUser()
144+
{
145+
$api = $this->getApiMock();
146+
$api->expects($this->once())
147+
->method('put')
148+
->with('/orgs/acme/organization-roles/users/admin/1')
149+
->will($this->returnValue(''));
150+
151+
$api->assignRoleToUser('acme', 1, 'admin');
152+
}
153+
154+
/**
155+
* @test
156+
*/
157+
public function shouldRemoveRoleFromUser()
158+
{
159+
$api = $this->getApiMock();
160+
$api->expects($this->once())
161+
->method('delete')
162+
->with('/orgs/acme/organization-roles/users/admin/1')
163+
->will($this->returnValue(''));
164+
165+
$api->removeRoleFromUser('acme', 1, 'admin');
166+
}
167+
168+
/**
169+
* @test
170+
*/
171+
public function shouldRemoveAllRolesFromUser()
172+
{
173+
$api = $this->getApiMock();
174+
$api->expects($this->once())
175+
->method('delete')
176+
->with('/orgs/acme/organization-roles/users/admin')
177+
->will($this->returnValue(''));
178+
179+
$api->removeAllRolesFromUser('acme', 'admin');
180+
}
181+
182+
protected function getApiClass(): string
183+
{
184+
return OrganizationRoles::class;
185+
}
186+
}

0 commit comments

Comments
 (0)