Commit b3a28f75 authored by Mikołaj Wawrzyniak's avatar Mikołaj Wawrzyniak

Merge branch '25381-add-a-custom-commit-message-when-applying-a-suggested-change' into 'master'

Add a message parameter for the suggestion endpoints

See merge request gitlab-org/gitlab!51245
parents 610b8049 0e5b4448
...@@ -2,8 +2,9 @@ ...@@ -2,8 +2,9 @@
module Suggestions module Suggestions
class ApplyService < ::BaseService class ApplyService < ::BaseService
def initialize(current_user, *suggestions) def initialize(current_user, *suggestions, message: nil)
@current_user = current_user @current_user = current_user
@message = message
@suggestion_set = Gitlab::Suggestions::SuggestionSet.new(suggestions) @suggestion_set = Gitlab::Suggestions::SuggestionSet.new(suggestions)
end end
...@@ -47,7 +48,7 @@ module Suggestions ...@@ -47,7 +48,7 @@ module Suggestions
end end
def commit_message def commit_message
Gitlab::Suggestions::CommitMessage.new(current_user, suggestion_set).message Gitlab::Suggestions::CommitMessage.new(current_user, suggestion_set, @message).message
end end
end end
end end
---
title: Add a commit message parameter for the suggestion endpoints
merge_request: 51245
author:
type: added
...@@ -21,6 +21,7 @@ PUT /suggestions/:id/apply ...@@ -21,6 +21,7 @@ PUT /suggestions/:id/apply
| Attribute | Type | Required | Description | | Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- | | --------- | ---- | -------- | ----------- |
| `id` | integer/string | yes | The ID of a suggestion | | `id` | integer/string | yes | The ID of a suggestion |
| `commit_message` | string | no | A custom commit message to use instead of the default generated message or the project's default message |
```shell ```shell
curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/suggestions/5/apply" curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/suggestions/5/apply"
......
...@@ -12,12 +12,13 @@ module API ...@@ -12,12 +12,13 @@ module API
end end
params do params do
requires :id, type: String, desc: 'The suggestion ID' requires :id, type: String, desc: 'The suggestion ID'
optional :commit_message, type: String, desc: "A custom commit message to use instead of the default generated message or the project's default message"
end end
put ':id/apply' do put ':id/apply' do
suggestion = Suggestion.find_by_id(params[:id]) suggestion = Suggestion.find_by_id(params[:id])
if suggestion if suggestion
apply_suggestions(suggestion, current_user) apply_suggestions(suggestion, current_user, params[:commit_message])
else else
render_api_error!(_('Suggestion is not applicable as the suggestion was not found.'), :not_found) render_api_error!(_('Suggestion is not applicable as the suggestion was not found.'), :not_found)
end end
...@@ -28,6 +29,7 @@ module API ...@@ -28,6 +29,7 @@ module API
end end
params do params do
requires :ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: "An array of suggestion ID's" requires :ids, type: Array[Integer], coerce_with: ::API::Validations::Types::CommaSeparatedToIntegerArray.coerce, desc: "An array of suggestion ID's"
optional :commit_message, type: String, desc: "A custom commit message to use instead of the default generated message or the project's default message"
end end
put 'batch_apply' do put 'batch_apply' do
ids = params[:ids] ids = params[:ids]
...@@ -35,7 +37,7 @@ module API ...@@ -35,7 +37,7 @@ module API
suggestions = Suggestion.id_in(ids) suggestions = Suggestion.id_in(ids)
if suggestions.size == ids.length if suggestions.size == ids.length
apply_suggestions(suggestions, current_user) apply_suggestions(suggestions, current_user, params[:commit_message])
else else
render_api_error!(_('Suggestions are not applicable as one or more suggestions were not found.'), :not_found) render_api_error!(_('Suggestions are not applicable as one or more suggestions were not found.'), :not_found)
end end
...@@ -43,10 +45,10 @@ module API ...@@ -43,10 +45,10 @@ module API
end end
helpers do helpers do
def apply_suggestions(suggestions, current_user) def apply_suggestions(suggestions, current_user, message)
authorize_suggestions(*suggestions) authorize_suggestions(*suggestions)
result = ::Suggestions::ApplyService.new(current_user, *suggestions).execute result = ::Suggestions::ApplyService.new(current_user, *suggestions, message: message).execute
if result[:status] == :success if result[:status] == :success
present suggestions, with: Entities::Suggestion, current_user: current_user present suggestions, with: Entities::Suggestion, current_user: current_user
......
...@@ -6,14 +6,15 @@ module Gitlab ...@@ -6,14 +6,15 @@ module Gitlab
DEFAULT_SUGGESTION_COMMIT_MESSAGE = DEFAULT_SUGGESTION_COMMIT_MESSAGE =
'Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)' 'Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)'
def initialize(user, suggestion_set) def initialize(user, suggestion_set, custom_message = nil)
@user = user @user = user
@suggestion_set = suggestion_set @suggestion_set = suggestion_set
@custom_message = custom_message
end end
def message def message
project = suggestion_set.project project = suggestion_set.project
user_defined_message = project.suggestion_commit_message.presence user_defined_message = @custom_message.presence || project.suggestion_commit_message.presence
message = user_defined_message || DEFAULT_SUGGESTION_COMMIT_MESSAGE message = user_defined_message || DEFAULT_SUGGESTION_COMMIT_MESSAGE
Gitlab::StringPlaceholderReplacer Gitlab::StringPlaceholderReplacer
......
...@@ -72,6 +72,17 @@ RSpec.describe Gitlab::Suggestions::CommitMessage do ...@@ -72,6 +72,17 @@ RSpec.describe Gitlab::Suggestions::CommitMessage do
end end
end end
context 'when a custom commit message is specified' do
let(:message) { "i'm a project message. a user's custom message takes precedence over me :(" }
let(:custom_message) { "hello there! i'm a cool custom commit message." }
it 'shows the custom commit message' do
expect(Gitlab::Suggestions::CommitMessage
.new(user, suggestion_set, custom_message)
.message).to eq(custom_message)
end
end
context 'is specified and includes all placeholders' do context 'is specified and includes all placeholders' do
let(:message) do let(:message) do
'*** %{branch_name} %{files_count} %{file_paths} %{project_name} %{project_path} %{user_full_name} %{username} %{suggestions_count} ***' '*** %{branch_name} %{files_count} %{file_paths} %{project_name} %{project_path} %{user_full_name} %{username} %{suggestions_count} ***'
......
...@@ -65,6 +65,19 @@ RSpec.describe API::Suggestions do ...@@ -65,6 +65,19 @@ RSpec.describe API::Suggestions do
end end
end end
context 'when a custom commit message is included' do
it 'renders an ok response and returns json content' do
project.add_maintainer(user)
message = "cool custom commit message!"
put api(url, user), params: { commit_message: message }
expect(response).to have_gitlab_http_status(:ok)
expect(project.repository.commit.message).to eq(message)
end
end
context 'when not able to apply patch' do context 'when not able to apply patch' do
let(:url) { "/suggestions/#{unappliable_suggestion.id}/apply" } let(:url) { "/suggestions/#{unappliable_suggestion.id}/apply" }
...@@ -113,9 +126,11 @@ RSpec.describe API::Suggestions do ...@@ -113,9 +126,11 @@ RSpec.describe API::Suggestions do
let(:url) { "/suggestions/batch_apply" } let(:url) { "/suggestions/batch_apply" }
context 'when successfully applies multiple patches as a batch' do context 'when successfully applies multiple patches as a batch' do
it 'renders an ok response and returns json content' do before do
project.add_maintainer(user) project.add_maintainer(user)
end
it 'renders an ok response and returns json content' do
put api(url, user), params: { ids: [suggestion.id, suggestion2.id] } put api(url, user), params: { ids: [suggestion.id, suggestion2.id] }
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
...@@ -123,6 +138,16 @@ RSpec.describe API::Suggestions do ...@@ -123,6 +138,16 @@ RSpec.describe API::Suggestions do
'appliable', 'applied', 'appliable', 'applied',
'from_content', 'to_content')) 'from_content', 'to_content'))
end end
it 'provides a custom commit message' do
message = "cool custom commit message!"
put api(url, user), params: { ids: [suggestion.id, suggestion2.id],
commit_message: message }
expect(response).to have_gitlab_http_status(:ok)
expect(project.repository.commit.message).to eq(message)
end
end end
context 'when not able to apply one or more of the patches' do context 'when not able to apply one or more of the patches' do
......
...@@ -32,8 +32,8 @@ RSpec.describe Suggestions::ApplyService do ...@@ -32,8 +32,8 @@ RSpec.describe Suggestions::ApplyService do
create(:suggestion, :content_from_repo, suggestion_args) create(:suggestion, :content_from_repo, suggestion_args)
end end
def apply(suggestions) def apply(suggestions, custom_message = nil)
result = apply_service.new(user, *suggestions).execute result = apply_service.new(user, *suggestions, message: custom_message).execute
suggestions.map { |suggestion| suggestion.reload } suggestions.map { |suggestion| suggestion.reload }
...@@ -111,6 +111,16 @@ RSpec.describe Suggestions::ApplyService do ...@@ -111,6 +111,16 @@ RSpec.describe Suggestions::ApplyService do
end end
end end
end end
context 'with a user suggested commit message' do
let(:message) { "i'm a custom commit message!" }
it "uses the user's commit message" do
apply(suggestions, message)
expect(project.repository.commit.message).to(eq(message))
end
end
end end
subject(:apply_service) { described_class } subject(:apply_service) { described_class }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment