Add code suggestion for PR review comments#36457
Add code suggestion for PR review comments#36457bircni wants to merge 7 commits intogo-gitea:mainfrom
Conversation
|
It also needs multiple lines selection like #36014 |
There was a problem hiding this comment.
Pull request overview
This PR implements GitHub-style code suggestions on PR review comments, including UI affordances to generate suggestion blocks from selected diff lines and backend support to apply those suggestions as commits to the PR’s head branch.
Changes:
- Add client-side support for selecting line ranges in PR diffs, generating ```suggestion blocks into review comments, and rendering “Apply suggestion” buttons next to suggestion blocks when applicable.
- Extend comment models, forms, and services to persist code-comment line ranges, parse suggestion blocks from comment content, build diff patches, and apply them to the PR head branch with appropriate permissions and outdated-checks.
- Wire up new routes, templates, CSS, and i18n strings to support suggestion application from both the Files tab diff view and code-comment conversations.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
web_src/js/features/repo-issue.ts |
Adds suggestion-block UI wiring: renders “Apply suggestion” buttons on suggestion code blocks, handles applying suggestions via POST, implements diff-line range selection and highlighting, and inserts ```suggestion blocks into comment textareas based on the selected diff range. |
web_src/js/features/repo-diff.ts |
Ensures dynamically loaded conversation blocks on the diff page are passed through initSuggestionApplyButtons so new suggestion blocks get their action buttons. |
web_src/css/repo.css |
Styles active diff rows for range selection highlighting, positions a left border on selected line numbers, styles suggestion action areas under comment bodies, and makes diff line numbers look clickable. |
templates/shared/combomarkdowneditor.tmpl |
Adds a “Insert a suggestion” toolbar button (lightbulb icon) for comment-mode editors, carrying localized tooltip and error messages for empty selection and non-proposed-side usage. |
templates/repo/issue/view_content/conversation.tmpl |
Augments code-comment entries in the PR conversation view with data attributes needed to render and invoke suggestion-apply actions. |
templates/repo/diff/comments.tmpl |
Extends diff-page code comments with data-comment-id, data-apply-suggestion-url, localized button text, and data-can-apply-suggestion flags for suggestion actions. |
templates/repo/diff/comment_form.tmpl |
Adds hidden line_start and line_end fields to diff comment forms so ranged selections can be persisted with new code comments. |
services/pull/suggestion.go |
Introduces ParseSuggestionBlocks to extract ```suggestion fenced blocks from markdown and BuildSuggestionPatch to construct unified diff patches replacing a line range with suggestion content, respecting configurable context lines. |
services/pull/suggestion_test.go |
Adds unit tests validating that only suggestion fences are parsed and that BuildSuggestionPatch produces the expected diff for a simple multiline replacement. |
services/pull/review.go |
Extends CreateCodeComment and createCodeComment to accept line range metadata and pass it into CreateCommentOptions as CodeCommentLineStart/End for storage in CommentMetaData. |
services/mailer/incoming/incoming_handler.go |
Updates mail-based code comment reply creation to populate the new line range parameters (using the single line as start and end). |
services/forms/repo_form.go |
Extends CodeCommentForm with LineStart and LineEnd fields to carry the selected range from diff comment forms. |
routers/web/web.go |
Registers a new route POST /comments/{id}/suggestions/apply mapped to the ApplySuggestion handler. |
routers/web/repo/pull_review_test.go |
Updates test setup for CreateCodeComment to use the new (line, lineStart, lineEnd) signature while preparing a conversation-rendering scenario. |
routers/web/repo/pull_review.go |
Computes signed lineStart/lineEnd for previous/proposed sides, passes them into CreateCodeComment, and adds ApplySuggestion which validates the request, enforces permissions, rejects outdated comments, builds a patch from a chosen suggestion block, applies it via ApplyDiffPatch, and optionally auto-resolves the conversation. |
routers/api/v1/repo/pull_review.go |
Adjusts API review comment creation to populate the new line range parameters (currently using the same line for start and end). |
options/locale/locale_en-US.json |
Adds localized strings for the suggestion toolbar button (tooltip and error messages) and suggestion application messages (success, generic failure, outdated). |
models/issues/comment.go |
Extends CommentMetaData with CodeCommentLineStart/End, refactors metadata construction in CreateComment to compose multiple metadata fields safely, and augments CreateCommentOptions to carry the new line range fields. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@silverwind your merge destroyed it xD |
|
@Copilot fix the problems and your review comments ;-) |
Odd, it was a very simple merge conflict in just one TS file. |
|
This new lint error is odd, this PR doesn't even touch
Copilot can't do changes in PRs which it has not started. Also, it's review comments should be addressed manually. |
|
gonna have a look at it asap |
Still not sure what's going on, but it definitely came from #36441. |
94cc2d9 to
8ffc678
Compare
8ffc678 to
4f1d922
Compare
|
Actually no, it's just that this PR needs to update |
|
PS: Run |
|
Don't do AI review before the design is overall right and you have overall reviewed the code. |
thanks! Vscode auto format is a weird thing idk why it happens sometimes |
|
@wxiaoguang what next? |
Ask the maintainers who have interests in this feature. |
|
@silverwind @a1012112796 could you have a Look at it? 😊 |
|
Could we also support to merge multiple suggestions at once in this PR? |
One step at a time please. First we need to make sure single merge works. That will already be a good batch of work. It will need to show the diff suggestion and provide the "apply" button. From the screenshot, this part seems to be missing. @bircni can you start by addressing the copilot review comments and click "Resolve Conversation" on all resolved ones? Also, there's a small merge conflict to resolve, please resolve on the CLI. |
|
@silverwind I though I had already addressed the review comments - grrr merge conflicts 🫤 |
|
try a |
|
I haven't really looked into details. But, overall, the code doesn't look good or right. Please make sure you can fully understand every line before asking for reviews. |
dc8afd1 to
5e6d23a
Compare
|
Marking this as WIP for now as I think there needs to be some ideas and more checks - will return later |
|
Is there a "help-wanted" label? |
|
Not really, but I also think we don't really need such a label. Gitea repo already has too many labels imho, better to trim down. |
|
will close this for now |
range selection).
successful apply.
Request for Help
This is an initial implementation and only a start. I need help with the remaining work and would appreciate guidance or
contributions, especially around:
Resolves #14765.
Example