Skip to content

Commit a7bfdf2

Browse files
YugoHinoEnricoMi
andauthored
Adding feature for enterprise consumed license (#2626)
Co-authored-by: Enrico Minack <github@enrico.minack.dev>
1 parent eadc241 commit a7bfdf2

13 files changed

+560
-3
lines changed

doc/examples/MainClass.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ Get organization by name
3939
>>> org.login
4040
u'PyGithub'
4141
42+
Get enterprise consumed licenses by name
43+
------------------------
44+
45+
.. code-block:: python
46+
47+
>>> enterprise = g.get_enterprise_consumed_licenses("PyGithub")
48+
>>> enterprise_consumed_licenses = enterprise.get_enterprise_consumed_licenses()
49+
>>> enterprise_consumed_licenses.total_seats_consumed
50+
5000
51+
4252
Search repositories by language
4353
-------------------------------
4454

github/Enterprise.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
############################ Copyrights and license ############################
2+
# #
3+
# Copyright 2023 Yugo Hino <henom06@gmail.com> #
4+
# #
5+
# This file is part of PyGithub. #
6+
# http://pygithub.readthedocs.io/ #
7+
# #
8+
# PyGithub is free software: you can redistribute it and/or modify it under #
9+
# the terms of the GNU Lesser General Public License as published by the Free #
10+
# Software Foundation, either version 3 of the License, or (at your option) #
11+
# any later version. #
12+
# #
13+
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
14+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15+
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
16+
# details. #
17+
# #
18+
# You should have received a copy of the GNU Lesser General Public License #
19+
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
20+
# #
21+
################################################################################
22+
from typing import Any, Dict
23+
24+
from github.EnterpriseConsumedLicenses import EnterpriseConsumedLicenses
25+
from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet
26+
from github.Requester import Requester
27+
28+
29+
class Enterprise(NonCompletableGithubObject):
30+
"""
31+
This class represents Enterprises. Such objects do not exist in the Github API, so this class merely collects all endpoints the start with /enterprises/{enterprise}/. See methods below for specific endpoints and docs.
32+
https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin?apiVersion=2022-11-28
33+
"""
34+
35+
def __init__(
36+
self,
37+
requester: Requester,
38+
enterprise: str,
39+
):
40+
super().__init__(requester, {}, {"enterprise": enterprise, "url": f"/enterprises/{enterprise}"}, True)
41+
42+
def _initAttributes(self) -> None:
43+
self._enterprise: Attribute[str] = NotSet
44+
self._url: Attribute[str] = NotSet
45+
46+
def __repr__(self) -> str:
47+
return self.get__repr__({"enterprise": self._enterprise.value})
48+
49+
@property
50+
def enterprise(self) -> str:
51+
return self._enterprise.value
52+
53+
@property
54+
def url(self) -> str:
55+
return self._url.value
56+
57+
def get_consumed_licenses(self) -> EnterpriseConsumedLicenses:
58+
"""
59+
:calls: `GET /enterprises/{enterprise}/consumed-licenses <https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/license#list-enterprise-consumed-licenses>`_
60+
"""
61+
headers, data = self._requester.requestJsonAndCheck("GET", self.url + "/consumed-licenses")
62+
if "url" not in data:
63+
data["url"] = self.url + "/consumed-licenses"
64+
65+
return EnterpriseConsumedLicenses(self._requester, headers, data, completed=True)
66+
67+
def _useAttributes(self, attributes: Dict[str, Any]) -> None:
68+
if "enterprise" in attributes: # pragma no branch
69+
self._enterprise = self._makeStringAttribute(attributes["enterprise"])
70+
if "url" in attributes: # pragma no branch
71+
self._url = self._makeStringAttribute(attributes["url"])
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
############################ Copyrights and license ############################
2+
# #
3+
# Copyright 2023 Yugo Hino <henom06@gmail.com> #
4+
# #
5+
# This file is part of PyGithub. #
6+
# http://pygithub.readthedocs.io/ #
7+
# #
8+
# PyGithub is free software: you can redistribute it and/or modify it under #
9+
# the terms of the GNU Lesser General Public License as published by the Free #
10+
# Software Foundation, either version 3 of the License, or (at your option) #
11+
# any later version. #
12+
# #
13+
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
14+
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15+
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
16+
# details. #
17+
# #
18+
# You should have received a copy of the GNU Lesser General Public License #
19+
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
20+
# #
21+
################################################################################
22+
from typing import Any, Dict
23+
24+
from github.GithubObject import Attribute, CompletableGithubObject, NotSet
25+
from github.NamedEnterpriseUser import NamedEnterpriseUser
26+
from github.PaginatedList import PaginatedList
27+
28+
29+
class EnterpriseConsumedLicenses(CompletableGithubObject):
30+
"""
31+
This class represents license consumed by enterprises. The reference can be found here https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/license#list-enterprise-consumed-licenses
32+
"""
33+
34+
def _initAttributes(self) -> None:
35+
self._total_seats_consumed: Attribute[int] = NotSet
36+
self._total_seats_purchased: Attribute[int] = NotSet
37+
self._enterprise: Attribute[str] = NotSet
38+
self._url: Attribute[str] = NotSet
39+
40+
def __repr__(self) -> str:
41+
return self.get__repr__({"enterprise": self._enterprise.value})
42+
43+
@property
44+
def total_seats_consumed(self) -> int:
45+
return self._total_seats_consumed.value
46+
47+
@property
48+
def total_seats_purchased(self) -> int:
49+
return self._total_seats_purchased.value
50+
51+
@property
52+
def enterprise(self) -> str:
53+
self._completeIfNotSet(self._enterprise)
54+
return self._enterprise.value
55+
56+
@property
57+
def url(self) -> str:
58+
self._completeIfNotSet(self._url)
59+
return self._url.value
60+
61+
def get_users(self) -> PaginatedList[NamedEnterpriseUser]:
62+
"""
63+
:calls: `GET /enterprises/{enterprise}/consumed-licenses <https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/license#list-enterprise-consumed-licenses>`_
64+
"""
65+
66+
url_parameters: Dict[str, Any] = {}
67+
return PaginatedList(
68+
NamedEnterpriseUser,
69+
self._requester,
70+
self.url,
71+
url_parameters,
72+
None,
73+
"users",
74+
self.raw_data,
75+
self.raw_headers,
76+
)
77+
78+
def _useAttributes(self, attributes: Dict[str, Any]) -> None:
79+
if "total_seats_consumed" in attributes: # pragma no branch
80+
self._total_seats_consumed = self._makeIntAttribute(attributes["total_seats_consumed"])
81+
if "total_seats_purchased" in attributes: # pragma no branch
82+
self._total_seats_purchased = self._makeIntAttribute(attributes["total_seats_purchased"])
83+
if "enterprise" in attributes: # pragma no branch
84+
self._enterprise = self._makeStringAttribute(attributes["enterprise"])
85+
if "url" in attributes: # pragma no branch
86+
self._url = self._makeStringAttribute(attributes["url"])

github/MainClass.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
# Copyright 2018 itsbruce <it.is.bruce@gmail.com> #
2929
# Copyright 2019 Tomas Tomecek <tomas@tomecek.net> #
3030
# Copyright 2019 Rigas Papathanasopoulos <rigaspapas@gmail.com> #
31+
# Copyright 2023 Yugo Hino <henom06@gmail.com> #
3132
# #
3233
# This file is part of PyGithub. #
3334
# http://pygithub.readthedocs.io/ #
@@ -55,6 +56,7 @@
5556
import urllib3
5657

5758
import github.ApplicationOAuth
59+
import github.Enterprise
5860
import github.Event
5961
import github.Gist
6062
import github.GithubObject
@@ -341,6 +343,16 @@ def get_organizations(self, since=github.GithubObject.NotSet):
341343
url_parameters,
342344
)
343345

346+
def get_enterprise(self, enterprise):
347+
"""
348+
:calls: `GET /enterprises/{enterprise} <https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin>`_
349+
:param enterprise: string
350+
:rtype: :class:`Enterprise`
351+
"""
352+
assert isinstance(enterprise, str), enterprise
353+
# There is no native "/enterprises/{enterprise}" api, so this function is a hub for apis that start with "/enterprise/{enterprise}".
354+
return github.Enterprise.Enterprise(self.__requester, enterprise)
355+
344356
def get_repo(self, full_name_or_id, lazy=False):
345357
"""
346358
:calls: `GET /repos/{owner}/{repo} <https://docs.github.com/en/rest/reference/repos>`_ or `GET /repositories/{id} <https://docs.github.com/en/rest/reference/repos>`_

github/MainClass.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ from github.Auth import Auth
1010
from github.AuthenticatedUser import AuthenticatedUser
1111
from github.Commit import Commit
1212
from github.ContentFile import ContentFile
13+
from github.Enterprise import Enterprise
1314
from github.Event import Event
1415
from github.Gist import Gist
1516
from github.GithubApp import GithubApp
@@ -79,6 +80,7 @@ class Github:
7980
def get_licenses(self) -> PaginatedList[License]: ...
8081
def get_organization(self, login: str) -> Organization: ...
8182
def get_organizations(self, since: Union[int, _NotSetType] = ...) -> PaginatedList[Organization]: ...
83+
def get_enterprise(self, login: str) -> Enterprise: ...
8284
def get_project(self, id: int) -> Project: ...
8385
def get_project_column(self, id: int) -> ProjectColumn: ...
8486
def get_rate_limit(self) -> RateLimit: ...

0 commit comments

Comments
 (0)