Skip to content

Commit 62b0b62

Browse files
authored
Merge pull request #968 from mitar/stats
Stats
2 parents 36bbd37 + 8760efc commit 62b0b62

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

docs/gl_objects/projects.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -754,3 +754,54 @@ Protect a single repository tag or several project repository tags using a wildc
754754
Unprotect the given protected tag or wildcard protected tag.::
755755

756756
protected_tag.delete()
757+
758+
Additional project statistics
759+
=============================
760+
761+
Reference
762+
---------
763+
764+
* v4 API:
765+
766+
+ :class:`gitlab.v4.objects.ProjectAdditionalStatistics`
767+
+ :class:`gitlab.v4.objects.ProjectAdditionalStatisticsManager`
768+
+ :attr:`gitlab.v4.objects.Project.additionalstatistics`
769+
770+
* GitLab API: https://docs.gitlab.com/ce/api/project_statistics.html
771+
772+
Examples
773+
---------
774+
775+
Get all additional statistics of a project::
776+
777+
statistics = project.additionalstatistics.get()
778+
779+
Get total fetches in last 30 days of a project::
780+
781+
total_fetches = project.additionalstatistics.get()['fetches']['total']
782+
783+
Project issues statistics
784+
=========================
785+
786+
Reference
787+
---------
788+
789+
* v4 API:
790+
791+
+ :class:`gitlab.v4.objects.ProjectIssuesStatistics`
792+
+ :class:`gitlab.v4.objects.ProjectIssuesStatisticsManager`
793+
+ :attr:`gitlab.v4.objects.Project.issuesstatistics`
794+
795+
* GitLab API: https://docs.gitlab.com/ce/api/issues_statistics.html#get-project-issues-statistics
796+
797+
Examples
798+
---------
799+
800+
Get statistics of all issues in a project::
801+
802+
statistics = project.issuesstatistics.get()
803+
804+
Get statistics of issues in a project with ``foobar`` in ``title`` and
805+
``description``::
806+
807+
statistics = project.issuesstatistics.get(search='foobar')

gitlab/tests/test_gitlab.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,62 @@ def resp_get_environment(url, request):
553553
self.assertEqual(environment.last_deployment, "sometime")
554554
self.assertEqual(environment.name, "environment_name")
555555

556+
def test_project_additional_statistics(self):
557+
@urlmatch(
558+
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"
559+
)
560+
def resp_get_project(url, request):
561+
headers = {"content-type": "application/json"}
562+
content = '{"name": "name", "id": 1}'.encode("utf-8")
563+
return response(200, content, headers, None, 5, request)
564+
565+
@urlmatch(
566+
scheme="http",
567+
netloc="localhost",
568+
path="/api/v4/projects/1/statistics",
569+
method="get",
570+
)
571+
def resp_get_environment(url, request):
572+
headers = {"content-type": "application/json"}
573+
content = """{"fetches": {"total": 50, "days": [{"count": 10, "date": "2018-01-10"}]}}""".encode(
574+
"utf-8"
575+
)
576+
return response(200, content, headers, None, 5, request)
577+
578+
with HTTMock(resp_get_project, resp_get_environment):
579+
project = self.gl.projects.get(1)
580+
statistics = project.additionalstatistics.get()
581+
self.assertIsInstance(statistics, ProjectAdditionalStatistics)
582+
self.assertEqual(statistics.fetches["total"], 50)
583+
584+
def test_project_issues_statistics(self):
585+
@urlmatch(
586+
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"
587+
)
588+
def resp_get_project(url, request):
589+
headers = {"content-type": "application/json"}
590+
content = '{"name": "name", "id": 1}'.encode("utf-8")
591+
return response(200, content, headers, None, 5, request)
592+
593+
@urlmatch(
594+
scheme="http",
595+
netloc="localhost",
596+
path="/api/v4/projects/1/issues_statistics",
597+
method="get",
598+
)
599+
def resp_get_environment(url, request):
600+
headers = {"content-type": "application/json"}
601+
content = """{"statistics": {"counts": {"all": 20, "closed": 5, "opened": 15}}}""".encode(
602+
"utf-8"
603+
)
604+
return response(200, content, headers, None, 5, request)
605+
606+
with HTTMock(resp_get_project, resp_get_environment):
607+
project = self.gl.projects.get(1)
608+
statistics = project.issuesstatistics.get()
609+
self.assertIsInstance(statistics, ProjectIssuesStatistics)
610+
self.assertEqual(statistics.statistics["counts"]["all"], 20)
611+
556612
def test_groups(self):
557613
@urlmatch(
558614
scheme="http", netloc="localhost", path="/api/v4/groups/1", method="get"

gitlab/v4/objects.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3997,6 +3997,26 @@ class ProjectImportManager(GetWithoutIdMixin, RESTManager):
39973997
_from_parent_attrs = {"project_id": "id"}
39983998

39993999

4000+
class ProjectAdditionalStatistics(RefreshMixin, RESTObject):
4001+
_id_attr = None
4002+
4003+
4004+
class ProjectAdditionalStatisticsManager(GetWithoutIdMixin, RESTManager):
4005+
_path = "/projects/%(project_id)s/statistics"
4006+
_obj_cls = ProjectAdditionalStatistics
4007+
_from_parent_attrs = {"project_id": "id"}
4008+
4009+
4010+
class ProjectIssuesStatistics(RefreshMixin, RESTObject):
4011+
_id_attr = None
4012+
4013+
4014+
class ProjectIssuesStatisticsManager(GetWithoutIdMixin, RESTManager):
4015+
_path = "/projects/%(project_id)s/issues_statistics"
4016+
_obj_cls = ProjectIssuesStatistics
4017+
_from_parent_attrs = {"project_id": "id"}
4018+
4019+
40004020
class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
40014021
_short_print_attr = "path"
40024022
_managers = (
@@ -4042,6 +4062,8 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
40424062
("variables", "ProjectVariableManager"),
40434063
("wikis", "ProjectWikiManager"),
40444064
("clusters", "ProjectClusterManager"),
4065+
("additionalstatistics", "ProjectAdditionalStatisticsManager"),
4066+
("issuesstatistics", "ProjectIssuesStatisticsManager"),
40454067
)
40464068

40474069
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))

0 commit comments

Comments
 (0)