Skip to content

Commit

Permalink
feat: add personal access token API
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wittig committed Feb 17, 2021
1 parent bec2094 commit 2bb16fa
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 0 deletions.
28 changes: 28 additions & 0 deletions docs/gl_objects/personal_access_tokens.rst
@@ -0,0 +1,28 @@
######################
Personal Access Tokens
######################

Get a list of personal access tokens

References
----------

* v4 API:

+ :class:`gitlab.v4.objects.PersonalAccessToken`
+ :class:`gitlab.v4.objects.PersonalAcessTokenManager`
+ :attr:`gitlab.Gitlab.personal_access_tokens`

* GitLab API: https://docs.gitlab.com/ee/api/personal_access_tokens.html

Examples
--------

List personal access tokens::

access_tokens = gl.personal_access_tokens.list()
print(access_tokens[0].name)

List personal access tokens from other user_id (admin only)::

access_tokens = gl.personal_access_tokens.list(user_id=25)
1 change: 1 addition & 0 deletions gitlab/__init__.py
Expand Up @@ -143,6 +143,7 @@ def __init__(
self.user_activities = objects.UserActivitiesManager(self)
self.applications = objects.ApplicationManager(self)
self.variables = objects.VariableManager(self)
self.personal_access_tokens = objects.PersonalAccessTokenManager(self)

def __enter__(self):
return self
Expand Down
46 changes: 46 additions & 0 deletions gitlab/tests/objects/test_personal_access_tokens.py
@@ -0,0 +1,46 @@
"""
GitLab API: https://docs.gitlab.com/ee/api/personal_access_tokens.html
"""

import pytest
import responses


@pytest.fixture
def resp_list_personal_access_token():
content = [
{
"id": 4,
"name": "Test Token",
"revoked": False,
"created_at": "2020-07-23T14:31:47.729Z",
"scopes": ["api"],
"active": True,
"user_id": 24,
"expires_at": None,
}
]

with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
method=responses.GET,
url="http://localhost/api/v4/personal_access_tokens",
json=content,
content_type="application/json",
status=200,
)
yield rsps


def test_list_personal_access_tokens(gl, resp_list_personal_access_token):
access_tokens = gl.personal_access_tokens.list()
assert len(access_tokens) == 1
assert access_tokens[0].revoked is False
assert access_tokens[0].name == "Test Token"


def test_list_personal_access_tokens_filter(gl, resp_list_personal_access_token):
access_tokens = gl.personal_access_tokens.list(user_id=24)
assert len(access_tokens) == 1
assert access_tokens[0].revoked is False
assert access_tokens[0].user_id == 24
1 change: 1 addition & 0 deletions gitlab/v4/objects/__init__.py
Expand Up @@ -69,6 +69,7 @@
from .users import *
from .variables import *
from .wikis import *
from .personal_access_tokens import *


# TODO: deprecate these in favor of gitlab.const.*
Expand Down
18 changes: 18 additions & 0 deletions gitlab/v4/objects/personal_access_tokens.py
@@ -0,0 +1,18 @@
from gitlab.base import * # noqa
from gitlab.mixins import * # noqa


__all__ = [
"PersonalAccessToken",
"PersonalAccessTokenManager",
]


class PersonalAccessToken(RESTObject):
pass


class PersonalAccessTokenManager(ListMixin, RESTManager):
_path = "/personal_access_tokens"
_obj_cls = PersonalAccessToken
_list_filters = ("user_id",)

0 comments on commit 2bb16fa

Please sign in to comment.