debian-mirror-gitlab/app/services/deployments/create_service.rb

49 lines
1.3 KiB
Ruby
Raw Permalink Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
module Deployments
class CreateService
attr_reader :environment, :current_user, :params
def initialize(environment, current_user, params)
@environment = environment
@current_user = current_user
@params = params
end
def execute
2021-03-11 19:13:27 +05:30
return last_deployment if last_deployment&.equal_to?(params)
2020-01-01 13:55:28 +05:30
environment.deployments.build(deployment_attributes).tap do |deployment|
# Deployment#change_status already saves the model, so we only need to
# call #save ourselves if no status is provided.
if (status = params[:status])
deployment.update_status(status)
else
deployment.save
end
2019-12-21 20:55:43 +05:30
end
end
def deployment_attributes
# We use explicit parameters here so we never by accident allow parameters
# to be set that one should not be able to set (e.g. the row ID).
{
cluster_id: environment.deployment_platform&.cluster_id,
project_id: environment.project_id,
environment_id: environment.id,
ref: params[:ref],
tag: params[:tag],
sha: params[:sha],
user: current_user,
2020-01-01 13:55:28 +05:30
on_stop: params[:on_stop]
2019-12-21 20:55:43 +05:30
}
end
2021-03-11 19:13:27 +05:30
private
def last_deployment
@environment.last_deployment
end
2019-12-21 20:55:43 +05:30
end
end