Skip to content

Bug-2038705: Replace requests-based GitHub API calls with PyGithub across ETL modules#9664

Draft
moijes12 wants to merge 5 commits into
mozilla:masterfrom
moijes12:bug2038705-own
Draft

Bug-2038705: Replace requests-based GitHub API calls with PyGithub across ETL modules#9664
moijes12 wants to merge 5 commits into
mozilla:masterfrom
moijes12:bug2038705-own

Conversation

@moijes12

@moijes12 moijes12 commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Description

This PR migrates all GitHub REST API interactions from direct requests library
calls to the PyGithub library, providing a more maintainable and type-safe approach
to GitHub API interactions.

Changes

  • Core: Refactored treeherder/utils/github.py to use PyGithub objects instead
    of raw REST API responses with requests library
  • ETL modules affected:
    • treeherder/changelog/collector.py - Updated to work with PyGithub commit and
      release objects
    • treeherder/changelog/tasks.py - Updated datetime handling for PyGithub
      datetime objects
    • treeherder/etl/management/commands/ingest.py - Refactored query_data() and
      ingest_git_pushes() to use PyGithub comparison and commit objects
  • Tests: Added comprehensive test coverage in tests/utils/test_github.py and
    updated existing tests in changelog and ingest modules to mock PyGithub objects

Benefits

  • Better maintainability with a proper Python GitHub client library
  • Type safety through PyGithub object attributes instead of dict key access
  • Reduced code complexity by removing custom fetch/http utility functions
  • Clearer intent through explicit PyGithub API calls

Related Issue

Fixes Bug-2038705

moijes12 added 5 commits July 7, 2026 11:58
Updated the below functions in treeherder/utils/github.py to use
PyGithub
- get_repository
- compare_shas
- get_all_commits
- get_commit
- get_pull_request
- get_pull_request_commits
- get_releases

Pending work:
- Callers need to be updated.
- Failing tests need to be examined
- New tests needed for `github.py`

Fixes
[Bug-2038705](https://bugzilla.mozilla.org/show_bug.cgi?id=2038705)
- Fixed failing unit tests for push_loader
- Updated changelog/collector.py
Fixed collector.py and its unit tests
Also, updated tasks.py
Migrated the `query_data` function in ingest.py to use the
library written over PyGithub. Also, updated the unit tests for
the same.
- Added tests for treeherder/utils/github.py
- Removed fetch_api and fetch_api_full_url
@moijes12 moijes12 marked this pull request as ready for review July 7, 2026 12:11
@moijes12 moijes12 changed the title Bug-2038705: Use PyGithub for calls to GitHub v3 Bug-2038705: Replace requests-based GitHub API calls with PyGithub across ETL modules Jul 7, 2026

commits = []
for _commit in compare_response["commits"]:
for temp_commit in compare_result.commits:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the old variable name be kept? With temp_commit, I'd associate commit local changes which will not be pushed upstream but be removed with e.g. git reset --hard ...

from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.db import connection
from github.Commit import Commit

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the github API calls in this file also be routed through the helper github.py which sets the authentication token if available? There might be cases when the ingest command needs to be run on non-prod (for testing, ...) or production (to recover or for other reasons)? This can be done in this PR or in a separate one.

for release in github.get_releases(owner, repository):
# no "since" option for releases() we filter manually here
if "since" in kw and release["published_at"] <= kw["since"]:
print(f"kw={kw}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug code which needs removal.

@moijes12

Copy link
Copy Markdown
Contributor Author

Thanks @Archaeopteryx

I will move this to draft as I work on the review feedback

@moijes12 moijes12 marked this pull request as draft July 10, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants