forked from zendesk/samson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctokit.rb
39 lines (31 loc) · 1.29 KB
/
octokit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require 'octokit'
require 'faraday-http-cache'
# we don't want to forever pay the price of redirects, but make users fix them
# https://github.com/octokit/octokit.rb/issues/771
#
# to reproduce/remove: rename a repository and try to create a diff with the old name
# it should return a NullComparison and not a broken Changeset with nil commits
# tested via test/models/changeset_test.rb
class Octokit::RedirectAsError < Faraday::Response::Middleware
private
def on_complete(response)
if [301, 302].include?(response[:status].to_i)
raise Octokit::RepositoryUnavailable, response
end
end
end
Octokit.middleware = Faraday::RackBuilder.new do |builder|
builder.use Faraday::HttpCache, shared_cache: false, store: Rails.cache, serializer: Marshal
builder.response :logger, Rails.logger
builder.use Octokit::Response::RaiseError
builder.use Octokit::RedirectAsError
builder.adapter Faraday.default_adapter
end
Octokit.connection_options[:request] = { open_timeout: 2 }
token = ENV['GITHUB_TOKEN']
unless Rails.env.test? || ENV['PRECOMPILE']
raise "No GitHub token available" if token.blank?
end
Octokit.api_endpoint = Rails.application.config.samson.github.api_url
Octokit.web_endpoint = Rails.application.config.samson.github.web_url
GITHUB = Octokit::Client.new(access_token: token)