publish_code_suggestions repeats the same validation skeleton in every provider
publish_code_suggestions is implemented separately in every provider, 561 lines in total, and the implementations share a skeleton that is not provider-specific.
Verified on main at 275d1386:
| Lines | File |
|---|---|
| 154 | azuredevops_provider.py |
| 111 | gitlab_provider.py |
| 65 | bitbucket_server_provider.py |
| 63 | bitbucket_provider.py |
| 53 | github_provider.py |
| 41 | gerrit_provider.py |
| 36 | codecommit_provider.py |
| 27 | gitea_provider.py |
| 7 | plain_diff_provider.py |
| 2 | git_provider.py, local_git_provider.py |
The bitbucket and bitbucket server implementations are 95% identical to each other.
The shared part
Reading github and bitbucket side by side, the common skeleton is: deep-copy the suggestions, stamp each with a _dedup_code_fp fingerprint via code_fingerprint(...), run validate_comments_inside_hunks(...), then per suggestion reject a missing or -1 relevant_lines_start, reject relevant_lines_end < relevant_lines_start, and log the rejection. Only what happens after that, building and posting the provider's comment payload, genuinely differs.
Suggested shape
Move the fingerprinting, hunk validation, line-range validation and the loop into GitProvider.publish_code_suggestions, and have it delegate payload construction and posting to the provider. create_inline_comment and publish_inline_comments already exist as the per-provider seam, so in most cases the subclass method should reduce to building parameters.
Two cautions. Azure DevOps at 154 lines and GitLab at 111 almost certainly carry real provider behaviour beyond the skeleton, so do not force them into the template if it fights back; leaving one or two providers with a full override is an acceptable outcome. And publish_code_suggestions is the plural interface method that tools actually call, so do not confuse it with the singular publish_code_suggestion, which is dead and is being removed in #3182.
Reproduce
python3 - <<'PY'
import ast
from pathlib import Path
for p in sorted(Path("pr_agent/git_providers").glob("*.py")):
for cls in [n for n in ast.parse(p.read_text()).body if isinstance(n, ast.ClassDef)]:
for m in cls.body:
if isinstance(m, (ast.FunctionDef, ast.AsyncFunctionDef)) and m.name == "publish_code_suggestions":
print(f"{m.end_lineno-m.lineno+1:5d} {p.name}")
PYBefore you start
Re-run the above. Note also that the project is moving to an agentic review loop, which may change how suggestions are published; if that work has started by the time you pick this up, check with a maintainer before investing in the larger providers.
Source: The-PR-Agent/pr-agent