Class: Deployments::CreateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/deployments/create_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment, current_user, params) ⇒ CreateService

Returns a new instance of CreateService.



7
8
9
10
11
# File 'app/services/deployments/create_service.rb', line 7

def initialize(environment, current_user, params)
  @environment = environment
  @current_user = current_user
  @params = params
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



5
6
7
# File 'app/services/deployments/create_service.rb', line 5

def current_user
  @current_user
end

#environmentObject (readonly)

Returns the value of attribute environment.



5
6
7
# File 'app/services/deployments/create_service.rb', line 5

def environment
  @environment
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'app/services/deployments/create_service.rb', line 5

def params
  @params
end

Instance Method Details

#deployment_attributesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/deployments/create_service.rb', line 27

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,
    on_stop: params[:on_stop]
  }
end

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/deployments/create_service.rb', line 13

def execute
  return last_deployment if last_deployment&.equal_to?(params)

  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
  end
end