- Notifications
You must be signed in to change notification settings - Fork673
Service account improvements#3109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Changes fromall commits
ec818f3
fc185a7
8d817a2
4405890
454b3ea
cd9cfa8
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,14 +1,55 @@ | ||||||||||||||||||||||
from gitlab.base import RESTObject | ||||||||||||||||||||||
from gitlab.mixins import ( | ||||||||||||||||||||||
CreateMixin, | ||||||||||||||||||||||
DeleteMixin, | ||||||||||||||||||||||
ListMixin, | ||||||||||||||||||||||
ObjectDeleteMixin, | ||||||||||||||||||||||
ObjectRotateMixin, | ||||||||||||||||||||||
RotateMixin, | ||||||||||||||||||||||
) | ||||||||||||||||||||||
from gitlab.types import ArrayAttribute, RequiredOptional | ||||||||||||||||||||||
__all__ = [ | ||||||||||||||||||||||
"ServiceAccount", | ||||||||||||||||||||||
"ServiceAccountManager", | ||||||||||||||||||||||
"GroupServiceAccount", | ||||||||||||||||||||||
"GroupServiceAccountManager", | ||||||||||||||||||||||
"GroupServiceAccountAccessToken", | ||||||||||||||||||||||
"GroupServiceAccountAccessTokenManager", | ||||||||||||||||||||||
] | ||||||||||||||||||||||
class GroupServiceAccountAccessToken(ObjectRotateMixin, RESTObject): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
class GroupServiceAccountAccessTokenManager( | ||||||||||||||||||||||
CreateMixin[GroupServiceAccountAccessToken], | ||||||||||||||||||||||
RotateMixin[GroupServiceAccountAccessToken], | ||||||||||||||||||||||
): | ||||||||||||||||||||||
_path = "/groups/{group_id}/service_accounts/{user_id}/personal_access_tokens" | ||||||||||||||||||||||
_obj_cls = GroupServiceAccountAccessToken | ||||||||||||||||||||||
_from_parent_attrs = {"group_id": "group_id", "user_id": "id"} | ||||||||||||||||||||||
_create_attrs = RequiredOptional( | ||||||||||||||||||||||
required=("name", "scopes"), optional=("expires_at",) | ||||||||||||||||||||||
) | ||||||||||||||||||||||
_types = {"scopes": ArrayAttribute} | ||||||||||||||||||||||
class ServiceAccount(RESTObject): | ||||||||||||||||||||||
pass | ||||||||||||||||||||||
class ServiceAccountManager(CreateMixin[ServiceAccount], ListMixin[ServiceAccount]): | ||||||||||||||||||||||
Comment on lines +39 to +43 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. ServiceAccount currently has no delete support; add ObjectDeleteMixin to the class and DeleteMixin to ServiceAccountManager so that instance-level service accounts can be deleted. Suggested change
Copilot uses AI. Check for mistakes. | ||||||||||||||||||||||
_path = "/service_accounts" | ||||||||||||||||||||||
_obj_cls = ServiceAccount | ||||||||||||||||||||||
_create_attrs = RequiredOptional(optional=("name", "username", "email")) | ||||||||||||||||||||||
class GroupServiceAccount(ObjectDeleteMixin, RESTObject): | ||||||||||||||||||||||
access_tokens: GroupServiceAccountAccessTokenManager | ||||||||||||||||||||||
class GroupServiceAccountManager( | ||||||||||||||||||||||
CreateMixin[GroupServiceAccount], | ||||||||||||||||||||||
DeleteMixin[GroupServiceAccount], | ||||||||||||||||||||||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
GitLab API: https://docs.gitlab.com/ee/api/user_service_accounts.html | ||
""" | ||
import pytest | ||
import responses | ||
create_service_account_defaults_content = { | ||
"id": 57, | ||
"username": "service_account_6018816a18e515214e0c34c2b33523fc", | ||
"name": "Service account user", | ||
"email": "service_account_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.example.com", | ||
} | ||
create_service_account_content = { | ||
"id": 42, | ||
"username": "my_service_account", | ||
"name": "My Service account user", | ||
"email": "servicebot@example.com", | ||
} | ||
@pytest.fixture | ||
def resp_create_service_account_defaults(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/service_accounts", | ||
json=create_service_account_defaults_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
@pytest.fixture | ||
def resp_create_service_account(): | ||
with responses.RequestsMock() as rsps: | ||
rsps.add( | ||
method=responses.POST, | ||
url="http://localhost/api/v4/service_accounts", | ||
json=create_service_account_content, | ||
content_type="application/json", | ||
status=200, | ||
) | ||
yield rsps | ||
def test_create_service_account_defaults(gl, resp_create_service_account_defaults): | ||
service_account = gl.service_accounts.create() | ||
assert service_account.id == 57 | ||
assert ( | ||
service_account.username == "service_account_6018816a18e515214e0c34c2b33523fc" | ||
) | ||
assert service_account.name == "Service account user" | ||
assert ( | ||
service_account.email | ||
== "service_account_6018816a18e515214e0c34c2b33523fc@noreply.gitlab.example.com" | ||
) | ||
def test_create_service_account(gl, resp_create_service_account): | ||
service_account = gl.service_accounts.create( | ||
{ | ||
"name": "My Service account user", | ||
"username": "my_service_account", | ||
"email": "servicebot@example.com", | ||
} | ||
) | ||
assert service_account.id == 42 | ||
assert service_account.username == "my_service_account" | ||
assert service_account.name == "My Service account user" | ||
assert service_account.email == "servicebot@example.com" |