Class: Escobar::Heroku::BuildRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/escobar/heroku/build_request.rb

Overview

Class representing a heroku build request

Defined Under Namespace

Classes: Error, MissingContextsError, RequiresTwoFactorError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline, app_id) ⇒ BuildRequest

Returns a new instance of BuildRequest.



38
39
40
41
# File 'lib/escobar/heroku/build_request.rb', line 38

def initialize(pipeline, app_id)
  @app_id   = app_id
  @pipeline = pipeline
end

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



34
35
36
# File 'lib/escobar/heroku/build_request.rb', line 34

def app_id
  @app_id
end

#custom_payloadObject

Returns the value of attribute custom_payload.



36
37
38
# File 'lib/escobar/heroku/build_request.rb', line 36

def custom_payload
  @custom_payload
end

#environmentObject

Returns the value of attribute environment.



36
37
38
# File 'lib/escobar/heroku/build_request.rb', line 36

def environment
  @environment
end

#forcedObject

Returns the value of attribute forced.



36
37
38
# File 'lib/escobar/heroku/build_request.rb', line 36

def forced
  @forced
end

#github_deployment_urlObject (readonly)

Returns the value of attribute github_deployment_url.



34
35
36
# File 'lib/escobar/heroku/build_request.rb', line 34

def github_deployment_url
  @github_deployment_url
end

#pipelineObject (readonly)

Returns the value of attribute pipeline.



34
35
36
# File 'lib/escobar/heroku/build_request.rb', line 34

def pipeline
  @pipeline
end

#refObject

Returns the value of attribute ref.



36
37
38
# File 'lib/escobar/heroku/build_request.rb', line 36

def ref
  @ref
end

#shaObject (readonly)

Returns the value of attribute sha.



34
35
36
# File 'lib/escobar/heroku/build_request.rb', line 34

def sha
  @sha
end

Instance Method Details

#appObject



43
44
45
# File 'lib/escobar/heroku/build_request.rb', line 43

def app
  @app ||= Escobar::Heroku::App.new(pipeline.client, app_id)
end

#cache_keyObject



51
52
53
# File 'lib/escobar/heroku/build_request.rb', line 51

def cache_key
  app.cache_key
end

#create(task, environment, ref, forced, custom_payload) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/escobar/heroku/build_request.rb', line 55

def create(task, environment, ref, forced, custom_payload)
  raise_2fa_error if app.locked?

  @environment = environment
  @ref = ref
  @forced = forced
  @custom_payload = custom_payload

  create_in_api(task)
end

#create_deployment_status(url, payload) ⇒ Object



140
141
142
# File 'lib/escobar/heroku/build_request.rb', line 140

def create_deployment_status(url, payload)
  github_client.create_deployment_status(url, payload)
end

#create_github_deployment(task) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/escobar/heroku/build_request.rb', line 127

def create_github_deployment(task)
  options = {
    ref: ref,
    task: task,
    auto_merge: !forced,
    payload: custom_payload.merge(custom_deployment_payload),
    environment: environment,
    required_contexts: required_commit_contexts
  }
  response = github_client.create_deployment(options)
  handle_github_deployment_response(response)
end

#create_github_deployment_status(url, target_url, state, description) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/escobar/heroku/build_request.rb', line 153

def create_github_deployment_status(url, target_url, state, description)
  payload = {
    state: state,
    target_url: target_url,
    description: description
  }
  create_deployment_status(url, payload)
end

#create_github_pending_deployment_status(heroku_build) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/escobar/heroku/build_request.rb', line 144

def create_github_pending_deployment_status(heroku_build)
  create_github_deployment_status(
    github_deployment_url,
    heroku_build.dashboard_build_output_url,
    "pending",
    "Build running."
  )
end

#create_heroku_buildObject



98
99
100
101
102
103
104
105
106
107
# File 'lib/escobar/heroku/build_request.rb', line 98

def create_heroku_build
  body = {
    source_blob: {
      url: github_client.archive_link(sha),
      version: sha,
      version_description: "#{pipeline.github_repository}:#{sha}"
    }
  }
  app.client.heroku.post("/apps/#{app.name}/builds", body)
end

#create_in_api(task) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/escobar/heroku/build_request.rb', line 71

def create_in_api(task)
  create_github_deployment(task)

  build = create_heroku_build
  if build["id"] =~ Escobar::UUID_REGEX
    process_heroku_build(build)
  else
    raise error_for(
      "Unable to create heroku build for #{app.name}: #{build['message']}"
    )
  end
end

#custom_deployment_payloadObject



162
163
164
# File 'lib/escobar/heroku/build_request.rb', line 162

def custom_deployment_payload
  { name: app.name, pipeline: pipeline.to_hash, provider: "slash-heroku" }
end

#error_for(message) ⇒ Object



47
48
49
# File 'lib/escobar/heroku/build_request.rb', line 47

def error_for(message)
  Error.new_from_build_request(self, message)
end

#github_clientObject



171
172
173
174
175
176
# File 'lib/escobar/heroku/build_request.rb', line 171

def github_client
  @github_client ||= Escobar::GitHub::Client.new(
    app.client.github_token,
    pipeline.github_repository
  )
end

#handle_github_deployment_error(response) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/escobar/heroku/build_request.rb', line 119

def handle_github_deployment_error(response)
  error = Escobar::GitHub::DeploymentError.new(
    pipeline.github_repository, response, required_commit_contexts
  )
  raise error_for(error.default_message) unless error.missing_contexts?
  raise MissingContextsError.new_from_build_request_and_error(self, error)
end

#handle_github_deployment_response(response) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/escobar/heroku/build_request.rb', line 109

def handle_github_deployment_response(response)
  unless response["sha"]
    handle_github_deployment_error(response)
  end

  @sha = response["sha"]
  @github_deployment_url = response["url"]
  response
end

#process_heroku_build(build) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/escobar/heroku/build_request.rb', line 84

def process_heroku_build(build)
  heroku_build = Escobar::Heroku::Build.new(
    pipeline.client, app_id, build["id"]
  )

  create_github_pending_deployment_status(heroku_build)

  heroku_build.github_url = github_deployment_url
  heroku_build.pipeline_name = pipeline.name
  heroku_build.sha = sha

  heroku_build
end

#raise_2fa_errorObject



66
67
68
69
# File 'lib/escobar/heroku/build_request.rb', line 66

def raise_2fa_error
  message = "Application requires second factor: #{app.name}"
  raise RequiresTwoFactorError.new_from_build_request(self, message)
end

#required_commit_contextsObject



166
167
168
169
# File 'lib/escobar/heroku/build_request.rb', line 166

def required_commit_contexts
  return [] if forced
  pipeline.required_commit_contexts(false)
end