Skip to content

Commit

Permalink
nonetype handling for repo collaborators
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Brauer <[email protected]>
  • Loading branch information
danbrauer committed Dec 12, 2024
1 parent 5d5f856 commit 428c396
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions cartography/intel/github/repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

from cartography.intel.github.util import fetch_all
from cartography.intel.github.util import PaginatedGraphqlData
from cartography.util import backoff_handler
from cartography.util import retries_with_backoff
from cartography.util import run_cleanup_job
from cartography.util import timeit

Expand Down Expand Up @@ -162,14 +164,27 @@ def _get_repo_collaborators_for_multiple_repos(
result[repo_url] = []
continue

collab_users = []
collab_permission = []
collaborators = _get_repo_collaborators(token, api_url, org, repo_name, affiliation)
# nodes and edges are expected to always be present given that we only call for them if totalCount is > 0
for collab in collaborators.nodes:
collab_users.append(collab)
for perm in collaborators.edges:
collab_permission.append(perm['permission'])
collab_users: List[dict[str, Any]] = []
collab_permission: List[str] = []

def get_repo_collaborators_inner_func(
org: str, api_url: str, token: str, repo_name: str, affiliation: str,
collab_users: List[dict[str, Any]], collab_permission: List[str],
) -> None:
logger.info(f"Loading collaborators for repo {repo_name}.")
collaborators = _get_repo_collaborators(token, api_url, org, repo_name, affiliation)
# nodes and edges are expected to always be present given that we only call for them if totalCount is > 0
# however sometimes GitHub returns None, as in issue 1334
for collab in collaborators.nodes or []:
collab_users.append(collab)
# The `or []` is because `.edges` can be None.
for perm in collaborators.edges:
collab_permission.append(perm['permission'])

retries_with_backoff(get_repo_collaborators_inner_func, TypeError, 5, backoff_handler)(
org=org, api_url=api_url, token=token, repo_name=repo_name, affiliation=affiliation,
collab_users=collab_users, collab_permission=collab_permission,
)

result[repo_url] = [
UserAffiliationAndRepoPermission(user, permission, affiliation)
Expand Down

0 comments on commit 428c396

Please sign in to comment.