escobar 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 923902c750e029b0b46ecdda4bff5eb2960846b0
4
- data.tar.gz: 4a95e5cc637b28ed6f0b38e19edca6e6cddd595c
3
+ metadata.gz: 43c11ea0237775c7cc976d2e875e1c65d7529e38
4
+ data.tar.gz: 84133f99aa2ec696eeb4a1c407972894b721f2a1
5
5
  SHA512:
6
- metadata.gz: b28917db0f1ef6ca05efb1d53a3f135fdd6b6e3ac04741a129e963d8d0f4f131bf7a5f5c5202eb07254f0d6bbdb63ebba299373239bfe3ccf828b3efa3c311ea
7
- data.tar.gz: 2b370d85d2f1137566dd50c7b5131c56a9f47ccbeaeddda04ec8a20b3dcb55647b91091ea40dc144646b0e53e23b1904f9a81cd39950f15b970d07c51986f483
6
+ metadata.gz: 3186ae4bc9555ff17682e04442b02a93d514f557d22b5e41a1880a935aec31812232cafadf0277b51123811439024ac3cb91beac10feb31373ec5221f415cc0e
7
+ data.tar.gz: 3db647aee08e12fcb1a48b5ffc8f28d0b64fcde32b99b270245dbe1160b76168fbcb2298f917efce219a9b187d69f95d906f83fd4c5538ffec49ae043ead371e
@@ -50,6 +50,7 @@ require_relative "./escobar/client"
50
50
  require_relative "./escobar/github/client"
51
51
  require_relative "./escobar/heroku/app"
52
52
  require_relative "./escobar/heroku/build"
53
+ require_relative "./escobar/heroku/build_request"
53
54
  require_relative "./escobar/heroku/client"
54
55
  require_relative "./escobar/heroku/coupling"
55
56
  require_relative "./escobar/heroku/pipeline"
@@ -29,6 +29,10 @@ module Escobar
29
29
  response = client.heroku.get("/apps/#{id}/config-vars")
30
30
  response["id"] == "two_factor"
31
31
  end
32
+
33
+ def build_request_for(pipeline)
34
+ Escobar::Heroku::BuildRequest.new(pipeline, self)
35
+ end
32
36
  end
33
37
  end
34
38
  end
@@ -0,0 +1,154 @@
1
+ module Escobar
2
+ module Heroku
3
+ # Class representing a heroku build request
4
+ class BuildRequest
5
+ # Class representing some failure when requesting a build
6
+ class Error < StandardError
7
+ attr_accessor :build_request
8
+
9
+ def self.new_from_build_request(build_request, message)
10
+ error = new(message)
11
+ error.build_request = build_request
12
+ error
13
+ end
14
+
15
+ def dashboard_url
16
+ "https://dashboard.heroku.com/apps/#{build_request.app.name}"
17
+ end
18
+ end
19
+
20
+ attr_reader :app, :github_deployment_url, :pipeline, :sha
21
+
22
+ attr_accessor :environment, :ref, :forced, :custom_payload
23
+
24
+ def initialize(pipeline, app)
25
+ @app = app
26
+ @pipeline = pipeline
27
+ end
28
+
29
+ def error_for(message)
30
+ Error.new_from_build_request(self, message)
31
+ end
32
+
33
+ def create(task, environment, ref, forced, custom_payload)
34
+ if app.locked?
35
+ raise error_for("Application requires second factor: #{app.name}")
36
+ end
37
+
38
+ @environment = environment
39
+ @ref = ref
40
+ @forced = forced
41
+ @custom_payload = custom_payload
42
+
43
+ create_in_api(task)
44
+ end
45
+
46
+ def create_in_api(task)
47
+ create_github_deployment(task)
48
+
49
+ build = create_heroku_build
50
+ if build["id"] =~ Escobar::UUID_REGEX
51
+ process_heroku_build(build)
52
+ else
53
+ raise error_for(
54
+ "Unable to create heroku build for #{app.name}: #{build['message']}"
55
+ )
56
+ end
57
+ end
58
+
59
+ def process_heroku_build(build)
60
+ heroku_build = Escobar::Heroku::Build.new(
61
+ app.client, app, build["id"]
62
+ )
63
+
64
+ create_github_pending_deployment_status(heroku_build)
65
+
66
+ heroku_build.github_url = github_deployment_url
67
+ heroku_build.pipeline_name = pipeline.name
68
+ heroku_build.sha = sha
69
+
70
+ heroku_build
71
+ end
72
+
73
+ def create_heroku_build
74
+ body = {
75
+ source_blob: {
76
+ url: github_client.archive_link(sha),
77
+ version: sha[0..7],
78
+ version_description: "#{pipeline.github_repository}:#{sha}"
79
+ }
80
+ }
81
+ app.client.heroku.post("/apps/#{app.name}/builds", body)
82
+ end
83
+
84
+ def handle_github_deployment_response(response)
85
+ unless response["sha"]
86
+ raise error_for(
87
+ "Unable to create GitHub deployments for " \
88
+ "#{pipeline.github_repository}: #{response['message']}"
89
+ )
90
+ end
91
+
92
+ @sha = response["sha"]
93
+ @github_deployment_url = response["url"]
94
+ response
95
+ end
96
+
97
+ def create_github_deployment(task)
98
+ options = {
99
+ ref: ref,
100
+ task: task,
101
+ auto_merge: !forced,
102
+ payload: custom_payload.merge(custom_deployment_payload),
103
+ environment: environment,
104
+ required_contexts: required_commit_contexts
105
+ }
106
+ response = github_client.create_deployment(options)
107
+ handle_github_deployment_response(response)
108
+ end
109
+
110
+ def create_deployment_status(url, payload)
111
+ github_client.create_deployment_status(url, payload)
112
+ end
113
+
114
+ def create_github_pending_deployment_status(heroku_build)
115
+ create_github_deployment_status(
116
+ github_deployment_url,
117
+ heroku_build.dashboard_build_output_url,
118
+ "pending",
119
+ "Build running.."
120
+ )
121
+ end
122
+
123
+ def create_github_deployment_status(url, target_url, state, description)
124
+ payload = {
125
+ state: state,
126
+ target_url: target_url,
127
+ description: description
128
+ }
129
+ create_deployment_status(url, payload)
130
+ end
131
+
132
+ def custom_deployment_payload
133
+ { name: app.name, pipeline: pipeline.to_hash, provider: "slash-heroku" }
134
+ end
135
+
136
+ def required_commit_contexts
137
+ return [] if forced
138
+ github_client.required_contexts.map do |context|
139
+ if context == "continuous-integration/travis-ci"
140
+ context = "continuous-integration/travis-ci/push"
141
+ end
142
+ context
143
+ end
144
+ end
145
+
146
+ def github_client
147
+ @github_client ||= Escobar::GitHub::Client.new(
148
+ app.client.github_token,
149
+ pipeline.github_repository
150
+ )
151
+ end
152
+ end
153
+ end
154
+ end
@@ -92,43 +92,20 @@ module Escobar
92
92
  end
93
93
 
94
94
  # rubocop:disable Metrics/LineLength
95
- def create_deployment_from(app, github_deployment, sha, build)
96
- case build["id"]
97
- when "two_factor"
98
- description = "A second factor is required. Use your configured authenticator app or yubikey."
99
- create_github_deployment_status(github_deployment["url"], nil, "failure", description)
100
- raise ArgumentError, build["message"]
101
- when Escobar::UUID_REGEX
102
- heroku_build = Escobar::Heroku::Build.new(
103
- client, app, build["id"]
104
- )
105
- heroku_build.github_url = github_deployment["url"]
106
- heroku_build.pipeline_name = name
107
- heroku_build.sha = sha
108
-
109
- create_github_deployment_status(
110
- github_deployment["url"],
111
- heroku_build.dashboard_build_output_url,
112
- "pending",
113
- "Build running.."
114
- )
115
-
116
- heroku_build
117
- else
118
- raise ArgumentError, "Unable to create heroku build for #{name}"
119
- end
120
- end
121
-
122
95
  def create_deployment(ref, environment, force = false, custom_payload = {})
123
96
  app = environments[environment] && environments[environment].last
124
- return({ error: "No '#{environment}' environment for #{name}." }) unless app
97
+ unless app
98
+ raise ArgumentError, "No '#{environment}' environment for #{name}."
99
+ end
100
+
101
+ heroku_app = app.app
125
102
 
126
- github_deployment = create_github_deployment("deploy", ref, environment, force, custom_payload)
127
- return({ error: github_deployment["message"] }) unless github_deployment["sha"]
103
+ build_request = heroku_app.build_request_for(self)
104
+ heroku_build = build_request.create(
105
+ "deploy", environment, ref, force, custom_payload
106
+ )
128
107
 
129
- sha = github_deployment["sha"]
130
- build = create_heroku_build(app.name, sha)
131
- create_deployment_from(app, github_deployment, sha, build)
108
+ heroku_build
132
109
  end
133
110
  # rubocop:enable Metrics/LineLength
134
111
 
@@ -160,20 +137,6 @@ module Escobar
160
137
  { name: name, pipeline: self.to_hash, provider: "slash-heroku" }
161
138
  end
162
139
 
163
- def create_github_deployment(task, ref, environment, force, extras = {})
164
- required_contexts = required_commit_contexts(force)
165
-
166
- options = {
167
- ref: ref,
168
- task: task,
169
- auto_merge: !force,
170
- payload: extras.merge(custom_deployment_payload),
171
- environment: environment,
172
- required_contexts: required_contexts
173
- }
174
- github_client.create_deployment(options)
175
- end
176
-
177
140
  def create_github_deployment_status(url, target_url, state, description)
178
141
  payload = {
179
142
  state: state,
@@ -1,3 +1,3 @@
1
1
  module Escobar
2
- VERSION = "0.3.1".freeze
2
+ VERSION = "0.3.2".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: escobar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Donohoe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-05 00:00:00.000000000 Z
11
+ date: 2017-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -178,6 +178,7 @@ files:
178
178
  - lib/escobar/github/client.rb
179
179
  - lib/escobar/heroku/app.rb
180
180
  - lib/escobar/heroku/build.rb
181
+ - lib/escobar/heroku/build_request.rb
181
182
  - lib/escobar/heroku/client.rb
182
183
  - lib/escobar/heroku/coupling.rb
183
184
  - lib/escobar/heroku/pipeline.rb