Repository navigation
fix(gitea): use PR head commits for last_commit instead of default branch - #2555
Sanjays2402 wants to merge 1 commit into
Conversation
…anch
GiteaProvider.__init__ populated self.pr_commits via list_all_commits()
without a ref, which calls GET /repos/{owner}/{repo}/commits and returns
the repository default branch. Since Gitea returns commits newest-first,
self.pr_commits[-1] was the oldest default-branch commit, so last_commit
never pointed at the PR head.
That wrong SHA propagated to get_latest_commit_url(), the commit_id used
by publish_inline_comments(), _get_file_content_from_latest_commit(), and
the incremental-review change detection.
The listing is now anchored at the PR head sha and the head commit is
selected from it, matching get_commit_messages() which already uses the
PR-scoped endpoint.
Adds a regression test asserting the sha is passed and last_commit is the
PR head.
PR Summary by QodoFix Gitea last_commit to use PR head commit instead of default branch
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
Context used 1. __select_last_commit may return None
|
| @staticmethod | ||
| def __select_last_commit(commits, head_sha: str): | ||
| """Pick the PR head commit from a Gitea commit listing. | ||
|
|
||
| Gitea returns commits newest-first, so the head is the first entry; | ||
| prefer an exact ``head_sha`` match when it is present. | ||
| """ | ||
| if not commits: | ||
| return None | ||
| if head_sha: | ||
| for commit in commits: | ||
| if getattr(commit, "sha", None) == head_sha: | ||
| return commit | ||
| return commits[0] |
There was a problem hiding this comment.
1. __select_last_commit may return none 📘 Rule violation ☼ Reliability
GiteaProvider.__select_last_commit() returns None when the commit list is empty, but later methods dereference self.last_commit (e.g., get_latest_commit_url() and _get_file_content_from_latest_commit()), which can raise runtime AttributeError. This unhandled edge case creates a deferred failure mode where initialization may succeed but PR-head dependent behavior can crash when the commits listing fails or is empty.
Agent Prompt
## Issue description
`__select_last_commit()` can return `None` when the commits listing is empty, but downstream code assumes `self.last_commit` is always present and dereferences `.html_url` / `.sha`, leading to `AttributeError` at runtime or invalid behavior. Fix this edge case so PR-head dependent features behave predictably even when the commits API returns empty results.
## Issue Context
The provider now selects `last_commit` via `__select_last_commit(self.pr_commits, self.sha)` during initialization, and `__select_last_commit()` explicitly allows returning `None` when `commits` is empty. Methods like `get_latest_commit_url()` and `_get_file_content_from_latest_commit()` still dereference `self.last_commit` unconditionally, so initialization can appear successful but later operations crash; the provider should either fail fast with a clear error message or provide a safe fallback (e.g., based on `self.sha`) to satisfy graceful edge-case handling expectations.
## Fix Focus Areas
- pr_agent/git_providers/gitea_provider.py[89-104]
- pr_agent/git_providers/gitea_provider.py[111-124]
- pr_agent/git_providers/gitea_provider.py[248-249]
- pr_agent/git_providers/gitea_provider.py[458-464]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
|
This landed in main on 1 August via #2569 (commit Your diagnosis matches what was merged exactly — the difference is only in approach: the merged version resolves So this is superseded and now conflicts; it can probably be closed. Worth flagging that yours was filed a few days earlier, in case the maintainers want to look at how the duplicate work happened. |
|
Confirmed - main now resolves Closing this as superseded - no point carrying the conflict. |
Closes #2206
GiteaProvider.__init__calledlist_all_commits()with no ref, which hitsGET /repos/{owner}/{repo}/commitsand returns the default branch. Gitea returns commits newest-first, soself.pr_commits[-1]was the oldest default-branch commit —last_commitnever pointed at the PR head, which then propagated toget_latest_commit_url(), thecommit_idused bypublish_inline_comments(),_get_file_content_from_latest_commit(), and incremental-review change detection.The listing is now anchored at the PR head sha and the head commit selected from it, matching
get_commit_messages(), which already uses the PR-scoped endpoint.Regression test in
tests/unittest/test_gitea_provider.pyfails without the fix (last_commit.shaisNone/default-branch) and passes with it; the gitea suite is 20 passed.