Skip to content

Commit 5f78f21

Browse files
authored
feat: add github-repo to collaborator transformer (#8)
* feat: add github-repo to collaborator transformer * chore: bump version
1 parent 9080f0d commit 5f78f21

File tree

11 files changed

+340
-170
lines changed

11 files changed

+340
-170
lines changed

nodestream_github/client/githubclient.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import nodestream_github.types as types
2525
from nodestream_github.logging import get_plugin_logger
26+
from nodestream_github.types.enums import CollaboratorAffiliation
2627

2728
DEFAULT_REQUEST_RATE_LIMIT_PER_MINUTE = int(13000 / 60)
2829
DEFAULT_MAX_RETRIES = 20
@@ -81,6 +82,7 @@ def __init__(
8182
max_retries: int | None = None,
8283
rate_limit_per_minute: int | None = None,
8384
max_retry_wait_seconds: int | None = None,
85+
**_kwargs: any,
8486
):
8587
if per_page is None:
8688
per_page = DEFAULT_PAGE_SIZE
@@ -97,8 +99,10 @@ def __init__(
9799
self._auth_token = auth_token
98100
if github_hostname == "api.github.com" or github_hostname is None:
99101
self._base_url = "https://api.github.com"
102+
self._is_default_hostname = True
100103
else:
101104
self._base_url = f"https://{github_hostname}/api/v3"
105+
self._is_default_hostname = False
102106

103107
self._per_page = per_page
104108
self._limit_storage = MemoryStorage()
@@ -181,6 +185,10 @@ def base_url(self) -> str:
181185
def per_page(self) -> int:
182186
return self._per_page
183187

188+
@property
189+
def is_default_hostname(self) -> bool:
190+
return self._is_default_hostname
191+
184192
async def _get(
185193
self,
186194
url: str,
@@ -390,7 +398,11 @@ async def fetch_webhooks_for_repo(
390398
_fetch_problem(f"webhooks for repo {owner_login}/{repo_name}", e)
391399

392400
async def fetch_collaborators_for_repo(
393-
self, owner_login: str, repo_name: str, affiliation: str
401+
self,
402+
*,
403+
owner_login: str,
404+
repo_name: str,
405+
affiliation: CollaboratorAffiliation,
394406
) -> AsyncGenerator[types.GithubUser]:
395407
"""Try to get collaborator data for this repo.
396408

nodestream_github/repos.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .interpretations.relationship.user import simplify_user
1515
from .logging import get_plugin_logger
1616
from .types import GithubRepo, RepositoryRecord
17+
from .types.enums import CollaboratorAffiliation
1718

1819
logger = get_plugin_logger(__name__)
1920

@@ -106,15 +107,15 @@ async def _extract_repo(self, repo: GithubRepo) -> RepositoryRecord:
106107
repo["collaborators"] = []
107108

108109
async for user in self.client.fetch_collaborators_for_repo(
109-
owner["login"],
110-
repo["name"],
111-
"direct",
110+
owner_login=owner["login"],
111+
repo_name=repo["name"],
112+
affiliation=CollaboratorAffiliation.DIRECT,
112113
):
113114
repo["collaborators"].append(simplify_user(user, affiliation="direct"))
114115
async for user in self.client.fetch_collaborators_for_repo(
115-
owner["login"],
116-
repo["name"],
117-
"outside",
116+
owner_login=owner["login"],
117+
repo_name=repo["name"],
118+
affiliation=CollaboratorAffiliation.OUTSIDE,
118119
):
119120
repo["collaborators"].append(simplify_user(user, affiliation="outside"))
120121

nodestream_github/transformer/__init__.py

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import logging
2+
from collections.abc import AsyncGenerator
3+
4+
from nodestream.pipeline import Transformer
5+
6+
from nodestream_github import types
7+
from nodestream_github.client import GithubRestApiClient
8+
from nodestream_github.interpretations.relationship.repository import simplify_repo
9+
from nodestream_github.logging import get_plugin_logger
10+
from nodestream_github.types.enums import CollaboratorAffiliation
11+
12+
logger = get_plugin_logger(__name__)
13+
14+
15+
class RepoToCollaboratorsTransformer(Transformer):
16+
def __init__(
17+
self,
18+
*,
19+
full_name_key: str = "full_name",
20+
**kwargs: any,
21+
):
22+
23+
self.client = GithubRestApiClient(**kwargs)
24+
self.full_name_key = full_name_key
25+
26+
async def transform_record(
27+
self,
28+
record: types.GithubRepo,
29+
) -> AsyncGenerator[types.GithubUser]:
30+
(repo_owner, repo_name) = record[self.full_name_key].split("/")
31+
logging.debug("Transforming record %s/%s", repo_owner, repo_name)
32+
33+
simplified_repo = simplify_repo(record)
34+
35+
async for collaborator in self.client.fetch_collaborators_for_repo(
36+
owner_login=repo_owner,
37+
repo_name=repo_name,
38+
affiliation=CollaboratorAffiliation.DIRECT,
39+
):
40+
yield collaborator | {
41+
"repository": simplified_repo,
42+
"affiliation": CollaboratorAffiliation.DIRECT,
43+
}
44+
45+
async for collaborator in self.client.fetch_collaborators_for_repo(
46+
owner_login=repo_owner,
47+
repo_name=repo_name,
48+
affiliation=CollaboratorAffiliation.OUTSIDE,
49+
):
50+
yield collaborator | {
51+
"repository": simplified_repo,
52+
"affiliation": CollaboratorAffiliation.OUTSIDE,
53+
}

nodestream_github/types/enums.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from enum import StrEnum
2+
3+
4+
class CollaboratorAffiliation(StrEnum):
5+
ALL = "all"
6+
OUTSIDE = "outside"
7+
DIRECT = "direct"

0 commit comments

Comments
 (0)