Commit 3ca75a1f authored by Shinya Maeda's avatar Shinya Maeda

Merge branch 'ali/refactor-deployment-approval-service-params' into 'master'

Refactor Deployments::ApprovalService interface

See merge request gitlab-org/gitlab!82455
parents 1dfa4fcf 2942221e
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
module Deployments module Deployments
class ApprovalService < ::BaseService class ApprovalService < ::BaseService
def execute(deployment:, status:, comment: nil) def execute(deployment, status)
error_message = validate(deployment, status) error_message = validate(deployment, status)
return error(error_message) if error_message return error(error_message) if error_message
approval = upsert_approval(deployment, status, comment) approval = upsert_approval(deployment, status, params[:comment])
return error(approval.errors.full_messages) if approval.errors.any? return error(approval.errors.full_messages) if approval.errors.any?
process_build!(deployment, approval) process_build!(deployment, approval)
......
...@@ -22,8 +22,8 @@ module EE ...@@ -22,8 +22,8 @@ module EE
post ':id/deployments/:deployment_id/approval' do post ':id/deployments/:deployment_id/approval' do
deployment = user_project.deployments.find(params[:deployment_id]) deployment = user_project.deployments.find(params[:deployment_id])
result = ::Deployments::ApprovalService.new(user_project, current_user) result = ::Deployments::ApprovalService.new(user_project, current_user, declared_params(include_missing: false))
.execute(deployment: deployment, status: params[:status], comment: params[:comment]) .execute(deployment, params[:status])
if result[:status] == :success if result[:status] == :success
present(result[:approval], with: ::API::Entities::Deployments::Approval, current_user: current_user) present(result[:approval], with: ::API::Entities::Deployments::Approval, current_user: current_user)
......
...@@ -5,7 +5,8 @@ require 'spec_helper' ...@@ -5,7 +5,8 @@ require 'spec_helper'
RSpec.describe Deployments::ApprovalService do RSpec.describe Deployments::ApprovalService do
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let(:service) { described_class.new(project, user) } let(:service) { described_class.new(project, user, params) }
let(:params) { { comment: comment } }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:environment) { create(:environment, project: project) } let(:environment) { create(:environment, project: project) }
let(:status) { 'approved' } let(:status) { 'approved' }
...@@ -28,7 +29,7 @@ RSpec.describe Deployments::ApprovalService do ...@@ -28,7 +29,7 @@ RSpec.describe Deployments::ApprovalService do
end end
shared_examples_for 'reject' do shared_examples_for 'reject' do
it 'rejects the deployment' do it 'rejects the deployment', :aggregate_failures do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:approval].status).to eq('rejected') expect(subject[:approval].status).to eq('rejected')
expect(subject[:approval].user).to eq(user) expect(subject[:approval].user).to eq(user)
...@@ -39,7 +40,7 @@ RSpec.describe Deployments::ApprovalService do ...@@ -39,7 +40,7 @@ RSpec.describe Deployments::ApprovalService do
end end
shared_examples_for 'approve' do shared_examples_for 'approve' do
it 'approves the deployment' do it 'approves the deployment', :aggregate_failures do
expect(subject[:status]).to eq(:success) expect(subject[:status]).to eq(:success)
expect(subject[:approval].status).to eq('approved') expect(subject[:approval].status).to eq('approved')
expect(subject[:approval].user).to eq(user) expect(subject[:approval].user).to eq(user)
...@@ -61,7 +62,7 @@ RSpec.describe Deployments::ApprovalService do ...@@ -61,7 +62,7 @@ RSpec.describe Deployments::ApprovalService do
end end
describe '#execute' do describe '#execute' do
subject { service.execute(deployment: deployment, status: status, comment: comment) } subject { service.execute(deployment, status) }
context 'when status is approved' do context 'when status is approved' do
include_examples 'approve' include_examples 'approve'
...@@ -76,17 +77,21 @@ RSpec.describe Deployments::ApprovalService do ...@@ -76,17 +77,21 @@ RSpec.describe Deployments::ApprovalService do
end end
context 'when user already approved' do context 'when user already approved' do
let(:comment) { 'Changed comment' } let(:comment) { 'Original comment' }
before do before do
service.execute(deployment: deployment, status: :approved, comment: 'Original comment') service.execute(deployment, :approved)
end end
context 'and is approving again' do context 'and is approving again' do
include_examples 'approve' include_examples 'approve'
it 'does not change the comment' do context 'with a different comment' do
expect(subject[:approval].comment).to eq('Original comment') it 'does not change the comment' do
service = described_class.new(project, user, params.merge(comment: 'Changed comment'))
expect(service.execute(deployment, status)[:approval].comment).to eq('Original comment')
end
end end
end end
...@@ -95,8 +100,12 @@ RSpec.describe Deployments::ApprovalService do ...@@ -95,8 +100,12 @@ RSpec.describe Deployments::ApprovalService do
include_examples 'reject' include_examples 'reject'
it 'saves the changed comment' do context 'with a different comment' do
expect(subject[:approval].comment).to eq('Changed comment') it 'changes the comment' do
service = described_class.new(project, user, params.merge(comment: 'Changed comment'))
expect(service.execute(deployment, status)[:approval].comment).to eq('Changed comment')
end
end end
end end
end end
......
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