Visitar URL original
Derive `Commit.repository` from the commit url by b84330808 · Pull Request #3599 · PyGithub/PyGithub · GitHub
Skip to content

Derive Commit.repository from the commit url - #3599

Open
b84330808 wants to merge 1 commit into
PyGithub:mainfrom
b84330808:fix-commit-repository
Open

b84330808 wants to merge 1 commit into
PyGithub:mainfrom
b84330808:fix-commit-repository

Conversation

@b84330808

Copy link
Copy Markdown

Fixes #3276

Problem

Commit.repository is typed as Repository, but the API only includes a repository field in commit search results. For commits from get_commit, get_commits, branches, tags, comparisons or create_file, it always returned None. The existing testAttributes even asserted that.

Fix

When the attribute isn't set, the getter derives the repository from the commit url, the same idea as Issue.repository. Commit urls come in two shapes:

  • /repos/{owner}/{repo}/commits/{sha} (normal commits)
  • /repos/{owner}/{repo}/git/commits/{sha} (the commit returned by create_file / update_file)

so the url is matched on the /repos/{owner}/{repo} part rather than by cutting off the last segments. Cutting would give .../git for the second shape, or get a repo literally named git wrong.

Unlike Issue.repository, it doesn't complete the commit first: a commit's own response never has repository, so that would only cost a request. The returned Repository is lazy, so lazy commits stay lazy. Search results still use their real repository field.

Tests

  • Commit.testAttributes now checks repository.full_name / url instead of asserting None.
  • Commit.testLazyAttributes checks the repository of a lazy commit (no request is made).
  • Repository.testCreateFile checks the repository of the returned git/commits commit.

All three fail on main and pass with this change.

I used Claude Code to write this change.

Only commit search results contain a `repository` field, so for commits
returned by `get_commit`, `get_commits`, branches, tags, comparisons and
file contents, `Commit.repository` was always `None`, although it is typed
as `Repository`.

When the field is not set, look the repository up from the commit url
(`/repos/{owner}/{repo}/commits/{sha}` or
`/repos/{owner}/{repo}/git/commits/{sha}`), like `Issue.repository` does.
This does not need an extra request, so lazy commits stay lazy.

Fixes PyGithub#3276

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

Commit.repository is None

1 participant