Commit e7617820 authored by Gary Holtz's avatar Gary Holtz

Adding a 'message' parameter to the suggestion endpoints

* This adds the ability to add a message to the suggestion apply and
batch_apply endpoints.
* This is part of a number of MRs towards
https://gitlab.com/gitlab-org/gitlab/-/issues/25381
parent 96492111
...@@ -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
...@@ -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 :message, type: String, desc: 'A commit message to use instead of the default generated message or project 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[: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 :message, type: String, desc: 'A commit message to use instead of the default generated message or project 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[: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 || 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: { 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],
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: 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