diff --git a/app/controllers/projects/merge_requests_controller.rb b/app/controllers/projects/merge_requests_controller.rb index 036fde876198490a654f66e25329a6b6f8288df0..5a2136303c92c033699a68348f5315a3f34ca1a7 100644 --- a/app/controllers/projects/merge_requests_controller.rb +++ b/app/controllers/projects/merge_requests_controller.rb @@ -302,9 +302,13 @@ class Projects::MergeRequestsController < Projects::ApplicationController end def cancel_merge_when_build_succeeds - return access_denied! unless @merge_request.can_cancel_merge_when_build_succeeds?(current_user) + unless @merge_request.can_cancel_merge_when_build_succeeds?(current_user) + return access_denied! + end - MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user).cancel(@merge_request) + MergeRequests::MergeWhenPipelineSucceedsService + .new(@project, current_user) + .cancel(@merge_request) end def merge @@ -331,8 +335,9 @@ class Projects::MergeRequestsController < Projects::ApplicationController end if @merge_request.pipeline.active? - MergeRequests::MergeWhenBuildSucceedsService.new(@project, current_user, merge_params) - .execute(@merge_request) + MergeRequests::MergeWhenPipelineSucceedsService + .new(@project, current_user, merge_params) + .execute(@merge_request) @status = :merge_when_build_succeeds elsif @merge_request.pipeline.success? # This can be triggered when a user clicks the auto merge button while diff --git a/app/services/merge_requests/merge_when_build_succeeds_service.rb b/app/services/merge_requests/merge_when_pipeline_succeeds_service.rb similarity index 95% rename from app/services/merge_requests/merge_when_build_succeeds_service.rb rename to app/services/merge_requests/merge_when_pipeline_succeeds_service.rb index dc159de00581d2dd9608db3794b7cc99ea1f99fe..5616edf8b4a303db8b65cf37dc59ded8a1270e2a 100644 --- a/app/services/merge_requests/merge_when_build_succeeds_service.rb +++ b/app/services/merge_requests/merge_when_pipeline_succeeds_service.rb @@ -1,5 +1,5 @@ module MergeRequests - class MergeWhenBuildSucceedsService < MergeRequests::BaseService + class MergeWhenPipelineSucceedsService < MergeRequests::BaseService # Marks the passed `merge_request` to be merged when the build succeeds or # updates the params for the automatic merge def execute(merge_request) diff --git a/app/workers/pipeline_success_worker.rb b/app/workers/pipeline_success_worker.rb index 2aa6fff24da1878615bad011dfcc9e4a2f9942e1..cc0eb708cf9c1e79aba0bbd72c94de96bb5b23f3 100644 --- a/app/workers/pipeline_success_worker.rb +++ b/app/workers/pipeline_success_worker.rb @@ -4,7 +4,7 @@ class PipelineSuccessWorker def perform(pipeline_id) Ci::Pipeline.find_by(id: pipeline_id).try do |pipeline| - MergeRequests::MergeWhenBuildSucceedsService + MergeRequests::MergeWhenPipelineSucceedsService .new(pipeline.project, nil) .trigger(pipeline) end diff --git a/lib/api/merge_requests.rb b/lib/api/merge_requests.rb index 4176c7eec0676e5ac7037644d1f9072968f2a2c9..8c55f09a9ce299a2354ce3ba3d1c756252d1377d 100644 --- a/lib/api/merge_requests.rb +++ b/lib/api/merge_requests.rb @@ -205,11 +205,13 @@ module API } if params[:merge_when_build_succeeds] && merge_request.pipeline && merge_request.pipeline.active? - ::MergeRequests::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user, merge_params). - execute(merge_request) + ::MergeRequests::MergeWhenPipelineSucceedsService + .new(merge_request.target_project, current_user, merge_params) + .execute(merge_request) else - ::MergeRequests::MergeService.new(merge_request.target_project, current_user, merge_params). - execute(merge_request) + ::MergeRequests::MergeService + .new(merge_request.target_project, current_user, merge_params) + .execute(merge_request) end present merge_request, with: Entities::MergeRequest, current_user: current_user, project: user_project @@ -223,7 +225,9 @@ module API unauthorized! unless merge_request.can_cancel_merge_when_build_succeeds?(current_user) - ::MergeRequest::MergeWhenBuildSucceedsService.new(merge_request.target_project, current_user).cancel(merge_request) + ::MergeRequest::MergeWhenPipelineSucceedsService + .new(merge_request.target_project, current_user) + .cancel(merge_request) end desc 'Get the comments of a merge request' do diff --git a/spec/controllers/projects/merge_requests_controller_spec.rb b/spec/controllers/projects/merge_requests_controller_spec.rb index 1d0750d17192767c86fe3bfe86b939cf7643fa1d..9e0b80205d8f80c4e8d0d2ab6abb6853ee8cc529 100644 --- a/spec/controllers/projects/merge_requests_controller_spec.rb +++ b/spec/controllers/projects/merge_requests_controller_spec.rb @@ -292,7 +292,9 @@ describe Projects::MergeRequestsController do it 'sets the MR to merge when the build succeeds' do service = double(:merge_when_build_succeeds_service) - expect(MergeRequests::MergeWhenBuildSucceedsService).to receive(:new).with(project, anything, anything).and_return(service) + expect(MergeRequests::MergeWhenPipelineSucceedsService) + .to receive(:new).with(project, anything, anything) + .and_return(service) expect(service).to receive(:execute).with(merge_request) merge_when_build_succeeds diff --git a/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb b/spec/services/merge_requests/merge_when_pipeline_succeeds_service_spec.rb similarity index 92% rename from spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb rename to spec/services/merge_requests/merge_when_pipeline_succeeds_service_spec.rb index 1f90efdbd6af69f2dc36bf8393cc2a670479bcb0..793806bd69a00a1046d5a15d3b888483e2186b30 100644 --- a/spec/services/merge_requests/merge_when_build_succeeds_service_spec.rb +++ b/spec/services/merge_requests/merge_when_pipeline_succeeds_service_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe MergeRequests::MergeWhenBuildSucceedsService do +describe MergeRequests::MergeWhenPipelineSucceedsService do let(:user) { create(:user) } let(:project) { create(:project) } @@ -10,8 +10,14 @@ describe MergeRequests::MergeWhenBuildSucceedsService do source_project: project, target_project: project, state: "opened") end - let(:pipeline) { create(:ci_pipeline_with_one_job, ref: mr_merge_if_green_enabled.source_branch, project: project) } - let(:service) { MergeRequests::MergeWhenBuildSucceedsService.new(project, user, commit_message: 'Awesome message') } + let(:pipeline) do + create(:ci_pipeline_with_one_job, ref: mr_merge_if_green_enabled.source_branch, + project: project) + end + + let(:service) do + described_class.new(project, user, commit_message: 'Awesome message') + end describe "#execute" do let(:merge_request) do @@ -39,7 +45,7 @@ describe MergeRequests::MergeWhenBuildSucceedsService do end context 'already approved' do - let(:service) { MergeRequests::MergeWhenBuildSucceedsService.new(project, user, new_key: true) } + let(:service) { described_class.new(project, user, new_key: true) } let(:build) { create(:ci_build, ref: mr_merge_if_green_enabled.source_branch) } before do diff --git a/spec/workers/pipeline_success_worker_spec.rb b/spec/workers/pipeline_success_worker_spec.rb index 5e31cc2c8e7754ccc2b61fb032ef0c62f6c756cc..d1c84adda6f9fb91aa5b08f9919e5e0e58a9f98c 100644 --- a/spec/workers/pipeline_success_worker_spec.rb +++ b/spec/workers/pipeline_success_worker_spec.rb @@ -7,7 +7,7 @@ describe PipelineSuccessWorker do it 'performs "merge when pipeline succeeds"' do expect_any_instance_of( - MergeRequests::MergeWhenBuildSucceedsService + MergeRequests::MergeWhenPipelineSucceedsService ).to receive(:trigger) described_class.new.perform(pipeline.id)