gh 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/gh.rb CHANGED
@@ -5,6 +5,7 @@ require 'forwardable'
5
5
  module GH
6
6
  autoload :Cache, 'gh/cache'
7
7
  autoload :Case, 'gh/case'
8
+ autoload :CustomLimit, 'gh/custom_limit'
8
9
  autoload :Error, 'gh/error'
9
10
  autoload :FaradayAdapter, 'gh/faraday_adapter'
10
11
  autoload :Instrumentation, 'gh/instrumentation'
@@ -38,6 +39,11 @@ module GH
38
39
  self.current = was if was
39
40
  end
40
41
 
42
+ def self.set(options)
43
+ Thread.current[:GH] = nil
44
+ DefaultStack.options.merge! options
45
+ end
46
+
41
47
  def self.current
42
48
  Thread.current[:GH] ||= DefaultStack.new
43
49
  end
@@ -47,7 +53,7 @@ module GH
47
53
  end
48
54
 
49
55
  extend SingleForwardable
50
- def_delegators :current, :api_host, :[], :reset, :load, :post, :delete, :patch, :put, :in_parallel, :in_parallel?, :options
56
+ def_delegators :current, :api_host, :[], :reset, :load, :post, :delete, :patch, :put, :in_parallel, :in_parallel?, :options, :head
51
57
 
52
58
  DefaultStack = Stack.new do
53
59
  use Instrumentation
@@ -57,6 +63,7 @@ module GH
57
63
  use MergeCommit
58
64
  use LazyLoader
59
65
  use Normalizer
66
+ use CustomLimit
60
67
  use Remote
61
68
  end
62
69
  end
@@ -0,0 +1,27 @@
1
+ module GH
2
+ # Adds Client info so even unauthenticated requests can use a custom request limit
3
+ class CustomLimit < Wrapper
4
+ attr_accessor :client_id, :client_secret
5
+
6
+ def setup(backend, options)
7
+ @client_id = options[:client_id]
8
+ @client_secret = options[:client_secret]
9
+ super
10
+ end
11
+
12
+ def fetch_resource(key)
13
+ return super unless client_id
14
+
15
+ url = full_url(key)
16
+ params = url.query_values || {}
17
+
18
+ unless params.include? 'client_id'
19
+ params['client_id'] = client_id
20
+ params['client_secret'] = client_secret
21
+ end
22
+
23
+ url.query_values = params
24
+ super url.request_uri
25
+ end
26
+ end
27
+ end
@@ -2,26 +2,51 @@ require 'gh'
2
2
 
3
3
  module GH
4
4
  class Error < Exception
5
- attr_reader :error, :payload
5
+ attr_reader :info
6
6
 
7
- def self.new(error, *)
8
- Error === error ? error : super
7
+ def initialize(error = nil, payload = nil, info = {})
8
+ info = info.merge(error.info) if error.respond_to? :info and Hash === error.info
9
+ error = error.error while error.respond_to? :error
10
+ @info = info.merge(:error => error, :payload => payload)
11
+
12
+ if error
13
+ set_backtrace error.backtrace
14
+ if error.respond_to? :response and error.response
15
+ case response = error.response
16
+ when Hash
17
+ @info[:response_status] = response[:status]
18
+ @info[:response_headers] = response[:headers]
19
+ @info[:response_body] = response[:body]
20
+ when Faraday::Response
21
+ @info[:response_status] = response.status
22
+ @info[:response_headers] = response.headers
23
+ @info[:response_body] = response.body
24
+ else
25
+ @info[:response] = response
26
+ end
27
+ end
28
+ end
29
+ end
30
+
31
+ def payload
32
+ info[:payload]
9
33
  end
10
34
 
11
- def initialize(error = nil, payload = nil)
12
- error = error.error while error.respond_to? :error
13
- @error, @payload = error, payload
14
- set_backtrace error.backtrace if error
35
+ def error
36
+ info[:error]
15
37
  end
16
38
 
17
39
  def message
18
- "GH request failed (#{error.class}: #{error.message}) with payload: #{short(payload.inspect)}"
40
+ "GH request failed\n" + info.map { |k,v| entry(k,v) }.join("\n")
19
41
  end
20
42
 
21
43
  private
22
44
 
23
- def short(string)
24
- string.length < 101 ? string : string[0,97] + '...'
45
+ def entry(key, value)
46
+ value = "#{value.class}: #{value.message}" if Exception === value
47
+ value = value.inspect unless String === value
48
+ value = value.gsub(/[^\n]{80}/, "\\0\n").lines.map { |l| "\n #{l}" }.join.gsub(/\n+/, "\n")
49
+ (key.to_s + ": ").ljust(12) + value
25
50
  end
26
51
  end
27
52
  end
@@ -58,22 +58,25 @@ module GH
58
58
 
59
59
  def github_done_checking?(hash)
60
60
  case hash['mergeable_state']
61
- when 'checking' then false
62
- when 'unknown' then hash['merged']
63
- when 'clean', 'dirty' then true
61
+ when 'checking' then false
62
+ when 'unknown' then hash['merged']
63
+ when 'clean', 'dirty', 'unstable', 'stable' then true
64
64
  else fail "unknown mergeable_state #{hash['mergeable_state'].inspect} for #{url(hash)}"
65
65
  end
66
66
  end
67
67
 
68
68
  def force_merge_commit(hash)
69
- Timeout.timeout(600) do
69
+ Timeout.timeout(180) do
70
70
  update(hash) until github_done_checking? hash
71
71
  end
72
+ rescue TimeoutError
73
+ status = hash['mergeable_state'].inspect
74
+ raise TimeoutError, "gave up waiting for github to check the merge status (current status is #{status})"
72
75
  end
73
76
 
74
77
  def update(hash)
75
78
  hash.merge! backend[url(hash)]
76
- sleep 0.1
79
+ sleep 0.5
77
80
  end
78
81
 
79
82
  def url(hash)
@@ -26,7 +26,7 @@ module GH
26
26
  api_host = api_host.api_host if api_host.respond_to? :api_host
27
27
  @api_host = Addressable::URI.parse(api_host)
28
28
  @headers = options[:headers].try(:dup) || {
29
- "Origin" => options[:origin] || "http://example.org",
29
+ #"Origin" => options[:origin] || "http://example.org",
30
30
  "Accept" => "application/vnd.github.v3.full+json," \
31
31
  "application/vnd.github.beta.full+json;q=0.5," \
32
32
  "application/json;q=0.1",
@@ -47,7 +47,8 @@ module GH
47
47
  builder.request(:basic_auth, username, password) if username and password
48
48
  builder.request(:retry)
49
49
  builder.response(:raise_error)
50
- builder.use(options[:adapter] || GH::FaradayAdapter)
50
+ #builder.use(options[:adapter] || GH::FaradayAdapter)
51
+ builder.adapter(:net_http)
51
52
  end
52
53
  end
53
54
 
@@ -72,6 +73,8 @@ module GH
72
73
  # Internal: ...
73
74
  def http(verb, url, headers = {}, &block)
74
75
  connection.run_request(verb, url, nil, headers, &block)
76
+ rescue Exception => error
77
+ raise Error.new(error, nil, :verb => verb, :url => url, :headers => headers)
75
78
  end
76
79
 
77
80
  # Internal: ...
@@ -92,6 +95,11 @@ module GH
92
95
  frontend.request(:delete, key)
93
96
  end
94
97
 
98
+ # Public: ...
99
+ def head(key)
100
+ frontend.request(:head, key)
101
+ end
102
+
95
103
  # Public: ...
96
104
  def patch(key, body)
97
105
  frontend.request(:patch, key, body)
@@ -1,4 +1,4 @@
1
1
  module GH
2
2
  # Public: Library version.
3
- VERSION = "0.8.0"
3
+ VERSION = "0.9.0"
4
4
  end
@@ -45,6 +45,9 @@ module GH
45
45
  # Public: ...
46
46
  def_delegator :backend, :delete
47
47
 
48
+ # Public: ...
49
+ def_delegator :backend, :head
50
+
48
51
  # Public: ...
49
52
  def_delegator :backend, :patch
50
53
 
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe GH::CustomLimit do
4
+ before do
5
+ subject.client_id = 'foo'
6
+ subject.client_secret = 'bar'
7
+ end
8
+
9
+ it 'adds client_id and client_secret to a request' do
10
+ subject.backend.
11
+ should_receive(:fetch_resource).
12
+ with('/x?client_id=foo&client_secret=bar').
13
+ and_return(GH::Response.new)
14
+ subject['/x']
15
+ end
16
+ end
@@ -28,4 +28,9 @@ describe GH::Error do
28
28
  it 'keeps the payload around' do
29
29
  exception.payload.should be == {'foo' => 'bar'}
30
30
  end
31
+
32
+ it 'works for long content' do
33
+ error = GH::Error.new(nil, nil, 'foo' => 'a'*1000)
34
+ expect { error.message }.not_to raise_error
35
+ end
31
36
  end
@@ -13,7 +13,7 @@ describe GH::Remote do
13
13
 
14
14
  it 'raises an exception for missing resources' do
15
15
  stub_request(:get, "https://api.github.com/foo").to_return(:status => 404)
16
- expect { subject['foo'] }.to raise_error(Faraday::Error::ResourceNotFound)
16
+ expect { subject['foo'] }.to raise_error(GH::Error)
17
17
  end
18
18
 
19
19
  it 'parses the body' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-09 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70202419542180 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70202419542180
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: webmock
27
- requirement: &70202419541760 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70202419541760
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: faraday
38
- requirement: &70202419541120 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ~>
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0.8'
44
54
  type: :runtime
45
55
  prerelease: false
46
- version_requirements: *70202419541120
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.8'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: backports
49
- requirement: &70202419556580 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ~>
@@ -54,10 +69,15 @@ dependencies:
54
69
  version: '2.3'
55
70
  type: :runtime
56
71
  prerelease: false
57
- version_requirements: *70202419556580
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '2.3'
58
78
  - !ruby/object:Gem::Dependency
59
79
  name: multi_json
60
- requirement: &70202419555500 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
61
81
  none: false
62
82
  requirements:
63
83
  - - ~>
@@ -65,10 +85,15 @@ dependencies:
65
85
  version: '1.0'
66
86
  type: :runtime
67
87
  prerelease: false
68
- version_requirements: *70202419555500
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.0'
69
94
  - !ruby/object:Gem::Dependency
70
95
  name: addressable
71
- requirement: &70202419554560 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
72
97
  none: false
73
98
  requirements:
74
99
  - - ! '>='
@@ -76,10 +101,15 @@ dependencies:
76
101
  version: '0'
77
102
  type: :runtime
78
103
  prerelease: false
79
- version_requirements: *70202419554560
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
80
110
  - !ruby/object:Gem::Dependency
81
111
  name: net-http-persistent
82
- requirement: &70202419553260 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
83
113
  none: false
84
114
  requirements:
85
115
  - - ! '>='
@@ -87,10 +117,15 @@ dependencies:
87
117
  version: '2.7'
88
118
  type: :runtime
89
119
  prerelease: false
90
- version_requirements: *70202419553260
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '2.7'
91
126
  - !ruby/object:Gem::Dependency
92
127
  name: net-http-pipeline
93
- requirement: &70202419551820 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
94
129
  none: false
95
130
  requirements:
96
131
  - - ! '>='
@@ -98,7 +133,12 @@ dependencies:
98
133
  version: '0'
99
134
  type: :runtime
100
135
  prerelease: false
101
- version_requirements: *70202419551820
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
102
142
  description: multi-layer client for the github api v3
103
143
  email:
104
144
  - konstantin.mailinglists@googlemail.com
@@ -115,6 +155,7 @@ files:
115
155
  - lib/gh.rb
116
156
  - lib/gh/cache.rb
117
157
  - lib/gh/case.rb
158
+ - lib/gh/custom_limit.rb
118
159
  - lib/gh/error.rb
119
160
  - lib/gh/faraday_adapter.rb
120
161
  - lib/gh/instrumentation.rb
@@ -132,6 +173,7 @@ files:
132
173
  - lib/gh/version.rb
133
174
  - lib/gh/wrapper.rb
134
175
  - spec/cache_spec.rb
176
+ - spec/custom_limit_spec.rb
135
177
  - spec/error_spec.rb
136
178
  - spec/gh_spec.rb
137
179
  - spec/instrumentation_spec.rb
@@ -189,12 +231,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
231
  version: '0'
190
232
  requirements: []
191
233
  rubyforge_project:
192
- rubygems_version: 1.8.11
234
+ rubygems_version: 1.8.23
193
235
  signing_key:
194
236
  specification_version: 3
195
237
  summary: layered github client
196
238
  test_files:
197
239
  - spec/cache_spec.rb
240
+ - spec/custom_limit_spec.rb
198
241
  - spec/error_spec.rb
199
242
  - spec/gh_spec.rb
200
243
  - spec/instrumentation_spec.rb