Skip to content

Commit ef0c4d1

Browse files
committed
split _fetch_collaborators_of_all_organization_repos() into smaller parts
1 parent 5f543f1 commit ef0c4d1

File tree

1 file changed

+44
-41
lines changed

1 file changed

+44
-41
lines changed

gh_org_mgr/_gh_org.py

Lines changed: 44 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -983,50 +983,20 @@ def _fetch_collaborators_of_all_organization_repos(self):
983983
# Initial query parameters
984984
variables = {"owner": self.org.login, "cursor": None}
985985

986+
# dicts in which we store the collaborators of each repo, and the repos
987+
# for which there are more than 100 collaborators
986988
repo_collaborators: dict[str, dict] = {}
987-
missing_data_for_repos = {}
988-
more_repos = True
989+
missing_data_for_repos: dict[str, str] = {}
989990

991+
more_repos = True
990992
while more_repos:
991993
logging.debug("Requesting collaborators for %s", self.org.login)
992994
result = run_graphql_query(graphql_query, variables, self.gh_token)
993-
try:
994-
for repo in result["data"]["organization"]["repositories"]["edges"]:
995-
try:
996-
repo_name = repo["node"]["name"]
997-
logging.debug(
998-
"Extracting collaborators for %s from GraphQL response", repo_name
999-
)
1000-
except KeyError:
1001-
logging.error(
1002-
"Did not find a repo name in the GraphQL response which "
1003-
"seems to hint to a bug: %s",
1004-
repo,
1005-
)
1006-
sys.exit(1)
1007-
1008-
# fill in collaborators of repo
1009-
collaborators = repo["node"]["collaborators"]["edges"]
1010-
repo_collaborators[repo_name] = collaborators
1011-
1012-
# Find out if there are more than 100 collaborators in the
1013-
# GraphQL response for this repo
1014-
if repo["node"]["collaborators"]["pageInfo"]["hasNextPage"]:
1015-
missing_data_for_repos[repo_name] = repo["node"]["collaborators"][
1016-
"pageInfo"
1017-
]["endCursor"]
1018-
1019-
# Find out if there are more than 100 repos in the GraphQL
1020-
# response. If so, get cursor
1021-
more_repos = result["data"]["organization"]["repositories"]["pageInfo"][
1022-
"hasNextPage"
1023-
]
1024-
variables["cursor"] = result["data"]["organization"]["repositories"]["pageInfo"][
1025-
"endCursor"
1026-
]
1027-
except (TypeError, KeyError):
1028-
logging.debug("Repo %s does not seem to have any collaborators", repo_name)
1029-
continue
995+
self._process_graphql_response(result, repo_collaborators, missing_data_for_repos)
996+
more_repos = result["data"]["organization"]["repositories"]["pageInfo"]["hasNextPage"]
997+
variables["cursor"] = result["data"]["organization"]["repositories"]["pageInfo"][
998+
"endCursor"
999+
]
10301000

10311001
if missing_data_for_repos:
10321002
logging.warning(
@@ -1035,8 +1005,41 @@ def _fetch_collaborators_of_all_organization_repos(self):
10351005
)
10361006
# TODO: Need to make individual graphql queries for these repos
10371007

1038-
# Iterate repos, and fill self.current_repos_collaborators[repo] with
1039-
# collaborators as fetched from GraphQL and put into repo_collaborators
1008+
self._populate_current_repos_collaborators(repo_collaborators)
1009+
1010+
def _process_graphql_response(
1011+
self, result, repo_collaborators: dict[str, dict], missing_data_for_repos: dict[str, str]
1012+
):
1013+
"""Process the GraphQL response and extract collaborators"""
1014+
try:
1015+
for repo_edges in result["data"]["organization"]["repositories"]["edges"]:
1016+
try:
1017+
repo_name: str = repo_edges["node"]["name"]
1018+
logging.debug(
1019+
"Extracting collaborators for %s from GraphQL response", repo_name
1020+
)
1021+
except KeyError:
1022+
logging.error(
1023+
"Did not find a repo name in the GraphQL response which "
1024+
"seems to hint to a bug: %s",
1025+
repo_edges,
1026+
)
1027+
sys.exit(1)
1028+
1029+
# fill in collaborators of repo
1030+
repo_collaborators[repo_name] = repo_edges["node"]["collaborators"]["edges"]
1031+
1032+
# Find out if there are more than 100 collaborators in the
1033+
# GraphQL response for this repo
1034+
if repo_edges["node"]["collaborators"]["pageInfo"]["hasNextPage"]:
1035+
missing_data_for_repos[repo_name] = repo_edges["node"]["collaborators"][
1036+
"pageInfo"
1037+
]["endCursor"]
1038+
except (TypeError, KeyError):
1039+
logging.debug("Repo does not seem to have any collaborators")
1040+
1041+
def _populate_current_repos_collaborators(self, repo_collaborators: dict[str, dict]):
1042+
"""Populate self.current_repos_collaborators with data from repo_collaborators"""
10401043
for repo, collaborators in self.current_repos_collaborators.items():
10411044
if repo.name in repo_collaborators:
10421045
# Extract each collaborator from the GraphQL response for this repo

0 commit comments

Comments
 (0)