octokit 1.21.0 → 1.22.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ # 1.22.0
4
+
5
+ * Patch from @[gonzoyumo][] to fix service hooks
6
+
7
+ [gonzoyumo]: https://github.com/gonzoyumo
8
+
9
+ View [the full changelog][1.22.0].
10
+ [1.22.0]: https://github.com/pengwynn/octokit/compare/v1.21.0...v1.22.0
11
+
3
12
  # 1.21.0
4
13
 
5
14
  [@joeyw](https://github.com/joeyw) added:
@@ -19,22 +19,7 @@ module Faraday
19
19
 
20
20
  def on_complete(response)
21
21
  key = response[:status].to_i
22
- raise ERROR_MAP[key], error_message(response) if ERROR_MAP.has_key? key
23
- end
24
-
25
- def error_message(response)
26
- message = if (body = response[:body]) && !body.empty?
27
- if body.is_a?(String)
28
- body = MultiJson.load(body, :symbolize_keys => true)
29
- end
30
- ": #{body[:error] || body[:message] || ''}"
31
- else
32
- ''
33
- end
34
- errors = unless message.empty?
35
- body[:errors] ? ": #{body[:errors].map{|e|e[:message]}.join(', ')}" : ''
36
- end
37
- "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:status]}#{message}#{errors}"
22
+ raise ERROR_MAP[key].new(response) if ERROR_MAP.has_key? key
38
23
  end
39
24
  end
40
25
  end
@@ -14,9 +14,9 @@ module Octokit
14
14
  :"hub.mode" => "subscribe",
15
15
  :"hub.topic" => topic,
16
16
  :"hub.callback" => callback,
17
+ :force_urlencoded => true
17
18
  }
18
- post("hub", options)
19
- true
19
+ boolean_from_response(:post, "hub", options)
20
20
  end
21
21
 
22
22
  # Unsubscribe from a pubsub topic
@@ -32,9 +32,9 @@ module Octokit
32
32
  :"hub.mode" => "unsubscribe",
33
33
  :"hub.topic" => topic,
34
34
  :"hub.callback" => callback,
35
+ :force_urlencoded => true
35
36
  }
36
- post("hub", options)
37
- true
37
+ boolean_from_response(:post, "hub", options)
38
38
  end
39
39
  end
40
40
  end
@@ -16,7 +16,6 @@ module Octokit
16
16
  topic = "#{Octokit.web_endpoint}#{Repository.new(repo)}/events/push"
17
17
  callback = "github://#{service_name}?#{service_arguments.collect{ |k,v| [ k,v ].join("=") }.join("&") }"
18
18
  subscribe(topic, callback)
19
- true
20
19
  end
21
20
 
22
21
  # Unsubscribe repository through pubsub
@@ -31,7 +30,6 @@ module Octokit
31
30
  topic = "#{Octokit.web_endpoint}#{Repository.new(repo)}/events/push"
32
31
  callback = "github://#{service_name}"
33
32
  unsubscribe(topic, callback)
34
- true
35
33
  end
36
34
  end
37
35
  end
@@ -25,7 +25,11 @@ module Octokit
25
25
  # TODO: Don't build on every request
26
26
  connection = Faraday.new(options) do |builder|
27
27
 
28
- builder.request :json
28
+ if options[:force_urlencoded]
29
+ builder.request :url_encoded
30
+ else
31
+ builder.request :json
32
+ end
29
33
 
30
34
  builder.use Faraday::Response::RaiseOctokitError
31
35
  builder.use FaradayMiddleware::FollowRedirects
@@ -1,6 +1,38 @@
1
1
  module Octokit
2
2
  # Custom error class for rescuing from all GitHub errors
3
- class Error < StandardError; end
3
+ class Error < StandardError
4
+ def initialize(response)
5
+ @response = response
6
+ super(build_error_message)
7
+ end
8
+
9
+ def response_body
10
+ @response_body ||=
11
+ if (body = @response[:body]) && !body.empty?
12
+ if body.is_a?(String)
13
+ MultiJson.load(body, :symbolize_keys => true)
14
+ else
15
+ body
16
+ end
17
+ else
18
+ nil
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def build_error_message
25
+ message = if response_body
26
+ ": #{response_body[:error] || response_body[:message] || ''}"
27
+ else
28
+ ''
29
+ end
30
+ errors = unless message.empty?
31
+ response_body[:errors] ? ": #{response_body[:errors].map{|e|e[:message]}.join(', ')}" : ''
32
+ end
33
+ "#{@response[:method].to_s.upcase} #{@response[:url].to_s}: #{@response[:status]}#{message}#{errors}"
34
+ end
35
+ end
4
36
 
5
37
  # Raised when GitHub returns a 400 HTTP status code
6
38
  class BadRequest < Error; end
@@ -51,8 +51,11 @@ module Octokit
51
51
  options.delete(:oauth_token) ||
52
52
  oauth_token
53
53
 
54
+ force_urlencoded = options.delete(:force_urlencoded) || false
55
+
54
56
  conn_options = {
55
- :authenticate => token.nil?
57
+ :authenticate => token.nil?,
58
+ :force_urlencoded => force_urlencoded
56
59
  }
57
60
 
58
61
  response = connection(conn_options).send(method) do |request|
@@ -74,7 +77,11 @@ module Octokit
74
77
  request.url(path, options)
75
78
  when :patch, :post, :put
76
79
  request.path = path
77
- request.body = MultiJson.dump(options) unless options.empty?
80
+ if force_urlencoded
81
+ request.body = options unless options.empty?
82
+ else
83
+ request.body = MultiJson.dump(options) unless options.empty?
84
+ end
78
85
  end
79
86
 
80
87
  if Octokit.request_host
@@ -1,3 +1,3 @@
1
1
  module Octokit
2
- VERSION = "1.21.0" unless defined?(Octokit::VERSION)
2
+ VERSION = "1.22.0" unless defined?(Octokit::VERSION)
3
3
  end
@@ -49,6 +49,14 @@ describe Faraday::Response do
49
49
  @client.user('sferik')
50
50
  }.to raise_error(Octokit::BadRequest, /#{body.values.first}/)
51
51
  end
52
+
53
+ it "includes the body on the error" do
54
+ expect {
55
+ @client.user('sferik')
56
+ }.to raise_error { |error|
57
+ error.response_body.should == body
58
+ }
59
+ end
52
60
  end
53
61
  end
54
62
 
@@ -16,10 +16,12 @@ describe Octokit::Client::PubSubHubbub::ServiceHooks do
16
16
  it "subscribes to pull events on specified topic" do
17
17
  stub_post("/hub").
18
18
  with(subscribe_request_body).
19
- to_return(:body => nil)
19
+ to_return(:status => 204)
20
20
 
21
21
  expect(client.subscribe_service_hook("joshk/completeness-fu", "Travis", { :token => 'travistoken' })).to eq(true)
22
- assert_requested :post, "https://api.github.com/hub", :body => subscribe_request_body, :times => 1
22
+ assert_requested :post, "https://api.github.com/hub", :body => subscribe_request_body, :times => 1,
23
+ :headers => {'Content-type' => 'application/x-www-form-urlencoded'}
24
+
23
25
  end
24
26
  end
25
27
 
@@ -35,10 +37,11 @@ describe Octokit::Client::PubSubHubbub::ServiceHooks do
35
37
  it "unsubscribes to stop receiving events on specified topic" do
36
38
  stub_post("/hub").
37
39
  with(unsubscribe_request_body).
38
- to_return(:body => nil)
40
+ to_return(:status => 204)
39
41
 
40
42
  expect(client.unsubscribe_service_hook("joshk/completeness-fu", "Travis")).to eq(true)
41
- assert_requested :post, "https://api.github.com/hub", :body => unsubscribe_request_body, :times => 1
43
+ assert_requested :post, "https://api.github.com/hub", :body => unsubscribe_request_body, :times => 1,
44
+ :headers => {'Content-type' => 'application/x-www-form-urlencoded'}
42
45
  end
43
46
  end
44
47
  end
@@ -5,44 +5,63 @@ describe Octokit::Client::PubSubHubbub do
5
5
  let(:client) { Octokit::Client.new(:oauth_token => 'myfaketoken') }
6
6
 
7
7
  describe ".subscribe" do
8
- it "subscribes to pull events" do
9
- stub_post("/hub").
10
- with({
8
+ context "with valid params" do
9
+ let(:subscribe_request_body) {
10
+ {
11
11
  :"hub.callback" => 'github://Travis?token=travistoken',
12
12
  :"hub.mode" => 'subscribe',
13
13
  :"hub.topic" => 'https://github.com/joshk/completeness-fu/events/push'
14
- }).
15
- to_return(:body => nil)
14
+ }
15
+ }
16
+ it "subscribes to pull events" do
17
+ stub_post("/hub").
18
+ with(subscribe_request_body).
19
+ to_return(:status => 204)
16
20
 
17
- expect(client.subscribe("https://github.com/joshk/completeness-fu/events/push", "github://Travis?token=travistoken")).to eq(true)
21
+ expect(client.subscribe("https://github.com/joshk/completeness-fu/events/push", "github://Travis?token=travistoken")).to eq(true)
22
+ assert_requested :post, "https://api.github.com/hub", :body => subscribe_request_body, :times => 1,
23
+ :headers => {'Content-type' => 'application/x-www-form-urlencoded'}
24
+ end
18
25
  end
19
-
20
- it "raises an error if the topic is not recognized" do
21
- stub_post("/hub").
22
- with({
26
+ context "when topic is not recognized" do
27
+ let(:subscribe_request_body) {
28
+ {
23
29
  :"hub.callback" => 'github://Travis?token=travistoken',
24
30
  :"hub.mode" => 'subscribe',
25
- :"hub.topic" => 'https://github.com/joshk/completeness-fud/events/push'
26
- }).
27
- to_return(:status => 422)
31
+ :"hub.topic" => 'https://github.com/joshk/not_existing_project/events/push'
32
+ }
33
+ }
34
+ it "raises an error" do
35
+ stub_post("/hub").
36
+ with(subscribe_request_body).
37
+ to_return(:status => 422)
28
38
 
29
- expect {
30
- client.subscribe("https://github.com/joshk/completeness-fud/events/push", "github://Travis?token=travistoken")
31
- }.to raise_exception
39
+ expect {
40
+ client.subscribe("https://github.com/joshk/not_existing_project/events/push", "github://Travis?token=travistoken")
41
+ }.to raise_exception
42
+ assert_requested :post, "https://api.github.com/hub", :body => subscribe_request_body, :times => 1,
43
+ :headers => {'Content-type' => 'application/x-www-form-urlencoded'}
44
+ end
32
45
  end
33
46
  end
34
47
 
35
48
  describe ".unsubscribe" do
36
- it "unsubscribes from pull events" do
37
- stub_post("/hub").
38
- with({
49
+ let(:unsubscribe_request_body) {
50
+ {
39
51
  :"hub.callback" => 'github://Travis?token=travistoken',
40
52
  :"hub.mode" => 'unsubscribe',
41
53
  :"hub.topic" => 'https://github.com/joshk/completeness-fu/events/push'
42
- }).
43
- to_return(:body => nil)
54
+ }
55
+ }
56
+
57
+ it "unsubscribes from pull events" do
58
+ stub_post("/hub").
59
+ with(unsubscribe_request_body).
60
+ to_return(:status => 204)
44
61
 
45
62
  expect(client.unsubscribe("https://github.com/joshk/completeness-fu/events/push", "github://Travis?token=travistoken")).to eq(true)
63
+ assert_requested :post, "https://api.github.com/hub", :body => unsubscribe_request_body, :times => 1,
64
+ :headers => {'Content-type' => 'application/x-www-form-urlencoded'}
46
65
  end
47
66
  end
48
67
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: octokit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.21.0
4
+ version: 1.22.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-01-10 00:00:00.000000000 Z
14
+ date: 2013-01-15 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: addressable