Skip to content

Commit 9317c64

Browse files
REST fallback in python code
Signed-off-by: Lukasz Gryglicki <[email protected]> Assisted by [OpenAI](https://platform.openai.com/) Assisted by [GitHub Copilot](https://github.com/features/copilot)
1 parent c05daf1 commit 9317c64

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

cla-backend/cla/models/github_models.py

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2210,6 +2210,7 @@ def iter_pr_commits_full(g, owner: str, repo_name: str, number: int, page_size:
22102210
)
22112211
if res is None:
22122212
cla.log.error(f"Failed to fetch commits for {owner}/{repo_name} PR #{number}")
2213+
raise ValueError("failed to fetch commits using GraphQL")
22132214
return
22142215
commits = res["repository"]["pullRequest"]["commits"]
22152216
for n in commits["nodes"]:
@@ -2341,29 +2342,61 @@ def get_pull_request_commit_authors(client, org, pull_request, installation_id,
23412342

23422343
pr_number = pull_request.number
23432344
repo_name = pull_request.base.repo.name
2345+
gql_ok = True
2346+
pr_commits = None
2347+
23442348
count = get_pr_commit_count_gql(client, org, repo_name, pr_number)
23452349
if count is not None:
2346-
cla.log.debug(f"{fn} - PR: {pr_number}, number of commits: {count}")
2350+
cla.log.debug(f"{fn} - PR: {pr_number}, number of commits (GraphQL): {count}")
23472351
else:
2348-
cla.log.debug(f"{fn} - PR: {pr_number}, number of commits: (unknown)")
2352+
cla.log.debug(f"{fn} - PR: {pr_number}, failed to get commits count using GraphQL, fallback to REST")
2353+
gql_ok = False
2354+
try:
2355+
pr_commits = pull_request.get_commits()
2356+
count = pr_commits.totalCount
2357+
except Exception as exc:
2358+
cla.log.warning(f"{fn} - PR: {pr_number}, get PR commits raised: {exc}")
2359+
raise
2360+
cla.log.debug(f"{fn} - PR: {pr_number}, number of commits (REST API): {count}")
2361+
if count == 250:
2362+
cla.log.warning(f"{fn} - PR: {pr_number}, commit count is 250, which is the max for REST API, there can be more commits")
23492363

23502364
commit_authors = []
23512365
any_missing = False
23522366
max_workers = 16
23532367
submit_chunk = 256
23542368

2355-
try:
2356-
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
2357-
for chunk in iter_chunks(iter_pr_commits_full(client, org, repo_name, pr_number), submit_chunk):
2358-
futures = [executor.submit(get_author_summary_gql, c, pr_number, installation_id, with_co_authors)
2359-
for c in chunk]
2369+
if gql_ok:
2370+
try:
2371+
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
2372+
for chunk in iter_chunks(iter_pr_commits_full(client, org, repo_name, pr_number), submit_chunk):
2373+
futures = [executor.submit(get_author_summary_gql, c, pr_number, installation_id, with_co_authors) for c in chunk]
2374+
for fut in concurrent.futures.as_completed(futures):
2375+
authors, missing = fut.result()
2376+
any_missing = any_missing or missing
2377+
commit_authors.extend(authors)
2378+
except Exception as exc:
2379+
cla.log.warning(f"{fn} - PR: {pr_number}, GraphQL processing failed: {exc}, falling back to REST")
2380+
gql_ok = False
2381+
commit_authors = []
2382+
any_missing = False
2383+
if not gql_ok:
2384+
if pr_commits is None:
2385+
try:
2386+
pr_commits = pull_request.get_commits()
2387+
except Exception as exc:
2388+
cla.log.warning(f"{fn} - PR: {pr_number}, fallback get PR commits raised: {exc}")
2389+
raise
2390+
try:
2391+
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
2392+
futures = [executor.submit(get_author_summary, c, pr_number, installation_id, with_co_authors) for c in pr_commits]
23602393
for fut in concurrent.futures.as_completed(futures):
23612394
authors, missing = fut.result()
23622395
any_missing = any_missing or missing
23632396
commit_authors.extend(authors)
2364-
except Exception as exc:
2365-
cla.log.warning(f"{fn} - PR: {pr_number}, get_author_summary_gql raised: {exc}")
2366-
raise
2397+
except Exception as exc:
2398+
cla.log.warning(f"{fn} - PR: {pr_number}, REST processing failed: {exc}")
2399+
raise
23672400

23682401
return (commit_authors, any_missing)
23692402

0 commit comments

Comments
 (0)