Skip to content

Commit 53dcd8c

Browse files
SachinKSingh28JohnVillalovos
authored andcommitted
feat(api): add support for gitlab service account
1 parent 58d50f2 commit 53dcd8c

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

docs/gl_objects/groups.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,24 @@ Edit group push rules::
458458
Delete group push rules::
459459

460460
pr.delete()
461+
462+
Group Service Account
463+
=====================
464+
465+
Reference
466+
---------
467+
468+
* v4 API:
469+
470+
+ :class:`gitlab.v4.objects.GroupServiceAccount`
471+
+ :class:`gitlab.v4.objects.GroupServiceAccountManager`
472+
+ :attr:`gitlab.v4.objects.Group.serviceaccounts`
473+
474+
* GitLab API: https://docs.gitlab.com/ee/api/groups.html#service-accounts
475+
476+
Examples
477+
---------
478+
479+
Create group service account (only allowed at top level group)::
480+
481+
group.serviceaccount.create({'name': 'group-service-account', 'username': 'group-service-account'})

gitlab/v4/objects/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from .reviewers import *
6060
from .runners import *
6161
from .secure_files import *
62+
from .service_accounts import *
6263
from .settings import *
6364
from .sidekiq import *
6465
from .snippets import *

gitlab/v4/objects/groups.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
from .projects import GroupProjectManager, SharedProjectManager # noqa: F401
4747
from .push_rules import GroupPushRulesManager
4848
from .runners import GroupRunnerManager # noqa: F401
49+
from .service_accounts import GroupServiceAccountManager # noqa: F401
4950
from .statistics import GroupIssuesStatisticsManager # noqa: F401
5051
from .variables import GroupVariableManager # noqa: F401
5152
from .wikis import GroupWikiManager # noqa: F401
@@ -102,6 +103,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
102103
variables: GroupVariableManager
103104
wikis: GroupWikiManager
104105
saml_group_links: "GroupSAMLGroupLinkManager"
106+
service_accounts: "GroupServiceAccountManager"
105107

106108
@cli.register_custom_action("Group", ("project_id",))
107109
@exc.on_http_error(exc.GitlabTransferProjectError)

gitlab/v4/objects/service_accounts.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from gitlab.base import RESTManager, RESTObject
2+
from gitlab.mixins import CreateMixin
3+
from gitlab.types import RequiredOptional
4+
5+
__all__ = ["GroupServiceAccount", "GroupServiceAccountManager"]
6+
7+
8+
class GroupServiceAccount(RESTObject):
9+
_id_attr = "id"
10+
11+
12+
class GroupServiceAccountManager(CreateMixin, RESTManager):
13+
_path = "/groups/{group_id}/service_accounts"
14+
_obj_cls = GroupServiceAccount
15+
_from_parent_attrs = {"group_id": "id"}
16+
_create_attrs = RequiredOptional(
17+
optional=(
18+
"name",
19+
"username",
20+
),
21+
)

tests/functional/api/test_groups.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,12 @@ def test_group_saml_group_links(group):
308308
group.saml_group_links.create(
309309
{"saml_group_name": "saml-group-1", "access_level": 10}
310310
)
311+
312+
313+
@pytest.mark.gitlab_premium
314+
def test_group_service_account(group):
315+
service_account = group.service_accounts.create(
316+
{"name": "gitlab-service-account", "username": "gitlab-service-account"}
317+
)
318+
assert service_account.name == "gitlab-service-account"
319+
assert service_account.username == "gitlab-service-account"

tests/unit/objects/test_groups.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@
8383
"max_file_size": 100,
8484
}
8585

86+
service_account_content = {
87+
"name": "gitlab-service-account",
88+
"username": "gitlab-service-account",
89+
}
90+
8691

8792
@pytest.fixture
8893
def resp_groups():
@@ -325,6 +330,19 @@ def resp_restore_group(created_content):
325330
yield rsps
326331

327332

333+
@pytest.fixture
334+
def resp_create_group_service_account():
335+
with responses.RequestsMock() as rsps:
336+
rsps.add(
337+
method=responses.POST,
338+
url="http://localhost/api/v4/groups/1/service_accounts",
339+
json=service_account_content,
340+
content_type="application/json",
341+
status=200,
342+
)
343+
yield rsps
344+
345+
328346
def test_get_group(gl, resp_groups):
329347
data = gl.groups.get(1)
330348
assert isinstance(data, gitlab.v4.objects.Group)
@@ -466,3 +484,11 @@ def test_delete_saml_group_link(group, resp_delete_saml_group_link):
466484

467485
def test_group_restore(group, resp_restore_group):
468486
group.restore()
487+
488+
489+
def test_create_group_service_account(group, resp_create_group_service_account):
490+
service_account = group.service_accounts.create(
491+
{"name": "gitlab-service-account", "username": "gitlab-service-account"}
492+
)
493+
assert service_account.name == "gitlab-service-account"
494+
assert service_account.username == "gitlab-service-account"

0 commit comments

Comments
 (0)