Repository navigation
fix(gitea): resolve last_commit from PR commits, not repo commits - #2569
Conversation
GiteaProvider.__init__ called list_all_commits(owner, repo), which hits
GET /repos/{owner}/{repo}/commits and returns commits of the default
branch. Taking [-1] of that newest-first list left last_commit pointing
at the oldest commit of the default branch, so the latest-commit URL,
inline comment commit_id and the file content read from "the latest
commit" all referenced a commit unrelated to the PR. Because that SHA
never changed between pushes, persistent reviews also stopped detecting
new commits.
Fetch the PR's own commits instead, via the existing get_pr_commits()
wrapper. Gitea returns them newest-first, so they are reversed to the
oldest-first order the other providers use, and last_commit is matched
against the PR head SHA. The raw payload is wrapped in a small adapter
so callers keep the .sha/.html_url attribute access they already relied
on, mirroring _AzureCommitAdapter in the Azure DevOps provider.
An empty commit list no longer raises IndexError; last_commit stays
None and the affected getters degrade to the head SHA or an empty
string.
PR Summary by QodoFix GiteaProvider to derive last_commit from PR commits (not default-branch commits)
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1.
|
|
Hey @vaibzde, |
…s nothing _set_pr_commits() could leave last_commit/last_commit_id as None when the PR commits endpoint returned an empty list, but downstream code (the description header, publish_inline_comments) dereferences .sha unconditionally. Fall back to a commit wrapping self.sha instead of None, so those paths always get a real SHA. Also guard against a non-list commits payload instead of assuming list/dict shape, and switch the new test file's string literals to double quotes to match repo style.
|
@naorpeled pushed a fix addressing all three Qodo findings:
Updated |
|
Code review by qodo was updated up to the latest commit e0bdea9 |
Problem
GiteaProvider.__init__populatedself.pr_commitswithlist_all_commits(owner, repo), which callsGET /repos/{owner}/{repo}/commits— the commits of the repository's default branch, not of the PR. It then took[-1]of that list; since Gitea returns commits newest-first,last_commitended up being the oldest commit on the default branch.Everything that reads
last_commitwas therefore pointing at a commit unrelated to the PR:get_latest_commit_url()returned a default-branch commit URL.publish_inline_comments()posted with a wrongcommit_id._get_file_content_from_latest_commit()read file content from the wrong commit.Fix
Fetch the PR's own commits through the
get_pr_commits()wrapper that already exists inRepoApi(GET /repos/{owner}/{repo}/pulls/{pr_number}/commits).last_commitis matched against the PR head SHA (pr.head.sha), so it does not silently depend on the ordering.get_pr_commits()returns raw JSON dicts, while the rest of the codebase expects attribute access on commit objects (for examplepr_description.pyusesgit_provider.last_commit_id.sha). The payload is wrapped in a small_GiteaCommitAdapterexposing.sha/.html_url, mirroring_AzureCommitAdapterinazuredevops_provider.py.IndexErrorfrom__init__; nowlast_commitstaysNoneand the getters degrade to the head SHA or an empty string.list_all_commits()is left in place — it is a plain endpoint wrapper and removing it would widen the surface of a behaviour fix.Tests
Added
TestGiteaProviderPRCommitsintests/unittest/test_gitea_provider.py, following the existing pattern in that file of building the provider with__new__and wiring only the attributes under test:pr_number, andlist_all_commitsis not called (this is the assertion that pins the regression);last_commitis the PR head andpr_commitsis stored oldest-first;last_commitfalls back to the newest commit;last_commit is Noneandget_latest_commit_url()returns""instead of raising;_get_file_content_from_latest_commit()reads from the PR head SHA.Run locally in a clean venv built from this repo:
ruff checkon both touched files reports the same pre-existing findings as onmain, and none from this change.Notes
/pulls/{index}/commitsreturns commits newest-first; the head-SHA match meanslast_commitis correct either way, and only the order ofpr_commitsdepends on it.RepoApiwrappers for pagination, while this change is in the provider that consumes them. No functional overlap.Closes #2206