2019-10-16 14:08:01 -04:00
|
|
|
# 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-01-19 13:11:04 -05:00
|
|
|
return last_deployment if last_deployment&.equal_to?(params)
|
|
|
|
|
2019-12-14 10:07:56 -05:00
|
|
|
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-10-16 14:08:01 -04:00
|
|
|
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,
|
2019-12-14 10:07:56 -05:00
|
|
|
on_stop: params[:on_stop]
|
2019-10-16 14:08:01 -04:00
|
|
|
}
|
|
|
|
end
|
2021-01-19 13:11:04 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def last_deployment
|
|
|
|
@environment.last_deployment
|
|
|
|
end
|
2019-10-16 14:08:01 -04:00
|
|
|
end
|
|
|
|
end
|