Skip to content

Commit 1f5c5bf

Browse files
author
Clayton Walker
committed
Move code into audit_events, add tests
1 parent b7e5dfb commit 1f5c5bf

File tree

3 files changed

+78
-13
lines changed

3 files changed

+78
-13
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/audit_events.html#project-audit-events
4+
"""
5+
6+
import re
7+
8+
import pytest
9+
import responses
10+
11+
from gitlab.v4.objects.audit_events import ProjectAudit
12+
13+
audit_events_url = re.compile(
14+
r"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/audit_events"
15+
)
16+
17+
audit_events_content = {
18+
"id": 5,
19+
"author_id": 1,
20+
"entity_id": 7,
21+
"entity_type": "Project",
22+
"details": {
23+
"change": "prevent merge request approval from reviewers",
24+
"from": "",
25+
"to": "true",
26+
"author_name": "Administrator",
27+
"target_id": 7,
28+
"target_type": "Project",
29+
"target_details": "twitter/typeahead-js",
30+
"ip_address": "127.0.0.1",
31+
"entity_path": "twitter/typeahead-js"
32+
},
33+
"created_at": "2020-05-26T22:55:04.230Z"
34+
}
35+
36+
37+
@pytest.fixture
38+
def resp_list_audit_events():
39+
with responses.RequestsMock() as rsps:
40+
rsps.add(
41+
method=responses.GET,
42+
url=audit_events_url,
43+
json=[audit_events_content],
44+
content_type="application/json",
45+
status=200,
46+
)
47+
yield rsps
48+
49+
50+
def test_list_project_audit_events(project, resp_list_audit_events):
51+
audit_events = project.audit_events.list()
52+
assert isinstance(audit_events, list)
53+
assert isinstance(audit_events[0], ProjectAudit)
54+
assert audit_events[0].id == 5

gitlab/v4/objects/audit_events.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/audit_events.html#project-audit-events
4+
"""
5+
6+
from gitlab.base import * # noqa
7+
from gitlab.mixins import * # noqa
8+
9+
__all__ = [
10+
"ProjectAudit",
11+
"ProjectAuditManager",
12+
]
13+
14+
15+
class ProjectAudit(RESTObject):
16+
_id_attr = "id"
17+
18+
19+
class ProjectAuditManager(RetrieveMixin, RESTManager):
20+
_path = "/projects/%(project_id)s/audit_events"
21+
_obj_cls = ProjectAudit
22+
_from_parent_attrs = {"project_id": "id"}
23+
_list_filters = ("created_after", "created_before")

gitlab/v4/objects/projects.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from .deployments import ProjectDeploymentManager
1717
from .environments import ProjectEnvironmentManager
1818
from .events import ProjectEventManager
19+
from .audit_events import ProjectAuditManager
1920
from .export_import import ProjectExportManager, ProjectImportManager
2021
from .files import ProjectFileManager
2122
from .hooks import ProjectHookManager
@@ -51,8 +52,6 @@
5152
"GroupProjectManager",
5253
"Project",
5354
"ProjectManager",
54-
"ProjectAudit",
55-
"ProjectAuditManager",
5655
"ProjectFork",
5756
"ProjectForkManager",
5857
"ProjectRemoteMirror",
@@ -87,17 +86,6 @@ class GroupProjectManager(ListMixin, RESTManager):
8786
)
8887

8988

90-
class ProjectAudit(RESTObject):
91-
_id_attr = "id"
92-
93-
94-
class ProjectAuditManager(RetrieveMixin, RESTManager):
95-
_path = "/projects/%(project_id)s/audit_events"
96-
_obj_cls = ProjectAudit
97-
_from_parent_attrs = {"project_id": "id"}
98-
_list_filters = ("created_after", "created_before")
99-
100-
10189
class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
10290
_short_print_attr = "path"
10391
_managers = (

0 commit comments

Comments
 (0)