debian-mirror-gitlab/doc/user/project/merge_requests/reviews/suggestions.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
8.4 KiB
Markdown
Raw Normal View History

2021-06-08 01:23:25 +05:30
---
stage: Create
group: Code Review
2022-11-25 23:54:43 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments
2021-06-08 01:23:25 +05:30
type: index, reference
---
# Suggest changes **(FREE)**
2021-12-11 22:18:48 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/25381) custom commit messages for suggestions in GitLab 13.9 [with a flag](../../../../administration/feature_flags.md) named `suggestions_custom_commit`. Disabled by default.
2023-05-27 22:25:52 +05:30
> - [Generally available](https://gitlab.com/gitlab-org/gitlab/-/issues/297404) in GitLab 13.10. Feature flag `suggestions_custom_commit` removed.
Reviewers can suggest code changes with a Markdown syntax in merge request diff threads.
The merge request author (or other users with the appropriate role) can apply any or
all of the suggestions from the GitLab UI. Applying suggestions adds a commit to the
merge request, authored by the user who suggested the changes.
## Create suggestions
1. On the top bar, select **Main menu > Projects** and find your project.
1. On the left sidebar, select **Merge requests** and find your merge request.
1. On the secondary menu, select **Changes**.
1. Find the lines of code you want to change.
- To select a single line:
- Hover over the line number, and
select **Add a comment to this line** (**{comment}**).
- To select multiple lines:
1. Hover over the line number, and select **Add a comment to this line** (**{comment}**).
1. Select and drag your selection until all desired lines are included. To
learn more, see [Multi-line suggestions](#multi-line-suggestions).
1. In the comment toolbar, select **Insert suggestion** (**{doc-code}**). GitLab
inserts a pre-populated code block into your comment, like this:
````markdown
```suggestion:-0+0
The content of the line you selected is shown here.
```
````
1. Edit the pre-populated code block to add your suggestion.
2021-06-08 01:23:25 +05:30
1. Select either **Start a review** or **Add to review** to add your comment to a
[review](index.md), or **Add comment now** to add the comment to the thread immediately.
2023-05-27 22:25:52 +05:30
### Multi-line suggestions
2021-06-08 01:23:25 +05:30
2023-01-13 00:05:48 +05:30
> [Changed](https://gitlab.com/gitlab-org/gitlab/-/issues/232339) in GitLab 13.11: suggestions in multi-line comments also become multi-line.
2023-06-20 00:43:36 +05:30
When you review a merge request diff, you can propose changes to multiple lines (up to 200)
in a single suggestion, by either:
- Selecting and dragging, as described in [Create suggestions](#create-suggestions).
GitLab creates a suggestion block for you.
- Selecting a single line, then manually adjusting the range offsets.
The range offsets in the first line of the suggestion describe line numbers relative
to the line you selected. The offsets specify the lines your suggestion intends to replace.
For example, this suggestion covers 3 lines above and 4 lines below the
commented line:
````markdown
```suggestion:-3+4
"--talk-name=ca.desrt.dconf",
"--socket=wayland",
```
````
2021-06-08 01:23:25 +05:30
2023-06-20 00:43:36 +05:30
When applied, the suggestion replaces from 3 lines above to 4 lines below the commented line:
2021-06-08 01:23:25 +05:30
![Multi-line suggestion preview](img/multi-line-suggestion-preview.png)
Suggestions for multiple lines are limited to 100 lines _above_ and 100
lines _below_ the commented diff line. This allows for up to 200 changed lines per
suggestion.
2023-05-27 22:25:52 +05:30
## Apply suggestions
2023-06-20 00:43:36 +05:30
Prerequisites:
- You must be the author of the merge request, or have at least the Developer role in the project.
To apply suggested changes directly from the merge request:
2023-05-27 22:25:52 +05:30
1. On the top bar, select **Main menu > Projects** and find your project.
1. On the left sidebar, select **Merge requests** and find your merge request.
1. Find the comment containing the suggestion you want to apply.
- To apply suggestions individually, select **Apply suggestion**.
- To apply multiple suggestions in a single commit, select **Add suggestion to batch**.
1. Optional. Provide a custom commit message to describe your change. If you don't provide a custom message, the default commit message is used.
1. Select **Apply**.
After a suggestion is applied:
- The suggestion is marked as **Applied**.
- The comment thread is resolved.
- GitLab creates a new commit with the changes.
- If the user has the Developer role, GitLab pushes
the suggested change directly into the codebase in the merge request's branch.
## Nest code blocks in suggestions
2021-06-08 01:23:25 +05:30
2021-12-11 22:18:48 +05:30
To add a suggestion that includes a
[fenced code block](../../../markdown.md#code-spans-and-blocks), wrap your suggestion
in four backticks instead of three:
2021-06-08 01:23:25 +05:30
2023-05-27 22:25:52 +05:30
`````markdown
2023-03-04 22:38:38 +05:30
````suggestion:-0+2
```shell
git config --global receive.advertisepushoptions true
```
````
2023-05-27 22:25:52 +05:30
`````
2021-06-08 01:23:25 +05:30
![Output of a comment with a suggestion with a fenced code block](img/suggestion_code_block_output_v12_8.png)
## Configure the commit message for applied suggestions
2023-05-27 22:25:52 +05:30
GitLab uses a default commit message when applying suggestions. This message
supports placeholders, and can be changed. For example, the default message
`Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)` renders
like this if you apply three suggestions to two different files:
2021-06-08 01:23:25 +05:30
2023-05-27 22:25:52 +05:30
```plaintext
Apply 3 suggestion(s) to 2 file(s)
```
2022-07-23 23:45:48 +05:30
2023-05-27 22:25:52 +05:30
Merge requests created from forks use the template defined in the target project.
2021-06-08 01:23:25 +05:30
2023-05-27 22:25:52 +05:30
To meet your project's needs, you can customize these messages and include other
placeholder variables:
2022-07-23 23:45:48 +05:30
2023-05-27 22:25:52 +05:30
1. On the top bar, select **Main menu > Projects** and find your project.
1. On the left sidebar, select **Settings > Merge requests**.
1. Scroll to **Merge suggestions**, and alter the text to meet your needs.
See [Supported variables](#supported-variables) for a list of placeholders
you can use in this message.
2021-06-08 01:23:25 +05:30
2023-05-27 22:25:52 +05:30
### Supported variables
2021-06-08 01:23:25 +05:30
2023-05-27 22:25:52 +05:30
The template for commit messages for applied suggestions supports these variables:
2021-06-08 01:23:25 +05:30
| Variable | Description | Output example |
|------------------------|-------------|----------------|
2021-12-11 22:18:48 +05:30
| `%{branch_name}` | The name of the branch to which suggestions were applied. | `my-feature-branch` |
2023-05-27 22:25:52 +05:30
| `%{files_count}` | The number of files to which suggestions were applied.| `2` |
2021-12-11 22:18:48 +05:30
| `%{file_paths}` | The paths of the file to which suggestions were applied. Paths are separated by commas.| `docs/index.md, docs/about.md` |
2021-06-08 01:23:25 +05:30
| `%{project_path}` | The project path. | `my-group/my-project` |
2023-05-27 22:25:52 +05:30
| `%{project_name}` | The human-readable name of the project. | `My Project` |
| `%{suggestions_count}` | The number of suggestions applied.| `3` |
2021-12-11 22:18:48 +05:30
| `%{username}` | The username of the user applying suggestions. | `user_1` |
2023-05-27 22:25:52 +05:30
| `%{user_full_name}` | The full name of the user applying suggestions. | `User 1` |
2021-06-08 01:23:25 +05:30
For example, to customize the commit message to output
2023-05-27 22:25:52 +05:30
`Addresses user_1's review`, set the custom text to
2021-06-08 01:23:25 +05:30
`Addresses %{username}'s review`.
## Batch suggestions
2023-06-20 00:43:36 +05:30
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/25486) in GitLab 13.1 as an [Experiment](../../../../policy/alpha-beta-support.md#experiment) with a flag named `batch_suggestions`, disabled by default.
2021-06-08 01:23:25 +05:30
> - [Enabled by default](https://gitlab.com/gitlab-org/gitlab/-/issues/227799) in GitLab 13.2.
2021-12-11 22:18:48 +05:30
> - [Generally available](https://gitlab.com/gitlab-org/gitlab/-/issues/320755) in GitLab 13.11. [Feature flag `batch_suggestions`](https://gitlab.com/gitlab-org/gitlab/-/issues/320755) removed.
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/326168) custom commit messages for batch suggestions in GitLab 14.4.
2021-06-08 01:23:25 +05:30
2023-06-20 00:43:36 +05:30
To reduce the number of commits added to your branch, you can apply multiple
suggestions in a single commit.
2021-06-08 01:23:25 +05:30
2023-06-20 00:43:36 +05:30
1. On the top bar, select **Main menu > Projects** and find your project.
1. On the left sidebar, select **Merge requests** and find your merge request.
1. For each suggestion you want to apply, and select **Add suggestion to batch**.
1. Optional. To remove a suggestion, select **Remove from batch**.
1. After you add your desired suggestions, select **Apply suggestions**.
WARNING:
If you apply a batch of suggestions containing changes from multiple authors,
you are credited as the resulting commit's author. If your project is configured
to [prevent approvals from users who add commits](../approvals/settings.md#prevent-approvals-by-users-who-add-commits), you are no longer an eligible
approver for this merge request.
1. Optional. Provide a custom commit message for [batch suggestions](#batch-suggestions)
(GitLab 14.4 and later) to describe your change. If you don't specify one,
the default commit message is used.
2021-09-04 01:27:46 +05:30
2022-01-26 12:08:38 +05:30
## Related topics
2021-09-04 01:27:46 +05:30
- [Suggestions API](../../../../api/suggestions.md)