Class: Escobar::Heroku::Pipeline

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

Overview

Class reperesenting a Heroku Pipeline

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, id, name) ⇒ Pipeline

Returns a new instance of Pipeline.



6
7
8
9
10
# File 'lib/escobar/heroku/pipeline.rb', line 6

def initialize(client, id, name)
  @id           = id
  @name         = name
  @client       = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/escobar/heroku/pipeline.rb', line 5

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/escobar/heroku/pipeline.rb', line 5

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/escobar/heroku/pipeline.rb', line 5

def name
  @name
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/escobar/heroku/pipeline.rb', line 40

def configured?
  couplings.any? && github_repository
end

#couplingsObject



49
50
51
# File 'lib/escobar/heroku/pipeline.rb', line 49

def couplings
  @couplings ||= remote_couplings_without_test_stage
end

#create_deployment(ref, environment, force = false, payload = {}) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/escobar/heroku/pipeline.rb', line 122

def create_deployment(ref, environment, force = false, payload = {})
  heroku_app = default_heroku_application(environment)

  build_request = heroku_app.build_request_for(self)
  heroku_build = build_request.create(
    "deploy", environment, ref, force, payload
  )

  heroku_build
end

#create_deployment_status(url, payload) ⇒ Object



180
181
182
# File 'lib/escobar/heroku/pipeline.rb', line 180

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

#default_branchObject



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

def default_branch
  github_client.default_branch
end

#default_branch_settings_uriObject



85
86
87
88
# File 'lib/escobar/heroku/pipeline.rb', line 85

def default_branch_settings_uri
  "https://github.com/#{github_repository}/" \
    "settings/branches/#{default_branch}"
end

#default_environmentObject



63
64
65
# File 'lib/escobar/heroku/pipeline.rb', line 63

def default_environment
  sorted_environments.last
end

#default_heroku_application(environment) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/escobar/heroku/pipeline.rb', line 108

def default_heroku_application(environment)
  app = environments[environment] && environments[environment].first
  unless app
    raise ArgumentError, "No '#{environment}' environment for #{name}."
  end
  app.app
end

#environment_hashObject



24
25
26
27
28
29
# File 'lib/escobar/heroku/pipeline.rb', line 24

def environment_hash
  sorted_environments.each_with_object({}) do |environment, sum|
    sum[environment.to_sym] = environments[environment].map(&:to_hash)
    sum
  end
end

#environmentsObject



16
17
18
19
20
21
22
# File 'lib/escobar/heroku/pipeline.rb', line 16

def environments
  @environments ||= couplings.each_with_object({}) do |part, sum|
    sum[part.stage] ||= []
    sum[part.stage].push(part)
    sum
  end
end

#get(path) ⇒ Object

rubocop:enable Metrics/ParameterLists rubocop:enable Metrics/LineLength



150
151
152
153
154
155
156
157
158
# File 'lib/escobar/heroku/pipeline.rb', line 150

def get(path)
  response = kolkrabbi_client.get do |request|
    request.url path
    request.headers["Content-Type"]  = "application/json"
    request.headers["Authorization"] = "Bearer #{client.heroku.token}"
  end

  JSON.parse(response.body)
end

#github_repositoryObject



44
45
46
47
# File 'lib/escobar/heroku/pipeline.rb', line 44

def github_repository
  remote_repository["repository"] &&
    remote_repository["repository"]["name"]
end

#heroku_application_by_environment_and_name(stage, name) ⇒ Object



116
117
118
119
120
# File 'lib/escobar/heroku/pipeline.rb', line 116

def heroku_application_by_environment_and_name(stage, name)
  environments[stage].find do |app|
    return app.app if name == app.app.name
  end
end


81
82
83
# File 'lib/escobar/heroku/pipeline.rb', line 81

def heroku_permalink
  "https://dashboard.heroku.com/pipelines/#{id}"
end

#kolkrabbi_clientObject



160
161
162
163
164
165
166
# File 'lib/escobar/heroku/pipeline.rb', line 160

def kolkrabbi_client
  @kolkrabbi ||= if Escobar.zipkin_enabled?
                   kolkrabbi_zipkin_client
                 else
                   kolkrabbi_default_client
                 end
end

#kolkrabbi_default_clientObject



176
177
178
# File 'lib/escobar/heroku/pipeline.rb', line 176

def kolkrabbi_default_client
  Faraday.new(url: "https://#{kolkrabbi_hostname}")
end

#kolkrabbi_hostnameObject



184
185
186
# File 'lib/escobar/heroku/pipeline.rb', line 184

def kolkrabbi_hostname
  ENV.fetch("KOLKRABBI_HOSTNAME", "kolkrabbi.heroku.com")
end

#kolkrabbi_zipkin_clientObject



168
169
170
171
172
173
174
# File 'lib/escobar/heroku/pipeline.rb', line 168

def kolkrabbi_zipkin_client
  Faraday.new(url: "https://#{kolkrabbi_hostname}") do |c|
    c.use :instrumentation
    c.use ZipkinTracer::FaradayHandler, kolkrabbi_hostname
    c.adapter Faraday.default_adapter
  end
end

#promote(source, targets, environment, force = false, payload = {}, second_factor = nil) ⇒ Object

source: A Escobar::Heroku::App to promote from targets: An array of Escobar::Heroku::App to promote to environment: The pipeline stage applying to the promotion force: true if github commit status checks shouldn’t be verified payload: Extra info to insert into the github deployment API second_factor: an OTP credential for protected resources rubocop:disable Metrics/ParameterLists rubocop:disable Metrics/LineLength



141
142
143
144
145
146
# File 'lib/escobar/heroku/pipeline.rb', line 141

def promote(source, targets, environment, force = false, payload = {}, second_factor = nil)
  promotion_request = Escobar::Heroku::PipelinePromotionRequest.new(
    client, self, source, targets, second_factor
  )
  promotion_request.create(environment, force, payload)
end

#reap_build(app_id, build_id) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/escobar/heroku/pipeline.rb', line 90

def reap_build(app_id, build_id)
  build = Escobar::Heroku::Build.new(client, app_id, build_id)
  case build.status
  when "succeeded", "failed"
    build
  end
end

#reap_release(app_id, build_id, release_id) ⇒ Object



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

def reap_release(app_id, build_id, release_id)
  release = Escobar::Heroku::Release.new(
    client, app_id, build_id, release_id
  )
  case release.status
  when "succeeded", "failed"
    release
  end
end

#remote_couplingsObject



57
58
59
60
61
# File 'lib/escobar/heroku/pipeline.rb', line 57

def remote_couplings
  client.heroku.get("/pipelines/#{id}/pipeline-couplings").map do |pc|
    Escobar::Heroku::Coupling.new(client, pc)
  end
end

#remote_couplings_without_test_stageObject



53
54
55
# File 'lib/escobar/heroku/pipeline.rb', line 53

def remote_couplings_without_test_stage
  remote_couplings.reject { |coupling| coupling.stage == "test" }
end

#required_commit_contexts(forced = false) ⇒ Object



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

def required_commit_contexts(forced = false)
  return [] if forced
  github_client.required_contexts.map do |context|
    if context == "continuous-integration/travis-ci"
      context = "continuous-integration/travis-ci/push"
    end
    context
  end
end

#sorted_environmentsObject



12
13
14
# File 'lib/escobar/heroku/pipeline.rb', line 12

def sorted_environments
  environments.keys.sort
end

#to_hashObject



31
32
33
34
35
36
37
38
# File 'lib/escobar/heroku/pipeline.rb', line 31

def to_hash
  {
    id: id,
    name: name,
    github_repository: github_repository,
    environments: environment_hash
  }
end