Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3276
Problem
Commit.repositoryis typed asRepository, but the API only includes arepositoryfield in commit search results. For commits fromget_commit,get_commits, branches, tags, comparisons orcreate_file, it always returnedNone. The existingtestAttributeseven 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}(thecommitreturned bycreate_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.../gitfor the second shape, or get a repo literally namedgitwrong.Unlike
Issue.repository, it doesn't complete the commit first: a commit's own response never hasrepository, so that would only cost a request. The returnedRepositoryis lazy, so lazy commits stay lazy. Search results still use their realrepositoryfield.Tests
Commit.testAttributesnow checksrepository.full_name/urlinstead of assertingNone.Commit.testLazyAttributeschecks the repository of a lazy commit (no request is made).Repository.testCreateFilechecks the repository of the returnedgit/commitscommit.All three fail on
mainand pass with this change.I used Claude Code to write this change.