Skip to content

Add code suggestion for PR review comments#36457

Closed
bircni wants to merge 7 commits intogo-gitea:mainfrom
bircni:feature/code-suggestion
Closed

Add code suggestion for PR review comments#36457
bircni wants to merge 7 commits intogo-gitea:mainfrom
bircni:feature/code-suggestion

Conversation

@bircni
Copy link
Copy Markdown
Member

@bircni bircni commented Jan 26, 2026

  • Add a suggestion toolbar button for diff comments that inserts ```suggestion blocks from selected lines (shift-click
    range selection).
  • Store selected line range metadata on code comments to apply suggestions reliably.
  • Apply suggestions by generating a patch and committing to the PR head branch; auto‑resolve the conversation after
    successful apply.
  • Block applying suggestions on outdated comments.

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:

  • Batch apply / “apply all suggestions” and shared commit message flow
  • Inline edit before apply and conflict handling
  • UI polish and parity with GitHub’s suggestion UX
  • Additional tests (integration/e2e coverage)

Resolves #14765.

Example

Screenshot

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jan 26, 2026
@github-actions github-actions bot added modifies/api This PR adds API routes or modifies them modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files modifies/frontend labels Jan 26, 2026
@lunny
Copy link
Copy Markdown
Member

lunny commented Jan 26, 2026

It also needs multiple lines selection like #36014

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 1, 2026

@silverwind your merge destroyed it xD

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 1, 2026

@Copilot fix the problems and your review comments ;-)

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 1, 2026

@silverwind your merge destroyed it xD

Odd, it was a very simple merge conflict in just one TS file.

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 1, 2026

This new lint error is odd, this PR doesn't even touch tests/integration/api_pull_review_test.go, there must be something else wrong coming from the merged changes.

@Copilot fix the problems and your review comments ;-)

Copilot can't do changes in PRs which it has not started. Also, it's review comments should be addressed manually.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 1, 2026

gonna have a look at it asap

@silverwind
Copy link
Copy Markdown
Member

This new lint error is odd, this PR doesn't even touch tests/integration/api_pull_review_test.go, there must be something else wrong coming from the merged changes.

Still not sure what's going on, but it definitely came from #36441.

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 1, 2026

Actually no, it's just that this PR needs to update tests/integration/api_pull_review_test.go:384 because this one added 2 new arguments to CreateCodeComment, and #36441 added a function that calls it with 11 instead of the new 13 arguments.

@silverwind
Copy link
Copy Markdown
Member

PS: Run make lint-frontend-fix to fix the stylistic issues.

@wxiaoguang
Copy link
Copy Markdown
Contributor

Don't do AI review before the design is overall right and you have overall reviewed the code.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 1, 2026

PS: Run make lint-frontend-fix to fix the stylistic issues.

thanks! Vscode auto format is a weird thing idk why it happens sometimes

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 9, 2026

@wxiaoguang what next?

@wxiaoguang
Copy link
Copy Markdown
Contributor

@wxiaoguang what next?

Ask the maintainers who have interests in this feature.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 9, 2026

@silverwind @a1012112796 could you have a Look at it? 😊

@lunny
Copy link
Copy Markdown
Member

lunny commented Feb 11, 2026

Could we also support to merge multiple suggestions at once in this PR?

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 11, 2026

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.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 11, 2026

@silverwind I though I had already addressed the review comments - grrr merge conflicts 🫤

@silverwind
Copy link
Copy Markdown
Member

silverwind commented Feb 11, 2026

try a git fetch origin/main and git merge origin/main in your branch. I'm afraid to do it on GitHub UI again because of the recent issue 😆.

@wxiaoguang
Copy link
Copy Markdown
Contributor

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.

@wxiaoguang wxiaoguang marked this pull request as draft February 11, 2026 18:36
@bircni bircni force-pushed the feature/code-suggestion branch from dc8afd1 to 5e6d23a Compare February 11, 2026 19:27
@bircni bircni marked this pull request as ready for review February 25, 2026 18:28
@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 26, 2026

Marking this as WIP for now as I think there needs to be some ideas and more checks - will return later

@bircni bircni marked this pull request as draft February 26, 2026 17:39
@bircni
Copy link
Copy Markdown
Member Author

bircni commented Feb 27, 2026

Is there a "help-wanted" label?

@silverwind
Copy link
Copy Markdown
Member

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.

@bircni
Copy link
Copy Markdown
Member Author

bircni commented Mar 13, 2026

will close this for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them modifies/frontend modifies/go Pull requests that update Go code modifies/templates This PR modifies the template files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Add code suggestion for PR review comments

7 participants