escobar 0.1.12 → 0.1.13

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: c89c1fd4c830261563b696dbcaf93580e6fe492b
4
- data.tar.gz: 9ddd3f3422dd5e909c59801bfe8aec4cb11cc75a
3
+ metadata.gz: f6c82bbf2a9b06a2b1cf46a6690fa5482d359b71
4
+ data.tar.gz: 36ebeb8e9e8eb19cdaf4351b048e42280e0823af
5
5
  SHA512:
6
- metadata.gz: 7ce23af22cf18a5591f5c8de390d916a70cde993e038ca3ead37cb5009aa29e71fd584fddf9632fe289f79aa33c4a116a5f6f18deebbd943630373c104e035ed
7
- data.tar.gz: 3a5ce8685f823ac731bb2ac6ddf2431eb1fb3061523fb24ee7afe796f3c492cc1c4724e0182fec205e720324d48ef5c027dd050b27b30754f577c4186d7d393d
6
+ metadata.gz: f1852e5d8f58f604587b69b5b2ef3818ac6b35f14698e85efbb2d3f59b17e42cba05f42b250405002909ab47122466cc7caf797d42c27b8c73d2177ef1cd5d54
7
+ data.tar.gz: 832138ca0c62be560ac8ba4bd9761b9cd004bb3e3523ebd85c5dfe91670afaa90e2b20af0e0a00c00a8e4de2e8282a08f9200f4af8bc557f3701227d0bb84d1c
data/.rubocop.yml CHANGED
@@ -42,7 +42,7 @@ Documentation:
42
42
  - !ruby/regexp /spec\/*\/*/
43
43
 
44
44
  ClassLength:
45
- Max: 100
45
+ Max: 125
46
46
  Exclude:
47
47
  - !ruby/regexp /spec\/*\/*/
48
48
 
data/escobar.gemspec CHANGED
@@ -24,7 +24,6 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_dependency "faraday", "~> 0.9.2"
26
26
  spec.add_dependency "netrc", "~> 0.11"
27
- spec.add_dependency "octokit", "~> 4.3.0"
28
27
 
29
28
  spec.add_development_dependency "bundler", "~> 1.11"
30
29
  spec.add_development_dependency "guard-rspec", "~> 4.6.2"
@@ -5,9 +5,9 @@ module Escobar
5
5
  new(Escobar.github_api_token, Escobar.heroku_api_token)
6
6
  end
7
7
 
8
- attr_reader :github, :heroku
8
+ attr_reader :github_token, :heroku
9
9
  def initialize(github_token, heroku_token)
10
- @github = Escobar::GitHub::Client.new(github_token)
10
+ @github_token = github_token
11
11
  @heroku = Escobar::Heroku::Client.new(heroku_token)
12
12
  end
13
13
 
@@ -2,17 +2,78 @@ module Escobar
2
2
  module GitHub
3
3
  # Top-level class for interacting with GitHub API
4
4
  class Client
5
- attr_reader :client
6
- def initialize(token)
7
- @client = Octokit::Client.new(access_token: token)
5
+ attr_reader :name_with_owner, :token
6
+ def initialize(token, name_with_owner)
7
+ @token = token
8
+ @name_with_owner = name_with_owner
8
9
  end
9
10
 
10
11
  def whoami
11
12
  client.get("/user")
12
13
  end
13
14
 
14
- def archive_link(name_with_owner, ref)
15
- client.archive_link(name_with_owner, ref: ref)
15
+ def archive_link(ref)
16
+ path = "/repos/#{name_with_owner}/tarball/#{ref}"
17
+ response = http_method(:head, path)
18
+ response && response.headers && response.headers["Location"]
19
+ end
20
+
21
+ def create_deployment(options)
22
+ body = {
23
+ ref: options[:ref] || "master",
24
+ task: "deploy",
25
+ auto_merge: false,
26
+ required_contexts: [],
27
+ payload: options[:payload] || {},
28
+ environment: options[:environment] || "staging",
29
+ description: "Shipped from chat with slash-heroku"
30
+ }
31
+ post("/repos/#{name_with_owner}/deployments", body)
32
+ end
33
+
34
+ def create_deployment_status(url, payload)
35
+ uri = URI.parse(url)
36
+ post("#{uri.path}/statuses", payload)
37
+ end
38
+
39
+ def head(path)
40
+ http_method(:head, path)
41
+ end
42
+
43
+ def get(path)
44
+ response = http_method(:get, path)
45
+ JSON.parse(response.body)
46
+ rescue StandardError
47
+ response && response.body
48
+ end
49
+
50
+ def http_method(verb, path)
51
+ client.send(verb) do |request|
52
+ request.url path
53
+ request.headers["Accept"] = "application/vnd.github+json"
54
+ request.headers["Content-Type"] = "application/json"
55
+ request.headers["Authorization"] = "token #{token}"
56
+ end
57
+ end
58
+
59
+ def post(path, body)
60
+ response = client.post do |request|
61
+ request.url path
62
+ request.headers["Accept"] = "application/vnd.github+json"
63
+ request.headers["Content-Type"] = "application/json"
64
+ request.headers["Authorization"] = "token #{token}"
65
+ request.body = body.to_json
66
+ end
67
+
68
+ JSON.parse(response.body)
69
+ rescue StandardError
70
+ response && response.body
71
+ end
72
+
73
+ private
74
+
75
+ def client
76
+ @client ||= Faraday.new(url: "https://api.github.com")
16
77
  end
17
78
  end
18
79
  end
@@ -54,47 +54,66 @@ module Escobar
54
54
  end
55
55
  end
56
56
 
57
+ def github_archive_link(ref)
58
+ github_client.archive_link(ref)
59
+ end
60
+
61
+ def github_client
62
+ @github_client ||= Escobar::GitHub::Client.new(client.github_token,
63
+ github_repository)
64
+ end
65
+
66
+ def create_github_deployment(task, ref, environment, force)
67
+ options = {
68
+ ref: ref,
69
+ task: task,
70
+ environment: environment,
71
+ required_contexts: [],
72
+ auto_merge: !force,
73
+ payload: {
74
+ name: name,
75
+ provider: "slash-heroku",
76
+ pipeline: self.to_hash
77
+ }
78
+ }
79
+ github_client.create_deployment(options)
80
+ end
81
+
82
+ def create_github_deployment_status(deployment_url, name, build_id, state)
83
+ path = "/apps/#{name}/activity/builds/#{build_id}"
84
+ payload = {
85
+ state: state,
86
+ target_url: "https://dashboard.heroku.com/#{path}",
87
+ description: "Deploying from escobar-#{Escobar::VERSION}"
88
+ }
89
+ github_client.create_deployment_status(deployment_url, payload)
90
+ end
91
+
92
+ def create_heroku_build(app_name, sha)
93
+ body = {
94
+ source_blob: {
95
+ url: github_archive_link(sha),
96
+ version: sha[0..7],
97
+ version_description: "#{github_repository}:#{sha}"
98
+ }
99
+ }
100
+ client.heroku.post("/apps/#{app_name}/builds", body)
101
+ end
102
+
57
103
  # rubocop:disable Metrics/AbcSize
58
104
  # rubocop:disable Metrics/LineLength
59
- # rubocop:disable Metrics/MethodLength
60
105
  def create_deployment(ref, environment, force)
61
- deployment = Escobar::GitHub::Deployment.new(client.github.client)
62
- deployment.ref = ref
63
- deployment.repo = github_repository
64
- deployment.force = force
65
- deployment.environment = environment
106
+ app = environments[environment] && environments[environment].last
107
+ return({ error: "No #{environment} for #{name}." }) unless app
66
108
 
67
- archive_link = deployment.archive_link
109
+ github_deployment = create_github_deployment("deploy", ref, environment, force)
110
+ return({ error: github_deployment["message"] }) unless github_deployment["sha"]
68
111
 
69
- app = environments[environment] && environments[environment].last
70
- if app
71
- payload = self.to_hash
72
- payload[:name] = name
73
- payload[:provider] = "slash-heroku"
74
-
75
- github_deployment = deployment.create(payload)
76
- body = {
77
- source_blob: {
78
- url: archive_link,
79
- version: github_deployment.sha[0..7],
80
- version_description: "#{deployment.repo}:#{github_deployment.sha}"
81
- }
82
- }
112
+ build = create_heroku_build(app.name, github_deployment["sha"])
113
+ return({ error: "Unable to create heroku build for #{name}" }) unless build["id"]
83
114
 
84
- build = client.heroku.post("/apps/#{app.name}/builds", body)
85
- if build["id"]
86
- status_payload = {
87
- target_url: "#{app.app.dashboard_url}/activity/builds/#{build['id']}",
88
- description: "Deploying from slash-heroku"
89
- }
90
-
91
- deployment.create_status(github_deployment.url, "pending", status_payload)
92
- {
93
- app_id: app.name, build_id: build["id"],
94
- deployment_url: github_deployment.url
95
- }
96
- end
97
- end
115
+ create_github_deployment_status(github_deployment["url"], app.name, build["id"], "pending")
116
+ { app_id: app.name, build_id: build["id"], deployment_url: github_deployment["url"] }
98
117
  end
99
118
 
100
119
  def get(path)
@@ -1,3 +1,3 @@
1
1
  module Escobar
2
- VERSION = "0.1.12".freeze
2
+ VERSION = "0.1.13".freeze
3
3
  end
data/lib/escobar.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "netrc"
2
2
  require "faraday"
3
- require "octokit"
4
3
  require "json"
5
4
  require "escobar/version"
6
5
 
@@ -47,7 +46,6 @@ end
47
46
 
48
47
  require_relative "./escobar/client"
49
48
  require_relative "./escobar/github/client"
50
- require_relative "./escobar/github/deployment"
51
49
  require_relative "./escobar/heroku/app"
52
50
  require_relative "./escobar/heroku/client"
53
51
  require_relative "./escobar/heroku/coupling"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: escobar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.12
4
+ version: 0.1.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Corey Donohoe
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0.11'
41
- - !ruby/object:Gem::Dependency
42
- name: octokit
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 4.3.0
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 4.3.0
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -190,7 +176,6 @@ files:
190
176
  - lib/escobar.rb
191
177
  - lib/escobar/client.rb
192
178
  - lib/escobar/github/client.rb
193
- - lib/escobar/github/deployment.rb
194
179
  - lib/escobar/heroku/app.rb
195
180
  - lib/escobar/heroku/client.rb
196
181
  - lib/escobar/heroku/coupling.rb
@@ -1,34 +0,0 @@
1
- module Escobar
2
- module GitHub
3
- # Class to create a Deployment record on GitHub
4
- class Deployment
5
- attr_accessor :client, :environment, :force, :ref, :repo
6
- def initialize(client)
7
- @client = client
8
- end
9
-
10
- def payload(custom_payload)
11
- {
12
- task: "deploy",
13
- payload: custom_payload,
14
- auto_merge: !force,
15
- required_contexts: [],
16
- environment: "staging",
17
- description: "Shipped from chat with slash-heroku"
18
- }
19
- end
20
-
21
- def archive_link
22
- client.archive_link(repo, ref: ref)
23
- end
24
-
25
- def create(custom_payload)
26
- client.create_deployment(repo, ref, payload(custom_payload))
27
- end
28
-
29
- def create_status(url, state, extra_payload)
30
- client.create_deployment_status(url, state, extra_payload)
31
- end
32
- end
33
- end
34
- end