Skip to content

Commit 647548e

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

File tree

5 files changed

+364
-0
lines changed

5 files changed

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

0 commit comments

Comments
 (0)