github-actions-admin 0.0.1b4303__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ from http import HTTPStatus
2
+
3
+ import requests
4
+ from python_sdk_remote.utilities import our_get_env
5
+
6
+ DEFAULT_GITHUB_API_URL = "https://api.github.com"
7
+ DEFAULT_ORGANIZATION = "circles-zone"
8
+ GITHUB_TOKEN_ADMIN_ENVIRONMENT_VARIABLE = "GITHUB_TOKEN_ADMIN"
9
+ REQUEST_TIMEOUT_SECONDS = 30
10
+
11
+
12
+ class GitHubActionsAdmin:
13
+ def __init__(self, organization: str = DEFAULT_ORGANIZATION, token: str | None = None,
14
+ api_url: str | None = None, session: requests.Session | None = None):
15
+ self.organization = organization
16
+ self.api_url = (api_url or our_get_env("GITHUB_API_URL", DEFAULT_GITHUB_API_URL)).rstrip("/")
17
+ self.token = token or our_get_env(GITHUB_TOKEN_ADMIN_ENVIRONMENT_VARIABLE, raise_if_empty=True)
18
+ self.session = session or requests.Session()
19
+
20
+ def get_github_actions_self_hosted_token(self) -> str:
21
+ """Registration token for a new self-hosted runner of the organization (valid for one hour)"""
22
+ response = self.session.post(
23
+ f"{self.api_url}/orgs/{self.organization}/actions/runners/registration-token",
24
+ headers={"Authorization": f"Bearer {self.token}", "Accept": "application/vnd.github+json"},
25
+ timeout=REQUEST_TIMEOUT_SECONDS)
26
+ response.raise_for_status()
27
+ return response.json()["token"]
28
+
29
+ def get_github_actions_self_hosted_token_for_developer(self, developer_github_token: str) -> str:
30
+ """Registration token for a developer who proves, with their own GitHub token, that they are a member of the
31
+ organization - so a developer's runner installer gets a token without asking the Team Lead"""
32
+ user_response = self.session.get(
33
+ f"{self.api_url}/user",
34
+ headers={"Authorization": f"Bearer {developer_github_token}", "Accept": "application/vnd.github+json"},
35
+ timeout=REQUEST_TIMEOUT_SECONDS)
36
+ user_response.raise_for_status()
37
+ login = user_response.json()["login"]
38
+ # NO_CONTENT = member; NOT_FOUND (or FOUND for a non-member token) = not a member
39
+ membership_response = self.session.get(
40
+ f"{self.api_url}/orgs/{self.organization}/members/{login}",
41
+ headers={"Authorization": f"Bearer {self.token}", "Accept": "application/vnd.github+json"},
42
+ timeout=REQUEST_TIMEOUT_SECONDS, allow_redirects=False)
43
+ if membership_response.status_code != HTTPStatus.NO_CONTENT:
44
+ raise PermissionError(f"GitHub user {login} is not a member of {self.organization}")
45
+ return self.get_github_actions_self_hosted_token()
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: github-actions-admin
3
+ Version: 0.0.1b4303
4
+ Requires-Python: >=3.10
5
+ Requires-Dist: requests
6
+ Requires-Dist: python-sdk-remote>=0.0.185
7
+ Provides-Extra: test
8
+ Requires-Dist: pytest; extra == "test"
9
+ Requires-Dist: pytest-cov; extra == "test"
10
+ Dynamic: requires-python
@@ -0,0 +1,5 @@
1
+ github_actions_admin/GitHubActionsAdmin.py,sha256=qGzrGpmm6EeHkH-2hShCxPfFocCuuFBwSuJnH3g7Z6I,2556
2
+ github_actions_admin-0.0.1b4303.dist-info/METADATA,sha256=KWlulMYH4GNjsm8fJ436q7vD7mQlLIWftQOZtXqhVdE,287
3
+ github_actions_admin-0.0.1b4303.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
4
+ github_actions_admin-0.0.1b4303.dist-info/top_level.txt,sha256=zsoi9QFdU61nw7pc8hiyioXWsLVmSZAoBzminueknqY,21
5
+ github_actions_admin-0.0.1b4303.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ github_actions_admin