Repository navigation
fix(gitea): report success/failure and submit reviews, not drafts - #2878
IsmaelMartinez merged 2 commits into
Conversation
`publish_inline_comments`/`publish_code_suggestions` never returned a
value, so `PRCodeSuggestions.push_inline_code_suggestions` always
treated a successful publish as failed and republished every
suggestion a second time, duplicating each one on the PR.
`create_inline_comment` also posted to `/pulls/{id}/reviews` without
an `event`. Gitea/Forgejo defaults an event-less review to a draft
(state `PENDING`), invisible to everyone but its author - there is no
later step anywhere in this flow that submits it, so with
`commitable_code_suggestions = true` every `/improve` review was
silently invisible to the PR author and any other reviewer.
Both bugs only surface with `commitable_code_suggestions = true`
(default `false`), which is presumably why they haven't been caught:
the default `/improve` path uses a different, single-comment publish
method untouched by this fix.
Verified against a live Forgejo instance (not just mocks): before the
fix, one `/improve` call left a `PENDING` review with a duplicated
suggestion; after, exactly one `COMMENT`-state review with one
comment. Added regression tests for both failure modes; confirmed
each new test fails against the pre-fix code and passes against the
fix.
PR Summary by QodoFix Gitea inline suggestion status and review submission
AI Description
Diagram
High-Level Assessment
Files changed (2)
|
Code Review by Qodo
1. API errors bypass fallback
|
| if not response: | ||
| self.logger.error("Failed to publish inline comment") | ||
| return | ||
| return False |
There was a problem hiding this comment.
5. Api errors bypass fallback 🐞 Bug ☼ Reliability
publish_inline_comments returns False only for a falsy normal response, but it does not catch ApiException or other errors from create_inline_comment. Consequently, ordinary request failures propagate out of publish_code_suggestions instead of returning False, so the caller's individual-suggestion fallback never runs.
Agent Prompt
## Issue description
The new boolean contract does not cover exceptions raised by the Gitea API client. A failed review request can escape from `publish_inline_comments`, abort the loop, and prevent `PRCodeSuggestions.push_inline_code_suggestions` from executing its fallback.
## Issue Context
Other Gitea provider operations explicitly catch `ApiException`, and direct `call_api` usage in this module documents that API failures are raised. Catch expected publication exceptions, log them, and return `False` while preserving an appropriate policy for unexpected programming errors.
## Fix Focus Areas
- pr_agent/git_providers/gitea_provider.py[399-414]
- pr_agent/git_providers/gitea_provider.py[883-902]
- pr_agent/tools/pr_code_suggestions.py[825-834]
- tests/unittest/test_gitea_provider.py[830-858]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Thanks, and both diagnoses hold: the event-less review really does stay PENDING, and the falsy return really does republish every suggestion.
Disclosure: I have an older PR of my own, #2716, covering the return-value half only. Yours is the fuller fix, so I will close mine in its favour once this lands.
One change first, on the duplication Qodo flagged. Patch inline. You can disregard its three phrasing findings, which are a platform rule rather than ours.
| else: | ||
| self.publish_inline_comments([payload]) | ||
| all_successful &= self.publish_inline_comments([payload]) | ||
| return all_successful |
There was a problem hiding this comment.
Qodo is right here. This aggregates all-succeeded, so with three suggestions where the second fails you return False, the caller replays the whole list, and the two that already succeeded post a second time. Measured on a merged tree: 6 API calls against 3 for any-succeeded.
Not a one-click suggestion, because the initialiser on line 418 sits in a different hunk from this one and applying half of it would break the function. The whole change:
- all_successful = True
+ publishable_count = 0
+ published_count = 0
for suggestion in suggestions:
...
payload = dict(body=body, path=path, old_position=old_position,new_position = new_position)
+ publishable_count += 1
if title_body:
title_body = f"**Suggestion:** {title_body}"
- all_successful &= self.publish_inline_comments([payload],title_body)
+ published = self.publish_inline_comments([payload],title_body)
else:
- all_successful &= self.publish_inline_comments([payload])
- return all_successful
+ published = self.publish_inline_comments([payload])
+ if published:
+ published_count += 1
+
+ # A partial failure must not report failure: the caller republishes the whole
+ # list, which would post the already-accepted suggestions a second time.
+ return published_count > 0 or publishable_count == 0Worth a sixth test for the partial-failure case, since none of the five cover it.
There was a problem hiding this comment.
Agreed. all_successful is unsafe here because the caller retries the entire batch after any False, duplicating suggestions that already succeeded. The published_count > 0 or publishable_count == 0 result preserves the caller’s retry contract while preventing retries after partial success. I also agree a partial-failure regression test is needed, asserting three suggestions make only three API calls and return True when one fails.
There was a problem hiding this comment.
Applied exactly as diffed - publishable_count/published_count, false only when nothing published. Added the partial-failure test too: 3 suggestions, second fails, asserts call_count == 3 and result is True. Confirmed it fails against the pre-fix all_successful &= logic before reapplying the patch.
Separately: Qodo also flagged that publish_inline_comments does not catch ApiException from create_inline_comment, so a real API error propagates instead of returning False, which would skip the caller's fallback too. You didn't call that one out - want it folded into this PR, or should it be a separate issue/PR?
There was a problem hiding this comment.
Keep it as a separate issue/PR. This change already fixes two focused behaviors: review visibility and duplicate retries after partial success. Catching ApiException changes error-handling and retry semantics, especially when earlier suggestions in the batch were already accepted.
If pursued separately, add tests for:
- an exception on the first suggestion returning
False; - an exception after a successful suggestion not causing the caller to replay the whole batch;
- logging the exception details without hiding the failure.
The current partial-failure test covers False responses, but not raised API exceptions.
publish_code_suggestions aggregated per-suggestion results with `all_successful &=`, so any single failure in a batch made the whole call report failure. The caller (push_inline_code_suggestions) then replays the entire suggestion list on any falsy result, reposting the ones that already succeeded - three suggestions with one failure becomes six API calls instead of three. Track publishable/published counts instead and report failure only when nothing published, matching the caller's retry contract: republish only on a genuinely empty result. Per review on The-PR-Agent#2878.
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Approving. You applied the aggregation change exactly as diffed and the partial-failure test does what it claims: against main all six new tests go red, and the 45 gitea tests pass on the merged tree.
Agreed on deferring Qodo's ApiException finding, and I traced it rather than taking the agreement at face value. push_inline_code_suggestions runs inside a try/except in pr_code_suggestions.py, so an exception is logged rather than crashing /improve. It costs the fallback republish, which is a degradation and not a break, so a separate PR is the right call.
As promised, I will close my own #2716 in favour of this once it lands. Same gate as the other first-time contributions: only the release drafter has run, so the queued workflows need releasing before it can merge.
Problem
With
pr_code_suggestions.commitable_code_suggestions = true,/improveon a Gitea/Forgejo provider produces two bugs:GiteaProvider.publish_inline_comments/publish_code_suggestionsnever returned a value (implicitNone).PRCodeSuggestions.push_inline_code_suggestionschecks the return ofpublish_code_suggestionsand, on falsy, republishes every suggestion individually as a fallback - so a fully successful publish is always treated as a failure and every suggestion is posted twice.RepoApi.create_inline_commentPOSTs to/pulls/{id}/reviewswithout aneventfield. Per the Gitea/Forgejo API, an event-less review defaults to a draft (PENDING), visible only to its author until someone manually submits it - and nothing in this flow ever does.Fix
publish_inline_commentsandpublish_code_suggestionsnow returnbool, reflecting whether the underlying API call actually succeeded.create_inline_commentsubmits"event": "COMMENT"so the review lands as a normal, visible comment instead of a stuck draft.Verification
TestGiteaProviderInlineCommentStatus(5 tests) covering both failure modes. Confirmed each fails against the pre-fix code and passes against the fix.pytest tests/unittest/test_gitea_provider.py: 44 passed./improvecall left aPENDINGreview with a duplicated inline comment; after the fix, exactly oneCOMMENT-state review with exactly one comment.Both bugs are gated behind
commitable_code_suggestions = true(defaultfalse), which explains why they haven't surfaced before - the default/improvepath uses a different, single-comment publish method untouched by this change.